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 configuration changes against their ancestor (#3860)

injaneity5fcd67326dd0
diff --git a/.github/scripts/release_attribution.py b/.github/scripts/release_attribution.py
index c847eb72f5..18dde9820e 100644
--- a/.github/scripts/release_attribution.py
+++ b/.github/scripts/release_attribution.py
@@ -361,7 +361,7 @@ def validate_pr_attribution(
pull: Mapping[str, Any],
commits: Sequence[Mapping[str, Any]],
base_config: Mapping[str, Any],
- head_config: Mapping[str, Any],
+ identity_changes: Mapping[str, str | None],
github: GitHubClient,
) -> None:
"""Fail closed when preserved human authorship cannot reach a GitHub login.
@@ -376,21 +376,17 @@ def validate_pr_attribution(
opt_out = _normalized_set(base_config, "optOutHandles")
ignored = _normalized_set(base_config, "ignoredCoauthorEmails")
base_overrides = _normalized_map(base_config, "identityOverrides")
- head_overrides = _normalized_map(head_config, "identityOverrides")
coauthor_overrides = _normalized_map(base_config, "coauthorOverrides")
- changed_existing = {
- email: (login, head_overrides.get(email))
- for email, login in base_overrides.items()
- if head_overrides.get(email) != login
- }
- if changed_existing:
- details = ", ".join(
- f"{email}={actual!r} (expected {expected!r})"
- for email, (expected, actual) in sorted(changed_existing.items())
- )
+ override_errors = [
+ f"{email}={login!r} (expected {base_overrides[email]!r})"
+ for email, login in sorted(identity_changes.items())
+ if email in base_overrides and login != base_overrides[email]
+ ]
+ if override_errors:
raise ReleaseError(
- "the pull request removes or changes trusted identityOverrides: " + details
+ "the pull request removes or changes trusted identityOverrides: "
+ + ", ".join(override_errors)
)
pull_number = int(pull.get("number") or 0)
@@ -520,7 +516,9 @@ def validate_pr_attribution(
)
new_overrides = {
- email: login for email, login in head_overrides.items() if email not in base_overrides
+ email: login
+ for email, login in identity_changes.items()
+ if login is not None and email not in base_overrides
}
if not unresolved and not new_overrides and not preserved_authors and not early_errors:
return
@@ -615,7 +613,7 @@ def validate_pr_attribution(
)
for email, expected in sorted(suggestions.items()):
- actual = head_overrides.get(email)
+ actual = identity_changes.get(email)
if actual != expected:
fragment = json.dumps(
{"identityOverrides": {email: expected}}, indent=2, sort_keys=True
@@ -1220,12 +1218,27 @@ def validate_pr_command(args: argparse.Namespace) -> None:
base_config = json.loads(args.config.read_text())
head_config = client.file_json(head_repository, args.config.as_posix(), head_sha)
+ trusted_sha = run_git(Path.cwd(), "rev-parse", "HEAD").strip()
+ comparison = client.get(
+ f"repos/{repository}/compare/{trusted_sha}...{head_sha}?per_page=1"
+ )
+ ancestor_sha = str((comparison.get("merge_base_commit") or {}).get("sha") or "")
+ if not ancestor_sha:
+ raise ReleaseError("GitHub returned no merge base for attribution validation")
+ ancestor_config = client.file_json(repository, args.config.as_posix(), ancestor_sha)
+ ancestor_overrides = _normalized_map(ancestor_config, "identityOverrides")
+ head_overrides = _normalized_map(head_config, "identityOverrides")
+ identity_changes = {
+ email: head_overrides.get(email)
+ for email in ancestor_overrides.keys() | head_overrides.keys()
+ if head_overrides.get(email) != ancestor_overrides.get(email)
+ }
validate_pr_attribution(
repository=repository,
pull=pull,
commits=commits,
base_config=base_config,
- head_config=head_config,
+ identity_changes=identity_changes,
github=client,
)
print(f"contributor attribution is merge-ready for pull request #{number}")
diff --git a/.github/scripts/tests/test_pr_attribution.py b/.github/scripts/tests/test_pr_attribution.py
index 9cdf808639..2653b3019a 100644
--- a/.github/scripts/tests/test_pr_attribution.py
+++ b/.github/scripts/tests/test_pr_attribution.py
@@ -76,7 +76,7 @@ def validate(
pull_value=None,
commits=None,
base=None,
- head=None,
+ changes=None,
authors=None,
source_emails=None,
):
@@ -86,7 +86,7 @@ def validate(
pull=pull_value or pull(),
commits=commits or [],
base_config=base,
- head_config=head or base,
+ identity_changes=changes or {},
github=SourceGitHub(authors or {}, source_emails or {}),
)
@@ -291,11 +291,10 @@ def test_verified_source_pr_produces_exact_override_and_accepts_it():
error.value
)
- head = config(identityOverrides={"[email protected]": "source-login"})
validate(
pull_value=landing,
commits=commits,
- head=head,
+ changes={"[email protected]": "source-login"},
authors={2280: "source-login"},
source_emails={2280: ["[email protected]"]},
)
@@ -337,15 +336,15 @@ def test_source_pr_author_without_exact_email_is_not_mapping_evidence():
def test_mapping_only_override_requires_and_accepts_exact_source_evidence():
- head = config(identityOverrides={"[email protected]": "historic-author"})
+ changes = {"[email protected]": "historic-author"}
with pytest.raises(ReleaseError, match="has no explicit same-repository source PR"):
- validate(head=head)
+ validate(changes=changes)
validate(
pull_value=pull(
body=("The identity is verified by [PR #20](https://github.com/trycua/cua/pull/20)")
),
- head=head,
+ changes=changes,
authors={20: "historic-author"},
source_emails={20: ["[email protected]"]},
)
@@ -354,14 +353,24 @@ def test_mapping_only_override_requires_and_accepts_exact_source_evidence():
def test_existing_identity_override_cannot_be_removed_or_changed():
base = config(identityOverrides={"[email protected]": "known-author"})
with pytest.raises(ReleaseError, match="removes or changes trusted identityOverrides"):
- validate(base=base, head=config())
+ validate(base=base, changes={"[email protected]": None})
with pytest.raises(ReleaseError, match="removes or changes trusted identityOverrides"):
validate(
base=base,
- head=config(identityOverrides={"[email protected]": "other-author"}),
+ changes={"[email protected]": "other-author"},
)
+def test_protected_mapping_errors_are_complete_and_sorted():
+ base = config(identityOverrides={"[email protected]": "zoe", "[email protected]": "alice"})
+ with pytest.raises(ReleaseError) as error:
+ validate(base=base, changes={"[email protected]": None, "[email protected]": "other"})
+ assert str(error.value) == (
+ "the pull request removes or changes trusted identityOverrides: "
+ "[email protected]='other' (expected 'alice'), [email protected]=None (expected 'zoe')"
+ )
+
+
def test_internal_bot_and_ignored_coauthors_are_excluded():
validate(
commits=[
diff --git a/.github/scripts/tests/test_pr_attribution_command.py b/.github/scripts/tests/test_pr_attribution_command.py
new file mode 100644
index 0000000000..56f06be8ab
--- /dev/null
+++ b/.github/scripts/tests/test_pr_attribution_command.py
@@ -0,0 +1,209 @@
+from __future__ import annotations
+
+import base64
+from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
+import json
+import os
+from pathlib import Path
+import subprocess
+import sys
+from threading import Thread
+
+import pytest
+
+
+ROOT = Path(__file__).resolve().parents[3]
+CONFIG = ".github/release-attribution-config.json"
+ANCESTOR = "a" * 40
+HEAD = "b" * 40
+KNOWN = {"[email protected]": "known-author"}
+
+
+def contents(overrides):
+ if overrides is None:
+ return None
+ data = json.dumps({"identityOverrides": overrides}).encode()
+ return {"encoding": "base64", "content": base64.b64encode(data).decode()}
+
+
+def github_api():
+ responses = {}
+
+ class Handler(BaseHTTPRequestHandler):
+ def log_message(self, *_args):
+ pass
+
+ def do_GET(self):
+ payload = responses.get(self.path)
+ data = json.dumps(payload).encode()
+ self.send_response(404 if payload is None else 200)
+ self.send_header("Content-Type", "application/json")
+ self.send_header("Content-Length", str(len(data)))
+ self.end_headers()
+ self.wfile.write(data)
+
+ server = ThreadingHTTPServer(("127.0.0.1", 0), Handler)
+ thread = Thread(target=server.serve_forever, daemon=True)
+ thread.start()
+ try:
+ yield f"http://127.0.0.1:{server.server_port}", responses
+ finally:
+ server.shutdown()
+ server.server_close()
+ thread.join(timeout=5)
+
+
+def validate_command(tmp_path, github_api):
+ api_url, responses = github_api
+ env = {
+ "HOME": str(tmp_path),
+ "PATH": os.defpath,
+ "GIT_CONFIG_NOSYSTEM": "1",
+ "GH_TOKEN": "local-fixture-token",
+ "GITHUB_API_URL": api_url,
+ }
+
+ def git(*args):
+ return subprocess.check_output(
+ ["git", *args], cwd=tmp_path, env=env, text=True
+ ).strip()
+
+ def validate(*, ancestor, head, body="", merge_base_sha=ANCESTOR):
+ config = tmp_path / CONFIG
+ config.parent.mkdir()
+ config.write_text(json.dumps({"identityOverrides": KNOWN}))
+ git("init", "-q", "--template=")
+ git("add", CONFIG)
+ git(
+ "-c", "user.name=Fixture",
+ "commit", "-qm", "trusted policy",
+ )
+ trusted_sha = git("rev-parse", "HEAD")
+ (tmp_path / "event.json").write_text(json.dumps({
+ "repository": {"full_name": "trycua/cua"},
+ "pull_request": {"number": 50},
+ }))
+ responses.update({
+ "/repos/trycua/cua/pulls/50": {
+ "number": 50,
+ "user": {"login": "landing-author"},
+ "body": body,
+ "commits": 1,
+ "base": {"sha": ANCESTOR},
+ "head": {"sha": HEAD, "repo": {"full_name": "contributor/cua"}},
+ },
+ "/repos/trycua/cua/pulls/50/commits?per_page=100&page=1": [{
+ "sha": HEAD,
+ "author": {"login": "landing-author"},
+ "commit": {
+ "author": {
+ "name": "Landing Author",
+ "email": "[email protected]",
+ },
+ "message": "docs: update guide\n\nCo-authored-by: Known <[email protected]>",
+ },
+ }],
+ f"/repos/contributor/cua/contents/{CONFIG}?ref={HEAD}": contents(head),
+ f"/repos/trycua/cua/compare/{trusted_sha}...{HEAD}?per_page=1": {
+ "merge_base_commit": {"sha": merge_base_sha},
+ },
+ f"/repos/trycua/cua/contents/{CONFIG}?ref={ANCESTOR}": contents(ancestor),
+ })
+ return subprocess.run(
+ [
+ sys.executable, str(ROOT / ".github/scripts/release_attribution.py"),
+ "validate-pr", "--event", "event.json",
+ ],
+ cwd=tmp_path,
+ env=env,
+ capture_output=True,
+ text=True,
+ timeout=30,
+ )
+
+ return validate
+
+
+ "ancestor",
+ [
+ {},
+ {"[email protected]": "previous-author"},
+ {"[email protected]": "retired-author"},
+ KNOWN,
+ ],
+ ids=["before-addition", "before-update", "before-removal", "current"],
+)
+def test_unchanged_configuration_uses_current_trusted_policy(validate_command, ancestor):
+ result = validate_command(ancestor=ancestor, head=ancestor)
+
+ assert result.returncode == 0, result.stdout + result.stderr
+ assert "merge-ready for pull request #50" in result.stdout
+
+
+ "head", [{}, {"[email protected]": "other-author"}], ids=["removed", "altered"]
+)
+def test_actual_changes_to_trusted_mappings_are_rejected(validate_command, head):
+ result = validate_command(ancestor=KNOWN, head=head)
+
+ assert result.returncode == 1
+ assert "removes or changes trusted identityOverrides" in result.stderr
+ assert "merge-ready" not in result.stdout
+
+
+def test_unverified_addition_on_stale_branch_is_rejected(validate_command):
+ result = validate_command(ancestor={}, head={"[email protected]": "new-author"})
+
+ assert result.returncode == 1
+ assert "[email protected]" in result.stderr
+ assert "has no explicit same-repository source PR" in result.stderr
+ assert "merge-ready" not in result.stdout
+
+
[email protected]("login,exit_code", [("source-author", 0), ("another-author", 1)])
+def test_addition_on_stale_branch_requires_verified_login(
+ validate_command, github_api, login, exit_code
+):
+ _, responses = github_api
+ responses["/repos/trycua/cua/pulls/12"] = {
+ "number": 12, "user": {"login": "source-author"},
+ }
+ responses["/repos/trycua/cua/pulls/12/commits?per_page=100&page=1"] = [
+ {"commit": {"author": {"email": "[email protected]"}}},
+ ]
+ result = validate_command(
+ ancestor={},
+ head={"[email protected]": login},
+ body="The identity is verified by https://github.com/trycua/cua/pull/12",
+ )
+
+ assert result.returncode == exit_code, result.stdout + result.stderr
+ if exit_code:
+ assert "verified source PR author is @source-author" in result.stderr
+ assert "merge-ready" not in result.stdout
+ else:
+ assert "merge-ready for pull request #50" in result.stdout
+
+
[email protected]("missing", ["ancestor", "head"])
+def test_unreadable_configuration_is_not_approved(validate_command, missing):
+ configurations = {"ancestor": {}, "head": {}}
+ configurations[missing] = None
+ result = validate_command(**configurations)
+
+ assert result.returncode == 1
+ assert "GitHub API GET" in result.stderr
+ assert "404" in result.stderr
+ assert "merge-ready" not in result.stdout
+
+
+def test_missing_merge_base_is_not_approved(validate_command):
+ result = validate_command(ancestor={}, head={}, merge_base_sha="")
+
+ assert result.returncode == 1
+ assert "no merge base" in result.stderr
+ assert "merge-ready" not in result.stdout