Skip to content

Commit 041771d

Browse files
luisleo526claude
andcommitted
ci: resolve the merged PR with python3, not jq (CR-231)
The preflight host has no jq, so the behavioural resolver tests failed with exit 127 before any selection ran. The resolver now parses the paginated gh response with python3's json module, passing the SHA and the page JSON through the environment; the gh call, its argv, the four PR conditions and the exactly-one-match rule are unchanged. Malformed JSON, JSON constants, or a shape that is not an array of page arrays of PR objects exits green with a notice and ok=false, never merge metadata. The exacttree step keeps jq -es (it runs only on the real runner). Pins and tests updated to require the python selection; the tests still run the workflow's own Resolve step under bash with only gh faked. docs/ci.md: rewrites the resolver sentence introduced by 99a25b7; no rule sentence is removed. Co-Authored-By: Claude Code <[email protected]>
1 parent 99a25b7 commit 041771d

4 files changed

Lines changed: 133 additions & 18 deletions

File tree

‎.github/workflows/promote-baseline.yml‎

Lines changed: 45 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -145,18 +145,51 @@ jobs:
145145
# Search every page: a second match must defer, never pick a PR.
146146
pulls=$(gh api --paginate --slurp \
147147
"repos/${{ github.repository }}/commits/$SHA/pulls?per_page=100")
148-
matches=$(jq -c --arg sha "$SHA" '
149-
[.[][] | select(.state == "closed" and .merged_at != null
150-
and .merge_commit_sha == $sha and .base.ref == "main")]
151-
' <<< "$pulls")
152-
count=$(jq 'length' <<< "$matches")
153-
if [ "$count" != "1" ]; then
154-
echo "::notice::found $count merged PRs with merge commit $SHA and base main; skipping"
155-
echo "ok=false" >> "$GITHUB_OUTPUT"; exit 0
156-
fi
157-
jq -r '.[0] | "merge_sha=\(.merge_commit_sha)", "head_sha=\(.head.sha)",
158-
"pr_number=\(.number)", "pr_url=\(.html_url)", "merged_at=\(.merged_at)",
159-
"ok=true"' <<< "$matches" >> "$GITHUB_OUTPUT"
148+
PULLS="$pulls" python3 - <<'PY'
149+
import json
150+
import os
151+
152+
def skip(reason):
153+
print(f"::notice::{reason}; skipping")
154+
with open(os.environ["GITHUB_OUTPUT"], "a", encoding="utf-8") as output:
155+
output.write("ok=false\n")
156+
157+
def reject_constant(value):
158+
raise ValueError(f"invalid JSON constant: {value}")
159+
160+
sha = os.environ["SHA"]
161+
try:
162+
pages = json.loads(os.environ["PULLS"], parse_constant=reject_constant)
163+
except ValueError:
164+
skip("invalid merged PR response JSON")
165+
raise SystemExit(0)
166+
if (not isinstance(pages, list)
167+
or any(not isinstance(page, list) for page in pages)
168+
or any(not isinstance(pr, dict) for page in pages for pr in page)):
169+
skip("invalid merged PR response page array")
170+
raise SystemExit(0)
171+
matches = [pr for page in pages for pr in page
172+
if pr.get("state") == "closed"
173+
and pr.get("merged_at") is not None
174+
and pr.get("merge_commit_sha") == sha
175+
and isinstance(pr.get("base"), dict)
176+
and pr["base"].get("ref") == "main"]
177+
if len(matches) != 1:
178+
skip(f"found {len(matches)} merged PRs with merge commit {sha} and base main")
179+
raise SystemExit(0)
180+
pr = matches[0]
181+
values = {
182+
"merge_sha": pr["merge_commit_sha"],
183+
"head_sha": pr["head"]["sha"],
184+
"pr_number": pr["number"],
185+
"pr_url": pr["html_url"],
186+
"merged_at": pr["merged_at"],
187+
"ok": "true",
188+
}
189+
with open(os.environ["GITHUB_OUTPUT"], "a", encoding="utf-8") as output:
190+
for key, value in values.items():
191+
output.write(f"{key}={value}\n")
192+
PY
160193
161194
# The safety property is the TREE: the snapshot describes the gated
162195
# head's tree, so we promote only when the merge commit now on the base

‎docs/ci.md‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1163,7 +1163,12 @@ refusing both PR events. The named promotion exception requires exactly
11631163
`push` and `workflow_dispatch`, pins the push filter to `branches: [main]`,
11641164
and requires the shared concurrency group without cancellation. Its tests
11651165
run the resolver's own shell against fixture API responses, asserting all
1166-
five merge values or a green notice with no metadata. The resolved PR still
1166+
five merge values or a green notice with no metadata. The resolver uses
1167+
`python3` and the standard-library `json` module, with the SHA and page JSON
1168+
passed through the environment. Malformed JSON or a response that is not an
1169+
array of page arrays of PR objects exits green with a notice and `ok=false`,
1170+
without merge metadata. The pins retain all four PR conditions and the
1171+
exactly-one rule; the tests fake only `gh` and need no `jq`. The resolved PR still
11671172
passes the exact-tree and newest head-status checks before credentials.
11681173
Both workflows require exact runner values without a fallback and carry no
11691174
`PF_CI_LABELS`.

‎scripts/ci_preflight.py‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -895,8 +895,22 @@ def ci_workflow_findings(ci: str, native: str, promote: str, cmake: str,
895895
'INPUT_PR_URL: ${{ github.event.inputs.pr_url }}',
896896
'INPUT_MERGED_AT: ${{ github.event.inputs.merged_at }}',
897897
)
898+
resolver_selection = (
899+
'PULLS="$pulls" python3 - <<\'PY\'',
900+
'sha = os.environ["SHA"]',
901+
'pages = json.loads(os.environ["PULLS"], parse_constant=reject_constant)',
902+
('matches = [pr for page in pages for pr in page '
903+
'if pr.get("state") == "closed" '
904+
'and pr.get("merged_at") is not None '
905+
'and pr.get("merge_commit_sha") == sha '
906+
'and isinstance(pr.get("base"), dict) '
907+
'and pr["base"].get("ref") == "main"]'),
908+
'if len(matches) != 1:',
909+
)
910+
resolver_code = ' '.join(line.strip() for line in _code(resolved))
898911
if (not resolved or _step_values(resolved, 'if')
899912
or any(value not in resolved for value in resolver_env)
913+
or any(value not in resolver_code for value in resolver_selection)
900914
or 'gh api --paginate --slurp' not in resolved
901915
or 'repos/${{ github.repository }}/commits/$SHA/pulls?per_page=100' not in resolved
902916
or 'github.event.pull_request' in promote):

‎scripts/test_ci_preflight.py‎

Lines changed: 68 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1225,6 +1225,25 @@ def test_baseline_promotion_keeps_resolution_and_guards_before_credentials(self)
12251225
'"$HEAD_SHA:refs/remotes/origin/pr-head"')
12261226
self.assertIn('baseline promotion must admit a squash merge by tree equality only', findings)
12271227

