Skip to content
Closed
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
36 changes: 9 additions & 27 deletions src/loopzero/candidate.py
Original file line number Diff line number Diff line change
Expand Up @@ -495,40 +495,31 @@ def _attest_candidate_once(
"Candidate head SHA is invalid",
)
candidate_path = f"/repos/{full_name}/pulls/{candidate_number}"
candidate_before = _object(
github_get(candidate_path),
"GitHub candidate lookup returned an invalid payload",
)
_candidate_snapshot(
candidate_before,
event_pr=event_pr,
repository=repository,
expected_identity=expected_identity,
)

def recheck_candidate(message: str = "GitHub candidate lookup returned an invalid payload") -> None:
current = _object(github_get(candidate_path), message)
_candidate_snapshot(current, event_pr=event_pr, repository=repository,
expected_identity=expected_identity)

def live_lineage() -> tuple[str, list[int], list[str]]:
delay = 2
while True:
try:
return _candidate_lineage(mergify_status(owner, repo, "main"), candidate_number)
except CandidateBatchError:
def recheck_candidate() -> None:
current = _object(github_get(candidate_path), "Invalid candidate lookup")
_candidate_snapshot(current, event_pr=event_pr, repository=repository,
expected_identity=expected_identity)

recheck_candidate()
recheck_candidate("Invalid candidate lookup")
remaining = batch_deadline - _retry_clock()
if remaining <= 0:
raise
started = _retry_clock()
_retry_sleep(min(delay, remaining))
batch_wait[0] += _retry_clock() - started
recheck_candidate()
recheck_candidate("Invalid candidate lookup")
if _retry_clock() >= batch_deadline:
raise
delay = min(delay * 2, 30)

recheck_candidate()
first_lineage = live_lineage()
main_path = f"/repos/{full_name}/git/ref/heads/main"
main_before = github_get(main_path) if first_lineage[2] else None
Expand Down Expand Up @@ -586,16 +577,7 @@ def recheck_candidate() -> None:
"Mergify candidate membership changed during attestation",
CandidateRaceError,
)
candidate_after = _object(
github_get(candidate_path),
"GitHub candidate lookup returned an invalid payload",
)
_candidate_snapshot(
candidate_after,
event_pr=event_pr,
repository=repository,
expected_identity=expected_identity,
)
recheck_candidate()
for number, (expected_sha, expected_fingerprint) in source_snapshots.items():
current = _object(
github_get(f"/repos/{full_name}/pulls/{number}"),
Expand Down
25 changes: 10 additions & 15 deletions src/loopzero/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ def _pr_list_error_guidance(args: tuple[str, ...], stderr: str, using_app: bool)
return ""


def _gh(*args: str, cwd: Path | None = None, timeout: float = 120,
def _gh(*args: str, timeout: float = 120,
token: str | None = None, retry_idempotent: bool = False) -> str:
diagnostic_api = _api_error_diagnostics.get() and args[:1] == ("api",)
rest_api = args[:1] == ("api",) and args[:2] != ("api", "graphql")
Expand All @@ -308,11 +308,12 @@ def _gh(*args: str, cwd: Path | None = None, timeout: float = 120,
if (rest_api or diagnostic_api) and not {"--include", "-i"} & set(args):
request_args = (*args, "--include")
argv = ("gh", *request_args)
cwd = cwd or Path("/")
cwd = Path("/")
read_only = _read_only(args)
retryable = read_only or retry_idempotent
scope = _read_scope.get() if read_only else None
delays = (None,) if scope and not scope[2] else (*RETRY_DELAYS, None)
rate_guidance = None
for delay in delays:
selected = _select_token(token, scope, read_only) # a retry backoff can outlive the app token
try:
Expand All @@ -337,13 +338,9 @@ def _gh(*args: str, cwd: Path | None = None, timeout: float = 120,
tail = "\n".join(part for part in (done.stderr, body if rest_api else done.stdout) if part)
if done.exit_code and ((rest_api and status == "429")
or re.search(r"rate limit|HTTP 429", tail, re.IGNORECASE)):
guidance = (_redact_tokens(_rest_rate_limit(headers), selected) if rest_api
else _graphql_reset(args, tail, cwd, selected) + RATE_LIMIT_GUIDANCE)
if diagnostic_api or patch_error:
detail = _api_error_detail(args, done.stdout if diagnostic_api else "", done.stderr, selected,
message_tail=None if diagnostic_api else tail)
raise GhError(("GitHub API request",), detail, token=selected, guidance=guidance)
raise GhError(argv, tail, token=selected, guidance=guidance)
rate_guidance = (_redact_tokens(_rest_rate_limit(headers), selected) if rest_api
else _graphql_reset(args, tail, cwd, selected) + RATE_LIMIT_GUIDANCE)
break
if done.exit_code == 0 or delay is None or not retryable or not _transient_error(tail, retry_idempotent):
break
_retry_sleep(_read_timeout(delay) if scope else delay)
Expand All @@ -352,6 +349,8 @@ def _gh(*args: str, cwd: Path | None = None, timeout: float = 120,
error_tail = (_api_error_detail(args, done.stdout if diagnostic_api else "", done.stderr, selected,
message_tail=None if diagnostic_api else tail)
if diagnostic_api or patch_error else tail)
if rate_guidance is not None:
raise GhError(error_command, error_tail, token=selected, guidance=rate_guidance)
if "gh auth login" in tail or "not logged in" in tail.lower():
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))
Expand Down Expand Up @@ -979,7 +978,7 @@ def open_findings(repo: str, number: int) -> list[tuple[str, str, Finding]]:
"""(marker id, thread id, finding) for every unresolved blocking thread, any head."""
return [
(MARKER_RE.search(node["comments"]["nodes"][0]["body"]).group("id") or node["id"], node["id"], f)
for node in _unresolved_threads(repo, number)
for node in _review_threads(repo, number)
if (f := _parse_thread(node, None))
]

Expand Down Expand Up @@ -1078,14 +1077,10 @@ def open_blocking_findings(
outdated thread is ignored unless its marker names that head.
"""
return [
f for node in _unresolved_threads(repo, number) if (f := _parse_thread(node, current_head))
f for node in _review_threads(repo, number) if (f := _parse_thread(node, current_head))
]


def _unresolved_threads(repo: str, number: int) -> list[dict]:
return _review_threads(repo, number)


def _review_threads(repo: str, number: int, *, include_resolved: bool = False) -> list[dict]:
owner, name = repo.split("/", 1)
after: str | None = None
Expand Down
Loading