Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
226 changes: 226 additions & 0 deletions .github/workflows/verify-group-version.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,226 @@
name: Verify group version

on: workflow_call

permissions:
contents: read
pull-requests: read
statuses: write

jobs:
verify:
name: Verify group version
runs-on: ubuntu-latest
concurrency:
group: group-version-guard-${{ github.event.pull_request.head.sha }}
cancel-in-progress: false
env:
GH_TOKEN: ${{ github.token }}
PR_HEAD_SHA: ${{ github.event.pull_request.head.sha }}
steps:
- name: Mark version check pending
run: |
gh api "repos/$GITHUB_REPOSITORY/statuses/$PR_HEAD_SHA" --method POST \
-f state=pending -f context=group-version-guard \
-f description='正在检查成员版本与本轮发布记录' \
-f target_url="$GITHUB_SERVER_URL/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID" > /dev/null

- name: Install deps
run: pip install --quiet pyyaml

- name: Verify changelog version
id: verify
run: |
python3 - <<'PY'
import base64
import json
import os
import re
import subprocess
import tempfile
from pathlib import Path
from urllib.parse import urlencode

import yaml

MARKER = "<!-- group-release\n"

def api(path, paginate=False):
args = ["gh", "api", path]
if paginate:
args += ["--paginate", "--slurp"]
result = subprocess.run(args, capture_output=True, text=True)
if result.returncode:
raise RuntimeError(f"GitHub API 失败: {path}: {result.stderr.strip()}")
value = json.loads(result.stdout)
return [item for page in value for item in page] if paginate else value

def content(repo, path, sha):
value = api(f"repos/{repo}/contents/{path}?{urlencode({'ref': sha})}")
return base64.b64decode(value["content"]).decode("utf-8")

def head_version(repo, sha):
with tempfile.NamedTemporaryFile(mode="w", encoding="utf-8") as f:
f.write(content(repo, "debian/changelog", sha))
f.flush()
result = subprocess.run(["dpkg-parsechangelog", "-l", f.name, "-S", "Version"],
capture_output=True, text=True)
if result.returncode or not result.stdout.strip():
raise RuntimeError(f"无法读取 {repo}@{sha} 的 changelog: {result.stderr.strip()}")
return result.stdout.strip()

def repo_names(cfg, group, owner):
overrides = cfg.get("repo_overrides", {})
return [f"{owner}/{overrides.get(m, m)}" for m in cfg["groups"][group]["members"]]

def release_record(repo, sha, group):
commit = api(f"repos/{repo}/git/commits/{sha}")
for hops in range(101):
if MARKER in commit["message"]:
release = json.loads(commit["message"].split(MARKER, 1)[1].split("\n-->", 1)[0])
if release["group"] != group or [p["sha"] for p in commit["parents"]] != [release["base_sha"]]:
raise RuntimeError("DDE 发布记录已改变,请先修复原基线 PR")
return release
if hops == 100 or len(commit["parents"]) != 2:
break
commit = api(f"repos/{repo}/git/commits/{commit['parents'][0]['sha']}")
raise RuntimeError("DDE 基线 PR 缺少原始发布记录,请先核实")

def check(repo, pr):
before = head_version(repo, pr["base"]["sha"])
source = pr["head"]["repo"]
if not source:
raise RuntimeError("PR 来源仓库已删除,无法校验")
after = head_version(source["full_name"], pr["head"]["sha"])
if before == after:
return "changelog 版本未变化"
if subprocess.run(["dpkg", "--compare-versions", after, "gt", before]).returncode:
raise RuntimeError(f"成员版本只能升版: {before} -> {after}")

