Skip to content

armgit / peach

Public Imported from GitHub
0

Peach: Local-first video metadata scraper and media manager.

fix(release): 补打标签的提交可晋级手动触发的发布演练

longmeidaoc709de36bf5b
diff --git a/scripts/github_release_candidate.py b/scripts/github_release_candidate.py
index c9ea4d71d..975301ba4 100644
--- a/scripts/github_release_candidate.py
+++ b/scripts/github_release_candidate.py
@@ -50,13 +50,16 @@ def api(repo: str, endpoint: str):
def select(repo: str, sha: str) -> dict:
if not re.fullmatch(r"[0-9a-f]{40}", sha) or not re.fullmatch(r"[A-Za-z0-9_.-]+/[A-Za-z0-9_.-]+", repo):
raise ValueError("GitHub 晋级需要明确仓库与完整提交")
- runs = api(repo, f"actions/workflows/release.yml/runs?head_sha={sha}&event=push&per_page=100")["workflow_runs"]
- matching = [row for row in runs if row["head_sha"] == sha and row["head_branch"] == "master" and row["event"] == "push"]
+ # 版本提交推送时自动演练;其他主线提交的推送演练被跳过,只能手动触发。
+ runs = api(repo, f"actions/workflows/release.yml/runs?head_sha={sha}&per_page=100")["workflow_runs"]
+ matching = [row for row in runs if row["head_sha"] == sha and row["head_branch"] == "master"
+ and row["event"] in {"push", "workflow_dispatch"}]
if not matching:
raise ValueError("未取得同一提交的主线发布演练")
- run = max(matching, key=lambda row: (row["id"], row.get("run_attempt", 1)))
- if run["status"] != "completed" or run["conclusion"] != "success":
+ succeeded = [row for row in matching if row["status"] == "completed" and row["conclusion"] == "success"]
+ if not succeeded:
raise ValueError("主线发布演练尚未成功")
+ run = max(succeeded, key=lambda row: (row["id"], row.get("run_attempt", 1)))
jobs = api(repo, f"actions/runs/{run['id']}/jobs?per_page=100")["jobs"]
passed = {row["name"] for row in jobs if row["conclusion"] == "success"}
if not REQUIRED_JOBS <= passed:
diff --git a/scripts/release_tag.py b/scripts/release_tag.py
index 6d3642176..e52a628fe 100644
--- a/scripts/release_tag.py
+++ b/scripts/release_tag.py
@@ -25,7 +25,7 @@ import time
if not __package__:
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
__package__ = "scripts"
-from . import changelog, version_bump
+from . import changelog, github_release_candidate, version_bump
ROOT = Path(__file__).resolve().parents[1]
GITLAB = None
@@ -390,8 +390,16 @@ def plan_commit(repo: str, sha: str) -> dict:
_require_free_tag(repo, tag)
if command("git", "tag", "--list", tag):
raise ValueError(f"本地 {tag} 已存在,请先检查它的归属")
- return {**verify(repo, sha), "tag": tag, "version": version, "repo": repo,
- "section": changelog.section_of(document, version)}
+ return {**verify(repo, sha), **require_candidate(repo, sha), "tag": tag, "version": version,
+ "repo": repo, "section": changelog.section_of(document, version)}
+
+
+def require_candidate(repo: str, sha: str) -> dict:
+ """补打标签沿用标签工作流的晋级判据,取不到演练产物时不留下无法发布的标签。"""
+ if GITLAB:
+ return {}
+ run = github_release_candidate.select(repo, sha)
+ return {"rehearsal": f"https://github.com/{repo}/actions/runs/{run['run_id']}"}
def main(argv=None) -> int:
diff --git a/tests/test_release_candidate.py b/tests/test_release_candidate.py
index aa99442fb..c9be3f940 100644
--- a/tests/test_release_candidate.py
+++ b/tests/test_release_candidate.py
@@ -262,6 +262,26 @@ class GitHubCandidateTests(unittest.TestCase):
with self.assertRaises(ValueError):
github.select("owner/repo", "a" * 40)
+ def test_a_dispatched_rehearsal_is_promoted_when_the_push_run_was_skipped(self):
+ def row(run_id, event, conclusion, branch="master"):
+ return {"id": run_id, "head_sha": "a" * 40, "head_branch": branch, "event": event,
+ "status": "completed", "conclusion": conclusion}
+ runs = [row(5, "workflow_dispatch", "success"), row(6, "push", "skipped"),
+ row(8, "push", "failure", branch="v0.37.0"), row(9, "pull_request", "success")]
+ jobs = [{"name": name, "conclusion": "success"} for name in github.REQUIRED_JOBS]
+ artifacts = [{"name": name, "id": index, "expired": False, "digest": "sha256:" + "b" * 64}
+ for index, name in enumerate(sorted(github.REQUIRED_ARTIFACTS))]
+ def api(_repo, endpoint):
+ return {"workflow_runs": runs} if "workflows/" in endpoint else {"jobs": jobs} if "/jobs?" in endpoint else {"artifacts": artifacts}
+ with mock.patch.object(github, "api", side_effect=api):
+ self.assertEqual(github.select("owner/repo", "a" * 40)["run_id"], 5)
+ runs[0]["conclusion"] = "failure"
+ with self.assertRaisesRegex(ValueError, "尚未成功"):
+ github.select("owner/repo", "a" * 40)
+ del runs[:2]
+ with self.assertRaisesRegex(ValueError, "未取得"):
+ github.select("owner/repo", "a" * 40)
+
def test_loaded_docker_image_must_match_the_recorded_image_id(self):
image = {"Id": "sha256:" + "b" * 64, "Config": {"Labels": {
"org.opencontainers.image.version": "1.2.3", "org.opencontainers.image.revision": "a" * 40}}}
diff --git a/tests/test_scripts.py b/tests/test_scripts.py
index be9062d5e..0d9d9036d 100644
--- a/tests/test_scripts.py
+++ b/tests/test_scripts.py
@@ -5694,6 +5694,12 @@ class PinnedReleaseTests(unittest.TestCase):
def setUpClass(cls):
cls.release = load_script("release_tag")
+ def setUp(self):
+ candidate = mock.patch.object(self.release.github_release_candidate, "select",
+ return_value={"run_id": 9})
+ self.candidate = candidate.start()
+ self.addCleanup(candidate.stop)
+
def _shell(self, changes=None):
answers = {
("git", "status", "--porcelain"): "",
@@ -5768,6 +5774,16 @@ class PinnedReleaseTests(unittest.TestCase):
self.assertEqual(self.release.main(["--release-sha", self.SHA, "--apply"]), 1)
self.assertEqual(ShipTests._writes(command), [])
+ def test_a_commit_without_a_promotable_rehearsal_gets_no_tag(self):
+ with self._shell(), self._api():
+ self.assertEqual(self.release.plan_commit("owner/repo", self.SHA)["rehearsal"],
+ "https://github.com/owner/repo/actions/runs/9")
+ self.candidate.assert_called_with("owner/repo", self.SHA)
+ self.candidate.side_effect = ValueError("主线发布演练尚未成功")
+ with self._shell() as command, self._api(), redirect_stdout(io.StringIO()):
+ self.assertEqual(self.release.main(["--release-sha", self.SHA, "--apply"]), 1)
+ self.assertEqual(ShipTests._writes(command), [])
+
def test_apply_rechecks_the_selected_commit_and_pushes_only_its_annotated_tag(self):
with self._shell() as command, self._api(), \
mock.patch.object(self.release, "plan_commit", wraps=self.release.plan_commit) as plan, \