Skip to content

Commit 438e07b

Browse files
authored
fix(ci): report maintainer changes introduced to main (#5569)
* fix(ci): compare maintainer changes against main merge Signed-off-by: Jim Meyer <jimeyer@nvidia.com> * fix(ci): retry stale merge refs and parse the workflow step in tests Run the parent check inside the retry loop so a stale refs/pull/N/merge no longer fails the job, and select the workflow step by name from parsed YAML instead of splitting the file text. Signed-off-by: Jim Meyer <jimeyer@nvidia.com> --------- Signed-off-by: Jim Meyer <jimeyer@nvidia.com>
1 parent ad144db commit 438e07b

3 files changed

Lines changed: 135 additions & 2 deletions

File tree

‎.github/workflows/maintainers-change-alert.yml‎

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ name: Maintainers Change Alert
66
on:
77
pull_request_target:
88
types: [opened, reopened, synchronize]
9+
branches: [main]
910
paths:
1011
- MAINTAINERS.md
1112

@@ -76,8 +77,29 @@ jobs:
7677
return 1
7778
}
7879
80+
# GitHub computes the prospective merge asynchronously, and the ref
81+
# can still point at the previous merge right after a push. Retry
82+
# until its parents match this event. Never compare the head file
83+
# directly: it may omit changes already on main.
84+
merge_info="$(mktemp)"
85+
MERGE_SHA=""
86+
for attempt in 1 2 3 4 5 6 7 8 9 10; do
87+
if gh api "repos/$GH_REPO/commits/refs/pull/$PR_NUMBER/merge" > "$merge_info" 2>/dev/null \
88+
&& MERGE_SHA=$(jq -er --arg base "$BASE_SHA" --arg head "$HEAD_SHA" \
89+
'select(.parents | length == 2 and .[0].sha == $base and .[1].sha == $head) | .sha' "$merge_info"); then
90+
break
91+
fi
92+
MERGE_SHA=""
93+
sleep 3
94+
done
95+
rm -f "$merge_info"
96+
if [ -z "$MERGE_SHA" ]; then
97+
echo "::error::Could not obtain a prospective merge commit matching this event's base and head"
98+
exit 1
99+
fi
100+
79101
fetch_maintainers "$BASE_SHA" before.md
80-
fetch_maintainers "$HEAD_SHA" after.md
102+
fetch_maintainers "$MERGE_SHA" after.md
81103
82104
# An unparseable result exits non-zero, but the comment explaining
83105
# why still has to be posted before this job fails.

‎tasks/scripts/alert_maintainer_change_test.py‎

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,13 @@
1010

1111
from __future__ import annotations
1212

13+
import os
14+
import subprocess
15+
import textwrap
16+
from pathlib import Path
17+
1318
import alert_maintainer_change as alert
19+
import yaml
1420

1521
MARKER = "<!-- maintainer-approval-delta -->"
1622

@@ -85,3 +91,108 @@ def test_login_pattern_matches_the_gate() -> None:
8591
import check_maintainer_approval as gate
8692