owner = repo.split("/", 1)[0]
baseline = f"{owner}/dde"
default = api(f"repos/{baseline}")["default_branch"]
cfg = yaml.safe_load(content(baseline, "groups.yaml", default))
groups = [g for g in cfg["groups"] if repo in repo_names(cfg, g, owner)]
if len(groups) != 1:
raise RuntimeError(f"{repo} 不属于唯一的 DDE 版本组,请核实分组配置")
group = groups[0]
query = urlencode({"state": "all", "head": f"{owner}:baseline/{group}",
"sort": "created", "direction": "desc", "per_page": 1})
prs = api(f"repos/{baseline}/pulls?{query}")
if not prs:
raise RuntimeError(f"{group} 没有统一发布记录,请从 DDE Release group 发起,不能单独改版")
baseline_pr = prs[0]
if (not baseline_pr["head"]["repo"] or baseline_pr["head"]["repo"]["full_name"] != baseline
or baseline_pr["head"]["ref"] != f"baseline/{group}"
or baseline_pr["base"]["ref"] != default):
raise RuntimeError("DDE 基线 PR 的来源或目标分支不符")
release = release_record(baseline, baseline_pr["head"]["sha"], group)
if (release["base_branch"] != default
or [m["repo"] for m in release["members"]] != repo_names(cfg, group, owner)):
raise RuntimeError("DDE 本轮成员或目标分支已改变,请先核实原发布")
member = next(m for m in release["members"] if m["repo"] == repo)
baseline_version = cfg["groups"][group]["version"]
if "version" in member:
baseline_version = baseline_version.split(":", 1)[-1]
if subprocess.run(["dpkg", "--compare-versions", release["version"], "ge", baseline_version]).returncode:
raise RuntimeError("DDE 发布记录已过期,不能继续旧版本")
target = member.get("version", release["version"])
if subprocess.run(["dpkg", "--validate-version", target]).returncode:
raise RuntimeError(f"无效成员目标版本: {target}")
if "version" in member and target.split(":", 1)[-1] != release["version"]:
raise RuntimeError("成员目标版本与组版本不符")
safe = re.sub(r"[^A-Za-z0-9._-]+", "-", target).strip("-")
branch = f"release-{target}" if group == "G1" else f"automation/update-changelog/{safe}"
if (after != target or source["full_name"] != repo or pr["head"]["ref"] != branch
or pr["base"]["ref"] != member["base"] or pr["created_at"] < release["started_at"]):
raise RuntimeError(f"{repo} 的改版 PR 不属于 {group} 本轮 {target},请从 DDE Release group 发起")
return f"{group} 本轮发布 {target},成员 PR 与发布记录一致"

def identity(pr):
source = pr["head"]["repo"] or {}
return {"number": pr["number"], "head_sha": pr["head"]["sha"],
"head_repo": source.get("full_name"), "head_ref": pr["head"]["ref"],
"base_ref": pr["base"]["ref"]}

def main():
repo = os.environ["GITHUB_REPOSITORY"]
with open(os.environ["GITHUB_EVENT_PATH"], encoding="utf-8") as f:
event = json.load(f)
pr = api(f"repos/{repo}/pulls/{event['number']}")
if pr["head"]["sha"] != os.environ["PR_HEAD_SHA"]:
raise RuntimeError("PR 提交已变化,请等待新提交的检查")
query = urlencode({"state": "open", "per_page": 100})
related = api(f"repos/{repo}/pulls?{query}", paginate=True)
open_prs = [p for p in related
if p["state"] == "open" and p["head"]["sha"] == os.environ["PR_HEAD_SHA"]]
if not any(p["number"] == pr["number"] for p in open_prs) and pr["state"] == "open":
open_prs.append(pr)
for item in open_prs:
check(repo, item)
Path(os.environ["RUNNER_TEMP"], "verified-prs.json").write_text(
json.dumps(sorted((identity(pr) for pr in open_prs), key=lambda pr: pr["number"])) + "\n",
encoding="utf-8")
print(f"同提交开放 PR 校验通过: {len(open_prs)}")

if __name__ == "__main__":
main()
PY