1228+
def test_baseline_promotion_pins_python_selection_and_unique_match(self):
1229+
for before, after in (
1230+
('PULLS="$pulls" python3 - <<\'PY\'', "python3 - <<'PY'"),
1231+
('sha = os.environ["SHA"]', 'sha = "a" * 40'),
1232+
('pages = json.loads(os.environ["PULLS"], parse_constant=reject_constant)',
1233+
'pages = [[]]'),
1234+
('matches = [pr for page in pages for pr in page',
1235+
'matches = [pr for page in pages[:1] for pr in page'),
1236+
('if pr.get("state") == "closed"', 'if True'),
1237+
('and pr.get("merged_at") is not None', 'and True'),
1238+
('and pr.get("merge_commit_sha") == sha', 'and True'),
1239+
('and isinstance(pr.get("base"), dict)', 'and True'),
1240+
('and pr["base"].get("ref") == "main"]', 'and True]'),
1241+
('if len(matches) != 1:', 'if not matches:')):
1242+
with self.subTest(selection=after):
1243+
findings = self.mutated_file_findings(2, 'promote', before, after)
1244+
self.assertIn('baseline promotion must resolve the pushed SHA or dispatch '
1245+
'inputs without a PR payload', findings)
1246+
12281247
def test_new_workflows_require_spot_routing_with_hosted_exceptions(self):
12291248
original, others = self.ci_sources(), self.other_sources()
12301249
new = 'name: lint\non: {on}\njobs:\n lint:\n runs-on: {runner}\n steps:\n - run: "true"\n'
@@ -1705,15 +1724,15 @@ def merged_pr(self, *, sha=None, head=None, number=231,
17051724
'html_url': f'https://github.com/owner/engine/pull/{number}',
17061725
'merged_at': merged_at, 'base': {'ref': 'main'}}
17071726

