Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion scripts/pr_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

files.tsv: one line per file of the PR – path, status, binary (true/false), blob sha. --local
asks git instead of the GitHub API (whether a commit is on main, a blob's size).
For a fork with a fetched upstream, use --local --main-ref upstream/main.
--test-only skips only the smoke check. GitHub Actions passes it after a test-only label approval.
"""
import argparse, os, re, subprocess, sys
Expand Down Expand Up @@ -170,6 +171,7 @@ def main():
a.add_argument("--files", required=True)
a.add_argument("--repo", default=os.environ.get("REPO", ""))
a.add_argument("--local", action="store_true")
a.add_argument("--main-ref", default="origin/main", help="Git ref for main in --local mode (default: %(default)s)")
a.add_argument("--test-only", action="store_true", help="skip the retail smoke check for an approved test-only diff")
o = a.parse_args()
body = open(o.body, encoding="utf-8").read()
Expand All @@ -189,7 +191,7 @@ def blob_size(sha):

def is_main_ancestor(sha):
if o.local:
return subprocess.run(["git", "merge-base", "--is-ancestor", sha, "origin/main"], capture_output=True).returncode == 0
return subprocess.run(["git", "merge-base", "--is-ancestor", sha, o.main_ref], capture_output=True).returncode == 0
r = subprocess.run(["gh", "api", f"repos/{o.repo}/compare/{sha}...main", "--jq", ".status"], capture_output=True, text=True)
return r.returncode == 0 and r.stdout.strip() in ("ahead", "identical")

Expand Down
36 changes: 36 additions & 0 deletions scripts/test_pr_check.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
from pathlib import Path
import subprocess
import sys
import tempfile
import unittest

from pr_check import check, smoke_blocks
Expand Down Expand Up @@ -103,6 +106,39 @@ def test_non_numeric_frames_shown_fails(self):
"has a non-numeric `frames shown`: many", problems)


class LocalMainRefTests(unittest.TestCase):
def test_local_check_uses_requested_main_ref(self):
with tempfile.TemporaryDirectory() as tmp:
def git(*args):
return subprocess.check_output(["git", "-c", "user.name=Test", "-c", "user.email=test@example.com",
"-c", "commit.gpgSign=false", *args], cwd=tmp, text=True).strip()

git("init", "--quiet")
git("commit", "--quiet", "--allow-empty", "-m", "Fork main")
git("update-ref", "refs/remotes/origin/main", "HEAD")
git("commit", "--quiet", "--allow-empty", "-m", "Upstream main")
git("update-ref", "refs/remotes/upstream/main", "HEAD")
base = git("rev-parse", "HEAD")

body = Path(tmp) / "body.md"
files = Path(tmp) / "files.tsv"
run = ("title: G\nseconds: 60\nexit: 0x0\nframes shown: 100\n"
"device removals: 0\ncrashes: 0\npeak commit: 4000 MB\n")
body.write_text(BODY + "".join(f"```\nxcl-smoke v1\ncommit: {sha}\n{run}```\n"
for sha in (base, HEAD)), encoding="utf-8")
files.write_text("crates/xcl/src/lib.rs\tmodified\tfalse\tunused\n", encoding="utf-8")
script = Path(__file__).resolve().with_name("pr_check.py")
command = [sys.executable, str(script), "--body", str(body), "--head", HEAD, "--files", str(files), "--local"]

default = subprocess.run(command, cwd=tmp, capture_output=True, text=True)
self.assertEqual(1, default.returncode, default.stderr)
self.assertIn("made on a commit of main", default.stdout)

upstream = subprocess.run([*command, "--main-ref", "upstream/main"], cwd=tmp, capture_output=True, text=True)
self.assertEqual(0, upstream.returncode, upstream.stdout + upstream.stderr)
self.assertIn("description ok", upstream.stdout)


class TemplatePlaceholderTests(unittest.TestCase):
def test_untouched_pr_template_fails_all_required_sections(self):
template = Path(__file__).resolve().parent.parent / ".github" / "pull_request_template.md"
Expand Down
Loading