- name: Publish version check result
if: always()
env:
VERIFY_OUTCOME: ${{ steps.verify.outcome }}
run: |
set -euo pipefail
current_state=success
if [ "$VERIFY_OUTCOME" != "success" ]; then
current_state=failure
fi
if [ "$current_state" = success ]; then
if ! gh api "repos/$GITHUB_REPOSITORY/pulls?state=open&per_page=100" --paginate --slurp \
> "$RUNNER_TEMP/current-prs.json"; then
echo '::error::无法刷新关联 PR,成员版本校验不能通过' >&2
current_state=failure
elif ! python3 - "$RUNNER_TEMP/verified-prs.json" "$RUNNER_TEMP/current-prs.json" "$PR_HEAD_SHA" <<'PY'
import json
import sys

expected_file, current_file, head_sha = sys.argv[1:]
with open(expected_file, encoding="utf-8") as f:
expected = json.load(f)
with open(current_file, encoding="utf-8") as f:
pages = json.load(f)
if pages and isinstance(pages[0], dict):
pages = [pages]
current = [item for page in pages for item in page]

def identity(pr):
source = pr["head"]["repo"] or {}
return {"number": pr["number"], "head_sha": pr["head"]["sha"],
"head_repo": source.get("full_name"), "head_ref": pr["head"]["ref"],
"base_ref": pr["base"]["ref"]}

current = sorted((identity(pr) for pr in current
if pr["state"] == "open" and pr["head"]["sha"] == head_sha),
key=lambda pr: pr["number"])
if current != expected:
print("::error::校验期间 PR 的目标、提交或开放集合发生变化,需要重新检查", file=sys.stderr)
raise SystemExit(1)
PY
then
current_state=failure
fi
fi
if [ "$current_state" = "success" ]; then
state=success
description='成员版本校验通过'
else
state=failure
description='成员版本校验失败,详见工作流日志'
fi
gh api "repos/$GITHUB_REPOSITORY/statuses/$PR_HEAD_SHA" --method POST \
-f state="$state" -f context=group-version-guard \
-f description="$description" \
-f target_url="$GITHUB_SERVER_URL/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID" > /dev/null
test "$current_state" = success
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,29 @@ commit信息检查
### cppcheck

cpp代码静态检查

### call-verify-group-version

校验 DDE 组成员 PR 的 `debian/changelog` 版本。普通代码 PR 不改版本时通过;改版 PR 须匹配
同组织 `dde` 仓库的原始发布记录,包括完整成员目标版本、成员仓库、自动化分支、目标分支和 PR 创建时间。
组目标不带 epoch;G2 成员保留现有 epoch,去掉 epoch 后须与组目标一致。
首轮成员起始版本可以不同,完整目标仍须高于当前版本。
缺少成员版本字段的旧记录继续按原组目标校验,不改变已有发布。
旧 epoch 组基线完成后,新记录按不带 epoch 的组版本判断是否过期,成员仍校验完整目标。

成员使用 `workflow-templates/call-verify-group-version.yml`,通过 `pull_request_target` 调用
`verify-group-version.yml`。工作流只读取 PR 文件,不检出或执行 PR 代码;使用本仓库的
`GITHUB_TOKEN` 把 `group-version-guard` 状态写到 PR 的 head 提交,不需要 GitHub App Secret。

`repos/linuxdeepin/*.json` 已为 25 个组成员配置此模板。dtk6log 已归档,其模板同步配置停用。
上线前先完成成员首用准备,包括 go-lib 的发布入口;合入共享工作流和模板配置,
确认成员入口安装完成后上线 DDE 协调器。带 epoch 的新发布须等协调器与成员校验均更新后触发。
默认分支上的入口文件接入后,给所有成员发布目标分支
启用 `group-version-guard` 必需检查,来源选择 GitHub Actions;同时要求 PR、分支保持最新,
禁止直接推送、强推和绕过。工作流本身不能阻止未受保护分支上的直接推送。

