Repository navigation
Compare readiness using GitHub timestamps #1583
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
Changes from all commits
3a241dc
3ca6df4
159b375
da40f81
2479b47
861dda1
8bdc79d
b02dc56
455e30f
90e32e1
4f8ea0c
489dab0
69d7b87
8ec055c
b072ab1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -354,7 +354,9 @@ def _pr_list_error_guidance(args: tuple[str, ...], stderr: str, using_app: bool) | |
|
|
||
|
|
||
| def _gh(*args: str, cwd: Path | None = None, timeout: float = 120, | ||
| token: str | None = None, retry_idempotent: bool = False) -> str: | ||
| token: str | None = None, retry_idempotent: bool = False, | ||
| dates: list[str] | None = None) -> str: | ||
| dates = [] if dates is None else dates | ||
| diagnostic_api = _api_error_diagnostics.get() and args[:1] == ("api",) | ||
| rest_api = args[:1] == ("api",) and args[:2] != ("api", "graphql") | ||
| patch_error = args[:1] == ("api",) and _api_method(args) == "PATCH" | ||
|
|
@@ -410,6 +412,7 @@ def _gh(*args: str, cwd: Path | None = None, timeout: float = 120, | |
| raise GhAuth(error_command, error_tail.rstrip(), token=selected, guidance=f"\n{GH_AUTH_REMEDY}") | ||
| guidance = _pr_list_error_guidance(args, done.stderr, bool(read_only and selected and token is None)) | ||
| raise _request_error(transient)(error_command, error_tail, token=selected, guidance=guidance) | ||
| dates[:] = [headers.get("date", "")] | ||
| return body | ||
|
|
||
|
|
||
|
|
@@ -486,8 +489,8 @@ def _graphql_reset(args: tuple[str, ...], tail: str, cwd: Path, token: str = "") | |
| return "" | ||
|
|
||
|
|
||
| def _gh_json(*args: str, token: str | None = None) -> object: | ||
| out = _gh(*args, token=token) | ||
| def _gh_json(*args: str, token: str | None = None, **kwargs) -> object: | ||
| out = _gh(*args, token=token, **kwargs) | ||
| try: | ||
| return json.loads(out) if out.strip() else None | ||
| except json.JSONDecodeError as exc: | ||
|
|
@@ -525,9 +528,16 @@ def _paged(endpoint: str, key: str = "check_runs") -> list: | |
| return list(_iter_paged(endpoint, key)) | ||
|
|
||
|
|
||
| def api_get(endpoint: str) -> object: | ||
| def api_get(endpoint: str, **kwargs) -> object: | ||
| """`gh api <endpoint>` parsed as JSON (None on an empty body).""" | ||
| return _gh_json("api", endpoint) | ||
| return _gh_json("api", endpoint, **kwargs) | ||
|
|
||
|
|
||
| def response_date(value: str) -> datetime | None: | ||
| with suppress(OverflowError, ValueError): | ||
| stamp = parsedate_to_datetime(value) | ||
|
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. important: Validate the HTTP Date before using it for review age
|
||
| return stamp if stamp.tzinfo is not None else None | ||
| return None | ||
|
|
||
|
|
||
| def auth_token() -> str: | ||
|
|
@@ -1192,55 +1202,55 @@ def _review_threads(repo: str, number: int, *, include_resolved: bool = False) - | |
| raise _not_found(number, exc) from exc | ||
|
|
||
|
|
||
| _READINESS_TIMESTAMP = re.compile(r"[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}(?:\.[0-9]+)?" | ||
|
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. important: Preserve fractional precision when ordering verdicts The pattern accepts arbitrary fractional precision, but |
||
| r"(?:Z|[+-](?:[01][0-9]|2[0-3]):[0-5][0-9])") | ||
|
|
||
|
|
||
| def readiness_timestamp(value: object) -> datetime | None: | ||
| if isinstance(value, str) and _READINESS_TIMESTAMP.fullmatch(value): | ||
| with suppress(OverflowError, ValueError): | ||
| return datetime.fromisoformat(value).astimezone(UTC) | ||
| return None | ||
|
|
||
|
|
||
| def _check_signals( | ||
| repo: str, | ||
| head_sha: str, | ||
| review_at: str | None = None, | ||
| review_at: datetime | None = None, | ||
| review_grace: bool = False, | ||
| required_ci: tuple[str, ...] = (), | ||
| stale_verdict_grace: bool = False, | ||
| ) -> tuple[dict[str, str], tuple[str, ...], tuple[str, ...], bool]: | ||
| signals: dict[str, list[tuple[str, str | None]]] = {} | ||
| pending = {"pending", "queued", "in_progress", "requested", "waiting", "expected"} | ||
| signals: dict[str, list[tuple[str, datetime | None]]] = {} | ||
| latest_runs: dict[str, dict] = {} | ||
| runs = _paged(f"repos/{repo}/commits/{head_sha}/check-runs") | ||
| for cr in runs: | ||
| previous = latest_runs.get(cr["name"]) | ||
| if previous is None or _check_run_order(cr) > _check_run_order(previous): | ||
| latest_runs[cr["name"]] = cr | ||
| for name, cr in latest_runs.items(): | ||
| signals[name] = [ | ||
| ( | ||
| cr.get("conclusion") or cr.get("status") or "unknown", | ||
| cr.get("completed_at") or cr.get("started_at"), | ||
| ) | ||
| ] | ||
| state = cr.get("conclusion") or cr.get("status") or "unknown" | ||
| timestamp = cr.get("started_at" if state in pending else "completed_at") | ||
| signals[name] = [(state, readiness_timestamp(timestamp))] | ||
| latest_status: dict[str, dict] = {} | ||
| for status in _paged(f"repos/{repo}/commits/{head_sha}/statuses"): | ||
| latest_status.setdefault(status["context"], status) | ||
| for name, status in latest_status.items(): | ||
| signals.setdefault(name, []).append( | ||
| (status.get("state") or "unknown", status.get("created_at")) | ||
| (status.get("state") or "unknown", readiness_timestamp(status.get("created_at"))) | ||
| ) | ||
| states = { | ||
| name: _aggregate_signal([state for state, _ in values]) | ||
| for name, values in signals.items() | ||
| } | ||
| pending = {"pending", "queued", "in_progress", "requested", "waiting", "expected"} | ||
| states = {name: _aggregate_signal([state for state, _ in values]) for name, values in signals.items()} | ||
| stale = tuple(name for name, values in signals.items() if review_at | ||
| and states[name] not in pending and states[name] != "success" | ||
| and all(at and at < review_at for state, at in values | ||
| if state != "success" and state not in pending) | ||
| and (review_grace or any(at and at >= review_at | ||
| for state, at in values if state in pending))) | ||
| stale_verdicts = tuple( | ||
| name | ||
| for name, values in signals.items() | ||
| if review_at | ||
| and stale_verdict_grace | ||
| and name not in stale | ||
| and states[name] in {"failure", "error", "cancelled", "timed_out"} | ||
| and all(at and at < review_at for state, at in values if state != "success") | ||
| ) | ||
| stale_verdicts = tuple(name for name, values in signals.items() | ||
| if review_at and stale_verdict_grace and name not in stale | ||
| and states[name] in {"failure", "error", "cancelled", "timed_out"} | ||
| and all(at and at < review_at for state, at in values if state != "success")) | ||
| stale = (*stale, *stale_verdicts) | ||
| superseded: list[str] = [] | ||
| workflows: list[dict] | None = None | ||
|
|
@@ -1344,7 +1354,7 @@ def readiness( | |
| required_ci: tuple[str, ...], | ||
| reviewed_head: str | None, | ||
| *, allow_behind: bool = False, ancestor_review: bool = False, | ||
| review_at: str | None = None, | ||
| review_at: datetime | None = None, | ||
| review_grace: bool = False, | ||
| workflow_wait: bool = False, | ||
| stale_verdict_grace: bool = False, | ||
|
|
||
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.
suggestion: The headers setup in _readiness could be simpler
headersis declared in both arms of the if/else, and_rest_rows/_pr_reviewspass an untyped**kwargsthrough toapi_get. Two smaller options: declareheaders: dict[str, str] = {}once before theif reviews is Nonecheck, and give_rest_rows/_pr_reviewsan explicitresponse_headers: dict[str, str] | None = Noneparameter instead of**kwargs. That keeps the plumbing type-checked and removes the duplicatedelsearm. Behaviour stays the same.