diff --git a/scripts/pr_check.py b/scripts/pr_check.py index cb0ef3ef9e..c92c573e51 100644 --- a/scripts/pr_check.py +++ b/scripts/pr_check.py @@ -37,6 +37,7 @@ # counts are checked where they are used), and those of them that are numbers SMOKE_FIELDS = ("title", "seconds", "frames shown") SMOKE_NUMBERS = ("seconds", "frames shown") +COMMIT_SHA = re.compile(r"[0-9a-f]{7,40}", re.I) def smoke_blocks(body): @@ -68,6 +69,14 @@ def field_problems(block, run): return out +def commit_sha(block): + """The 7-40 hex digit commit SHA at the start of a smoke block's `commit:` line, or None.""" + parts = block.get("commit", "").split() + if parts and COMMIT_SHA.fullmatch(parts[0]): + return parts[0].lower() + return None + + def runtime(path): return path.startswith("crates/xcl/src/") and "/tests" not in path and not path.endswith("tests.rs") @@ -129,13 +138,14 @@ def check(body, head, files, is_main_ancestor, *, test_only=False): if not test_only and any(runtime(p) or builds_runtime(p) for p, *_ in files): blocks = smoke_blocks(body) - mine = [b for b in blocks if len(b.get("commit", "")) >= 7 and head.startswith(b["commit"].split()[0])] + head_sha = head.lower() + mine = [b for b in blocks if (sha := commit_sha(b)) and head_sha.startswith(sha)] clean = [b for b in mine if "uncommitted" not in b.get("commit", "")] - base = [b for b in blocks if b not in mine and len(b.get("commit", "").split()[0] if b.get("commit") else "") >= 7] + base = [b for b in blocks if b not in mine and commit_sha(b)] if not clean: problems.append(f"the PR changes the runtime: **Regression run** needs an `xcl-smoke` block made on this PR's " f"head commit {head[:12]} with no uncommitted changes (rerun scripts/smoke.ps1 after every push)") - main = [b for b in base if is_main_ancestor(b["commit"].split()[0])] + main = [b for b in base if is_main_ancestor(commit_sha(b))] if not main: problems.append("**Regression run** needs an `xcl-smoke` block made on a commit of main (not the PR's), " "with the same title and time as the PR's") @@ -188,6 +198,8 @@ def blob_size(sha): files.append((parts[0], parts[1], parts[2] == "true", lambda sha=sha: blob_size(sha))) def is_main_ancestor(sha): + if not COMMIT_SHA.fullmatch(sha): + return False if o.local: return subprocess.run(["git", "merge-base", "--is-ancestor", sha, "origin/main"], capture_output=True).returncode == 0 r = subprocess.run(["gh", "api", f"repos/{o.repo}/compare/{sha}...main", "--jq", ".status"], capture_output=True, text=True) diff --git a/scripts/test_pr_check.py b/scripts/test_pr_check.py index 8cc45d5d06..1cb0f69edf 100644 --- a/scripts/test_pr_check.py +++ b/scripts/test_pr_check.py @@ -102,6 +102,66 @@ def test_non_numeric_frames_shown_fails(self): self.assertIn("the PR's regression run (`xcl-smoke` block of commit 1234567890) " "has a non-numeric `frames shown`: many", problems) + def test_non_hex_main_commits_are_refused_before_ancestor_check(self): + files = [changed("crates/xcl/src/hle/d3d12x/vram.rs")] + run = "title: G\nseconds: 60\nframes shown: 100\n" + bad_commits = ( + "refs/heads/main", + "origin/main", + "main~00", + "HEAD~000", + "47efa2e~0", + "../../pulls", + "main?per_page=1", + "--version", + "123456", + "a" * 41, + ) + for bad in bad_commits: + with self.subTest(commit=bad): + seen = [] + body = ( + f"{BODY}```\nxcl-smoke v1\ncommit: {bad}\n{run}```\n" + f"```\nxcl-smoke v1\ncommit: {HEAD}\n{run}```\n" + ) + problems = check(body, HEAD, files, lambda sha, seen=seen: (seen.append(sha), True)[1]) + self.assertEqual([], seen) + self.assertEqual(1, len(problems)) + self.assertIn("commit of main", problems[0]) + + def test_short_or_non_hex_head_commits_are_refused(self): + files = [changed("crates/xcl/src/hle/d3d12x/vram.rs")] + run = "title: G\nseconds: 60\nframes shown: 100\n" + for bad in ("1 some other", "123456", "1234567~0", "refs/heads/pr"): + with self.subTest(commit=bad): + body = ( + f"{BODY}```\nxcl-smoke v1\ncommit: 1111111aaaa\n{run}```\n" + f"```\nxcl-smoke v1\ncommit: {bad}\n{run}```\n" + ) + problems = check(body, HEAD, files, lambda sha: sha == "1111111aaaa") + self.assertEqual(1, len(problems)) + self.assertIn("head commit", problems[0]) + + def test_valid_hex_commits_pass_case_insensitively(self): + files = [changed("crates/xcl/src/hle/d3d12x/vram.rs")] + run = "title: G\nseconds: 60\nframes shown: 100\n" + full_main = "a" * 40 + full_head = "1234567890abcdef" + "0" * 24 + for main_sha, pr_sha in ( + ("1111111", full_head[:7]), + ("1111111AAAA", full_head[:12].upper()), + (full_main.upper(), full_head.upper()), + ): + with self.subTest(main_sha=main_sha, pr_sha=pr_sha): + seen = [] + body = ( + f"{BODY}```\nxcl-smoke v1\ncommit: {main_sha}\n{run}```\n" + f"```\nxcl-smoke v1\ncommit: {pr_sha}\n{run}```\n" + ) + problems = check(body, full_head, files, lambda sha, seen=seen: (seen.append(sha), sha == main_sha.lower())[1]) + self.assertEqual([main_sha.lower()], seen) + self.assertEqual([], problems) + class TemplatePlaceholderTests(unittest.TestCase): def test_untouched_pr_template_fails_all_required_sections(self):