1708-
def run_resolver(self, pages, *, sha=None, event='push', inputs=None):
1727+
def run_resolver(self, pages, *, sha=None, event='push', inputs=None, raw_json=None):
17091728
workflow = (ROOT / CI_SOURCES[2]).read_text()
17101729
script = job_script(workflow, 'promote', 'Resolve the merged PR')
17111730
script = script.replace('${{ github.repository }}', 'owner/engine')
17121731
self.assertNotIn('${{', script)
17131732
with tempfile.TemporaryDirectory() as directory:
17141733
root = Path(directory)
17151734
gh = root / 'gh'
1716-
# Only the API transport is fake; bash and jq run the workflow's
1735+
# Only the API transport is fake; bash and python3 run the workflow's
17171736
# actual selection and output formatting against the fixture JSON.
17181737
gh.write_text('#!' + sys.executable + '\n'
17191738
'import os, pathlib, sys\n'
@@ -1726,7 +1745,8 @@ def run_resolver(self, pages, *, sha=None, event='push', inputs=None):
17261745
output = root / 'outputs'
17271746
log = root / 'lookup'
17281747
env = {**os.environ, 'EVENT_NAME': event, 'SHA': sha or self.SHA,
1729-
'GH_TOKEN': 'fixture-token', 'PR_PAGES': json.dumps(pages),
1748+
'GH_TOKEN': 'fixture-token',
1749+
'PR_PAGES': json.dumps(pages) if raw_json is None else raw_json,
17301750
'INPUT_MERGE_SHA': '', 'INPUT_HEAD_SHA': '', 'INPUT_PR_NUMBER': '',
17311751
'INPUT_PR_URL': '', 'INPUT_MERGED_AT': '', **(inputs or {}),
17321752
'LOOKUP_LOG': str(log), 'GITHUB_OUTPUT': str(output),
@@ -1756,20 +1776,52 @@ def test_push_returns_exact_five_values_for_one_merged_pr(self):
17561776
def test_push_defers_with_notice_and_no_metadata_without_one_match(self):
17571777
pr = self.merged_pr()
17581778
second = self.merged_pr(head='c' * 40, number=466)
1779+
missing_merged_at = {key: value for key, value in pr.items() if key != 'merged_at'}
17591780
cases = (
17601781
('another merge commit', [[dict(pr, merge_commit_sha='d' * 40)]]),
1761-
('open', [[dict(pr, state='open', merged_at=None)]]),
1782+
('open with merge time', [[dict(pr, state='open')]]),
17621783
('closed unmerged', [[dict(pr, merged_at=None)]]),
1784+
('missing merge time', [[missing_merged_at]]),
17631785
('two matches', [[pr, second]]),
17641786
('two matches across pages', [[pr], [second]]),
17651787
('empty list', [[]]),
1788+
('no pages', []),
17661789
('another base', [[dict(pr, base={'ref': 'release'})]]),
1790+
('null base', [[dict(pr, base=None)]]),
1791+
('missing base', [[{key: value for key, value in pr.items() if key != 'base'}]]),
17671792
)
17681793
for name, pages in cases:
17691794
with self.subTest(case=name):
17701795
values, stdout, called = self.run_resolver(pages)
17711796
self.assertEqual(values, {'ok': 'false'})
1772-
self.assertIn('::notice::', stdout)
1797+
count = 2 if name.startswith('two matches') else 0
1798+
self.assertEqual(stdout, f'::notice::found {count} merged PRs with merge '
1799+
f'commit {self.SHA} and base main; skipping\n')
1800+
self.assertTrue(called)
1801+
1802+
def test_push_defers_on_malformed_json_without_metadata(self):
1803+
valid_pages = json.dumps([[self.merged_pr()]])
1804+
invalid_constants = [
1805+
json.dumps([[dict(self.merged_pr(), merged_at=None)]]).replace('null', constant)
1806+
for constant in ('NaN', 'Infinity', '-Infinity')]
1807+
for raw_json in ('', '{', valid_pages[:-1], valid_pages + '\ntrailing garbage',
1808+
*invalid_constants):
1809+
with self.subTest(raw_json=raw_json):
1810+
values, stdout, called = self.run_resolver(None, raw_json=raw_json)
1811+
self.assertEqual(values, {'ok': 'false'})
1812+
self.assertEqual(stdout, '::notice::invalid merged PR response JSON; skipping\n')
1813+
self.assertTrue(called)
1814+
1815+
def test_push_requires_an_array_of_page_arrays_of_pr_objects(self):
1816+
pr = self.merged_pr()
1817+
for pages in (None, {}, {'pages': [[pr]]}, pr, 231, True, 'not an array',
1818+
[pr], [[pr], None], [[pr], {'other': pr}],
1819+
[[pr], [None]], [[pr], [231]], [[pr], ['not a PR']]):
1820+
with self.subTest(pages=pages):
1821+
values, stdout, called = self.run_resolver(pages)
1822+
self.assertEqual(values, {'ok': 'false'})
1823+
self.assertEqual(stdout,
1824+
'::notice::invalid merged PR response page array; skipping\n')
17731825
self.assertTrue(called)
17741826

17751827
def test_push_selects_matching_pr_across_pages_among_unrelated_prs(self):
@@ -1785,6 +1837,17 @@ def test_push_selects_matching_pr_across_pages_among_unrelated_prs(self):
17851837
self.assertNotIn('::notice::', stdout)
17861838
self.assertTrue(called)
17871839

1840+
def test_push_treats_json_quotes_and_shell_text_as_data(self):
1841+
pr = self.merged_pr()
1842+
pr['title'] = '\nPY\nraise SystemExit(99)\n\'"$()`\n'
1843+
pr['html_url'] += '?literal=$(printf injected)`printf injected`\'";'
1844+
values, stdout, called = self.run_resolver([[pr]])
1845+
self.assertEqual(values, {
1846+
'merge_sha': self.SHA, 'head_sha': self.HEAD, 'pr_number': '231',
1847+
'pr_url': pr['html_url'], 'merged_at': pr['merged_at'], 'ok': 'true'})
1848+
self.assertNotIn('::notice::', stdout)
1849+
self.assertTrue(called)
1850+
17881851
def test_dispatch_returns_exact_inputs_without_a_pr_lookup(self):
17891852
values, stdout, called = self.run_resolver(
17901853
[[self.merged_pr()]], sha='f' * 40, event='workflow_dispatch', inputs={

0 commit comments

Comments
 (0)