From e5e81282be6f54b5632cbcf311f6a63d4ec2739b Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 14 Nov 2026 17:48:19 +0000 Subject: [PATCH] Re-run changelog guard checks when a peer PR changes A peer collision now clears when the lower-numbered PR is pushed, reopened, retargeted, merged or closed, by re-running only the later PRs' Check CHANGELOG.md job. Lower-numbered PRs that don't edit the changelog are skipped again. Self-test grows to 33 cases; the mutation script covers each docstring clause, regex and API filter (49 of 49). Co-Authored-By: Claude Opus 5.5 --- .github/scripts/changelog_guard.py | 58 +++++---- .github/scripts/changelog_guard_mutate.py | 66 +++++++---- .github/scripts/changelog_guard_test.py | 138 +++++++++++++++++++--- .github/workflows/changelog-guard.yml | 25 +++- 4 files changed, 228 insertions(+), 59 deletions(-) diff --git a/.github/scripts/changelog_guard.py b/.github/scripts/changelog_guard.py index 1d36be7..eb8f457 100644 --- a/.github/scripts/changelog_guard.py +++ b/.github/scripts/changelog_guard.py @@ -10,20 +10,24 @@ repeats another entry under the same heading (case and spacing ignored; duplicates already on the base branch are not blamed on the PR), - or, together with a lower-numbered open PR into the same base branch that also edits the - changelog, conflict on it or produce one of those duplicates. The later PR is the one that fails. + changelog, conflict on it or produce one of those duplicates. The later PR is the one that fails, + and its check re-runs by itself whenever that peer changes (see MODE=peers). Anything else inside [Unreleased], or inside a section the PR adds, is allowed: new headings, rewording and removals included. A PR that doesn't touch the changelog is not checked. Labels are read from the API, so a re-run sees the current ones. -MODE=recheck (push to the base branch that touches the changelog) and MODE=peers (a PR closed without -merging; PEER is its number): re-run this workflow's latest run on each affected open PR, so the PR's -own check is the one signal and the label, a push or a re-run clears it. recheck covers every open PR -that edits the changelog; peers covers the higher-numbered ones. A failure on one PR is reported and -the loop goes on. Both modes always exit 0, because the push or close is not what's being checked. +MODE=recheck (a push to the base branch that touches the changelog) and MODE=peers (a PR is pushed, +reopened, retargeted, merged or closed; PEER is its number and BASE_BRANCH the branch it targeted): +re-run the latest completed JOB job on each affected open PR, so the PR's own check is the one signal +and the label, a push, or the peer's change clears it. recheck covers every open PR that edits the +changelog; peers covers the higher-numbered ones. A run whose job is still queued or running is left +alone, since it reads the current state when it starts. A failure on one PR is reported and the loop +goes on. Both modes always exit 0, because the push or the peer is not what's being checked. Env: MODE, BASE (commit or ref to merge into), BASE_BRANCH, REPO (owner/name), PR_NUMBER in pr mode, PEER in peers mode, CHANGELOG (default CHANGELOG.md), OVERRIDE_LABEL (default changelog-override), -WORKFLOW (workflow file name, default changelog-guard.yml), GH (gh binary, default gh), HEAD_REF +WORKFLOW (workflow file name, default changelog-guard.yml), JOB (the PR check's job name, default +Check CHANGELOG.md), GH (gh binary, default gh), HEAD_REF (default HEAD). """ import difflib, json, os, re, subprocess, sys @@ -31,6 +35,7 @@ CL = os.environ.get("CHANGELOG", "CHANGELOG.md") OVERRIDE = os.environ.get("OVERRIDE_LABEL", "changelog-override") WORKFLOW = os.environ.get("WORKFLOW", "changelog-guard.yml") +JOB = os.environ.get("JOB", "Check CHANGELOG.md") GH = os.environ.get("GH", "gh") H2 = re.compile(r"^## \[([^\]]+)\]") H3 = re.compile(r"^### +(.+?)\s*$") @@ -166,29 +171,42 @@ def check_peers(base, head, me, prs): mine = show(head) or [] for n, _ in sorted(p for p in prs if p[0] < me): ref = fetch_pr(n) + if not touches(base, ref): + continue + again = f"this check re-runs by itself when #{n} is pushed, retargeted, merged or closed" tree, conflicts = merge(head, ref) if CL in conflicts: - errs.append(f"#{me}: {CL} conflicts with open #{n}; update this branch after #{n} merges") + errs.append(f"#{me}: {CL} conflicts with open #{n}; {again}") continue both = dup_keys(mine, released) | dup_keys(show(ref) or [], released) - errs += [f"#{me}: together with open #{n}, {describe(k)}; update this branch after #{n} merges" + errs += [f"#{me}: together with open #{n}, {describe(k)}; {again}" for k in sorted(dup_keys(show(tree) or [], released) - both, key=str)] return errs +def api(path): + return json.loads(run(GH, "api", path).stdout) + + def rerun(n, sha): - """Re-run this workflow's latest pull_request run on the PR's head commit. Returns a warning or None.""" - runs = json.loads(run(GH, "api", f"repos/{repo()}/actions/workflows/{WORKFLOW}/runs" - f"?event=pull_request&head_sha={sha}&per_page=1").stdout)["workflow_runs"] - if not runs: - return f"#{n}: no {WORKFLOW} run on {sha[:7]} to re-run; push to the PR to check it" - r = runs[0] - if r["status"] != "completed": - print(f"#{n}: run {r['id']} is still going and checks out the current base branch") + """Re-run the PR's newest JOB job on its head commit (only that job, so its own peer re-runs don't + cascade). Runs where the job was skipped, such as a title edit, are passed over. Returns a warning + or None.""" + runs = api(f"repos/{repo()}/actions/workflows/{WORKFLOW}/runs" + f"?event=pull_request&head_sha={sha}&per_page=20")["workflow_runs"] + for r in runs: # newest first + jobs = [j for j in api(f"repos/{repo()}/actions/runs/{r['id']}/jobs?per_page=100")["jobs"] + if j["name"] == JOB and j["conclusion"] != "skipped"] + if not jobs: + continue + j = jobs[0] + if j["status"] != "completed": + print(f"#{n}: job {j['id']} is still {j['status']} and reads the current state when it runs") + return None + run(GH, "api", "-X", "POST", f"repos/{repo()}/actions/jobs/{j['id']}/rerun") + print(f"#{n}: re-ran job {j['id']}") return None - run(GH, "api", "-X", "POST", f"repos/{repo()}/actions/runs/{r['id']}/rerun") - print(f"#{n}: re-ran {WORKFLOW} run {r['id']}") - return None + return f"#{n}: no {JOB} job on {sha[:7]} to re-run; push to the PR to check it" def report(errs, warns): diff --git a/.github/scripts/changelog_guard_mutate.py b/.github/scripts/changelog_guard_mutate.py index 3b2b0ce..82fb79d 100644 --- a/.github/scripts/changelog_guard_mutate.py +++ b/.github/scripts/changelog_guard_mutate.py @@ -1,39 +1,65 @@ #!/usr/bin/env python3 -"""Delete each rule the guard's docstring lists, one at a time, and check a self-test case fails.""" +"""Mutate each clause of the guard's docstring, each regex and each API filter, one at a time, and +check that a self-test case fails for every mutant.""" import shutil, subprocess, sys, tempfile from pathlib import Path SRC = Path(__file__).resolve().parent M = [ - ("base conflict", " if CL in conflicts:\n return [f\"{label}: {CL} conflicts with the base branch", " if False:\n return [f\"{label}: {CL} conflicts with the base branch"), - ("released: changed/removed line", "for i in range(i1, i2) if osec[i] in released", "for i in range(i1, i2) if False"), + # Docstring, pr mode: each rule + ("conflict with the base fails", " if CL in conflicts:\n return [f\"{label}: {CL} conflicts with the base branch", " if False:\n return [f\"{label}: {CL} conflicts with the base branch"), + ("released: changed or removed line", "for i in range(i1, i2) if osec[i] in released", "for i in range(i1, i2) if False"), ("released: added line", "for j in range(j1, j2) if nsec[j] in released", "for j in range(j1, j2) if False"), - ("LINKREF exclusion, old side", "and not LINKREF.match(old[i])]", "]"), - ("LINKREF exclusion, new side", "and not LINKREF.match(new[j])]", "]"), + ("released: blank lines don't count", "if osec[i] in released and old[i].strip() and", "if osec[i] in released and"), + ("released: blank lines don't count, new side", "if nsec[j] in released and new[j].strip() and", "if nsec[j] in released and"), + ("released means already on the base", "released = sections(old), sections(new), released_of(old)", "released = sections(old), sections(new), released_of(new)"), ("Unreleased is not released", 'if s and s.lower() != "unreleased"}', "if s}"), ("override label", " if allow_released:\n", " if False:\n"), + ("override label only covers released edits", " return errs, [f\"{label}: {h} (allowed", " return [], [f\"{label}: {h} (allowed"), + ("override label makes them warnings", "(allowed by the {OVERRIDE} label)\" for h in hits]", "(allowed by the {OVERRIDE} label)\" for h in []]"), + ("link references never count, old side", "and not LINKREF.match(old[i])]", "]"), + ("link references never count, new side", "and not LINKREF.match(new[j])]", "]"), ("duplicate ## heading", 'k, h3 = ("section", m.group(1)), None', "continue"), - ("duplicate ### heading", 'k, h3 = ("heading", sec, m.group(1)), m.group(1)', "h3 = m.group(1)\n continue"), + ("duplicate ### heading in one section", 'k, h3 = ("heading", sec, m.group(1)), m.group(1)', "h3 = m.group(1)\n continue"), ("duplicate entry", 'k = ("entry", sec, h3, norm(m.group(1)))', "continue"), - ("norm lowercasing", '.strip().lower()', ".strip()"), - ("norm whitespace", 're.sub(r"\\s+", " ", s)', "s"), - ("base-duplicate subtraction", "dup_keys(new, released) - dup_keys(old, released)", "dup_keys(new, released)"), + ("entry duplicates are per heading", 'k = ("entry", sec, h3, norm(m.group(1)))', 'k = ("entry", sec, None, norm(m.group(1)))'), + ("case ignored", '.strip().lower()', ".strip()"), + ("spacing ignored", 're.sub(r"\\s+", " ", s)', "s"), + ("duplicates already on the base aren't blamed", "dup_keys(new, released) - dup_keys(old, released)", "dup_keys(new, released)"), + ("untouched changelog is not checked", "if show(base) is None or not touches(base, head):", "if show(base) is None:"), + ("labels read from the API", "OVERRIDE in labels(me)", "False"), + ("errors are ::error", 'print(f"::error title=changelog-guard::{e}")', 'print(f"::warning title=changelog-guard::{e}")'), + ("pr exits 1 on errors", "sys.exit(1 if errs else 0)", "sys.exit(0)"), + # Docstring, peers ("peer: only lower-numbered", "for p in prs if p[0] < me)", "for p in prs if p[0] != me)"), + ("peer: only PRs that edit the changelog", " if not touches(base, ref):\n continue\n", ""), ("peer conflict", " if CL in conflicts:\n errs.append", " if False:\n errs.append"), ("peer duplicates", "for k in sorted(dup_keys(show(tree) or [], released) - both, key=str)]", "for k in []]"), - ("peer duplicate subtraction", "dup_keys(show(tree) or [], released) - both", "dup_keys(show(tree) or [], released)"), + ("peer duplicates the PRs bring alone aren't blamed", "dup_keys(show(tree) or [], released) - both", "dup_keys(show(tree) or [], released)"), + ("peer message says the check re-runs by itself", 'again = f"this check re-runs by itself when #{n} is pushed, retargeted, merged or closed"', 'again = f"update this branch after #{n} merges"'), + # Docstring, recheck and peers modes + ("recheck covers only PRs that edit the changelog", "if touches(base, fetch_pr(n)) and", "if fetch_pr(n) and"), + ("peers covers only higher-numbered PRs", "prs = [p for p in prs if p[0] > int(os.environ[\"PEER\"])]", "pass"), + ("a queued or running job is left alone", ' if j["status"] != "completed":\n', ' if j["status"] == "in_progress":\n'), + ("only the check job is re-run", 'if j["name"] == JOB and', "if"), + ("runs that skipped the check are passed over", ' and j["conclusion"] != "skipped"]', "]"), + ("older runs are looked at when the newest skipped", " if not jobs:\n continue\n", " if not jobs:\n break\n"), + ("re-run the job, not the whole run", 'f"repos/{repo()}/actions/jobs/{j[\'id\']}/rerun"', 'f"repos/{repo()}/actions/runs/{r[\'id\']}/rerun"'), + ("a PR with no check job gets a warning", ' return f"#{n}: no {JOB} job on', ' return None\n return f"#{n}: no {JOB} job on'), + ("a failure on one PR doesn't stop the loop", " except (CmdError, KeyError, ValueError) as e:\n warns.append", " except ZeroDivisionError as e:\n warns.append"), + ("modes exit 0 when listing fails", " except (CmdError, KeyError, ValueError) as e:\n return report", " except ZeroDivisionError as e:\n return report"), + # Regexes + ("H3 strips trailing space", 'H3 = re.compile(r"^### +(.+?)\\s*$")', 'H3 = re.compile(r"^### +(.+)$")'), + ("BULLET is top-level only", 'BULLET = re.compile(r"^[-*+]\\s+(.*\\S)")', 'BULLET = re.compile(r"^\\s*[-*+]\\s+(.*\\S)")'), + ("LINKREF matches link references", 'LINKREF = re.compile(r"^\\[[^\\]]+\\]:\\s")', 'LINKREF = re.compile(r"^\\[[^\\]]+\\]:\\s{2}")'), + ("H2 keys on the bracketed version only", 'H2 = re.compile(r"^## \\[([^\\]]+)\\]")', 'H2 = re.compile(r"^## (.+)")'), + # API filters and paging + ("open PRs only", "pulls?state=open&base=", "pulls?state=all&base="), + ("PRs into the same base only", "&base={os.environ['BASE_BRANCH']}", ""), + ("pull_request runs only", "?event=pull_request&head_sha=", "?head_sha="), + ("runs on the PR's head commit only", "&head_sha={sha}&per_page=20", "&per_page=20"), ("pagination", " page, i = dec.raw_decode(out, i)\n items += page", " items += json.loads(out[i:])\n break"), ("three-dot diff range", 'f"{base}...{head}"', 'f"{base}..{head}"'), - ("base filter on PR listing", "&base={os.environ['BASE_BRANCH']}", ""), - ("labels read from the API", "OVERRIDE in labels(me)", "False"), - ("pr skips untouched changelog", "if show(base) is None or not touches(base, head):", "if show(base) is None:"), - ("recheck skips PRs not touching the changelog", "if touches(base, fetch_pr(n)) and", "if fetch_pr(n) and"), - ("recheck keeps going past a failure", " except (CmdError, KeyError, ValueError) as e:\n warns.append", " except ZeroDivisionError as e:\n warns.append"), - ("recheck exits 0 when listing fails", " except (CmdError, KeyError, ValueError) as e:\n return report", " except ZeroDivisionError as e:\n return report"), - ("rerun skips runs still going", 'if r["status"] != "completed":', "if False:"), - ("rerun reports a PR with no run", " if not runs:\n return f", " if not runs:\n return None\n return f"), - ("peers: only higher-numbered", "prs = [p for p in prs if p[0] > int(os.environ[\"PEER\"])]", "pass"), - ("pr exits 1 on errors", "sys.exit(1 if errs else 0)", "sys.exit(0)"), ] diff --git a/.github/scripts/changelog_guard_test.py b/.github/scripts/changelog_guard_test.py index 7ef5b9f..874200e 100644 --- a/.github/scripts/changelog_guard_test.py +++ b/.github/scripts/changelog_guard_test.py @@ -2,9 +2,10 @@ """Fixture tests for changelog_guard.py. Each case builds a throwaway origin repo with refs/pull/N/head refs and a stub `gh` that serves the -open-PR listing (split over two pages and filtered by base branch, like the API), each PR's labels and -its latest workflow run, and records re-run requests. The guard then runs end to end: merge-tree, the -PR listing, the refs/pull fetch and the re-run call all run for real against the fixture. +open-PR listing (split over two pages and filtered by state and base branch, like the API), each PR's +labels, its workflow runs (filtered by event and head commit) with their jobs, and records job re-run +requests. The guard then runs end to end: merge-tree, the PR listing, the refs/pull fetch and the +re-run call all run for real against the fixture. """ import json, os, subprocess, sys, tempfile, textwrap, unittest from pathlib import Path @@ -51,10 +52,23 @@ elif parts[3] == "issues": p = [p for p in state["prs"] if p["number"] == int(parts[4])][0] print(json.dumps([{"name": l} for l in p["labels"]])) -elif parts[3] == "actions": - runs = [{"id": 1000 + p["number"], "status": p.get("run", "completed")} for p in state["prs"] - if p["sha"] == q["head_sha"] and p.get("run", "completed") != "none"] - print(json.dumps({"workflow_runs": runs[:1]})) +elif parts[3] == "actions" and parts[4] == "workflows": + # each PR has run 1000+n; job="skipped-latest" adds a newer run 2000+n whose check was skipped + runs = [] + for p in reversed(state["prs"]): + if q.get("event") != "pull_request" or p["sha"] != q.get("head_sha", p["sha"]) or p["job"] == "none": + continue + runs += ([{"id": 2000 + p["number"]}] if p["job"] == "skipped-latest" else []) + [{"id": 1000 + p["number"]}] + print(json.dumps({"workflow_runs": runs})) +elif parts[3] == "actions" and parts[4] == "runs": + rid = int(parts[5]) + p = [p for p in state["prs"] if p["number"] == rid % 1000][0] + status = "completed" if p["job"] in ("none", "skipped-latest") else p["job"] + check = ({"status": "completed", "conclusion": "skipped"} if rid >= 2000 else + {"status": status, "conclusion": "failure" if status == "completed" else None}) + print(json.dumps({"jobs": [ + {"id": rid * 10 + 2, "name": "Recheck open PRs", "status": "completed", "conclusion": "success"}, + dict(check, id=rid * 10 + 1, name="Check CHANGELOG.md")]})) else: sys.exit(f"unexpected call {args}") ''' @@ -85,14 +99,17 @@ def commit(self, branch, text, msg, new=False, start="main", path="CHANGELOG.md" (self.work / path).write_text(textwrap.dedent(text)) sh(self.work, "git", "add", "-A") sh(self.work, "git", "commit", "-q", "-m", msg) - sh(self.work, "git", "push", "-q", "origin", f"HEAD:refs/heads/{branch}") + sh(self.work, "git", "push", "-q", "origin", f"+HEAD:refs/heads/{branch}") return sh(self.work, "git", "rev-parse", "HEAD") - def pr(self, n, text, labels=(), base="main", path="CHANGELOG.md", push_ref=True, run="completed"): + def pr(self, n, text, labels=(), base="main", path="CHANGELOG.md", push_ref=True, job="completed"): + """Open PR #n, or force-push a new head to it. job: the state of its check job (completed, + queued, in_progress, none for no run, skipped-latest for a newer run that skipped the check).""" sha = self.commit(f"pr{n}", text, f"pr {n}", start=base, path=path) if push_ref: - sh(self.work, "git", "push", "-q", "origin", f"HEAD:refs/pull/{n}/head") - self.prs.append({"number": n, "sha": sha, "labels": list(labels), "base": base, "state": "open", "run": run}) + sh(self.work, "git", "push", "-q", "origin", f"+HEAD:refs/pull/{n}/head") + self.prs = [p for p in self.prs if p["number"] != n] + self.prs.append({"number": n, "sha": sha, "labels": list(labels), "base": base, "state": "open", "job": job}) return sha def get(self, n): @@ -110,9 +127,20 @@ def run(self, mode, n=None, **extra): return subprocess.run([sys.executable, str(GUARD)], cwd=self.work, env=env, capture_output=True, text=True) def reruns(self): + """PR numbers whose check job was re-run; anything else posted fails the test.""" if not self.log.exists(): return set() - return {int(l.split("/")[-2]) - 1000 for l in self.log.read_text().splitlines() if l.endswith("/rerun")} + done = set() + for l in self.log.read_text().splitlines(): + parts = l.split("/") + assert parts[-3] == "jobs" and parts[-1] == "rerun", f"unexpected POST {l}" + job = int(parts[-2]) + assert job % 10 == 1 and 1000 < job // 10 < 2000, f"re-ran job {job}, not the PR's latest check" + done.add(job // 10 - 1000) + return done + + def clear_log(self): + self.log.unlink(missing_ok=True) def add_unreleased(entry, heading="### Fixed", base=BASE_CL): @@ -135,6 +163,7 @@ def assertPass(self, p): def assertFail(self, p, needle): self.assertEqual(p.returncode, 1, p.stdout + p.stderr) self.assertIn(needle, p.stdout) + self.assertTrue(any(l.startswith("::error") and needle in l for l in p.stdout.splitlines()), p.stdout) # Allowed edits def test_entry_under_unreleased_passes(self): @@ -167,6 +196,28 @@ def test_untouched_changelog_skips_even_after_base_moved(self): self.assertPass(p) self.assertIn("nothing to check", p.stdout) + def test_new_release_section_with_entries_passes(self): + released = BASE_CL.replace("## [Unreleased]\n\n### Added\n- Existing feature\n\n### Fixed\n- Existing fix\n", + "## [Unreleased]\n\n## [1.1.0] - 2026-02-01\n\n### Added\n- Existing feature\n\n" + "### Fixed\n- Existing fix\n- Fix shipped in 1.1.0\n", 1) + self.assertNotEqual(released, BASE_CL) + self.f.pr(1, released) + self.assertPass(self.f.run("pr", 1)) + + def test_same_entry_under_different_headings_passes(self): + self.f.pr(1, add_unreleased("- Existing fix", heading="### Added")) + self.assertPass(self.f.run("pr", 1)) + + def test_repeated_nested_bullets_are_not_entries(self): + self.f.pr(1, add_unreleased("- Fix A\n - details\n- Fix B\n - details")) + self.assertPass(self.f.run("pr", 1)) + + def test_blank_lines_in_released_section_pass(self): + self.f.pr(1, BASE_CL.replace("- First release\n", "- First release\n\n")) + self.assertPass(self.f.run("pr", 1)) + self.f.pr(2, BASE_CL.replace("## [1.0.0] - 2026-01-01\n\n", "## [1.0.0] - 2026-01-01\n")) + self.assertPass(self.f.run("pr", 2)) + def test_duplicates_already_on_base_are_not_blamed(self): dup = BASE_CL.replace("- Existing fix\n", "- Existing fix\n- Existing fix\n") self.f.commit("main", dup, "base has a duplicate") @@ -184,6 +235,15 @@ def test_released_edit_fails_and_label_overrides(self): self.assertPass(p) self.assertIn("::warning", p.stdout) + def test_label_does_not_clear_other_errors(self): + self.f.pr(1, add_unreleased("- Existing fix", base=BASE_CL.replace("- First release", "- First")), + labels=["changelog-override"]) + self.assertFail(self.f.run("pr", 1), "appears twice under [Unreleased]") + + def test_line_removed_from_released_section_fails(self): + self.f.pr(1, BASE_CL.replace("- First release\n", "")) + self.assertFail(self.f.run("pr", 1), "changes released section [1.0.0]") + def test_line_added_to_released_section_fails(self): self.f.pr(1, BASE_CL.replace("- First release\n", "- First release\n- Late addition\n")) self.assertFail(self.f.run("pr", 1), "adds a line inside released section [1.0.0]") @@ -197,6 +257,10 @@ def test_duplicate_heading_fails(self): self.f.pr(1, BASE_CL.replace("- Existing fix\n", "- Existing fix\n\n### Fixed\n- Another\n")) self.assertFail(self.f.run("pr", 1), "`### Fixed` appears twice") + def test_duplicate_heading_with_trailing_space_fails(self): + self.f.pr(1, BASE_CL.replace("- Existing fix\n", "- Existing fix\n\n### Fixed \n- Another\n")) + self.assertFail(self.f.run("pr", 1), "`### Fixed` appears twice") + def test_duplicate_version_heading_fails(self): self.f.pr(1, BASE_CL.replace("## [Unreleased]\n", "## [Unreleased]\n\n## [1.0.0] - 2026-02-01\n", 1)) self.assertFail(self.f.run("pr", 1), "`## [1.0.0]` appears twice") @@ -219,6 +283,14 @@ def test_peers_that_merge_cleanly_into_a_duplicate_heading_fail(self): self.assertPass(self.f.run("pr", 1)) self.assertFail(self.f.run("pr", 2), "together with open #1, `### Security` appears twice") + def test_lower_numbered_pr_without_changelog_edit_is_not_a_peer(self): + self.f.pr(2, BASE_CL.replace("- Existing fix", "- Fix A")) + self.f.commit("main", BASE_CL.replace("- Existing fix", "- Fix B"), "main edits the same line") + self.f.pr(1, "x", path="x.txt") # newer base, no changelog edit + p = self.f.run("pr", 2) + self.assertFail(p, "conflicts with the base branch") + self.assertNotIn("#1", p.stdout) + def test_peer_into_another_base_branch_is_ignored(self): sh(self.f.work, "git", "push", "-q", "origin", "origin/main:refs/heads/release") self.f.pr(1, add_unreleased("- Fix A"), base="release") @@ -248,6 +320,41 @@ def test_closing_a_peer_reruns_later_prs_and_clears_them(self): self.assertEqual(self.f.reruns(), {2, 3}) self.assertPass(self.f.run("pr", 2)) + def test_peer_force_pushed_without_its_entry_clears_later_pr(self): + self.f.pr(1, add_unreleased("- Fix A")) + self.f.pr(2, add_unreleased("- Fix B")) + self.assertFail(self.f.run("pr", 2), "re-runs by itself when #1 is pushed") + self.f.pr(1, "x", path="x.txt") # force-push: #1 no longer edits the changelog + self.assertPass(self.f.run("peers", PEER="1")) + self.assertEqual(self.f.reruns(), {2}) + self.assertPass(self.f.run("pr", 2)) + + def test_peer_merged_without_changelog_change_clears_later_pr(self): + self.f.pr(1, add_unreleased("- Fix A")) + self.f.pr(2, add_unreleased("- Fix B")) + self.assertFail(self.f.run("pr", 2), "conflicts with open #1") + self.f.commit("main", "x", "Merge #1 (its entry was dropped)", path="x.txt") + self.f.get(1)["state"] = "closed" + self.assertPass(self.f.run("peers", PEER="1")) + self.assertEqual(self.f.reruns(), {2}) + self.assertPass(self.f.run("pr", 2)) + + def test_peer_retargeted_clears_later_pr(self): + sh(self.f.work, "git", "push", "-q", "origin", "origin/main:refs/heads/release") + self.f.pr(1, add_unreleased("- Fix A")) + self.f.pr(2, add_unreleased("- Fix B")) + self.assertFail(self.f.run("pr", 2), "conflicts with open #1") + self.f.get(1)["base"] = "release" + self.assertPass(self.f.run("peers", PEER="1")) # the branch #1 left + self.assertEqual(self.f.reruns(), {2}) + self.assertPass(self.f.run("pr", 2)) + + def test_rerun_skips_newer_runs_whose_check_was_skipped(self): + self.f.pr(1, add_unreleased("- Fix A")) + self.f.pr(2, add_unreleased("- Fix B"), job="skipped-latest") + self.assertPass(self.f.run("peers", PEER="1")) + self.assertEqual(self.f.reruns(), {2}) + def test_closing_a_peer_leaves_lower_numbered_prs_alone(self): self.f.pr(1, add_unreleased("- Fix A")) self.f.pr(2, add_unreleased("- Fix B", heading="### Added")) @@ -261,13 +368,14 @@ def test_recheck_survives_a_failed_fetch_and_a_missing_run(self): self.f.pr(1, add_unreleased("- Fix A")) self.f.pr(2, add_unreleased("- Fix B", heading="### Added"), push_ref=False) # fetch fails self.f.pr(3, BASE_CL.replace("# Changelog\n", "# Changelog\n\nIntro.\n")) - self.f.pr(4, BASE_CL.replace("- First release", "- First"), run="none") - self.f.pr(5, BASE_CL.replace("[1.0.0]: ", "[1.0.0]: "), run="in_progress") + self.f.pr(4, BASE_CL.replace("- First release", "- First"), job="none") + self.f.pr(5, BASE_CL.replace("[1.0.0]: ", "[1.0.0]: "), job="in_progress") + self.f.pr(6, BASE_CL.replace("[Unreleased]: ", "[Unreleased]: "), job="queued") p = self.f.run("recheck") self.assertPass(p) self.assertEqual(self.f.reruns(), {1, 3}) self.assertIn("#2: could not re-run", p.stdout) - self.assertIn("#4: no changelog-guard.yml run", p.stdout) + self.assertIn("#4: no Check CHANGELOG.md job", p.stdout) def test_recheck_exits_zero_when_listing_fails(self): self.f.pr(1, add_unreleased("- Fix A")) diff --git a/.github/workflows/changelog-guard.yml b/.github/workflows/changelog-guard.yml index b58e237..a4a8968 100644 --- a/.github/workflows/changelog-guard.yml +++ b/.github/workflows/changelog-guard.yml @@ -2,7 +2,7 @@ name: Changelog guard on: pull_request: - types: [opened, synchronize, reopened, labeled, unlabeled, closed] + types: [opened, synchronize, reopened, edited, labeled, unlabeled, closed] push: branches: [main, master] paths: [CHANGELOG.md] @@ -10,13 +10,17 @@ on: permissions: {} concurrency: - group: changelog-guard-${{ github.event.pull_request.number || github.ref }}${{ github.event.action == 'closed' && '-closed' || '' }} + # closed and edited runs get their own group so they never cancel a check that is still going + group: changelog-guard-${{ github.event.pull_request.number || github.ref }}${{ (github.event.action == 'closed' || github.event.action == 'edited') && format('-{0}', github.event.action) || '' }} cancel-in-progress: true jobs: pr: name: Check CHANGELOG.md - if: github.event_name == 'pull_request' && github.event.action != 'closed' + # an edit only matters when it retargets the PR + if: >- + github.event_name == 'pull_request' && github.event.action != 'closed' && + (github.event.action != 'edited' || github.event.changes.base) runs-on: ubuntu-latest permissions: contents: read @@ -41,9 +45,12 @@ jobs: recheck: name: Recheck open PRs + # a release or merged entry moves the base; a peer's push, reopen, retarget, merge or close can + # add or remove a collision for the higher-numbered PRs if: >- (github.event_name == 'push' && github.ref_name == github.event.repository.default_branch) || - (github.event.action == 'closed' && !github.event.pull_request.merged) + contains(fromJSON('["synchronize", "reopened", "closed"]'), github.event.action) || + (github.event.action == 'edited' && github.event.changes.base) runs-on: ubuntu-latest permissions: contents: read @@ -64,3 +71,13 @@ jobs: BASE_BRANCH: ${{ github.event_name == 'push' && github.ref_name || github.event.pull_request.base.ref }} PEER: ${{ github.event.pull_request.number }} run: python3 .github/scripts/changelog_guard.py + - name: Re-run the check on open PRs into the branch the PR left + if: github.event.action == 'edited' && github.event.changes.base + env: + GH_TOKEN: ${{ github.token }} + REPO: ${{ github.repository }} + MODE: peers + BASE: origin/${{ github.event.changes.base.ref.from }} + BASE_BRANCH: ${{ github.event.changes.base.ref.from }} + PEER: ${{ github.event.pull_request.number }} + run: python3 .github/scripts/changelog_guard.py