diff --git a/changelog.d/fixed-16199.md b/changelog.d/fixed-16199.md new file mode 100644 index 0000000000..327b7382a3 --- /dev/null +++ b/changelog.d/fixed-16199.md @@ -0,0 +1 @@ +- The automerge sweep now logs the "install the ok-to-test workflow" approval warning once per PR head instead of every tick ([#16199](https://github.com/hivecommons/hive/issues/16199)). diff --git a/src/pkg/github/automerge/automerge_held_runs.go b/src/pkg/github/automerge/automerge_held_runs.go index 4772482b1b..aa0752a4cd 100644 --- a/src/pkg/github/automerge/automerge_held_runs.go +++ b/src/pkg/github/automerge/automerge_held_runs.go @@ -79,6 +79,10 @@ const ( // waiting on a person's approval. workflowRunConclusionActionRequired = "action_required" workflowApprovalWorkflowMissingPrefix = "workflow-approval-required:missing-workflow:" + // workflowApprovalGraceWarnKey keys the once-per-head grace warning in + // terminalHoldLogged separately from the hold reason itself, so the sweep + // loop's "automerge hold" line (keyed on the bare reason) still fires. + workflowApprovalGraceWarnKey = "workflow-approval-grace-warned:" // workflowApprovalUntrustedPrefix: the repository's merge bot (Prow) owns // `/ok-to-test` and only honours it from its trusted users, which a // GitHub App login never is. Posting again cannot succeed; a person must @@ -224,8 +228,10 @@ func (c *Engine) workflowApprovalAttention(ctx context.Context, displayRepo, own c.postWorkflowApprovalCommand(ctx, displayRepo, owner, repo, number, headSHA) } if !firstSeen.IsZero() && headOlderThan(c.now, firstSeen, workflowApprovalGrace) { - c.warn("automerge workflow approval still required after ok-to-test grace; install the self-contained hive ok-to-test workflow", - "repo", displayRepo, "pr", number, "sha", headSHA, "held", held, "context", contextName, "command", workflowApprovalCommand) + if c.markTerminalHoldLogged(owner, repo, number, headSHA, workflowApprovalGraceWarnKey+contextName) { + c.warn("automerge workflow approval still required after ok-to-test grace; install the self-contained hive ok-to-test workflow", + "repo", displayRepo, "pr", number, "sha", headSHA, "held", held, "context", contextName, "command", workflowApprovalCommand) + } return workflowApprovalWorkflowMissingPrefix + contextName } c.info("automerge workflow approval required; posted ok-to-test command if needed", "repo", displayRepo, "pr", number, "sha", headSHA, "held", held, "context", contextName, "command", workflowApprovalCommand) diff --git a/src/pkg/github/automerge/automerge_held_runs_test.go b/src/pkg/github/automerge/automerge_held_runs_test.go index 4f0137aab0..85df75faf3 100644 --- a/src/pkg/github/automerge/automerge_held_runs_test.go +++ b/src/pkg/github/automerge/automerge_held_runs_test.go @@ -6,7 +6,9 @@ package automerge // and names a head whose runs were never created instead of polling it forever. import ( + "bytes" "context" + "log/slog" "net/http" "net/http/httptest" "strings" @@ -395,6 +397,8 @@ func TestTrySweepTrustedBotWorkflowApprovalMissingWorkflowAfterGrace(t *testing. c := newAutoMergeSweepClient(api.URL) c.trustedBotAuthors = func() map[string]bool { return map[string]bool{"dependabot[bot]": true} } c.now = func() time.Time { return now } + var logs bytes.Buffer + c.logger = slog.New(slog.NewTextHandler(&logs, nil)) _, reason, err := c.trySweepSelfAuthoredPR(context.Background(), "widget", "acme", "widget", 23, true) if err != nil { @@ -414,6 +418,17 @@ func TestTrySweepTrustedBotWorkflowApprovalMissingWorkflowAfterGrace(t *testing. if len(comments) != 1 || comments[0] != workflowApprovalCommandBody("sha23") { t.Fatalf("comments = %q, want one %q command with the head marker", comments, workflowApprovalCommand) } + if _, _, err = c.trySweepSelfAuthoredPR(context.Background(), "widget", "acme", "widget", 23, true); err != nil { + t.Fatalf("third tick returned error: %v", err) + } + if n := strings.Count(logs.String(), "still required after ok-to-test grace"); n != 1 { + t.Fatalf("grace warning logged %d times across repeat ticks, want once per head", n) + } + // The grace warning must not claim the hold reason's own dedupe key, or the + // sweep loop's once-per-head "automerge hold" line would never fire. + if !c.markTerminalHoldLogged("acme", "widget", 23, "sha23", reason) { + t.Fatalf("hold reason %q already marked logged by the grace warning; sweep loop would skip its automerge hold line", reason) + } } func TestTrySweepTrustedBotSentinelBlocksWorkflowApprovalAttention(t *testing.T) {