diff --git a/scripts/pr_check.py b/scripts/pr_check.py index 3a526b4a9..8757e0cdd 100644 --- a/scripts/pr_check.py +++ b/scripts/pr_check.py @@ -31,6 +31,7 @@ MAX_BINARY = 1 << 20 FRAMES_KEPT = 0.8 # the PR's frames at least this share of main's COMMIT_GROWTH = 1.25 # the PR's peak commit at most this times main's +COMMIT_SHA = re.compile(r"[0-9a-f]{7,40}", re.I) def smoke_blocks(body): @@ -52,6 +53,14 @@ def number(s): return int(m.group(1)) if m else None +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") @@ -113,13 +122,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") @@ -169,6 +179,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 e5de366a5..ce4d80354 100644 --- a/scripts/test_pr_check.py +++ b/scripts/test_pr_check.py @@ -63,6 +63,63 @@ def test_blocks_in_separate_fences_stay_separate(self): "```\nxcl-smoke v1\ncommit: 1234567890\ntitle: G\n```\n") self.assertEqual(["1111111aaaa", "1234567890"], [b["commit"] for b in smoke_blocks(body)]) + def test_non_hex_main_commits_are_refused_before_ancestor_check(self): + files = [changed("crates/xcl/src/hle/d3d12x/vram.rs")] + 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}\ntitle: G\nseconds: 60\n```\n" + f"```\nxcl-smoke v1\ncommit: {HEAD}\ntitle: G\nseconds: 60\n```\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")] + 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\ntitle: G\nseconds: 60\n```\n" + f"```\nxcl-smoke v1\ncommit: {bad}\ntitle: G\nseconds: 60\n```\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")] + 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}\ntitle: G\nseconds: 60\n```\n" + f"```\nxcl-smoke v1\ncommit: {pr_sha}\ntitle: G\nseconds: 60\n```\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):