From 4846de8c312c4dbf1b7fd93c7133f76534adfade Mon Sep 17 00:00:00 2001 From: Danathar <6772335+Danathar@users.noreply.github.com> Date: Tue, 17 Nov 2026 17:22:43 -0500 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20contributor=20lane:=20recognise?= =?UTF-8?q?=20the=20cncf/prow-github-actions=20ok-to-test=20refusal?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The lane stopped posting /ok-to-test only on upstream Prow's "Cannot trigger testing until a trusted user" reply naming @. cncf/prow-github-actions answers "Cannot /ok-to-test because Error: is not a reviewer or approver ..." with no @, so the hive posted the command again on every head and never left the needs-human note (projectbluefin/chairlift#802, projectbluefin/utah#1274, #1313). Recognise both forms (naming the App), quote the matched wording in the notes, and in the fork lane treat a command for an earlier head that is past the 10m grace with no ok-to-test label and no Prow presubmit on that head as refused, whatever the bot said. Signed-off-by: Danathar <6772335+Danathar@users.noreply.github.com> Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01BQYnPGFJx81c8JXR3o1sg1 --- .../fixed-ok-to-test-refusal-wordings.md | 1 + src/docs/fork-pr-presubmit-stall-runbook.md | 2 +- src/docs/hive-merge.md | 18 ++-- .../automerge_contributor_lane_test.go | 9 ++ .../automerge_contributor_prow_oktotest.go | 54 +++++++++-- ...utomerge_contributor_prow_oktotest_test.go | 91 +++++++++++++++++++ .../github/automerge/automerge_held_runs.go | 74 +++++++++++++-- .../automerge/automerge_held_runs_test.go | 61 ++++++++++++- 8 files changed, 286 insertions(+), 24 deletions(-) create mode 100644 changelog.d/fixed-ok-to-test-refusal-wordings.md diff --git a/changelog.d/fixed-ok-to-test-refusal-wordings.md b/changelog.d/fixed-ok-to-test-refusal-wordings.md new file mode 100644 index 0000000000..2798fa3ab9 --- /dev/null +++ b/changelog.d/fixed-ok-to-test-refusal-wordings.md @@ -0,0 +1 @@ +- The contributor lane stops posting `/ok-to-test` on a fork PR once the merge bot refuses the App, in cncf/prow-github-actions' wording too. Only upstream Prow's "Cannot trigger testing until a trusted user" was recognised, so repositories that run Prow commands through cncf/prow-github-actions ("Cannot /ok-to-test because Error: is not a reviewer or approver …") got the command again on every new head, refused every time, and never got the needs-human note asking a person to post it. A command for an earlier head that left no `ok-to-test` label and no presubmit after 10 minutes now also counts as refused, whatever the bot said. The same-repo held-runs path reads the same refusal forms. diff --git a/src/docs/fork-pr-presubmit-stall-runbook.md b/src/docs/fork-pr-presubmit-stall-runbook.md index 5ce9801981..e8dd05d195 100644 --- a/src/docs/fork-pr-presubmit-stall-runbook.md +++ b/src/docs/fork-pr-presubmit-stall-runbook.md @@ -22,7 +22,7 @@ gh pr view --repo / --comments | grep -i -E "tide|not mergeable|r ## 2. Start the presubmits -The contributor lane now posts `/ok-to-test` itself, once per head, on a fork PR it admitted whose author is not an org member or collaborator ([#17474](https://github.com/hivecommons/hive/issues/17474); see [hive-merge.md](hive-merge.md)). This step is needed only when that did not help: the PR carries the hive's `needs-human` note saying Prow rejected the App (`Cannot trigger testing until a trusted user`), or the lane did not admit the PR. +The contributor lane now posts `/ok-to-test` itself, once per head, on a fork PR it admitted whose author is not an org member or collaborator ([#17474](https://github.com/hivecommons/hive/issues/17474); see [hive-merge.md](hive-merge.md)). This step is needed only when that did not help: the PR carries the hive's `needs-human` note saying Prow rejected the App (`Cannot trigger testing until a trusted user`, or cncf/prow-github-actions' `Cannot /ok-to-test because …`) or that an earlier post had no effect (no `ok-to-test` label and no presubmit after 10 minutes), or the lane did not admit the PR. A human with write access comments on the PR. This is Prow's `/ok-to-test` (trigger plugin, shows as Tide `Jobs X, Y have not succeeded`), not the same-repo `hive-ok-to-test` GitHub Actions workflow (shows as `workflow-approval-required:`; see [github-app-setup.md](github-app-setup.md)), which never covers fork heads: diff --git a/src/docs/hive-merge.md b/src/docs/hive-merge.md index 18185fca26..0eada22384 100644 --- a/src/docs/hive-merge.md +++ b/src/docs/hive-merge.md @@ -1112,13 +1112,19 @@ author's `author_association` is not `OWNER`, `MEMBER` or `COLLABORATOR`, the PR has no `ok-to-test` label and Prow has reported no presubmit status on the head (a status other than `tide` from the account that reports `tide`), the lane posts `/ok-to-test` once per head with the same per-head marker and -mutation-boundary dedupe as the trusted-lane command. If Prow replies -`Cannot trigger testing until a trusted user`, the lane logs it once with +mutation-boundary dedupe as the trusted-lane command. The lane treats the +command as refused when the merge bot says so, in upstream Prow's words +(`@: Cannot trigger testing until a trusted user …`) or +cncf/prow-github-actions' (`Cannot /ok-to-test because Error: is not a +reviewer or approver …`), each naming the App. It also treats it as refused, +whatever the reply, when its command for an earlier head is more than 10 +minutes old and the PR still has no `ok-to-test` label and Prow reported no +presubmit status on that head. A refused command is logged once with `skipped=prow-rejects-bot-commands`, leaves one -`` needs-human note and never posts the -command on that PR again; making the App a Prow trusted user is the -repository's Prow configuration. The merge-request relay's read-only -evaluation posts nothing. +`` needs-human note and is never posted on +that PR again; making the App a Prow trusted user is the repository's Prow +configuration. The same refusal reader serves the same-repo held-runs +command. The merge-request relay's read-only evaluation posts nothing. A PR merges through this tier only when all of the following hold: diff --git a/src/pkg/github/automerge/automerge_contributor_lane_test.go b/src/pkg/github/automerge/automerge_contributor_lane_test.go index 524c068613..63ceb313f7 100644 --- a/src/pkg/github/automerge/automerge_contributor_lane_test.go +++ b/src/pkg/github/automerge/automerge_contributor_lane_test.go @@ -117,6 +117,9 @@ type contributorLaneFixture struct { // headStatuses is what GET commits/sha7/statuses (every commit status, // with its creator) serves; nil serves an empty list. headStatuses []map[string]any + // earlierStatuses is what GET commits//statuses serves for heads + // other than sha7; a head without an entry serves an empty list. + earlierStatuses map[string][]map[string]any // baseRef, when set, gives the PR base a branch name and serves the // branch, protection and compare routes the current-base gate // (automerge_current_base.go) and release hold need; empty leaves the @@ -379,6 +382,12 @@ func newContributorLaneAPI(t *testing.T, fx *contributorLaneFixture) *httptest.S statuses = []map[string]any{} } json.NewEncoder(w).Encode(statuses) + case r.Method == http.MethodGet && strings.HasPrefix(r.URL.Path, "/repos/acme/widget/commits/") && strings.HasSuffix(r.URL.Path, "/statuses"): + statuses := fx.earlierStatuses[strings.TrimSuffix(strings.TrimPrefix(r.URL.Path, "/repos/acme/widget/commits/"), "/statuses")] + if statuses == nil { + statuses = []map[string]any{} + } + json.NewEncoder(w).Encode(statuses) 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 { diff --git a/src/pkg/github/automerge/automerge_contributor_prow_oktotest.go b/src/pkg/github/automerge/automerge_contributor_prow_oktotest.go index 04e9252929..84eae660e9 100644 --- a/src/pkg/github/automerge/automerge_contributor_prow_oktotest.go +++ b/src/pkg/github/automerge/automerge_contributor_prow_oktotest.go @@ -66,15 +66,16 @@ func (c *Engine) requestProwPresubmitsForFork(ctx context.Context, displayRepo, if !scanned { return } - if state.rejected { + unanswered := !state.rejected && c.okToTestUnanswered(ctx, displayRepo, owner, repo, number, state.earlierPosts) + if state.rejected || unanswered { c.markWorkflowApprovalUntrusted(owner, repo, number) - args := []any{"repo", displayRepo, "pr", number, "sha", headSHA, "skipped", hgithub.PRApprovalCommandsSkippedProw} + args := []any{"repo", displayRepo, "pr", number, "sha", headSHA, "skipped", hgithub.PRApprovalCommandsSkippedProw, "unanswered", unanswered} const msg = "contributor-lane ok-to-test not posted again: Prow rejected the App's command on this fork PR; a trusted user must post it" if state.rejectionNoted { c.debug(msg, args...) return } - c.postWorkflowApprovalUntrustedNote(ctx, displayRepo, owner, repo, number, headSHA, prowPresubmitsRejectedNote(strings.TrimSpace(c.transport.AppBotLogin()))) + c.postWorkflowApprovalUntrustedNote(ctx, displayRepo, owner, repo, number, headSHA, prowPresubmitsRejectedNote(strings.TrimSpace(c.transport.AppBotLogin()), state.refusal)) c.warn(msg, args...) return } @@ -87,6 +88,29 @@ func (c *Engine) requestProwPresubmitsForFork(ctx context.Context, displayRepo, c.postWorkflowApprovalCommand(ctx, displayRepo, owner, repo, number, headSHA) } +// okToTestUnanswered reports whether the App's command for an earlier head +// evidently did nothing, whatever the merge bot's wording (or none): it was +// posted at least workflowApprovalGrace ago, the PR still has no `ok-to-test` +// label (the caller returned on one; the trigger plugin's label persists +// across pushes) and Prow reported no presubmit on that head. The newest such +// post is checked; a status lookup failure answers false. +func (c *Engine) okToTestUnanswered(ctx context.Context, displayRepo, owner, repo string, number int, posts []okToTestPost) bool { + var last okToTestPost + now := c.clock() + for _, post := range posts { + if post.at.IsZero() || now.Sub(post.at) < workflowApprovalGrace { + continue + } + if last.at.IsZero() || post.at.After(last.at) { + last = post + } + } + if last.sha == "" { + return false + } + return !c.prowPresubmitStatusPresent(ctx, displayRepo, owner, repo, number, last.sha) +} + // prowPresubmitStatusPresent reports whether Prow already reported a job on // headSHA: a commit status other than Tide's own, created by the account that // created Tide's status. Without a Tide status nothing on the head can be @@ -130,8 +154,9 @@ func (c *Engine) prowPresubmitStatusPresent(ctx context.Context, displayRepo, ow } // prowPresubmitsRejectedNote is the one needs-human comment posted when Prow -// refuses the App's `/ok-to-test` on a non-member fork PR. -func prowPresubmitsRejectedNote(appLogin string) string { +// refuses the App's `/ok-to-test` on a non-member fork PR. refusal is the +// merge bot's wording; "" means it never acted on an earlier post. +func prowPresubmitsRejectedNote(appLogin, refusal string) string { if appLogin == "" { appLogin = "the Hive App" } @@ -139,11 +164,20 @@ func prowPresubmitsRejectedNote(appLogin string) string { b.WriteString(workflowApprovalRejectedMarker) b.WriteString("\nneeds-human: Prow has not run this fork PR's presubmits. Its author is not one of Prow's trusted users, so the trigger plugin waits for `") b.WriteString(workflowApprovalCommand) - b.WriteString("`; Hive posted it and Prow rejected `") - b.WriteString(appLogin) - b.WriteString("` (\"") - b.WriteString(prowOkToTestRejection) - b.WriteString("\"). Hive will not post the command again on this PR.\n\nA maintainer can comment `") + if refusal != "" { + b.WriteString("`; Hive posted it and Prow rejected `") + b.WriteString(appLogin) + b.WriteString("` (\"") + b.WriteString(refusal) + b.WriteString("\")") + } else { + b.WriteString("`; Hive posted it for an earlier head and nothing came of it: no `") + b.WriteString(prowOkToTestLabel) + b.WriteString("` label and no presubmit status, so Prow does not accept it from `") + b.WriteString(appLogin) + b.WriteString("`") + } + b.WriteString(". Hive will not post the command again on this PR.\n\nA maintainer can comment `") b.WriteString(workflowApprovalCommand) b.WriteString("` (plus `/test all` for the current head), or make `") b.WriteString(appLogin) diff --git a/src/pkg/github/automerge/automerge_contributor_prow_oktotest_test.go b/src/pkg/github/automerge/automerge_contributor_prow_oktotest_test.go index 2a985bd06a..632b01bad6 100644 --- a/src/pkg/github/automerge/automerge_contributor_prow_oktotest_test.go +++ b/src/pkg/github/automerge/automerge_contributor_prow_oktotest_test.go @@ -168,6 +168,97 @@ func TestContributorLaneStopsProwOkToTestAfterRejection(t *testing.T) { } } +// cncf/prow-github-actions answers in its own words and names the App with no +// @ (projectbluefin/chairlift#802: five posts, five refusals, no note). That +// reply stops the lane exactly like Prow's, and the note quotes it. +func TestContributorLaneStopsProwOkToTestAfterProwActionsRefusal(t *testing.T) { + fx := prowOkToTestFixture() + fx.prComments = []map[string]any{ + appComment(2, workflowApprovalCommandBody("oldhead")), + prowComment(3, "Cannot /ok-to-test because Error: "+testHiveAppBotLogin+" is not a reviewer or approver for any changed file"), + } + runProwOkToTestTicks(t, &fx, 3) + runProwOkToTestTicks(t, &fx, 2) + if fx.comments != 1 { + t.Fatalf("comments = %d %q, want only the one needs-human note", fx.comments, fx.allComments) + } + note := fx.allComments[0] + if !strings.HasPrefix(note, workflowApprovalRejectedMarker) || !strings.Contains(note, prowActionsOkToTestRefusal) || strings.Contains(note, prowOkToTestRejection) { + t.Fatalf("note = %q, want the rejected marker quoting the action's wording, not Prow's", note) + } +} + +// commentAt stamps a fixture comment with its created_at. +func commentAt(comment map[string]any, at time.Time) map[string]any { + comment["created_at"] = at.Format(time.RFC3339) + return comment +} + +// Whatever a merge bot replies (or if it says nothing), a command posted for +// an earlier head that is past the grace, left no ok-to-test label and drew +// no Prow presubmit on that head did nothing: the lane does not post again +// on a new head and leaves the one note. A command that did start +// presubmits, or one still inside the grace, is not treated as refused. +func TestContributorLaneTreatsUnansweredProwOkToTestAsRefused(t *testing.T) { + clockStart := time.Date(2026, 11, 9, 12, 0, 0, 0, time.UTC) + command := workflowApprovalCommandBody("sha7") + for _, tc := range []struct { + name string + postedAt time.Time + mutate func(fx *contributorLaneFixture) + ticks int + wantPost bool + wantNote bool + }{ + {name: "earlier post past the grace with no effect", postedAt: clockStart.Add(-30 * time.Minute), ticks: 3, wantNote: true}, + {name: "earlier post still inside the grace", postedAt: clockStart.Add(-2 * time.Minute), ticks: 1, wantPost: true}, + { + name: "earlier post that drew a Prow presubmit on its head", + postedAt: clockStart.Add(-30 * time.Minute), + mutate: func(fx *contributorLaneFixture) { + fx.earlierStatuses = map[string][]map[string]any{"oldhead": { + prowStatus(prowTideContext, prowOkToTestTestBot), + prowStatus("pull-widget-unit", prowOkToTestTestBot), + }} + }, + ticks: 3, + wantPost: true, + }, + { + // The trigger plugin's label persists across pushes: a command + // that took effect is never mistaken for a refusal. + name: "ok-to-test label present", + postedAt: clockStart.Add(-30 * time.Minute), + mutate: func(fx *contributorLaneFixture) { fx.labels = []string{"size/S", "ok-to-test"} }, + ticks: 3, + }, + } { + t.Run(tc.name, func(t *testing.T) { + fx := prowOkToTestFixture() + fx.prComments = []map[string]any{commentAt(appComment(2, workflowApprovalCommandBody("oldhead")), tc.postedAt)} + if tc.mutate != nil { + tc.mutate(&fx) + } + runProwOkToTestTicks(t, &fx, tc.ticks) + posted, notes := 0, 0 + for _, body := range fx.allComments { + switch { + case body == command: + posted++ + case strings.HasPrefix(body, workflowApprovalRejectedMarker): + notes++ + if strings.Contains(body, prowOkToTestRejection) || !strings.Contains(body, "earlier head") { + t.Fatalf("note = %q, want the no-effect wording, not a quoted refusal", body) + } + } + } + if (posted == 1) != tc.wantPost || posted > 1 || (notes == 1) != tc.wantNote || notes > 1 { + t.Fatalf("posts=%d notes=%d (comments %q), want post=%v note=%v", posted, notes, fx.allComments, tc.wantPost, tc.wantNote) + } + }) + } +} + // The merge-request relay evaluates the lane read-only and posts nothing. func TestContributorLaneRelayEvaluationDoesNotPostProwOkToTest(t *testing.T) { fx := prowOkToTestFixture() diff --git a/src/pkg/github/automerge/automerge_held_runs.go b/src/pkg/github/automerge/automerge_held_runs.go index 94152ede07..00dd81cb0d 100644 --- a/src/pkg/github/automerge/automerge_held_runs.go +++ b/src/pkg/github/automerge/automerge_held_runs.go @@ -114,6 +114,11 @@ const ( // prowOkToTestRejection is the reply Prow's trigger plugin posts when the // commenter is not one of its trusted users. prowOkToTestRejection = "Cannot trigger testing until a trusted user" + // prowActionsOkToTestRefusal opens the reply cncf/prow-github-actions + // posts instead ("Cannot /ok-to-test because Error: is not a + // reviewer or approver for any changed file"); the reason after it names + // the commenter without an @. + prowActionsOkToTestRefusal = "Cannot " + workflowApprovalCommand + " because " // prowApprovalNotifier appears in every comment Prow's approve plugin // posts; its presence means `/ok-to-test` on this PR is Prow's command. prowApprovalNotifier = "[APPROVALNOTIFIER]" @@ -256,7 +261,7 @@ func (c *Engine) workflowApprovalAttention(ctx context.Context, displayRepo, own // hand the command to a person once. c.markWorkflowApprovalUntrusted(owner, repo, number) if !state.rejectionNoted { - c.postWorkflowApprovalUntrustedNote(ctx, displayRepo, owner, repo, number, headSHA, workflowApprovalUntrustedNote(strings.TrimSpace(c.transport.AppBotLogin()), state.rejected)) + c.postWorkflowApprovalUntrustedNote(ctx, displayRepo, owner, repo, number, headSHA, workflowApprovalUntrustedNote(strings.TrimSpace(c.transport.AppBotLogin()), state.refusal)) } c.warn("automerge workflow approval cannot be requested by the App; merge bot trusts only its own users", "repo", displayRepo, "pr", number, "sha", headSHA, "held", held, "context", contextName, "rejected", state.rejected, "prow", state.prowManaged) @@ -345,12 +350,65 @@ type workflowApprovalComments struct { postedForHead bool // rejected: the merge bot replied that the commenter is not trusted. rejected bool + // refusal is the merge bot's wording for that reply, quoted in the + // needs-human note: prowOkToTestRejection or prowActionsOkToTestRefusal. + refusal string + // earlierPosts are the App's commands for heads other than this one. + earlierPosts []okToTestPost // rejectionNoted: the App already posted its needs-human note. rejectionNoted bool // prowManaged: Prow's approve plugin comments on this PR. prowManaged bool } +// okToTestPost is one of the App's /ok-to-test comments: the head its marker +// names and when it was posted (zero when GitHub did not say). +type okToTestPost struct { + sha string + at time.Time +} + +// okToTestRefusal returns the merge bot's wording when body refuses the +// App's /ok-to-test, or "" when it does not. Upstream Prow's trigger plugin +// @-mentions the commenter; cncf/prow-github-actions opens its reply with +// "Cannot /ok-to-test because" and names the commenter bare. Either reply +// must name the App, so a refusal of someone else's command does not count. +// Like Prow's check, the author is not restricted beyond not being the App. +func okToTestRefusal(body, appLogin string) string { + names := func(withAt bool) bool { + if appLogin == "" { + return true + } + login := strings.ToLower(appLogin) + if withAt { + login = "@" + login + } + return strings.Contains(strings.ToLower(body), login) + } + switch { + case strings.Contains(body, prowOkToTestRejection) && names(true): + return prowOkToTestRejection + case strings.HasPrefix(strings.TrimSpace(body), prowActionsOkToTestRefusal) && names(false): + return prowActionsOkToTestRefusal + "…" + } + return "" +} + +// okToTestPostSHA returns the head an App comment's command marker names, +// or "" when body carries no marker. +func okToTestPostSHA(body string) string { + i := strings.Index(body, workflowApprovalCommandMarkerPrefix) + if i < 0 { + return "" + } + rest := body[i+len(workflowApprovalCommandMarkerPrefix):] + j := strings.Index(rest, " -->") + if j < 0 { + return "" + } + return strings.ToLower(strings.TrimSpace(rest[:j])) +} + // workflowApprovalCommentState reads the PR's comments when a scan is due. // The second result is false when nothing was read (not due, or the API // failed), in which case the caller keeps its in-process knowledge. @@ -380,6 +438,8 @@ func (c *Engine) workflowApprovalCommentState(ctx context.Context, displayRepo, if appLogin != "" && strings.EqualFold(login, appLogin) { if strings.Contains(body, marker) { state.postedForHead = true + } else if sha := okToTestPostSHA(body); sha != "" { + state.earlierPosts = append(state.earlierPosts, okToTestPost{sha: sha, at: comment.GetCreatedAt().Time}) } if strings.Contains(body, workflowApprovalRejectedMarker) { state.rejectionNoted = true @@ -389,8 +449,9 @@ func (c *Engine) workflowApprovalCommentState(ctx context.Context, displayRepo, if strings.Contains(body, prowApprovalNotifier) { state.prowManaged = true } - if strings.Contains(body, prowOkToTestRejection) && (appLogin == "" || strings.Contains(strings.ToLower(body), "@"+strings.ToLower(appLogin))) { + if refusal := okToTestRefusal(body, appLogin); refusal != "" { state.rejected = true + state.refusal = refusal } } if resp == nil || resp.NextPage == 0 { @@ -468,8 +529,9 @@ func (c *Engine) postWorkflowApprovalCommand(ctx context.Context, displayRepo, o } // workflowApprovalUntrustedNote is the one needs-human comment posted when -// the merge bot refuses the App's command. -func workflowApprovalUntrustedNote(appLogin string, rejected bool) string { +// the merge bot refuses the App's command; refusal is the bot's wording, "" +// when it never replied. +func workflowApprovalUntrustedNote(appLogin, refusal string) string { if appLogin == "" { appLogin = "the Hive App" } @@ -478,11 +540,11 @@ func workflowApprovalUntrustedNote(appLogin string, rejected bool) string { b.WriteString("\nneeds-human: GitHub Actions is holding this PR's workflow runs for approval, and Hive cannot start them. `") b.WriteString(workflowApprovalCommand) b.WriteString("` on this repository belongs to Prow, which only accepts it from its trusted users") - if rejected { + if refusal != "" { b.WriteString("; it rejected `") b.WriteString(appLogin) b.WriteString("` (\"") - b.WriteString(prowOkToTestRejection) + b.WriteString(refusal) b.WriteString("\")") } else { b.WriteString(", and `") diff --git a/src/pkg/github/automerge/automerge_held_runs_test.go b/src/pkg/github/automerge/automerge_held_runs_test.go index 98751a08ed..d896007132 100644 --- a/src/pkg/github/automerge/automerge_held_runs_test.go +++ b/src/pkg/github/automerge/automerge_held_runs_test.go @@ -678,6 +678,65 @@ func TestTrySweepSelfAuthoredPRStopsOkToTestAfterProwRejection(t *testing.T) { } } +// cncf/prow-github-actions refuses the App in its own words, naming the +// login without an @ (projectbluefin/utah#1274): the same-repo path stops on +// that reply too, and its note quotes the action's wording, not Prow's. +func TestTrySweepSelfAuthoredPRStopsOkToTestAfterProwActionsRefusal(t *testing.T) { + var comments []string + pr := missingRequiredPR(func(p *selfAuthoredPR) { + p.workflowRuns = []map[string]any{heldRun(1, "acme/widget")} + p.commentBodies = &comments + p.existingComments = &[]map[string]any{ + appComment(1, workflowApprovalCommandBody("oldhead")), + prowComment(2, "Cannot /ok-to-test because Error: "+testHiveAppBotLogin+" is not a reviewer or approver for any changed file"), + } + }) + var merged []int + api := newSelfAuthoredAutoMergeAPI(t, []selfAuthoredPR{pr}, &merged) + defer api.Close() + c := newAutoMergeSweepClient(api.URL) + + want := workflowApprovalUntrustedPrefix + "ci/build" + for tick := 1; tick <= 3; tick++ { + _, reason, err := c.trySweepSelfAuthoredPR(context.Background(), "widget", "acme", "widget", 23, true) + if err != nil { + t.Fatalf("tick %d returned error: %v", tick, err) + } + if reason != want { + t.Fatalf("tick %d reason = %q, want %q", tick, reason, want) + } + } + if len(comments) != 1 { + t.Fatalf("comments = %q, want exactly one needs-human note and no further %q", comments, workflowApprovalCommand) + } + if !strings.HasPrefix(comments[0], workflowApprovalRejectedMarker) || !strings.Contains(comments[0], prowActionsOkToTestRefusal) || strings.Contains(comments[0], prowOkToTestRejection) { + t.Fatalf("note = %q, want the rejected marker quoting the action's wording", comments[0]) + } +} + +func TestOkToTestRefusal(t *testing.T) { + const app = testHiveAppBotLogin + for _, tc := range []struct { + name, body, login, want string + }{ + {name: "Prow trigger plugin, @App", body: "@" + app + ": Cannot trigger testing until a trusted user reviews the PR and leaves an `/ok-to-test` message.", login: app, want: prowOkToTestRejection}, + {name: "Prow wording without @App is someone else's", body: app + ": Cannot trigger testing until a trusted user reviews the PR", login: app}, + {name: "prow-github-actions, bare login", body: "Cannot /ok-to-test because Error: " + app + " is not a reviewer or approver for any changed file", login: app, want: prowActionsOkToTestRefusal + "…"}, + {name: "prow-github-actions, login case differs", body: "Cannot /ok-to-test because Error: " + strings.ToUpper(app) + " is not a org member or collaborator", login: app, want: prowActionsOkToTestRefusal + "…"}, + {name: "prow-github-actions refusing another commenter", body: "Cannot /ok-to-test because Error: karibhou is not a reviewer or approver for any changed file", login: app}, + {name: "a different command's refusal", body: "Cannot /lgtm because Error: " + app + " is not a reviewer or approver for any changed file", login: app}, + {name: "the wording quoted mid-comment is not the action's reply", body: "FYI the bot said: Cannot /ok-to-test because Error: " + app + " is not a reviewer", login: app}, + {name: "unknown App login keeps Prow's lenient match", body: "Cannot /ok-to-test because Error: someone is not a reviewer", want: prowActionsOkToTestRefusal + "…"}, + {name: "unrelated comment", body: "/ok-to-test", login: app}, + } { + t.Run(tc.name, func(t *testing.T) { + if got := okToTestRefusal(tc.body, tc.login); got != tc.want { + t.Fatalf("okToTestRefusal(%q) = %q, want %q", tc.body, got, tc.want) + } + }) + } +} + // The needs-human note is posted once per PR, even across a restart: a // thread that already carries it gets nothing new. func TestTrySweepSelfAuthoredPRProwRejectionNoteIsOncePerPR(t *testing.T) { @@ -688,7 +747,7 @@ func TestTrySweepSelfAuthoredPRProwRejectionNoteIsOncePerPR(t *testing.T) { p.existingComments = &[]map[string]any{ appComment(1, workflowApprovalCommandBody("oldhead")), prowComment(2, "@"+testHiveAppBotLogin+": Cannot trigger testing until a trusted user reviews the PR and leaves an `/ok-to-test` message."), - appComment(3, workflowApprovalUntrustedNote(testHiveAppBotLogin, true)), + appComment(3, workflowApprovalUntrustedNote(testHiveAppBotLogin, prowOkToTestRejection)), } }) var merged []int