diff --git a/changelog.d/fixed-required-checks-rulesets.md b/changelog.d/fixed-required-checks-rulesets.md new file mode 100644 index 0000000000..9cbe86be4e --- /dev/null +++ b/changelog.d/fixed-required-checks-rulesets.md @@ -0,0 +1 @@ +- The automerge sweep reads a branch's ruleset required checks when classic branch protection says nothing is required, so rulesets-only repositories no longer read as "no required checks" and stall at `ci-unverified`; the contributor lane also approves a registry-trusted author's held runs at `ci-unverified` when every `pull_request` run on the head is held. diff --git a/src/docs/hive-merge.md b/src/docs/hive-merge.md index a77d293f55..61067d27b9 100644 --- a/src/docs/hive-merge.md +++ b/src/docs/hive-merge.md @@ -469,8 +469,9 @@ lane's held runs (the `hive-ok-to-test` workflow approves same-repo heads only). hivecommons/docs#481 by a `trusted` registry contributor sat 17 hours at `required-check-missing:Audit production dependencies` until an operator clicked "Approve and run". When the registry path admitted the author and the -lane stops at `required-check-missing:`, it now approves the head's held -runs once (`POST /repos/{owner}/{repo}/actions/runs/{id}/approve`, fork head +lane stops at `required-check-missing:`, or at `ci-unverified` while +every `pull_request` run on the head is held (the branch's required checks +are unreadable or empty), it now approves the head's held runs once (`POST /repos/{owner}/{repo}/actions/runs/{id}/approve`, fork head included), posts one `` audit comment naming the author and tier, and logs `contributor-lane approved held runs` with the run ids (`approve_held_runs_for_registry_trusted`, default @@ -480,6 +481,16 @@ nor one touching `.github/workflows/**`. The App needs **Actions: Read and write** for the call; without it the approve fails, is logged once per head, and the PR waits for a person as before. +The required checks come from classic branch protection first. When classic +protection says nothing is required (the branch is unprotected, or protected +with no required checks), the sweep also reads the branch's rulesets +(`GET /repos/{owner}/{repo}/rules/branches/{branch}`) and uses their +`required_status_checks` if any. On a rulesets-only repository the classic +answer is always empty, so before this the sweep logged `automerge required +checks source … source=branch known=false count=0`, a held head stayed at +`ci-unverified`, and its runs were never approved (projectbluefin/testsuite, +bluefin-bling). + A PR merges through this tier only when all of the following hold: - The PR author is admitted by one of three paths: diff --git a/src/docs/operator-reference.md b/src/docs/operator-reference.md index 04ae96ccc6..8d0f160576 100644 --- a/src/docs/operator-reference.md +++ b/src/docs/operator-reference.md @@ -500,7 +500,7 @@ advanced into the merge path. | `auto_merge.contributor_lane.registry_tiers_sufficient` | `true` | Admit a PR whose author's contributor-registry profile carries a `require_tiers` tier without a linked issue or `hive:claim` marker (logged as `contributor-lane:registry-trusted`). All other gates still apply. `false` requires the claim marker for every contributor. | | `auto_merge.contributor_lane.auto_promotion_scope` | `hive` | Where the clean-merged-PR ratchet (`auto_merge.contributor_tier.auto_promote_after_merged`) counts an author's merges: `hive` counts across every repository the hive manages (one `search/issues` query finds the repos holding the author's merges; the PR's own repo is always walked first), `repo` counts only the PR's repository. Revert and sentinel-alert evidence blocks promotion in either scope. | | `auto_merge.contributor_tier.auto_promote_after_merged` | `5` | Clean merged PRs (no revert, no sentinel label, not undone by a later merged revert) after which the contributor lane treats an author as `trusted` without an operator grant, unless the registry marks them `revoked`. Negative disables the ratchet. | -| `auto_merge.contributor_lane.approve_held_runs_for_registry_trusted` | `true` | When the registry path admitted the PR author and the lane stops at `required-check-missing:`, approve the head's `action_required` workflow runs once (`POST /repos/{owner}/{repo}/actions/runs/{id}/approve`, fork heads included), post one `hive:contributor-held-runs-approved` audit comment and log `contributor-lane approved held runs`. Never acts for claim-marker, auto-promoted or association admissions, sentinel/exclude-labelled PRs, or diffs touching `.github/workflows/**`. Needs the App's **Actions: Read and write** permission. See [hive-merge.md](hive-merge.md#contributor-lane-auto-merge). | +| `auto_merge.contributor_lane.approve_held_runs_for_registry_trusted` | `true` | When the registry path admitted the PR author and the lane stops at `required-check-missing:` (or at `ci-unverified` with every `pull_request` run on the head held), approve the head's `action_required` workflow runs once (`POST /repos/{owner}/{repo}/actions/runs/{id}/approve`, fork heads included), post one `hive:contributor-held-runs-approved` audit comment and log `contributor-lane approved held runs`. Never acts for claim-marker, auto-promoted or association admissions, sentinel/exclude-labelled PRs, or diffs touching `.github/workflows/**`. Needs the App's **Actions: Read and write** permission. See [hive-merge.md](hive-merge.md#contributor-lane-auto-merge). | | `auto_merge.contributor_lane.owner_approval_releases_workflow_fence` | `true` | A hive owner / trusted merger's explicit approval on the **current** head — a GitHub `APPROVED` review on that commit, or a dashboard `/hive decision approve` bound to it — releases the contributor lane's `contributor-lane:workflow-path` fence (never for a head that also removes tests, which stays `contributor-lane:tests-removed`), so the lane merges a PR editing `.github/workflows/**` / `.github/actions/**` itself once CI, soak and the other gates are clear. Logs `workflow-path fence released by owner approval`, leaves one `hive:workflow-approval` audit comment per head; approval on an older head does not count. An unmarked approval binds to the PR `updated_at` pinned on the hive's first sweep of the head, not to the push, so one typed between the push and that sweep is judged stale and must be typed again ([#14754](https://github.com/hivecommons/hive/issues/14754)). Needs the App's **Workflows: Read and write**; without it the merge is refused, the PR is marked `hive:unauthorized` (`merge-workflow-path`) and parks at 090 for a person's click. `false` keeps every workflow-path PR a person's merge ([#14664](https://github.com/hivecommons/hive/issues/14664)). See [hive-merge.md](hive-merge.md#contributor-lane-auto-merge). | | `auto_merge.contributor_lane.repos` | empty = all watched repos | Optional repo allow-list for the contributor-lane tier (bare name or `owner/repo`). | | `auto_merge.contributor_lane.exclude_labels` | `hold`, `do-not-merge`, `needs-human`, `blocked`, `needs-rebase` | Labels that keep a PR out of contributor-lane auto-merge (`do-not-merge/*` always excluded). | diff --git a/src/pkg/github/automerge/automerge_contributor_held_runs.go b/src/pkg/github/automerge/automerge_contributor_held_runs.go index f63ecafd43..37659244cf 100644 --- a/src/pkg/github/automerge/automerge_contributor_held_runs.go +++ b/src/pkg/github/automerge/automerge_contributor_held_runs.go @@ -11,8 +11,10 @@ package automerge // is pure drag (hivecommons/docs#481 sat 17h). The lane approves those runs // once per head with the App token when, and only when, the registry path // admitted the author and the lane's own CI reason is -// `required-check-missing:`. Claim-marker, auto-promotion and -// association admissions stay untouched. +// `required-check-missing:`, or `ci-unverified` with every +// `pull_request` run on the head held (a branch whose required checks the +// hive cannot read). Claim-marker, auto-promotion and association +// admissions stay untouched. import ( "context" @@ -35,11 +37,18 @@ const ( // contributorHeldRunsApprovalKeyPrefix namespaces the once-per-head // bookkeeping inside Engine.heldRunApprovals. contributorHeldRunsApprovalKeyPrefix = "registry-held-approve:" + // contributorHeldRunsCIUnverified is the commit-CI reason for a head with + // no CI evidence while its required checks are unknown or known-empty. + contributorHeldRunsCIUnverified = "ci-unverified" ) // approveHeldRunsForRegistryTrusted approves the `action_required` workflow // runs for a contributor-lane head once, when the author was admitted by the -// registry path and the lane stopped at reason (`required-check-missing:`). +// registry path and the lane stopped at reason `required-check-missing:`, +// or at `ci-unverified` while every `pull_request` run GitHub has for the +// head is held: with the required set unreadable or known-empty, no CI +// evidence ever appears until someone approves, so the lane would wait +// forever on a contributor the operator already trusts. // It refuses for any other admission path, when the toggle is off, when the // PR carries the sentinel or an exclude label, or when the diff touches CI // definitions. It returns the run ids it approved (nil when it did nothing) @@ -55,7 +64,7 @@ func (c *Engine) approveHeldRunsForRegistryTrusted(ctx context.Context, displayR if !policy.ApproveHeldRunsForRegistryTrusted || !claim.admittedByRegistry() { return nil, reason } - if !strings.HasPrefix(reason, requiredCheckMissingPrefix) { + if !contributorHeldRunsReason(reason) { return nil, reason } labels := labelNames(pr.Labels) @@ -73,7 +82,10 @@ func (c *Engine) approveHeldRunsForRegistryTrusted(ctx context.Context, displayR } return nil, reason } - missing := strings.TrimPrefix(reason, requiredCheckMissingPrefix) + missing := "" + if strings.HasPrefix(reason, requiredCheckMissingPrefix) { + missing = strings.TrimPrefix(reason, requiredCheckMissingPrefix) + } runs, _, err := c.gh.Actions.ListRepositoryWorkflowRuns(hgithub.WithRESTCaller(ctx, "hive:contributor_lane_automerge_sweep"), owner, repo, &gh.ListWorkflowRunsOptions{ HeadSHA: headSHA, Event: "pull_request", @@ -83,10 +95,16 @@ func (c *Engine) approveHeldRunsForRegistryTrusted(ctx context.Context, displayR c.warn("contributor-lane could not list workflow runs for held-run approval", "repo", displayRepo, "pr", number, "head", headSHA, "error", err) return nil, reason } - held := heldWorkflowRunsForHead(runs.WorkflowRuns, contributorLaneHeadRepo(pr, owner, repo)) + headRepo := contributorLaneHeadRepo(pr, owner, repo) + held := heldWorkflowRunsForHead(runs.WorkflowRuns, headRepo) if len(held) == 0 { return nil, reason } + if missing == "" && len(held) != len(workflowRunsForHeadRepo(runs.WorkflowRuns, headRepo)) { + // ci-unverified with some run already started or finished: the + // evidence gap is not the approval hold, so leave it alone. + return nil, reason + } // Mark before calling the API: a partial failure must not be retried // every tick against the same head. if !c.markContributorHeldRunsApprovalAttempted(owner, repo, number, headSHA) { @@ -120,6 +138,12 @@ func (c *Engine) approveHeldRunsForRegistryTrusted(ctx context.Context, displayR return approved, reason } +// contributorHeldRunsReason reports whether a lane CI reason is one the +// held-run approver acts on. +func contributorHeldRunsReason(reason string) bool { + return strings.HasPrefix(reason, requiredCheckMissingPrefix) || reason == contributorHeldRunsCIUnverified +} + // approveWorkflowRun is POST /repos/{owner}/{repo}/actions/runs/{run_id}/approve. // go-github v72 has no binding for it. func (c *Engine) approveWorkflowRun(ctx context.Context, owner, repo string, runID int64) error { @@ -143,6 +167,17 @@ func contributorLaneHeadRepo(pr *gh.PullRequest, owner, repo string) string { return owner + "/" + repo } +// workflowRunsForHeadRepo returns the runs whose head repository is headRepo. +func workflowRunsForHeadRepo(runs []*gh.WorkflowRun, headRepo string) []*gh.WorkflowRun { + var out []*gh.WorkflowRun + for _, run := range runs { + if run != nil && run.GetHeadRepository() != nil && strings.EqualFold(run.GetHeadRepository().GetFullName(), headRepo) { + out = append(out, run) + } + } + return out +} + // heldWorkflowRunsForHead returns the runs waiting on approval whose head // repository is headRepo. Unlike heldSameRepoWorkflowRuns the expected head // repository may be a fork: the PR author's registry tier, not the head's diff --git a/src/pkg/github/automerge/automerge_contributor_held_runs_test.go b/src/pkg/github/automerge/automerge_contributor_held_runs_test.go index b31434424e..ac88b4fef9 100644 --- a/src/pkg/github/automerge/automerge_contributor_held_runs_test.go +++ b/src/pkg/github/automerge/automerge_contributor_held_runs_test.go @@ -450,3 +450,105 @@ func TestUnauthorizedCommentAndMarker(t *testing.T) { t.Fatalf("reason = %q", got) } } + +// A rulesets-only repository (projectbluefin/testsuite, bluefin-bling): the +// classic protection answer is "Branch not protected", the ruleset lists the +// required checks. The lane must read the ruleset, stop at +// required-check-missing and approve the registry-trusted author's held runs +// rather than sit at ci-unverified forever. +func TestContributorLaneApprovesHeldRunsOnRulesetsOnlyBranch(t *testing.T) { + fx := validContributorFixture() + fx.baseRef = "main" + fx.body = "Part of #42\n\nSome change." + fx.issueComments = nil + fx.headRepo = "thatguywob/widget" + fx.rulesetContexts = []string{"ci/build", contributorHeldRunsTestContext} + fx.workflowRuns = []map[string]any{heldRun(11, "thatguywob/widget"), heldRun(12, "thatguywob/widget")} + api := newContributorLaneAPI(t, &fx) + defer api.Close() + policy := defaultContributorLanePolicy() + policy.RegistryTiersSufficient = true + policy.ApproveHeldRunsForRegistryTrusted = true + clock := &contributorLaneClock{at: time.Date(2026, 10, 29, 9, 0, 0, 0, time.UTC)} + engine := newContributorLaneEngine(api.URL, policy, clock, Options{ContributorRegistryTier: registryTiers(map[string]string{contributorLaneTestAuthor: "trusted"}), ContributorSoak: soakedTracker(clock)}) + + _, reason, err := engine.trySweepContributorLanePR(context.Background(), "widget", "acme", "widget", 7, policy) + if err != nil { + t.Fatal(err) + } + if want := requiredCheckMissingPrefix + contributorHeldRunsTestContext; reason != want { + t.Fatalf("reason = %q, want %q (ruleset required checks read)", reason, want) + } + if !slices.Equal(fx.approvedRuns, []int64{11, 12}) { + t.Fatalf("approved runs = %v, want [11 12]", fx.approvedRuns) + } + if fx.merges != 0 { + t.Fatalf("merges = %d, want 0", fx.merges) + } +} + +// With no readable required checks a held head reports ci-unverified. The +// approver acts only when every pull_request run on the head is held; a head +// where some run already started is an ordinary evidence gap. +func TestContributorLaneApprovesHeldRunsWhenCIUnverifiedAndAllHeld(t *testing.T) { + for _, tc := range []struct { + name string + runs []map[string]any + registry map[string]string + wantApproved []int64 + }{ + { + name: "every run held approves", + runs: []map[string]any{heldRun(11, "acme/widget"), heldRun(12, "acme/widget"), heldRun(14, "mallory/widget")}, + registry: map[string]string{contributorLaneTestAuthor: "trusted"}, + wantApproved: []int64{11, 12}, + }, + { + name: "a run already in progress leaves it alone", + runs: []map[string]any{ + heldRun(11, "acme/widget"), + {"id": 13, "name": "lint", "status": "in_progress", "head_repository": map[string]any{"full_name": "acme/widget"}}, + }, + registry: map[string]string{contributorLaneTestAuthor: "trusted"}, + }, + { + name: "claim marker author is not approved for", + runs: []map[string]any{heldRun(11, "acme/widget")}, + registry: nil, + }, + } { + t.Run(tc.name, func(t *testing.T) { + fx := validContributorFixture() + fx.baseRef = "main" + if tc.registry != nil { + fx.body = "Part of #42\n\nSome change." + fx.issueComments = nil + } + fx.noCIEvidence = true + fx.workflowRuns = tc.runs + api := newContributorLaneAPI(t, &fx) + defer api.Close() + policy := defaultContributorLanePolicy() + policy.RegistryTiersSufficient = true + policy.ApproveHeldRunsForRegistryTrusted = true + clock := &contributorLaneClock{at: time.Date(2026, 10, 29, 9, 0, 0, 0, time.UTC)} + engine := newContributorLaneEngine(api.URL, policy, clock, Options{ContributorRegistryTier: registryTiers(tc.registry), ContributorSoak: soakedTracker(clock)}) + + for tick := 1; tick <= 2; tick++ { + _, reason, err := engine.trySweepContributorLanePR(context.Background(), "widget", "acme", "widget", 7, policy) + if err != nil { + t.Fatalf("tick %d: %v", tick, err) + } + if reason != contributorHeldRunsCIUnverified { + t.Fatalf("tick %d reason = %q, want %q", tick, reason, contributorHeldRunsCIUnverified) + } + } + if !slices.Equal(fx.approvedRuns, tc.wantApproved) { + t.Fatalf("approved runs = %v, want %v (once across two ticks)", fx.approvedRuns, tc.wantApproved) + } + if fx.merges != 0 { + t.Fatalf("merges = %d, want 0", fx.merges) + } + }) + } +} diff --git a/src/pkg/github/automerge/automerge_contributor_lane.go b/src/pkg/github/automerge/automerge_contributor_lane.go index b636c7a65f..2dd4953dd4 100644 --- a/src/pkg/github/automerge/automerge_contributor_lane.go +++ b/src/pkg/github/automerge/automerge_contributor_lane.go @@ -1384,7 +1384,7 @@ func (c *Engine) evaluateContributorLanePR(ctx context.Context, displayRepo, own return eval, reason, err } if !green { - if strings.HasPrefix(reason, requiredCheckMissingPrefix) { + if contributorHeldRunsReason(reason) { _, reason = c.approveHeldRunsForRegistryTrusted(ctx, displayRepo, owner, repo, pr, number, headSHA, claim, policy, files, reason) } if !strings.HasPrefix(reason, unauthorizedReasonPrefix) { diff --git a/src/pkg/github/automerge/automerge_contributor_lane_test.go b/src/pkg/github/automerge/automerge_contributor_lane_test.go index a4c305b947..1c8c8f7c73 100644 --- a/src/pkg/github/automerge/automerge_contributor_lane_test.go +++ b/src/pkg/github/automerge/automerge_contributor_lane_test.go @@ -104,6 +104,12 @@ type contributorLaneFixture struct { // unprotected. The status/check-runs routes only ever report ci/build, // so any other name yields required-check-missing:. requiredContexts []string + // rulesetContexts, when set with baseRef, is what the branch's rulesets + // (rules/branches) list as required_status_checks; empty serves []. + rulesetContexts []string + // noCIEvidence makes the status and check-runs routes for sha7 report + // nothing, as for a head whose only workflow runs GitHub is holding. + noCIEvidence bool // workflowRuns is the payload of GET actions/runs?head_sha=sha7; nil // serves an empty list. workflowRuns []map[string]any @@ -302,6 +308,10 @@ func newContributorLaneAPI(t *testing.T, fx *contributorLaneFixture) *httptest.S json.NewEncoder(w).Encode(fx.reviews) case r.Method == http.MethodGet && (r.URL.Path == "/repos/acme/widget/commits/sha7/status" || (fx.recheckHeadSHA != "" && r.URL.Path == "/repos/acme/widget/commits/"+fx.recheckHeadSHA+"/status")): + if fx.noCIEvidence { + json.NewEncoder(w).Encode(map[string]any{"state": "pending", "total_count": 0, "statuses": []any{}}) + return + } json.NewEncoder(w).Encode(map[string]any{ "state": headStatus, "total_count": 1, @@ -309,6 +319,10 @@ func newContributorLaneAPI(t *testing.T, fx *contributorLaneFixture) *httptest.S }) case r.Method == http.MethodGet && (r.URL.Path == "/repos/acme/widget/commits/sha7/check-runs" || (fx.recheckHeadSHA != "" && r.URL.Path == "/repos/acme/widget/commits/"+fx.recheckHeadSHA+"/check-runs")): + if fx.noCIEvidence { + json.NewEncoder(w).Encode(map[string]any{"total_count": 0, "check_runs": []any{}}) + return + } run := map[string]string{"name": "build", "status": "completed", "conclusion": headStatus} if fx.checkCompletedAt != "" { run["completed_at"] = fx.checkCompletedAt @@ -361,6 +375,14 @@ func newContributorLaneAPI(t *testing.T, fx *contributorLaneFixture) *httptest.S } w.WriteHeader(http.StatusCreated) case fx.baseRef != "" && r.Method == http.MethodGet && r.URL.Path == "/repos/acme/widget/rules/branches/"+fx.baseRef: + if len(fx.rulesetContexts) > 0 { + checks := make([]map[string]string, 0, len(fx.rulesetContexts)) + for _, name := range fx.rulesetContexts { + checks = append(checks, map[string]string{"context": name}) + } + json.NewEncoder(w).Encode([]any{map[string]any{"type": "required_status_checks", "parameters": map[string]any{"required_status_checks": checks}}}) + return + } json.NewEncoder(w).Encode([]any{}) case fx.baseRef != "" && r.Method == http.MethodGet && r.URL.Path == "/repos/acme/widget/commits/base1/status": json.NewEncoder(w).Encode(map[string]any{"state": "success", "total_count": 0, "statuses": []any{}}) diff --git a/src/pkg/github/automerge/automerge_sweep_guards_test.go b/src/pkg/github/automerge/automerge_sweep_guards_test.go index 4ae700b15e..51632f2e83 100644 --- a/src/pkg/github/automerge/automerge_sweep_guards_test.go +++ b/src/pkg/github/automerge/automerge_sweep_guards_test.go @@ -174,6 +174,8 @@ func newSelfSweepGuardAPI(t *testing.T, fx selfSweepFixture) *httptest.Server { return } json.NewEncoder(w).Encode(map[string]any{"merged": fx.mergeApplied, "sha": "merge7"}) + case r.Method == http.MethodGet && strings.Contains(r.URL.Path, "/rules/branches/"): + json.NewEncoder(w).Encode([]any{}) // no rulesets default: t.Fatalf("unexpected request: %s %s", r.Method, r.URL.String()) } diff --git a/src/pkg/github/automerge/automerge_sweep_test.go b/src/pkg/github/automerge/automerge_sweep_test.go index cd3ef07017..ec87df0ca9 100644 --- a/src/pkg/github/automerge/automerge_sweep_test.go +++ b/src/pkg/github/automerge/automerge_sweep_test.go @@ -895,6 +895,8 @@ func TestCommitGreenKnownEmptyRequiredChecksBlocksCompletedFailures(t *testing.T runs = append(runs, map[string]string{"name": c.name, "status": c.status, "conclusion": c.conclusion}) } json.NewEncoder(w).Encode(map[string]any{"total_count": len(runs), "check_runs": runs}) + case r.Method == http.MethodGet && strings.Contains(r.URL.Path, "/rules/branches/"): + json.NewEncoder(w).Encode([]any{}) // no rulesets default: t.Fatalf("unexpected request: %s %s", r.Method, r.URL.Path) } @@ -1463,6 +1465,8 @@ func TestCommitGreenUnprotectedBaseIgnoresMetaWithEvidence(t *testing.T) { runs = append(runs, map[string]string{"name": c.name, "status": c.status, "conclusion": c.conclusion}) } json.NewEncoder(w).Encode(map[string]any{"total_count": len(runs), "check_runs": runs}) + case r.Method == http.MethodGet && strings.Contains(r.URL.Path, "/rules/branches/"): + json.NewEncoder(w).Encode([]any{}) // no rulesets default: t.Fatalf("unexpected request: %s %s", r.Method, r.URL.Path) } @@ -1666,6 +1670,8 @@ func TestCommitGreenKnownEmptyRequiredChecksBlocksFailedCheck(t *testing.T) { {"name": "build", "status": "completed", "conclusion": "failure"}, {"name": "lint", "status": "completed", "conclusion": "success"}, }}) + case r.Method == http.MethodGet && strings.Contains(r.URL.Path, "/rules/branches/"): + json.NewEncoder(w).Encode([]any{}) // no rulesets default: t.Fatalf("unexpected request: %s %s", r.Method, r.URL.Path) } @@ -3220,6 +3226,8 @@ func TestCommitGreenKnownEmptyRequiredChecksRequiresEvidence(t *testing.T) { json.NewEncoder(w).Encode(map[string]any{"state": "success", "total_count": 0}) case r.Method == http.MethodGet && r.URL.Path == "/repos/acme/widget/commits/sha/check-runs": json.NewEncoder(w).Encode(map[string]any{"total_count": 0, "check_runs": []map[string]any{}}) + case r.Method == http.MethodGet && strings.Contains(r.URL.Path, "/rules/branches/"): + json.NewEncoder(w).Encode([]any{}) // no rulesets default: t.Fatalf("unexpected request: %s %s", r.Method, r.URL.Path) } diff --git a/src/pkg/github/commit_ci.go b/src/pkg/github/commit_ci.go index 5d0dc20c24..99f6ccc745 100644 --- a/src/pkg/github/commit_ci.go +++ b/src/pkg/github/commit_ci.go @@ -165,9 +165,12 @@ func RequiredStatusCheckContexts(ctx context.Context, client *gh.Client, owner, } // RequiredStatusCheckContextsDetailed returns the branch-protection-required -// status/check contexts. GitHub branch protection is authoritative; the -// non-admin branch and rules endpoints are fallbacks when protection cannot be -// read, and the config set is the final operator-declared fallback. +// status/check contexts. GitHub branch protection is authoritative when it +// lists required checks; the non-admin branch and rules endpoints are +// fallbacks when protection cannot be read, and the config set is the final +// operator-declared fallback. A classic "nothing required" answer defers to +// the branch's rulesets when they list required checks +// (rulesetRequiredWhenClassicEmpty). func RequiredStatusCheckContextsDetailed(ctx context.Context, client *gh.Client, owner, repo, branch string, configSet map[string]bool, configKnown bool) (required map[string]bool, known bool, fromConfig bool, fallback bool) { required, known, fromConfig, fallback, _ = RequiredStatusCheckContextsDetailedWithSource(ctx, client, owner, repo, branch, configSet, configKnown) return required, known, fromConfig, fallback @@ -184,13 +187,21 @@ func RequiredStatusCheckContextsDetailedWithSource(ctx context.Context, client * if !sharedRequiredChecksForbiddenCache.get(cacheKey) { rsc, _, err := client.Repositories.GetRequiredStatusChecks(ctx, owner, repo, branch) if err == nil { - if rsc == nil { + if rsc != nil { + required = requiredContextsFromRequiredStatusChecks(rsc.Contexts, rsc.Checks) + } + if len(required) == 0 { + if rules, ok := rulesetRequiredWhenClassicEmpty(ctx, client, owner, repo, branch); ok { + return rules, true, false, false, "rules" + } return map[string]bool{}, true, false, false, "protection" } - required = requiredContextsFromRequiredStatusChecks(rsc.Contexts, rsc.Checks) return required, true, false, false, "protection" } if errors.Is(err, gh.ErrBranchNotProtected) { + if rules, ok := rulesetRequiredWhenClassicEmpty(ctx, client, owner, repo, branch); ok { + return rules, true, false, false, "rules" + } return map[string]bool{}, true, false, false, "protection-unprotected" } if isRequiredChecksForbiddenCacheable(err) { @@ -199,6 +210,11 @@ func RequiredStatusCheckContextsDetailedWithSource(ctx context.Context, client * } if branchRequired, branchKnown, err := requiredStatusChecksFromBranchEndpoint(ctx, client, owner, repo, branch); err == nil && branchKnown { + if len(branchRequired) == 0 { + if rules, ok := rulesetRequiredWhenClassicEmpty(ctx, client, owner, repo, branch); ok { + return rules, true, false, false, "rules" + } + } return branchRequired, true, false, false, "branch" } if rulesRequired, rulesKnown, err := requiredStatusChecksFromRulesEndpoint(ctx, client, owner, repo, branch); err == nil && rulesKnown { @@ -210,6 +226,23 @@ func RequiredStatusCheckContextsDetailedWithSource(ctx context.Context, client * return nil, false, false, false, "none" } +// rulesetRequiredWhenClassicEmpty reads the ruleset required checks for a +// branch whose classic answer was "nothing required": an unprotected branch, +// or protection (or the branch payload's protection, `enabled:false`) without +// required checks. Classic protection never lists ruleset rules, so on a +// rulesets-only repository that answer is not authoritative; GitHub still +// enforces the ruleset's checks at merge time (projectbluefin/testsuite and +// bluefin-bling read as known-empty and their held fork runs were never +// approved). It reports false when the rules endpoint fails or lists no +// required check, leaving the classic answer standing. +func rulesetRequiredWhenClassicEmpty(ctx context.Context, client *gh.Client, owner, repo, branch string) (map[string]bool, bool) { + rules, known, err := requiredStatusChecksFromRulesEndpoint(ctx, client, owner, repo, branch) + if err != nil || !known || len(rules) == 0 { + return nil, false + } + return rules, true +} + func requiredContextsFromRequiredStatusChecks(contexts *[]string, checks *[]*gh.RequiredStatusCheck) map[string]bool { required := make(map[string]bool) if contexts != nil { diff --git a/src/pkg/github/merge_ci_policy_gap_test.go b/src/pkg/github/merge_ci_policy_gap_test.go index 0846612f5e..b795a21999 100644 --- a/src/pkg/github/merge_ci_policy_gap_test.go +++ b/src/pkg/github/merge_ci_policy_gap_test.go @@ -192,6 +192,80 @@ func TestRequiredStatusCheckContexts(t *testing.T) { } }) + // Rulesets-only branches (projectbluefin/testsuite, bluefin-bling): every + // classic source answers "nothing required" while the ruleset requires + // checks GitHub enforces at merge time. + rulesetOnly := func(protection func(w http.ResponseWriter), branch string, rules string) *httptest.Server { + return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + switch { + case strings.HasSuffix(r.URL.Path, "/protection/required_status_checks"): + protection(w) + case r.URL.Path == "/repos/o/r/branches/main" && branch != "": + _, _ = io.WriteString(w, branch) + case r.URL.Path == "/repos/o/r/rules/branches/main" && rules != "": + _, _ = io.WriteString(w, rules) + default: + t.Fatalf("unexpected request: %s %s", r.Method, r.URL.Path) + } + })) + } + notProtected := func(w http.ResponseWriter) { + w.WriteHeader(http.StatusNotFound) + _, _ = io.WriteString(w, `{"message":"Branch not protected"}`) + } + forbidden := func(w http.ResponseWriter) { + w.WriteHeader(http.StatusForbidden) + _, _ = io.WriteString(w, `{"message":"Resource not accessible by integration"}`) + } + noChecks := func(w http.ResponseWriter) { + _, _ = io.WriteString(w, `{"strict":false,"contexts":[],"checks":[]}`) + } + rulesWithChecks := `[{"type":"pull_request"},{"type":"required_status_checks","parameters":{"required_status_checks":[{"context":"pytest"},{"context":"Lint & syntax"}]}}]` + // The live answer for a rulesets-only branch: protected, protection + // disabled, no contexts. + branchDisabled := `{"protected":true,"protection":{"enabled":false,"required_status_checks":{"checks":[],"contexts":[],"enforcement_level":"off"}}}` + wantRules := map[string]bool{"pytest": true, "Lint & syntax": true} + for _, tc := range []struct { + name string + protection func(w http.ResponseWriter) + branch string + rules string + want map[string]bool + wantSource string + }{ + {"unprotected classic defers to ruleset checks", notProtected, "", rulesWithChecks, wantRules, "rules"}, + {"protection without checks defers to ruleset checks", noChecks, "", rulesWithChecks, wantRules, "rules"}, + {"forbidden protection and disabled branch payload defer to ruleset checks", forbidden, branchDisabled, rulesWithChecks, wantRules, "rules"}, + {"unprotected with no ruleset checks stays known-empty", notProtected, "", `[{"type":"pull_request"}]`, map[string]bool{}, "protection-unprotected"}, + {"disabled branch payload with no rulesets stays known-empty", forbidden, branchDisabled, `[]`, map[string]bool{}, "branch"}, + {"ruleset rule with no contexts does not replace the classic answer", notProtected, "", `[{"type":"required_status_checks","parameters":{"required_status_checks":[]}}]`, map[string]bool{}, "protection-unprotected"}, + } { + t.Run(tc.name, func(t *testing.T) { + restore := resetRequiredChecksForbiddenCacheForTest(time.Now, time.Hour) + defer restore() + srv := rulesetOnly(tc.protection, tc.branch, tc.rules) + defer srv.Close() + c := NewClientForTest(srv.URL, "o", []string{"o/r"}, nil) + set, known, fromConfig, fallback, source := RequiredStatusCheckContextsDetailedWithSource(ctx, c.client, "o", "r", "main", nil, false) + if !known || fromConfig || fallback || source != tc.wantSource || !reflect.DeepEqual(set, tc.want) { + t.Fatalf("got set=%v known=%v fromConfig=%v fallback=%v source=%q, want %v true false false %q", set, known, fromConfig, fallback, source, tc.want, tc.wantSource) + } + }) + } + + t.Run("protection with checks does not read rulesets", func(t *testing.T) { + srv := rulesetOnly(func(w http.ResponseWriter) { + _, _ = io.WriteString(w, `{"strict":false,"contexts":["build"]}`) + }, "", "") // an empty rules answer makes a rules read an unexpected request + defer srv.Close() + c := NewClientForTest(srv.URL, "o", []string{"o/r"}, nil) + set, known, _, _, source := RequiredStatusCheckContextsDetailedWithSource(ctx, c.client, "o", "r", "main", nil, false) + if !known || source != "protection" || !reflect.DeepEqual(set, map[string]bool{"build": true}) { + t.Fatalf("got set=%v known=%v source=%q, want {build} true protection", set, known, source) + } + }) + t.Run("other API errors leave the set unknown", func(t *testing.T) { srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusInternalServerError) diff --git a/src/pkg/github/pr_ci_trail_recovery_test.go b/src/pkg/github/pr_ci_trail_recovery_test.go index 15d85e376d..2c45fdc807 100644 --- a/src/pkg/github/pr_ci_trail_recovery_test.go +++ b/src/pkg/github/pr_ci_trail_recovery_test.go @@ -175,6 +175,8 @@ func (f *trailFixture) serve(t *testing.T) *httptest.Server { }) } enc(map[string]any{"total_count": len(runs), "check_runs": runs}) + case strings.Contains(r.URL.Path, "/rules/branches/"): + enc([]any{}) // no rulesets default: t.Errorf("unexpected request %s %s", r.Method, r.URL) fail(http.StatusNotFound, "Not Found")