Close delivered items in scheduled runs by reading required checks from the branch summary

Merged
UN
1mo ago
+106-13

Scheduled closeout fails in nearly every fleet caller as soon as it has a delivered item to close: pm-rust and pm-jira since at least 10-25, pm-lint since 10-28, and 15 more repos today. Tracker item: pm-github-6g0m.

Cause

  • Unreadable endpoint: GITHUB_TOKEN can never read the administration-only classic protection endpoint (/branches/{b}/protection/required_status_checks).
  • No declarations: no caller declares the required-checks input.
  • Result: every closable row becomes required_checks_unverified, and the workflow refuses with "Closeout refused; check token read access…". Callers with nothing to close stay green, which hid the pattern.

Fix

  • Branch-summary fallback: when the protection endpoint answers 403/404, closeout reads the same required contexts and app bindings from the branch summary (protection.required_status_checks). Repository read access returns that endpoint.
  • Fail-closed cases:
    • An unprotected branch, or enforcement_level: "off", adds no classic requirements; rulesets still apply.
    • A protected branch whose summary omits its required checks stays unavailable.
    • An unverifiable protected flag is head_evidence_unavailable.
  • Callers: they need no edits and no declared names. required-checks remains for tokens that can read neither source.

Evidence

Acceptance criterionEvidence
A classic protection 403 falls back to the summary's required checks, including app bindingsNew test, cases "required context passes" and "summary app binding holds" (app 8 against required app 7 → head_checks_failed)
Unprotected or switched-off classic checks add nothingCases "unprotected branch" and "required checks switched off" → fallback
Missing details stay unavailable; an invalid flag is refusedCases "protected without check details" and "without a protection object" → required_checks_unverified; "unverifiable protected flag" → head_evidence_unavailable
A scheduled caller closes with no declared checksThe same test applies closeout with protection 403 and summary 200 → closed: 1
The fix fails its regression when revertedReturning before the summary read fails "required context passes"
  • Real data:
    • An anonymous GET /repos/unbraind/pm-lint/branches/main returns test (22), test (26) and codeql, while the protection endpoint returns 401.
    • With the owner token, a scratch clone's dry run plans pm-lint-4tem (PR #41) as head_checks_passed, and --apply closes it, on both 2026.10.27 and pm-lint's pinned 2026.10.16.
  • Tests: the closeout, changelog-placement and scheduled-refusals suites pass 553/553. Existing denied-protection tests keep their meaning, because the mock's branch summary also denies unless a test grants it.
  • Gates: lint, typecheck, docstring, duplication, privacy and changelog:check pass. CI runs the coverage gate.

Summary by Sourcery

Enable scheduled closeout to verify and close delivered items using branch-summary protection metadata when classic protection reads are unavailable.

New Features:

  • Fall back to branch-summary metadata to determine classic required checks and app bindings when the administration-only protection endpoint is unavailable.

Bug Fixes:

  • Allow scheduled closeout to process delivered items with GITHUB_TOKEN without requiring callers to declare required checks.
  • Preserve fail-closed behavior when branch protection details are missing, unverifiable, or otherwise unreadable.

Enhancements:

  • Clarify that the required-checks override is only needed when both classic and ruleset policy metadata cannot be read.

Documentation:

  • Update README and workflow and policy documentation to describe branch-summary fallback and the revised required-checks guidance.

Tests:

  • Add coverage for branch-summary requirements, app bindings, unprotected and disabled checks, incomplete metadata, invalid protection state, and scheduled closeout without declared checks.

Chores:

  • Record the tracker issue and history for the closeout fix.

Summary by cubic

Fixes scheduled closeout failing in fleet callers whenever there is a delivered item to close: GITHUB_TOKEN cannot read the administration-only branch-protection endpoint, so every closable row became required_checks_unverified and the workflow refused. Closeout now falls back to the branch summary, which returns the same required contexts and app bindings with repository read access.

  • Callers need no edits and no required-checks input; the input is now documented as needed only when both the classic source (protection or branch summary) and the ruleset metadata are unreadable.
  • Unprotected branches and enforcement_level: "off" add no classic requirements; a protected branch whose summary omits its checks stays unavailable.