@@ -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\n on: {on}\n jobs:\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 + '\n trailing 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' ] = '\n PY\n raise 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