Skip to content

Commit 0961019

Browse files
authored
fix(ci): validate pending release comparisons against their commit (#570)
## Summary Avoid the circular link-check failure in #557: its generated comparison references a tag that is only created after release. For the same-repository github-actions release-please PR only, prepare one exact comparison-URL replacement targeting its immutable head SHA. Require a well-formed first release heading, a matching prior release from the event base, and an increasing version. Continue checking the replacement URL; do not ignore it. All unrelated URLs and non-PR runs retain normal checks. No workflow permissions or authentication scope change. ## Verification - Signed head77407dfd05000d270de5bb39c5fea95cffbb73c3, base81f5089915c5e333d323d7fcabf001ecfe2a9983. - Isolated Python3.13/Node22: 779 tests plus58 subtests, Ruff lint/format and make validate passed. - Actual markdown-link-check3.15.0 followed the generated replacement to the release PR commit comparison and returned HTTP200. - Fifteen regression cases cover release context, forks, ordinary users, malformed headings, SHA validation, config/credential preservation and JavaScript RegExp behavior. - Normal signed commit and push hooks passed. Existing actionlint SC2016 on the unchanged envsubst expression reproduces on base; no suppression added. - Independent Git-capable QA/security reviewer inspected the complete three-file diff, pinned checker behavior and updated live release metadata; no blocking findings. Human CODEOWNER review remains required. ## Limits The replacement is only a pre-publication comparison check, not proof that the future tag or release exists. Push and merge-group events keep ordinary tag checks; other release gates remain in force. A changed release head requires fresh workflow/check evidence. ## AI assistance OpenCode assisted investigation, implementation, isolated validation and independent agent review. No broad link exclusions, token changes, or check bypasses.
1 parent a6266dd commit 0961019

3 files changed

Lines changed: 214 additions & 0 deletions

File tree

‎.github/workflows/ci.yml‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,21 @@ jobs:
157157
envsubst '${MLC_GITHUB_TOKEN}' \
158158
< .markdown-link-check.json > .markdown-link-check.ci.json
159159
160+
# Only the generated next-release comparison needs a pre-publication
161+
# target. Check that comparison against the real commit; do not ignore it.
162+
- name: Prepare pending release comparison
163+
if: github.event_name == 'pull_request'
164+
env:
165+
BASE_SHA: ${{ github.event.pull_request.base.sha }}
166+
EVENT_NAME: ${{ github.event_name }}
167+
run: |
168+
git fetch --no-tags origin "$BASE_SHA"
169+
git show "$BASE_SHA:CHANGELOG.md" > "$RUNNER_TEMP/base-changelog.md"
170+
python3 scripts/prepare_link_check.py \
171+
--event-name "$EVENT_NAME" --event "$GITHUB_EVENT_PATH" \
172+
--changelog CHANGELOG.md --base-changelog "$RUNNER_TEMP/base-changelog.md" \
173+
--config .markdown-link-check.ci.json
174+
160175
- uses: tcort/github-action-markdown-link-check@e047c5b37f24ab722bbef1a27b6fab7f96bc4068 # v1.1.3
161176
with:
162177
config-file: ".markdown-link-check.ci.json"

‎scripts/prepare_link_check.py‎

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
"""Check a pending release comparison against its immutable PR head, not a future tag."""
2+
3+
from __future__ import annotations
4+
5+
import argparse
6+
import json
7+
import re
8+
from pathlib import Path
9+
10+
REPOSITORY = "GSA-TTS/agentic-coding-patterns"
11+
VERSION = r"[0-9]+\.[0-9]+\.[0-9]+"
12+
HEADING = re.compile(
13+
rf"^## \[({VERSION})\]\(https://github\.com/{REPOSITORY}/compare/"
14+
rf"v({VERSION})\.\.\.v({VERSION})\) \([0-9]{{4}}-[0-9]{{2}}-[0-9]{{2}}\)$"
15+
)
16+
17+
18+
def first_heading(text: str) -> re.Match[str] | None:
19+
"""Do not search past a malformed first release heading."""
20+
for line in text.splitlines():
21+
if line.startswith("## "):
22+
return HEADING.fullmatch(line)
23+
return None
24+
25+
26+
def pending_comparison(event_name: str, event: dict, changelog: str, base: str) -> dict | None:
27+
"""Return one exact replacement only for the same-repository release-bot PR."""
28+
if event_name != "pull_request":
29+
return None
30+
pr = event.get("pull_request", {})
31+
head = pr.get("head", {})
32+
target = pr.get("base", {})
33+
if not (
34+
event.get("repository", {}).get("full_name") == REPOSITORY
35+
and head.get("repo", {}).get("full_name") == REPOSITORY
36+
and target.get("repo", {}).get("full_name") == REPOSITORY
37+
and head.get("ref") == "release-please--branches--main"
38+
and target.get("ref") == "main"
39+
and pr.get("user", {}).get("login") == "github-actions[bot]"
40+
and pr.get("user", {}).get("type") == "Bot"
41+
):
42+
return None
43+
sha = head.get("sha", "")
44+
if not re.fullmatch(r"[0-9a-f]{40}", sha):
45+
raise ValueError("release head is not an immutable commit SHA")
46+
new, old = first_heading(changelog), first_heading(base)
47+
if new is None or old is None:
48+
raise ValueError("release comparison heading is malformed")
49+
version, previous, destination = new.groups()
50+
if version != destination or previous != old.group(1):
51+
raise ValueError("release comparison does not match the base release")
52+
if tuple(map(int, version.split("."))) <= tuple(map(int, previous.split("."))):
53+
raise ValueError("pending release version must increase")
54+
prefix = f"https://github.com/{REPOSITORY}/compare/v{previous}..."
55+
# JavaScript RegExp consumes this pattern; only literal URL characters need escaping.
56+
return {"pattern": "^" + re.escape(prefix + "v" + version) + "$", "replacement": prefix + sha}
57+
58+
59+
def main() -> None:
60+
parser = argparse.ArgumentParser(description=__doc__)
61+
parser.add_argument("--event-name", required=True)
62+
parser.add_argument("--event", type=Path, required=True)
63+
parser.add_argument("--changelog", type=Path, required=True)
64+
parser.add_argument("--base-changelog", type=Path, required=True)
65+
parser.add_argument("--config", type=Path, required=True)
66+
args = parser.parse_args()
67+
rule = pending_comparison(
68+
args.event_name, json.loads(args.event.read_text()), args.changelog.read_text(), args.base_changelog.read_text()
69+
)
70+
if rule is not None:
71+
config = json.loads(args.config.read_text())
72+
config.setdefault("replacementPatterns", []).append(rule)
73+
args.config.write_text(json.dumps(config, indent=2) + "\n")
74+
print("Pending release comparison will be checked against its immutable PR head.")
75+
76+
77+
if __name__ == "__main__":
78+
main()
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
"""The temporary release link target is exact and never a broad exclusion."""
2+
3+
import copy
4+
import json
5+
import re
6+
import subprocess
7+
import sys
8+
from pathlib import Path
9+
10+
import pytest
11+
from prepare_link_check import REPOSITORY, pending_comparison
12+
13+
SHA = "a" * 40
14+
BASE = f"# Changelog\n\n## [1.18.0](https://github.com/{REPOSITORY}/compare/v1.17.0...v1.18.0) (2026-10-28)\n"
15+
PENDING = f"# Changelog\n\n## [1.19.0](https://github.com/{REPOSITORY}/compare/v1.18.0...v1.19.0) (2026-11-03)\n"
16+
EVENT = {
17+
"repository": {"full_name": REPOSITORY},
18+
"pull_request": {
19+
"user": {"login": "github-actions[bot]", "type": "Bot"},
20+
"head": {"sha": SHA, "ref": "release-please--branches--main", "repo": {"full_name": REPOSITORY}},
21+
"base": {"ref": "main", "repo": {"full_name": REPOSITORY}},
22+
},
23+
}
24+
25+
26+
def test_exact_pending_url_checked_against_sha():
27+
rule = pending_comparison("pull_request", EVENT, PENDING, BASE)
28+
url = f"https://github.com/{REPOSITORY}/compare/v1.18.0...v1.19.0"
29+
assert re.fullmatch(rule["pattern"], url)
30+
assert rule["replacement"] == f"https://github.com/{REPOSITORY}/compare/v1.18.0...{SHA}"
31+
for other in (url + "/bad", url + "?bad", url.replace("1.19.0", "1.20.0"), url.replace("github.com", "other.test")):
32+
assert not re.fullmatch(rule["pattern"], other)
33+
34+
35+
@pytest.mark.parametrize("name", ["push", "merge_group", "workflow_dispatch"])
36+
def test_non_pr_has_no_exception(name):
37+
assert pending_comparison(name, EVENT, PENDING, BASE) is None
38+
39+
40+
@pytest.mark.parametrize(
41+
"field,value",
42+
[
43+
("user", {"login": "contributor", "type": "User"}),
44+
("head", {"sha": SHA, "ref": "release-please--branches--main", "repo": {"full_name": "other/fork"}}),
45+
("head", {"sha": SHA, "ref": "feature", "repo": {"full_name": REPOSITORY}}),
46+
],
47+
)
48+
def test_non_release_context_keeps_normal_checks(field, value):
49+
event = copy.deepcopy(EVENT)
50+
event["pull_request"][field] = value
51+
assert pending_comparison("pull_request", event, PENDING, BASE) is None
52+
53+
54+
@pytest.mark.parametrize(
55+
"text",
56+
[
57+
PENDING.replace("v1.18.0...", "v0.0.0..."),
58+
PENDING.replace("[1.19.0]", "[1.20.0]"),
59+
PENDING.replace("github.com", "github.com.evil.test"),
60+
"## malformed\n" + PENDING,
61+
BASE,
62+
],
63+
)
64+
def test_bad_heading_fails_closed(text):
65+
with pytest.raises(ValueError):
66+
pending_comparison("pull_request", EVENT, text, BASE)
67+
68+
69+
def test_head_must_be_commit_not_branch():
70+
event = copy.deepcopy(EVENT)
71+
event["pull_request"]["head"]["sha"] = "main"
72+
with pytest.raises(ValueError):
73+
pending_comparison("pull_request", event, PENDING, BASE)
74+
75+
76+
def test_cli_preserves_other_checks_and_credentials(tmp_path):
77+
config = {"ignorePatterns": [{"pattern": "^#"}], "httpHeaders": [{"headers": {"Authorization": "fixture"}}]}
78+
paths = {}
79+
files = {"event": json.dumps(EVENT), "changelog": PENDING, "base": BASE, "config": json.dumps(config)}
80+
for name, text in files.items():
81+
paths[name] = tmp_path / name
82+
paths[name].write_text(text)
83+
result = subprocess.run(
84+
[
85+
sys.executable,
86+
str(Path(__file__).parents[1] / "prepare_link_check.py"),
87+
"--event-name",
88+
"pull_request",
89+
"--event",
90+
str(paths["event"]),
91+
"--changelog",
92+
str(paths["changelog"]),
93+
"--base-changelog",
94+
str(paths["base"]),
95+
"--config",
96+
str(paths["config"]),
97+
],
98+
capture_output=True,
99+
text=True,
100+
check=True,
101+
timeout=10,
102+
)
103+
actual = json.loads(paths["config"].read_text())
104+
assert actual.pop("replacementPatterns") == [pending_comparison("pull_request", EVENT, PENDING, BASE)]
105+
assert actual == config
106+
assert "fixture" not in result.stdout + result.stderr
107+
108+
109+
def test_replacement_uses_javascript_regexp_semantics():
110+
rule = pending_comparison("pull_request", EVENT, PENDING, BASE)
111+
url = f"https://github.com/{REPOSITORY}/compare/v1.18.0...v1.19.0"
112+
javascript = """
113+
const rule = JSON.parse(process.argv[1]);
114+
const url = process.argv[2];
115+
const pattern = new RegExp(rule.pattern);
116+
if (url.replace(pattern, rule.replacement) !== rule.replacement) process.exit(1);
117+
for (const suffix of ['/bad', '?bad', '#bad']) {
118+
if ((url + suffix).replace(pattern, rule.replacement) !== url + suffix) process.exit(2);
119+
}
120+
"""
121+
subprocess.run(["node", "-e", javascript, json.dumps(rule), url], check=True, timeout=10)

0 commit comments

Comments
 (0)