Skip to content

armgit / cua

Public Imported from GitHub
0

Scale computer-use 2.0 with open-source drivers, cross-OS sandboxes, and benchmarks for training, evaluation, and data generation.

fix(ci): validate attribution on pull requests past 250 commits (#4303)

ddupont4166a59808e3
diff --git a/.github/scripts/release_attribution.py b/.github/scripts/release_attribution.py
index 01414f0646..c2766cd3c1 100644
--- a/.github/scripts/release_attribution.py
+++ b/.github/scripts/release_attribution.py
@@ -120,6 +120,10 @@ class ConventionalEntry:
breaking: bool
+# The pull request commits endpoint lists at most 250 commits.
+PULL_COMMITS_ENDPOINT_CAP = 250
+MAX_COMPARE_PAGES = 100
+
class GitHubClient:
"""Small GitHub REST client with explicit, testable endpoints."""
@@ -167,6 +171,49 @@ class GitHubClient:
return commits
raise ReleaseError(f"pull request #{number} has too many commits to validate safely")
+ def compare_commits(self, repository: str, base: str, head: str) -> tuple[list[dict[str, Any]], int]:
+ """Every commit in ``base...head`` and GitHub's total, paging the compare API.
+
+ The pull request commits endpoint stops at 250 commits; the compare
+ endpoint pages through all of them.
+ """
+ commits: list[dict[str, Any]] = []
+ spec = f"{quote(base, safe='')}...{quote(head, safe='')}"
+ total = -1
+ for page in range(1, MAX_COMPARE_PAGES + 1):
+ payload = self.get(f"repos/{repository}/compare/{spec}?per_page=100&page={page}")
+ total = int(payload.get("total_commits") or 0)
+ batch = list(payload.get("commits") or [])
+ commits.extend(batch)
+ if not batch or len(commits) >= total:
+ return commits, total
+ raise ReleaseError(f"{base}...{head} has too many commits to validate safely")
+
+ def all_pull_commits(self, repository: str, pull: Mapping[str, Any]) -> list[dict[str, Any]]:
+ """Every commit of a pull request, including past the endpoint's 250 cap."""
+ number = int(pull.get("number") or 0)
+ expected = int(pull.get("commits") or 0)
+ if expected <= PULL_COMMITS_ENDPOINT_CAP:
+ commits = self.pull_commits(repository, number)
+ expected = expected or len(commits)
+ else:
+ base_ref = str((pull.get("base") or {}).get("ref") or "")
+ head_sha = str((pull.get("head") or {}).get("sha") or "")
+ if not base_ref or not head_sha:
+ raise ReleaseError(f"pull request #{number} has no readable base or head")
+ commits, total = self.compare_commits(repository, base_ref, head_sha)
+ if total != expected:
+ raise ReleaseError(
+ f"GitHub compares {total} commits but pull request #{number} has "
+ f"{expected}; refusing partial attribution validation"
+ )
+ if len(commits) != expected or len({c.get("sha") for c in commits}) != expected:
+ raise ReleaseError(
+ f"GitHub returned {len(commits)} of {expected} commits for pull request "
+ f"#{number}; refusing partial attribution validation"
+ )
+ return commits
+
def file_json(self, repository: str, path: str, ref: str) -> Mapping[str, Any]:
encoded_path = quote(path.strip("/"), safe="/")
payload = self.get(f"repos/{repository}/contents/{encoded_path}?ref={quote(ref, safe='')}")
@@ -1347,13 +1394,7 @@ def validate_pr_command(args: argparse.Namespace) -> None:
if not head_repository or not head_sha:
raise ReleaseError(f"pull request #{number} has no readable head repository")
- commits = client.pull_commits(repository, number)
- expected_commits = int(pull.get("commits") or len(commits))
- if len(commits) != expected_commits:
- raise ReleaseError(
- f"GitHub returned {len(commits)} of {expected_commits} commits for pull request "
- f"#{number}; refusing partial attribution validation"
- )
+ commits = client.all_pull_commits(repository, {**pull, "number": number})
base_config = json.loads(args.config.read_text())
head_config = client.file_json(head_repository, args.config.as_posix(), head_sha)
diff --git a/.github/scripts/tests/test_pr_attribution.py b/.github/scripts/tests/test_pr_attribution.py
index 2653b3019a..c78c6a6185 100644
--- a/.github/scripts/tests/test_pr_attribution.py
+++ b/.github/scripts/tests/test_pr_attribution.py
@@ -2,6 +2,8 @@ from __future__ import annotations
import pytest
+import release_attribution
+
from release_attribution import (
CommitRecord,
ReleaseError,
@@ -407,3 +409,50 @@ def test_coauthor_trailer_parsing_is_case_insensitive_and_multiline():
)
]
)
+
+
+class PagedCompareGitHub(release_attribution.GitHubClient):
+ """Serves a 300-commit pull request: the commits endpoint stops at 250."""
+
+ def __init__(self, total: int = 300, reported_total: int | None = None):
+ super().__init__("token", "https://api.example")
+ self.total = total
+ self.reported_total = total if reported_total is None else reported_total
+ self.paths: list[str] = []
+
+ def get(self, path: str):
+ self.paths.append(path)
+ if "/pulls/" in path:
+ raise AssertionError("a pull request past 250 commits must not use /pulls/N/commits")
+ page = int(path.rsplit("page=", 1)[1])
+ start = (page - 1) * 100
+ end = min(start + 100, self.total)
+ return {
+ "total_commits": self.reported_total,
+ "commits": [{"sha": f"{index:040x}"} for index in range(start, end)],
+ }
+
+
+def large_pull(commits: int = 300):
+ return {"number": 19, "commits": commits, "base": {"ref": "main"}, "head": {"sha": "f" * 40}}
+
+
+def test_pull_request_past_the_commits_endpoint_cap_pages_the_compare_api():
+ client = PagedCompareGitHub()
+ commits = client.all_pull_commits("trycua/cua-staging", large_pull())
+ assert len(commits) == 300
+ assert client.paths[0] == (
+ "repos/trycua/cua-staging/compare/main..." + "f" * 40 + "?per_page=100&page=1"
+ )
+
+
+def test_compare_total_disagreeing_with_the_pull_request_is_refused():
+ client = PagedCompareGitHub(total=300, reported_total=299)
+ with pytest.raises(release_attribution.ReleaseError, match="refusing partial"):
+ client.all_pull_commits("trycua/cua-staging", large_pull())
+
+
+def test_compare_pages_that_stop_short_are_refused():
+ client = PagedCompareGitHub(total=250, reported_total=300)
+ with pytest.raises(release_attribution.ReleaseError, match="refusing partial"):
+ client.all_pull_commits("trycua/cua-staging", large_pull())