-
Notifications
You must be signed in to change notification settings - Fork 3k
tooling: add deterministic CI state classifier command #4605
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
davidahmann
wants to merge
2
commits into
google:main
Choose a base branch
from
davidahmann:codex/issue-4604-ci-state-classifier
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+223
−0
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,223 @@ | ||||||
| #!/usr/bin/env python3 | ||||||
| """Classify GitHub PR checks into deterministic machine-readable states. | ||||||
|
|
||||||
| Outputs one of: | ||||||
| - passed | ||||||
| - failed | ||||||
| - pending | ||||||
| - no_checks | ||||||
| - policy_blocked | ||||||
| """ | ||||||
|
|
||||||
| from __future__ import annotations | ||||||
|
|
||||||
| import argparse | ||||||
| import json | ||||||
| import re | ||||||
| import subprocess | ||||||
| import sys | ||||||
| from dataclasses import dataclass | ||||||
| from pathlib import Path | ||||||
| from typing import Any | ||||||
|
|
||||||
| FAIL_VALUES = { | ||||||
| "FAILURE", | ||||||
| "ERROR", | ||||||
| "TIMED_OUT", | ||||||
| "CANCELLED", | ||||||
| "ACTION_REQUIRED", | ||||||
| "STARTUP_FAILURE", | ||||||
| "STALE", | ||||||
| } | ||||||
| PENDING_VALUES = { | ||||||
| "PENDING", | ||||||
| "QUEUED", | ||||||
| "IN_PROGRESS", | ||||||
| "REQUESTED", | ||||||
| "WAITING", | ||||||
| "EXPECTED", | ||||||
| } | ||||||
| POLICY_PATTERNS = [ | ||||||
| r"\bcla\b", | ||||||
| r"license/cla", | ||||||
| r"code[- ]?owners", | ||||||
| r"dco", | ||||||
| r"policy", | ||||||
| r"compliance", | ||||||
| r"signed[- ]off", | ||||||
| ] | ||||||
|
|
||||||
|
|
||||||
| @dataclass | ||||||
| class Check: | ||||||
| name: str | ||||||
| conclusion: str | ||||||
| status: str | ||||||
|
|
||||||
|
|
||||||
| def _norm(value: Any) -> str: | ||||||
| if value is None: | ||||||
| return "" | ||||||
| return str(value).strip().upper() | ||||||
|
|
||||||
|
|
||||||
| def _to_checks(raw: list[dict[str, Any]]) -> list[Check]: | ||||||
| checks: list[Check] = [] | ||||||
| for item in raw: | ||||||
| name = ( | ||||||
| item.get("name") | ||||||
| or item.get("context") | ||||||
| or item.get("__typename") | ||||||
| or "unknown" | ||||||
| ) | ||||||
| conclusion = _norm(item.get("conclusion") or item.get("state")) | ||||||
| status = _norm(item.get("status") or item.get("state")) | ||||||
| checks.append(Check(name=str(name), conclusion=conclusion, status=status)) | ||||||
| return checks | ||||||
|
|
||||||
|
|
||||||
| def _is_policy(check: Check) -> bool: | ||||||
| name = check.name.lower() | ||||||
| return any(re.search(pattern, name) for pattern in POLICY_PATTERNS) | ||||||
|
|
||||||
|
|
||||||
| def classify(checks: list[Check]) -> str: | ||||||
| if not checks: | ||||||
| return "no_checks" | ||||||
|
|
||||||
| failing = [ | ||||||
| c | ||||||
| for c in checks | ||||||
| if c.conclusion in FAIL_VALUES or c.status in FAIL_VALUES | ||||||
| ] | ||||||
| pending = [ | ||||||
| c | ||||||
| for c in checks | ||||||
| if c.status in PENDING_VALUES or c.conclusion in PENDING_VALUES | ||||||
| ] | ||||||
|
|
||||||
| if failing: | ||||||
| non_policy_failures = [c for c in failing if not _is_policy(c)] | ||||||
| if non_policy_failures: | ||||||
| return "failed" | ||||||
|
|
||||||
| non_policy_pending = [c for c in pending if not _is_policy(c)] | ||||||
| if non_policy_pending: | ||||||
| return "pending" | ||||||
|
|
||||||
| return "policy_blocked" | ||||||
|
|
||||||
| if pending: | ||||||
| if all(_is_policy(c) for c in pending): | ||||||
| return "policy_blocked" | ||||||
| return "pending" | ||||||
|
|
||||||
| return "passed" | ||||||
|
|
||||||
|
|
||||||
| def _load_status_rollup(path: Path) -> list[dict[str, Any]]: | ||||||
| payload = json.loads(path.read_text()) | ||||||
| if isinstance(payload, dict) and "statusCheckRollup" in payload: | ||||||
| rollup = payload["statusCheckRollup"] | ||||||
| if rollup is None: | ||||||
| return [] | ||||||
| if isinstance(rollup, list): | ||||||
| return rollup | ||||||
| if isinstance(payload, list): | ||||||
| return payload | ||||||
| raise ValueError("Expected JSON list or object with statusCheckRollup list") | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The
Suggested change
|
||||||
|
|
||||||
|
|
||||||
| def _fetch_status_rollup(repo: str, pr: int) -> list[dict[str, Any]]: | ||||||
| proc = subprocess.run( | ||||||
| [ | ||||||
| "gh", | ||||||
| "pr", | ||||||
| "view", | ||||||
| "--repo", | ||||||
| repo, | ||||||
| str(pr), | ||||||
| "--json", | ||||||
| "statusCheckRollup,url", | ||||||
| ], | ||||||
| capture_output=True, | ||||||
| text=True, | ||||||
| check=True, | ||||||
| ) | ||||||
| payload = json.loads(proc.stdout) | ||||||
| return payload.get("statusCheckRollup", []) or [] | ||||||
|
|
||||||
|
|
||||||
| def _report(repo: str, pr: int | None, checks: list[Check]) -> dict[str, Any]: | ||||||
| return { | ||||||
| "repo": repo, | ||||||
| "pr": pr, | ||||||
| "classification": classify(checks), | ||||||
| "check_count": len(checks), | ||||||
| "checks": [ | ||||||
| { | ||||||
| "name": c.name, | ||||||
| "conclusion": c.conclusion, | ||||||
| "status": c.status, | ||||||
| "is_policy_check": _is_policy(c), | ||||||
| } | ||||||
| for c in checks | ||||||
| ], | ||||||
| } | ||||||
|
|
||||||
|
|
||||||
| def _self_test() -> None: | ||||||
| assert classify([]) == "no_checks" | ||||||
| assert classify([Check("unit", "SUCCESS", "COMPLETED")]) == "passed" | ||||||
| assert classify([Check("build", "FAILURE", "COMPLETED")]) == "failed" | ||||||
| assert classify([Check("build", "STARTUP_FAILURE", "COMPLETED")]) == "failed" | ||||||
| assert classify([Check("license/cla", "", "QUEUED")]) == "policy_blocked" | ||||||
| assert classify([Check("tests", "", "IN_PROGRESS")]) == "pending" | ||||||
| assert classify([Check("required", "EXPECTED", "EXPECTED")]) == "pending" | ||||||
| assert ( | ||||||
| classify([ | ||||||
| Check("license/cla", "ACTION_REQUIRED", "COMPLETED"), | ||||||
| Check("tests", "", "IN_PROGRESS"), | ||||||
| ]) | ||||||
| == "pending" | ||||||
| ) | ||||||
| print(json.dumps({"self_test": "ok"})) | ||||||
|
|
||||||
|
|
||||||
| def main() -> int: | ||||||
| parser = argparse.ArgumentParser() | ||||||
| parser.add_argument("--repo", help="owner/repo") | ||||||
| parser.add_argument("--pr", type=int, help="PR number") | ||||||
| parser.add_argument( | ||||||
| "--input", help="Path to JSON payload for statusCheckRollup" | ||||||
| ) | ||||||
| parser.add_argument("--self-test", action="store_true") | ||||||
| parser.add_argument("--pretty", action="store_true") | ||||||
| args = parser.parse_args() | ||||||
|
|
||||||
| if args.self_test: | ||||||
| _self_test() | ||||||
| return 0 | ||||||
|
|
||||||
| if args.input: | ||||||
| raw = _load_status_rollup(Path(args.input)) | ||||||
| checks = _to_checks(raw) | ||||||
| report = _report(args.repo or "fixture", args.pr, checks) | ||||||
| else: | ||||||
| if not args.repo or not args.pr: | ||||||
| parser.error( | ||||||
| "--repo and --pr are required unless --input or --self-test is used" | ||||||
| ) | ||||||
| raw = _fetch_status_rollup(args.repo, args.pr) | ||||||
| checks = _to_checks(raw) | ||||||
| report = _report(args.repo, args.pr, checks) | ||||||
|
|
||||||
| if args.pretty: | ||||||
| print(json.dumps(report, indent=2, sort_keys=True)) | ||||||
| else: | ||||||
| print(json.dumps(report, sort_keys=True)) | ||||||
| return 0 | ||||||
|
|
||||||
|
|
||||||
| if __name__ == "__main__": | ||||||
| raise SystemExit(main()) | ||||||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
or []afterpayload.get("statusCheckRollup", [])is redundant. Thegetmethod with a default value already handles cases where the key is missing or its value isNoneby returning the default empty list.