成员启用保护后,后续模板更新须通过 PR 合入;自动创建模板 PR 的工具升级另行安排,也可手工创建 PR。
模板同步和发布机器人均不配置保护绕过。
最终关联 PR 列表或 PR 状态查询失败时,guard 发布失败状态,工作流同时失败;不会沿用之前的成功结果。
版本校验后会保存 PR 的提交、来源和目标分支;发布成功状态前再次核对开放 PR 集合和这些身份。
校验期间改投目标或出现其他 PR 变化时,本次结果写为失败,等待新事件重新检查。
9 changes: 8 additions & 1 deletion repos/linuxdeepin/dde-api.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,5 +74,12 @@
],
"src": "workflow-templates/call-update-changelog.yml",
"dest": "linuxdeepin/dde-api/.github/workflows/call-update-changelog.yml"
},
{
"branches": [
"master"
],
"src": "workflow-templates/call-verify-group-version.yml",
"dest": "linuxdeepin/dde-api/.github/workflows/call-verify-group-version.yml"
}
]
]
9 changes: 8 additions & 1 deletion repos/linuxdeepin/dde-appearance.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,12 @@
],
"src": "workflow-templates/call-update-changelog.yml",
"dest": "linuxdeepin/dde-appearance/.github/workflows/call-update-changelog.yml"
},
{
"branches": [
"master"
],
"src": "workflow-templates/call-verify-group-version.yml",
"dest": "linuxdeepin/dde-appearance/.github/workflows/call-verify-group-version.yml"
}
]
]
9 changes: 8 additions & 1 deletion repos/linuxdeepin/dde-application-manager.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,12 @@
],
"src": "workflow-templates/call-update-changelog.yml",
"dest": "linuxdeepin/dde-application-manager/.github/workflows/call-update-changelog.yml"
},
{
"branches": [
"master"
],
"src": "workflow-templates/call-verify-group-version.yml",
"dest": "linuxdeepin/dde-application-manager/.github/workflows/call-verify-group-version.yml"
}
]
]
7 changes: 7 additions & 0 deletions repos/linuxdeepin/dde-control-center.json
Original file line number Diff line number Diff line change
Expand Up @@ -90,5 +90,12 @@
],
"src": "workflow-templates/call-update-changelog.yml",
"dest": "linuxdeepin/dde-control-center/.github/workflows/call-update-changelog.yml"
},
{
"branches": [
"master"
],
"src": "workflow-templates/call-verify-group-version.yml",
"dest": "linuxdeepin/dde-control-center/.github/workflows/call-verify-group-version.yml"
}
]
9 changes: 8 additions & 1 deletion repos/linuxdeepin/dde-daemon.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,5 +74,12 @@
],
"src": "workflow-templates/call-update-changelog.yml",
"dest": "linuxdeepin/dde-daemon/.github/workflows/call-update-changelog.yml"
},
{
"branches": [
"master"
],
"src": "workflow-templates/call-verify-group-version.yml",
"dest": "linuxdeepin/dde-daemon/.github/workflows/call-verify-group-version.yml"
}
]
]
9 changes: 8 additions & 1 deletion repos/linuxdeepin/dde-launchpad.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,5 +76,12 @@
],
"src": "workflow-templates/call-update-changelog.yml",
"dest": "linuxdeepin/dde-launchpad/.github/workflows/call-update-changelog.yml"
},
{
"branches": [
"master"
],
"src": "workflow-templates/call-verify-group-version.yml",
"dest": "linuxdeepin/dde-launchpad/.github/workflows/call-verify-group-version.yml"
}
]
]
9 changes: 8 additions & 1 deletion repos/linuxdeepin/dde-network-core.json
Original file line number Diff line number Diff line change
Expand Up @@ -95,5 +95,12 @@
],
"src": "workflow-templates/call-update-changelog.yml",
"dest": "linuxdeepin/dde-network-core/.github/workflows/call-update-changelog.yml"
},
{
"branches": [
"master"
],
"src": "workflow-templates/call-verify-group-version.yml",
"dest": "linuxdeepin/dde-network-core/.github/workflows/call-verify-group-version.yml"
}
]
]
Loading
Loading