8793
assert alert.MAINTAINER_RE.pattern == gate.MAINTAINER_RE.pattern
94+
95+
96+
ROOT = Path(__file__).resolve().parents[2]
97+
WORKFLOW = ROOT / ".github/workflows/maintainers-change-alert.yml"
98+
STEP_NAME = "Post the maintainer delta"
99+
100+
101+
def workflow_step() -> dict:
102+
steps = yaml.safe_load(WORKFLOW.read_text())["jobs"]["describe-change"]["steps"]
103+
return next(step for step in steps if step.get("name") == STEP_NAME)
104+
105+
106+
def run_alert_step(tmp_path, merge_responses):
107+
"""Run the actual workflow step against a controlled GitHub API fixture.
108+
109+
`merge_responses` lists the parents the merge ref reports on each
110+
successive call; the last entry repeats once the list is exhausted.
111+
"""
112+
import json
113+
114+
step = workflow_step()
115+
env_values = {
116+
"GH_REPO": "NVIDIA/OpenShell",
117+
"PR_NUMBER": "42",
118+
"BASE_SHA": "base",
119+
"HEAD_SHA": "head",
120+
"COMMENT_MARKER": MARKER,
121+
"GH_TOKEN": "token",
122+
}
123+
# A renamed or added variable must fail here, not pass silently.
124+
assert set(step["env"]) == set(env_values)
125+
for index, parents in enumerate(merge_responses):
126+
(tmp_path / f"merge{index}.json").write_text(
127+
json.dumps({"sha": "merge", "parents": [{"sha": p} for p in parents]})
128+
)
129+
(tmp_path / "merge-count").write_text(str(len(merge_responses)))
130+
(tmp_path / "base.md").write_text(TABLE)
131+
(tmp_path / "merged.md").write_text(
132+
TABLE + "| Jim | [@purp](https://github.com/purp) | NVIDIA |\n"
133+
)
134+
gh = tmp_path / "gh"
135+
gh.write_text(
136+
textwrap.dedent("""\
137+
#!/bin/sh
138+
case "$*" in
139+
*commits/refs/pull/42/merge*)
140+
n=$(cat calls 2>/dev/null || echo 0)
141+
echo $((n + 1)) > calls
142+
last=$(($(cat merge-count) - 1))
143+
[ "$n" -gt "$last" ] && n=$last
144+
cat "merge$n.json" ;;
145+
*contents/MAINTAINERS.md?ref=base*) cat base.md ;;
146+
*contents/MAINTAINERS.md?ref=merge*) cat merged.md ;;
147+
*issues/42/comments*--jq*) printf '1\\n' ;;
148+
*issues/comments/1*) cp body.md posted.md ;;
149+
*) echo "unexpected gh call: $*" >&2; exit 1 ;;
150+
esac
151+
""")
152+
)
153+
gh.chmod(0o755)
154+
# No real waiting between retries.
155+
sleep = tmp_path / "sleep"
156+
sleep.write_text("#!/bin/sh\nexit 0\n")
157+
sleep.chmod(0o755)
158+
env = (
159+
os.environ
160+
| env_values
161+
| {
162+
"PATH": f"{tmp_path}:{os.environ['PATH']}",
163+
"GITHUB_STEP_SUMMARY": str(tmp_path / "summary"),
164+
}
165+
)
166+
(tmp_path / "tasks/scripts").mkdir(parents=True)
167+
(tmp_path / "tasks/scripts/alert_maintainer_change.py").symlink_to(
168+
ROOT / "tasks/scripts/alert_maintainer_change.py"
169+
)
170+
return subprocess.run(
171+
["bash", "-c", step["run"]],
172+
cwd=tmp_path,
173+
env=env,
174+
capture_output=True,
175+
text=True,
176+
)
177+
178+
179+
def test_workflow_reports_delta_from_verified_merge(tmp_path):
180+
result = run_alert_step(tmp_path, [["base", "head"]])
181+
assert result.returncode == 0, result.stderr
182+
body = (tmp_path / "posted.md").read_text()
183+
assert "@purp" in body
184+
assert "Loses approval rights" not in body
185+
186+
187+
def test_workflow_rejects_merge_for_different_head(tmp_path):
188+
result = run_alert_step(tmp_path, [["base", "other-head"]])
189+
assert result.returncode != 0
190+
assert not (tmp_path / "posted.md").exists()
191+
192+
193+
def test_workflow_retries_until_the_merge_ref_catches_up(tmp_path):
194+
# Right after a push the ref still points at the previous merge.
195+
result = run_alert_step(tmp_path, [["base", "old-head"], ["base", "head"]])
196+
assert result.returncode == 0, result.stderr
197+
assert int((tmp_path / "calls").read_text()) == 2
198+
assert "@purp" in (tmp_path / "posted.md").read_text()

‎tasks/test.toml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ hide = true
113113

114114
["test:maintainer-approval"]
115115
description = "Test the maintainer approval gate and change-alert tools"
116-
run = "uv run --no-project --with pytest pytest -o \"python_files=*_test.py\" tasks/scripts/check_maintainer_approval_test.py tasks/scripts/alert_maintainer_change_test.py"
116+
run = "uv run --no-project --with pytest --with pyyaml pytest -o \"python_files=*_test.py\" tasks/scripts/check_maintainer_approval_test.py tasks/scripts/alert_maintainer_change_test.py"
117117
hide = true
118118

119119
["test:review-readiness"]

0 commit comments

Comments
 (0)