diff --git a/changelog.d/fixed-shared-ci-permission-recovery.md b/changelog.d/fixed-shared-ci-permission-recovery.md new file mode 100644 index 0000000000..cbca2ce9e1 --- /dev/null +++ b/changelog.d/fixed-shared-ci-permission-recovery.md @@ -0,0 +1 @@ +- Surface shared-CI recovery permission refusals as an actionable operator handoff, retry after the permission cooldown even on unchanged PRs, and align bot-PR squash attribution with the existing required check. diff --git a/src/docs/github-app-setup.md b/src/docs/github-app-setup.md index ec8a31b59c..81db0abb0c 100644 --- a/src/docs/github-app-setup.md +++ b/src/docs/github-app-setup.md @@ -101,6 +101,27 @@ Organization permission: | --- | --- | --- | | Members | Read-only | Identify contributors and owners where configured. | +### Optional shared-CI recovery + +The shared-CI rerun sweep (`shared_ci_rerun.enabled`, default on) can inspect +failed runs with Actions read, but `rerun-failed-jobs` requires **Actions: Read +and write** on the repository's recovery identity. This is optional, not a new +baseline permission for every Hive App. A refusal is an operator handoff, not +a source-code defect: Hive records `unauthorized:rerun-shared-ci`, posts one +head-scoped actionable note using the existing unauthorized mechanism, and +retries no more than once per repository per permission cooldown (one hour). +An unchanged PR remains eligible after the cooldown; a successful rerun or +observing no failed runs clears the recovery block. + +A maintainer may re-run the named workflow with their already-authorized +identity. That manual recovery does **not** change the App's permissions. For +automatic recovery, the App owner must explicitly request Actions read and +write for the intended recovery installation, and its installation owner must +accept the update in **Settings → GitHub Apps → Configure**. Do not broaden +unrelated Apps or repositories or silently substitute a PAT. Leaving the grant +at read is supported; an operator may disable the optional sweep instead. +Neither action grants merge approval or bypasses required checks. + ### The optional Visual Hive App (#4030) Visual Hive needs two grants the Hive App does not otherwise require — Actions diff --git a/src/pkg/github/automerge/automerge_stage_dco.go b/src/pkg/github/automerge/automerge_stage_dco.go index 0c163fff08..4a540cfb0d 100644 --- a/src/pkg/github/automerge/automerge_stage_dco.go +++ b/src/pkg/github/automerge/automerge_stage_dco.go @@ -64,7 +64,7 @@ func dcoCommitStatsDetailedForAuthor(commits []*gh.RepositoryCommit, appLogin, p } else { stats.unsignedOther++ } - } else if dcoCommitByHive(rc, appLogin) && !messageHasPRAuthorSignoff(msg, prAuthor) { + } else if !botSquashAuthor(prAuthor) && dcoCommitByHive(rc, appLogin) && !messageHasPRAuthorSignoff(msg, prAuthor) { stats.squashUnsafeHive++ } } @@ -96,7 +96,11 @@ func (c *Engine) stageDCO(ctx context.Context, store *stagelabels.Store, key str // none; an in-place adoption decides repair-vs-rehome on it, // so read the commits once more for that case only. if (u > 0 || rec.DCOSquashUnsafeKnown) && (u == 0 || rec.DCOSplitKnown(obs.HeadSHA) || !rec.AdoptedInPlace()) { - return dcoStats{total: t, unsigned: u, unsignedHive: rec.DCOUnsignedHive, unsignedOther: u - rec.DCOUnsignedHive, squashUnsafeHive: rec.DCOSquashUnsafeHive}, nil + unsafe := rec.DCOSquashUnsafeHive + if botSquashAuthor(obs.Author) { + unsafe = 0 + } + return dcoStats{total: t, unsigned: u, unsignedHive: rec.DCOUnsignedHive, unsignedOther: u - rec.DCOUnsignedHive, squashUnsafeHive: unsafe}, nil } } } @@ -116,6 +120,12 @@ func (c *Engine) stageDCO(ctx context.Context, store *stagelabels.Store, key str return dcoCommitStatsDetailedForAuthor(all, c.transport.AppBotLogin(), obs.Author), nil } +// Matches the bot-PR policy in check-squash-signoff-attribution.sh: a squash +// remains bot-authored. Unsigned commits are still counted and rejected. +func botSquashAuthor(login string) bool { + return strings.HasSuffix(strings.ToLower(strings.TrimSpace(login)), "[bot]") +} + func messageHasPRAuthorSignoff(message, prAuthor string) bool { login := strings.ToLower(strings.TrimSpace(prAuthor)) if login == "" { diff --git a/src/pkg/github/automerge/automerge_stage_labels.go b/src/pkg/github/automerge/automerge_stage_labels.go index fc3f77e4f5..1b7f6f9741 100644 --- a/src/pkg/github/automerge/automerge_stage_labels.go +++ b/src/pkg/github/automerge/automerge_stage_labels.go @@ -1138,6 +1138,14 @@ func (c *Engine) ApplyStageLabels(ctx context.Context) (StageLabelResult, error) route := c.fixerExhaustedRoute(obs, fixer) obs.Route = &route } + if step := c.sharedCIRecoveryUnauthorized(ctx, obs); step != "" && verdict.Stage != stagelabels.None { + pass.RecoveryUnauthorized = step + verdict.Stage = stagelabels.NeedsDecision + verdict.Rank = stageRankOwned + obs.Reason = unauthorizedReason(step) + route := stagelabels.Route{Stage: stagelabels.NeedsDecision} + obs.Route = &route + } tr = store.Observe(key, verdict.Stage, obs.Reason) // hivecommons/hive#12502: remember the author so the dashboard can // list "PRs returned to this contributor" from the store alone. diff --git a/src/pkg/github/automerge/shared_ci_attribution_test.go b/src/pkg/github/automerge/shared_ci_attribution_test.go new file mode 100644 index 0000000000..d3d5e83a99 --- /dev/null +++ b/src/pkg/github/automerge/shared_ci_attribution_test.go @@ -0,0 +1,52 @@ +package automerge + +import ( + "context" + "testing" + "time" + + gh "github.com/google/go-github/v72/github" + "github.com/hivecommons/hive/pkg/stagelabels" +) + +func TestBotSquashAttributionMatchesRequiredCheck(t *testing.T) { + for _, tc := range []struct { + author string + message string + unsafe int + unsigned int + }{ + {"dependabot[bot]", "repair\n\nSigned-off-by: Hive ", 0, 0}, + {"renovate[bot]", "repair\n\nSigned-off-by: Hive ", 0, 0}, + {"person", "repair\n\nSigned-off-by: Hive ", 1, 0}, + {"person-bot", "repair\n\nSigned-off-by: Hive ", 1, 0}, + {"dependabot[bot]", "unsigned repair", 0, 1}, + } { + t.Run(tc.author+tc.message, func(t *testing.T) { + commits := []*gh.RepositoryCommit{{ + Author: &gh.User{Login: gh.Ptr("hive[bot]")}, + Commit: &gh.Commit{Message: gh.Ptr(tc.message)}, + }} + got := dcoCommitStatsDetailedForAuthor(commits, "hive[bot]", tc.author) + if got.total != 1 || got.squashUnsafeHive != tc.unsafe || got.unsigned != tc.unsigned { + t.Fatalf("stats = %+v, want unsafe=%d unsigned=%d", got, tc.unsafe, tc.unsigned) + } + }) + } +} + +func TestStageDCONormalizesCachedBotAttributionWithoutChangingSignoffs(t *testing.T) { + api := &stageAPI{labels: map[string]string{}} + srv := newStageAPI(t, api) + defer srv.Close() + e, _, store := newStageEngine(t, srv.URL, &contributorLaneClock{at: time.Now()}) + key := stageKey("acme", "widget", 42) + store.Observe(key, stagelabels.Fixing, stagelabels.LaneReasonHiveSignoffAttribution) + store.NoteDCO(key, "aaa", 3, 0, 0, 1) + got, err := e.stageDCO(context.Background(), store, key, stageObservation{ + Owner: "acme", Repo: "widget", Number: 42, HeadSHA: "aaa", Author: "dependabot[bot]", + }) + if err != nil || got.total != 3 || got.unsigned != 0 || got.squashUnsafeHive != 0 { + t.Fatalf("cached bot attribution: %+v, %v", got, err) + } +} diff --git a/src/pkg/github/automerge/shared_ci_recovery.go b/src/pkg/github/automerge/shared_ci_recovery.go new file mode 100644 index 0000000000..1ea42193e2 --- /dev/null +++ b/src/pkg/github/automerge/shared_ci_recovery.go @@ -0,0 +1,43 @@ +package automerge + +import ( + "context" + "fmt" + + gh "github.com/google/go-github/v72/github" + hgithub "github.com/hivecommons/hive/pkg/github" +) + +const unauthorizedStepRerunSharedCI = "rerun-shared-ci" + +type sharedCIRecoveryTransport interface { + SharedCIRerunBlocked(repo string, number int, head string) (hgithub.SharedCIRerunBlock, bool) +} + +func (c *Engine) sharedCIRecoveryUnauthorized(ctx context.Context, obs stageObservation) string { + t, ok := c.transport.(sharedCIRecoveryTransport) + if !ok { + return "" + } + pr := &gh.PullRequest{Head: &gh.PullRequestBranch{SHA: gh.Ptr(obs.HeadSHA)}} + for _, name := range obs.Labels { + pr.Labels = append(pr.Labels, &gh.Label{Name: gh.Ptr(name)}) + } + block, blocked := t.SharedCIRerunBlocked(obs.Owner+"/"+obs.Repo, obs.Number, obs.HeadSHA) + if !blocked { + c.unauthorizedMu.Lock() + marked := c.unauthorized[c.unauthorizedKey(obs.Owner, obs.Repo, obs.Number)].Step == unauthorizedStepRerunSharedCI + c.unauthorizedMu.Unlock() + if marked { + c.clearUnauthorized(ctx, obs.Display, obs.Owner, obs.Repo, pr, obs.Number, unauthorizedStepRerunSharedCI) + } + return "" + } + action := fmt.Sprintf("A repository maintainer with Actions write can re-run failed jobs at https://github.com/%s/%s/actions/runs/%d . "+ + "Alternatively, the App owner can request Actions read and write for this recovery installation and its owner must accept the update. "+ + "This is a CI recovery permission block, not a source-code failure or merge approval. Hive retries after its bounded permission cooldown; no alternate credential is selected.", + obs.Owner, obs.Repo, block.RunID) + c.markUnauthorized(ctx, obs.Display, obs.Owner, obs.Repo, pr, obs.Number, obs.HeadSHA, + unauthorizedStepRerunSharedCI, unauthorizedPermissionActionsWrite, action) + return unauthorizedStepRerunSharedCI +} diff --git a/src/pkg/github/automerge/shared_ci_recovery_test.go b/src/pkg/github/automerge/shared_ci_recovery_test.go new file mode 100644 index 0000000000..6a3618b8e6 --- /dev/null +++ b/src/pkg/github/automerge/shared_ci_recovery_test.go @@ -0,0 +1,48 @@ +package automerge + +import ( + "context" + "strings" + "testing" + "time" + + hgithub "github.com/hivecommons/hive/pkg/github" +) + +type recoveryBlockTransport struct { + Transport + blocked bool +} + +func (t *recoveryBlockTransport) SharedCIRerunBlocked(repo string, number int, head string) (hgithub.SharedCIRerunBlock, bool) { + return hgithub.SharedCIRerunBlock{HeadSHA: head, RunID: 11}, t.blocked +} + +func TestSharedCIRecoveryUsesUnauthorizedHandoffAndClearsOnRecovery(t *testing.T) { + api := &stageAPI{statefulComments: true} + srv := newStageAPI(t, api) + defer srv.Close() + e, _, _ := newStageEngine(t, srv.URL, &contributorLaneClock{at: time.Now()}) + transport := &recoveryBlockTransport{Transport: e.transport, blocked: true} + e.transport = transport + obs := stageObservation{Owner: "acme", Repo: "widget", Display: "acme/widget", Number: 42, HeadSHA: "aaa"} + ctx := context.Background() + if step := e.sharedCIRecoveryUnauthorized(ctx, obs); step != unauthorizedStepRerunSharedCI { + t.Fatalf("step = %q", step) + } + e.sharedCIRecoveryUnauthorized(ctx, obs) + if len(api.comments) != 1 || !strings.Contains(api.comments[0], "actions: write") || + !strings.Contains(api.comments[0], "/actions/runs/11") || + !strings.Contains(api.comments[0], "not a source-code failure") { + t.Fatalf("handoff must be actionable and head-deduplicated: %v", api.comments) + } + transport.blocked = false + obs.Labels = []string{unauthorizedLabel} + obs.HeadSHA = "new-head" + if step := e.sharedCIRecoveryUnauthorized(ctx, obs); step != "" { + t.Fatalf("recovered step = %q", step) + } + if len(api.removed) != 1 || api.removed[0] != unauthorizedLabel { + t.Fatalf("recovery/head replacement did not clear its own label: %v", api.removed) + } +} diff --git a/src/pkg/github/shared_ci_rerun.go b/src/pkg/github/shared_ci_rerun.go index 7cbc852550..27b71c0e95 100644 --- a/src/pkg/github/shared_ci_rerun.go +++ b/src/pkg/github/shared_ci_rerun.go @@ -3,6 +3,7 @@ package github import ( "context" "fmt" + "maps" "regexp" "sort" "strconv" @@ -70,6 +71,34 @@ type SharedCIRerunResult struct { type sharedCIRerunSeen struct { updatedAt time.Time incidents string + block SharedCIRerunBlock +} + +// SharedCIRerunBlock is an observed permission refusal, not a CI verdict. +type SharedCIRerunBlock struct { + HeadSHA string + RunID int64 +} + +// SharedCIRerunBlocked returns the recovery step owed on this exact head. +// The block survives the retry cooldown until a sweep observes recovery. +func (c *Client) SharedCIRerunBlocked(repo string, number int, head string) (SharedCIRerunBlock, bool) { + owner, name := c.splitRepo(repo) + c.sharedCIRerunMu.Lock() + defer c.sharedCIRerunMu.Unlock() + seen := c.sharedCIRerunSeen[fmt.Sprintf("%s/%s#%d", owner, name, number)] + return seen.block, seen.block.RunID != 0 && seen.block.HeadSHA == head +} + +func (c *Client) hasSharedCIRerunBlocks(owner, repo string) bool { + c.sharedCIRerunMu.Lock() + defer c.sharedCIRerunMu.Unlock() + for key, seen := range c.sharedCIRerunSeen { + if strings.HasPrefix(key, owner+"/"+repo+"#") && seen.block.RunID != 0 { + return true + } + } + return false } // SweepSharedCIReruns re-runs the failed jobs on open PRs that a fix lane @@ -107,7 +136,7 @@ func (c *Client) SweepSharedCIReruns(ctx context.Context, opts SharedCIRerunOpti if err != nil { return result, err } - if len(closed) == 0 { + if len(closed) == 0 && !c.hasSharedCIRerunBlocks(owner, repoName) { continue } result.Incidents += len(closed) @@ -230,12 +259,12 @@ func (c *Client) trySharedCIRerun(ctx context.Context, repo, owner, repoName str if labels := labelNames(pr.Labels); c.isHeld(labels) || c.isExempt(labels) { return SharedCIRerunEvent{}, false, nil } - seenKey := fmt.Sprintf("%s#%d", repo, pr.GetNumber()) + seenKey := fmt.Sprintf("%s/%s#%d", owner, repoName, pr.GetNumber()) updatedAt := pr.GetUpdatedAt().Time c.sharedCIRerunMu.Lock() prev, known := c.sharedCIRerunSeen[seenKey] c.sharedCIRerunMu.Unlock() - if known && prev.incidents == incidentKey && prev.updatedAt.Equal(updatedAt) { + if known && prev.incidents == incidentKey && prev.updatedAt.Equal(updatedAt) && prev.block.RunID == 0 { return SharedCIRerunEvent{}, false, nil } markSeen := func() { @@ -246,6 +275,17 @@ func (c *Client) trySharedCIRerun(ctx context.Context, repo, owner, repoName str c.sharedCIRerunSeen[seenKey] = sharedCIRerunSeen{updatedAt: updatedAt, incidents: incidentKey} c.sharedCIRerunMu.Unlock() } + markBlocked := func(runID int64) { + c.sharedCIRerunMu.Lock() + defer c.sharedCIRerunMu.Unlock() + if c.sharedCIRerunSeen == nil { + c.sharedCIRerunSeen = map[string]sharedCIRerunSeen{} + } + c.sharedCIRerunSeen[seenKey] = sharedCIRerunSeen{ + updatedAt: updatedAt, incidents: incidentKey, + block: SharedCIRerunBlock{HeadSHA: pr.GetHead().GetSHA(), RunID: runID}, + } + } comments, err := c.listIssueComments(ctx, owner, repoName, pr.GetNumber()) if err != nil { @@ -255,6 +295,20 @@ func (c *Client) trySharedCIRerun(ctx context.Context, repo, owner, repoName str for _, cm := range comments { texts = append(texts, cm.GetBody()) } + if prev.block.RunID != 0 && prev.block.HeadSHA == pr.GetHead().GetSHA() { + // Discovery's lookback must not strand an already-observed refusal. + // Keep its incident eligible without broadening discovery for other PRs. + closed = maps.Clone(closed) + if closed == nil { + closed = map[int]bool{} + } + for _, raw := range strings.Split(prev.incidents, ",") { + if n, err := strconv.Atoi(raw); err == nil && n > 0 { + closed[n] = true + } + } + incidentKey = sharedCIIncidentSetKey(closed) + } incidents := pendingSharedCIReruns(texts, closed) if len(incidents) == 0 { markSeen() @@ -275,8 +329,11 @@ func (c *Client) trySharedCIRerun(ctx context.Context, repo, owner, repoName str event := SharedCIRerunEvent{Repo: repo, Number: pr.GetNumber(), HeadSHA: sha, Incidents: incidents} var rerun []*gh.WorkflowRun + blocked := false for _, run := range runs { if c.sharedCIRerunRefused(repo) { + markBlocked(run.GetID()) + blocked = true break } if _, err := c.client.Actions.RerunFailedJobsByID(ctx, owner, repoName, run.GetID()); err != nil { @@ -287,6 +344,8 @@ func (c *Client) trySharedCIRerun(ctx context.Context, repo, owner, repoName str if c.markSharedCIRerunForbidden(repo) { c.logger.Warn("shared-ci rerun forbidden: the App installation lacks Actions write permission for rerun-failed-jobs", "repo", repo, "pr", pr.GetNumber(), "run", run.GetID(), "error", err) } + markBlocked(run.GetID()) + blocked = true break } c.logger.Warn("shared-ci rerun failed", "repo", repo, "pr", pr.GetNumber(), "run", run.GetID(), "error", err) @@ -296,16 +355,23 @@ func (c *Client) trySharedCIRerun(ctx context.Context, repo, owner, repoName str event.RunIDs = append(event.RunIDs, run.GetID()) } if len(rerun) == 0 { - // Every re-run was refused (permissions, run too old to re-run). - // Memoize so the sweep does not retry each pass for this PR state. - markSeen() + // A permission refusal must remain retryable after the repo cooldown, + // even when neither the PR nor the incident changed. + if !blocked { + markSeen() + } return SharedCIRerunEvent{}, false, nil } + if blocked { + // Do not stamp an incident complete when only some runs were accepted. + return event, true, nil + } body := renderSharedCIRerunComment(incidents, sha, rerun) if _, _, err := c.client.Issues.CreateComment(ctx, owner, repoName, pr.GetNumber(), &gh.IssueComment{Body: &body}); err != nil { return event, false, fmt.Errorf("stamping shared-CI rerun marker on %s#%d: %w", repo, pr.GetNumber(), err) } + markSeen() for _, incident := range incidents { c.logger.Info("shared-ci rerun", "repo", repo, "pr", pr.GetNumber(), "incident", "#"+strconv.Itoa(incident), "head", sha, "runs", len(event.RunIDs)) } diff --git a/src/pkg/github/shared_ci_rerun_test.go b/src/pkg/github/shared_ci_rerun_test.go index 88793f533c..a5e85374b3 100644 --- a/src/pkg/github/shared_ci_rerun_test.go +++ b/src/pkg/github/shared_ci_rerun_test.go @@ -47,6 +47,7 @@ type sharedCIRerunFake struct { posted map[int][]string commentLists map[int]int forbidRerun bool + forbidAfter int } func newSharedCIRerunFake(t *testing.T, issues []sharedCIRerunIssue, prs []*sharedCIRerunPR) (*httptest.Server, *sharedCIRerunFake) { @@ -135,7 +136,7 @@ func newSharedCIRerunFake(t *testing.T, issues []sharedCIRerunIssue, prs []*shar case rerunRE.MatchString(r.URL.Path) && r.Method == http.MethodPost: id, _ := strconv.ParseInt(rerunRE.FindStringSubmatch(r.URL.Path)[1], 10, 64) f.reruns = append(f.reruns, id) - if f.forbidRerun { + if f.forbidRerun || (f.forbidAfter > 0 && len(f.reruns) > f.forbidAfter) { w.WriteHeader(http.StatusForbidden) _ = json.NewEncoder(w).Encode(map[string]any{"message": "Resource not accessible by integration"}) return @@ -318,4 +319,76 @@ func TestSweepSharedCIReruns_ForbiddenStopsAndIsRememberedPerRepo(t *testing.T) if len(res.Rerun) != 0 || len(posted[1]) != 0 || len(posted[2]) != 0 { t.Fatalf("expected no rerun result or marker comment: result=%+v posted=%v", res, posted) } + if block, ok := c.SharedCIRerunBlocked("org/repo", 1, "aaaaaaaaaa"); !ok || block.RunID == 0 { + t.Fatalf("permission refusal was not recorded: %+v %v", block, ok) + } + if _, ok := c.SharedCIRerunBlocked("org/repo", 1, "new-head"); ok { + t.Fatal("old-head refusal applied to a new head") + } + if _, err := c.SweepSharedCIReruns(context.Background(), SharedCIRerunOptions{Now: sharedCIRerunNow}); err != nil { + t.Fatal(err) + } + reruns, _, _ = fake.snap() + if len(reruns) != 1 { + t.Fatalf("cooldown allowed another POST: %v", reruns) + } + + fake.mu.Lock() + fake.forbidRerun = false + fake.mu.Unlock() + c.sharedCIRerunMu.Lock() + c.sharedCIRerunForbidden["org/repo"] = time.Now().Add(-sharedCIRerunForbiddenTTL - time.Second) + c.sharedCIRerunMu.Unlock() + res, err = c.SweepSharedCIReruns(context.Background(), SharedCIRerunOptions{Now: sharedCIRerunNow}) + if err != nil || len(res.Rerun) != 2 { + t.Fatalf("unchanged PRs did not recover after permission change: %+v %v", res, err) + } + if _, ok := c.SharedCIRerunBlocked("org/repo", 1, "aaaaaaaaaa"); ok { + t.Fatal("successful recovery retained permission block") + } +} + +func TestSharedCIRerunManualRecoveryClearsBlockDuringCooldown(t *testing.T) { + now := sharedCIRerunNow() + pr := &sharedCIRerunPR{Number: 1, SHA: "aaa", Comments: []string{""}, Runs: []sharedCIRerunRun{failedRun(11, 1)}} + srv, fake := newSharedCIRerunFake(t, []sharedCIRerunIssue{{Number: 800, Title: "[shared-ci] incident", ClosedAt: now.Add(-time.Hour)}}, []*sharedCIRerunPR{pr}) + fake.forbidRerun = true + c := newTestClient(t, srv, "org", []string{"org/repo"}) + opts := SharedCIRerunOptions{Now: sharedCIRerunNow} + if _, err := c.SweepSharedCIReruns(context.Background(), opts); err != nil { + t.Fatal(err) + } + fake.mu.Lock() + pr.Runs[0].Conclusion = "success" + fake.mu.Unlock() + opts.Now = func() time.Time { return now.Add(DefaultSharedCIRerunLookback + time.Hour) } + if _, err := c.SweepSharedCIReruns(context.Background(), opts); err != nil { + t.Fatal(err) + } + if _, ok := c.SharedCIRerunBlocked("org/repo", 1, "aaa"); ok { + t.Fatal("manual recovery must clear the block without a PR edit or TTL expiry") + } + reruns, posted, _ := fake.snap() + if len(reruns) != 1 || len(posted[1]) != 0 { + t.Fatalf("manual recovery retried or stamped an automated success: %v %v", reruns, posted) + } +} + +func TestSharedCIRerunPartialPermissionFailureDoesNotFinishIncident(t *testing.T) { + now := sharedCIRerunNow() + pr := &sharedCIRerunPR{Number: 1, SHA: "aaa", Comments: []string{""}, Runs: []sharedCIRerunRun{failedRun(11, 1), failedRun(12, 2)}} + srv, fake := newSharedCIRerunFake(t, []sharedCIRerunIssue{{Number: 800, Title: "[shared-ci] incident", ClosedAt: now.Add(-time.Hour)}}, []*sharedCIRerunPR{pr}) + fake.forbidAfter = 1 + c := newTestClient(t, srv, "org", []string{"org/repo"}) + res, err := c.SweepSharedCIReruns(context.Background(), SharedCIRerunOptions{Now: sharedCIRerunNow}) + if err != nil || len(res.Rerun) != 1 || len(res.Rerun[0].RunIDs) != 1 { + t.Fatalf("partial recovery audit = %+v, %v", res, err) + } + _, posted, _ := fake.snap() + if len(posted[1]) != 0 { + t.Fatalf("partial recovery stamped incident complete: %v", posted) + } + if _, ok := c.SharedCIRerunBlocked("org/repo", 1, "aaa"); !ok { + t.Fatal("partial recovery hid outstanding permission block") + } } diff --git a/src/pkg/stageengine/derive_pr.go b/src/pkg/stageengine/derive_pr.go index 860b3c44ed..d3e25015db 100644 --- a/src/pkg/stageengine/derive_pr.go +++ b/src/pkg/stageengine/derive_pr.go @@ -302,6 +302,12 @@ func passDecision(verdict stageVerdict, role stagelabels.Role, obs stageObservat verdict.Rank = stageRankOwned obs.Reason = stagelabels.FixerExhaustedReason(pass.FixerExhausted) } + if !verdict.Terminal && verdict.Stage != stagelabels.None && pass.RecoveryUnauthorized != "" { + verdict.Stage = stagelabels.NeedsDecision + verdict.Rank = stageRankOwned + obs.Reason = prstage.StageReasonUnauthorizedPrefix + pass.RecoveryUnauthorized + human = &stagelabels.Route{Stage: stagelabels.NeedsDecision} + } d := Decision{ Stage: verdict.Stage, Role: role, diff --git a/src/pkg/stageengine/derive_pr_recovery_test.go b/src/pkg/stageengine/derive_pr_recovery_test.go new file mode 100644 index 0000000000..4635563e50 --- /dev/null +++ b/src/pkg/stageengine/derive_pr_recovery_test.go @@ -0,0 +1,22 @@ +package stageengine + +import ( + "testing" + + "github.com/hivecommons/hive/pkg/stagelabels" +) + +func TestDerivePRRecoveryPermissionBlockIsNotSourceFixing(t *testing.T) { + f := hiveFixingFacts("required-check-failing:gosec") + f.PR.Pass = &PRPassFacts{ + AuthorIsApp: true, RecoveryUnauthorized: "rerun-shared-ci", + } + d := DerivePR(f) + if d.Stage != stagelabels.NeedsDecision || d.Reason != "unauthorized:rerun-shared-ci" { + t.Fatalf("permission block looks like source fixing: %+v", d) + } + f.PR.Pass.RecoveryUnauthorized = "" + if d := DerivePR(f); d.Stage != stagelabels.Fixing { + t.Fatalf("cleared recovery block must preserve actual CI failure: %+v", d) + } +} diff --git a/src/pkg/stageengine/facts.go b/src/pkg/stageengine/facts.go index 61fc11eb70..e6802c83d5 100644 --- a/src/pkg/stageengine/facts.go +++ b/src/pkg/stageengine/facts.go @@ -286,6 +286,9 @@ type PRPassFacts struct { // stagelabels.FixerExhaustedReason(n) so a person decides instead of // the row claiming "Fixing" with nobody on it (hivecommons/hive#15744). FixerExhausted int + // RecoveryUnauthorized is an observed exact-head CI recovery permission + // refusal. It parks the item without treating infrastructure as source work. + RecoveryUnauthorized string // FixerExcluded is true when no fix job is active and the dispatcher // bars one for any reason (exhausted, no enabled fixer, …): a stale // answered decision on the same head must not pull the PR back from