diff --git a/changelog.d/fixed-17475-prow-approve-rejection-surfaced.md b/changelog.d/fixed-17475-prow-approve-rejection-surfaced.md new file mode 100644 index 0000000000..6b06dc8cd2 --- /dev/null +++ b/changelog.d/fixed-17475-prow-approve-rejection-surfaced.md @@ -0,0 +1 @@ +- Dashboard: an Approve on a Prow repo now reads Prow's reply to the relayed `/lgtm` + `/approve` and says Prow rejected them instead of showing "approved" (#17475, part of #17451). diff --git a/src/docs/dashboard-sections/activity-monitor.md b/src/docs/dashboard-sections/activity-monitor.md index 2213e3a2ab..7dbe054c99 100644 --- a/src/docs/dashboard-sections/activity-monitor.md +++ b/src/docs/dashboard-sections/activity-monitor.md @@ -489,7 +489,7 @@ A PR waiting for a person to merge it (*Ready for human merge*, a `human_merge_r *Close as superseded by…* on a nested PR with exactly one open sibling under the same issue pre-fills that sibling's number. Within the general *Decide…* group, the first entry is *Let hive decide* (see [Let hive decide](#let-hive-decide) below): the hive decides as the owner's delegate and records it the way a person's decision is recorded. For a PR: *Approve (/lgtm + /approve)* posts those two commands as comments attributed to you and records your approval against the PR's current head as the human answer the row was parked for — a `requires_human` / decision-requested reviewer verdict, the hive's own `needs-human` park (a supersession question, say), the `sentinel-alert` label and the evidence bundle's sentinel findings are all released by it (the response's `released` lists what was: `approval-recorded`, `needs-human`, `sentinel-label`, `sentinel-acknowledged`), the stage engine re-derives the stage on the spot and the tracker cache is dropped — and the sentinel sweep honours the release for that head, re-arming only on a new push — so the row leaves *Needs human decision* for 080 *Awaiting merge admission* on the next read; the merge gate (and the auto-merge lanes, which honour the recorded approval on that head exactly as a trusted merger's GitHub Approve review) takes it from there; this approval path neither submits a GitHub review as you nor merges immediately. To merge, use the 090 **Approve and let hive merge** action (or the secondary **Merge now as maintainer** for a direct merge). -On a Prow repo where the hive bot is not an approver, Prow has already answered the hive's earlier commands with "restricted to collaborators" / "NOT APPROVED"; a later Approve then records the decision without posting the two comments again and says so (`commands_skipped: prow-rejects-bot-commands`, and the toast reads *Approval recorded; `/lgtm` + `/approve` not posted*). A new head carries no answer: pushing to the PR re-asks. +On a Prow repo where the hive bot is not an approver, the first Approve posts the two commands and then waits a few seconds for Prow's answer; when Prow rejects them ("restricted to collaborators" / "NOT APPROVED") the response carries `prow_rejected: true` and the toast reads *Approval of #N recorded in hive; `/lgtm` + `/approve` posted, but Prow rejected them* instead of a plain "Approved". Prow's reply stays on the PR, so a later Approve records the decision without posting the two comments again and says so (`commands_skipped: prow-rejects-bot-commands`, and the toast reads *Approval recorded; `/lgtm` + `/approve` not posted*). A new head carries no answer: pushing to the PR re-asks. The delegated *Let hive decide* path still posts the comments only — it recommends and never clears a sentinel alert — *Close as superseded by…* (a picker listing the other open PRs in the repo, or type `#458` / `owner/repo#458` / a URL; then an optional comment, then the sweep's labelled close), *Won't fix…* (a comment naming you, then a not-planned close) and *Give direction…* (your text is recorded as `/hive decision ` against the head, so the author agent pushes a new head; only on PRs the hive opened). diff --git a/src/pkg/dashboard/api_repo_item_decision.go b/src/pkg/dashboard/api_repo_item_decision.go index cb22a66f79..c6a7026252 100644 --- a/src/pkg/dashboard/api_repo_item_decision.go +++ b/src/pkg/dashboard/api_repo_item_decision.go @@ -191,6 +191,10 @@ type repoItemDecisionResponse struct { // the hive bot is not an approver, so they would only draw another // "NOT APPROVED" (dibs#510). The approval itself is still recorded. CommandsSkipped string `json:"commands_skipped,omitempty"` + // ProwRejected is set when the `/lgtm` + `/approve` were posted and Prow + // replied that it does not accept them from the hive bot + // (hivecommons/hive#17475). + ProwRejected bool `json:"prow_rejected,omitempty"` // Rebase is set when an approval landed on a PR that is behind or // conflicting with its base: the rebase episode the approval opened // (hivecommons/hive#15103), so the row reads "Rebasing · attempt n/2". @@ -235,6 +239,9 @@ type repoItemApproveFacts struct { // because Prow already rejected the hive's commands on this PR // (dibs#510); the hive's own approval record is what counts. CommandsSkipped bool + // ProwRejected: the commands were posted and Prow's reply rejected them + // (hivecommons/hive#17475); later approvals on the PR skip them. + ProwRejected bool // ToContributor is the contributor whose hive a direction was carried // to (a clanker-authored PR, hivecommons/hive#15159); "" when the @@ -737,7 +744,7 @@ func (s *Server) handlePullRequestDecision(ctx context.Context, w http.ResponseW ) var released []string stage, commandsSkipped, reportingError := "", "", "" - sentinelOpen, prow := false, false + sentinelOpen, prow, prowRejected := false, false, false toContributor := "" if decision == repoItemDecisionApprove { var event ghpkg.PullRequestApprovalEvent @@ -748,7 +755,7 @@ func (s *Server) handlePullRequestDecision(ctx context.Context, w http.ResponseW }) head = event.HeadSHA released, stage, sentinelOpen, prow = repoItemApprovalReleased(event), event.Stage, event.SentinelLabelOpen, event.ProwManaged - commandsSkipped = event.CommandsSkipped + commandsSkipped, prowRejected = event.CommandsSkipped, event.ProwRejected reportingError = event.ReportingError } else { var event ghpkg.PullRequestDecisionEvent @@ -789,7 +796,7 @@ func (s *Server) handlePullRequestDecision(ctx context.Context, w http.ResponseW } s.audit.LogRecord(user, action, auditDetail("repo", repoFull, "number", numStr, "type", "pr", "head", head, "decision", comment, "released", strings.Join(released, ","), "stage", stage, "admitted_by", admitted, "commands_skipped", commandsSkipped, "to_contributor", toContributor), "", repoFull, number) result := repoItemDecisionResultWith(repoFull, number, "pr", decision, comment, "", "", repoItemApproveFacts{ - User: user, Released: released, Stage: stage, SentinelOpen: sentinelOpen, SentinelLabel: s.sentinelLabel(), SoakMinutes: s.approveSoakMinutes(admitted != ""), Prow: prow, CommandsSkipped: commandsSkipped != "", ToContributor: toContributor, + User: user, Released: released, Stage: stage, SentinelOpen: sentinelOpen, SentinelLabel: s.sentinelLabel(), SoakMinutes: s.approveSoakMinutes(admitted != ""), Prow: prow, CommandsSkipped: commandsSkipped != "", ProwRejected: prowRejected, ToContributor: toContributor, }) if reportingError != "" { s.audit.LogRecord(user, auditRepoItemDecisionApprove+"_reporting_failed", auditDetail("repo", repoFull, "number", numStr, "head", head, "error", reportingError), "", repoFull, number) @@ -809,7 +816,7 @@ func (s *Server) handlePullRequestDecision(ctx context.Context, w http.ResponseW // The row's park was just answered: serve the re-derived stage on the // next tracker read instead of the cached 085. s.invalidateStageTrackerForWebhook() - jsonResponse(w, repoItemDecisionResponse{OK: true, Repo: repoFull, Number: number, Decision: decision, Status: status, Released: released, Stage: stage, Head: head, AdmittedBy: admitted, CommandsSkipped: commandsSkipped, Rebase: rebase, + jsonResponse(w, repoItemDecisionResponse{OK: true, Repo: repoFull, Number: number, Decision: decision, Status: status, Released: released, Stage: stage, Head: head, AdmittedBy: admitted, CommandsSkipped: commandsSkipped, ProwRejected: prowRejected, Rebase: rebase, Result: result}) } @@ -892,7 +899,7 @@ func repoItemApproveResult(out *RowActionResult, number int, facts repoItemAppro released[r] = true } headline := "Approved #" + strconv.Itoa(number) - if facts.Prow { + if facts.Prow || facts.ProwRejected { headline = "Approval of #" + strconv.Itoa(number) + " recorded in hive" } if u := strings.TrimSpace(facts.User); u != "" { @@ -909,6 +916,8 @@ func repoItemApproveResult(out *RowActionResult, number int, facts repoItemAppro out.Outcome = headline + "." if facts.CommandsSkipped { out.Posted = append(out.Posted, "`/lgtm` + `/approve` not posted (Prow does not accept the hive bot's commands on this repo)") + } else if facts.ProwRejected { + out.Posted = append(out.Posted, "`/lgtm` + `/approve` posted, but Prow rejected them (the hive bot is not a collaborator or OWNERS approver)") } else { out.Posted = append(out.Posted, "`/lgtm` posted", "`/approve` posted") } @@ -929,7 +938,7 @@ func repoItemApproveResult(out *RowActionResult, number int, facts repoItemAppro default: out.Next = "Hive merges it on its next merge pass once CI is green and the head has soaked" + repoItemSoakNote(facts.SoakMinutes) + "." } - if facts.Prow || facts.CommandsSkipped { + if facts.Prow || facts.CommandsSkipped || facts.ProwRejected { // Prow reads `/approve` only from OWNERS; the hive's account is not // one, so the relayed command is "NOT APPROVED" there. out.Next = "Prow approval still needs an OWNERS reviewer: the relayed `/approve` came from hive's account, which Prow does not count. " + out.Next diff --git a/src/pkg/dashboard/api_repo_item_decision_test.go b/src/pkg/dashboard/api_repo_item_decision_test.go index 84e3a94dcb..1afe7755c2 100644 --- a/src/pkg/dashboard/api_repo_item_decision_test.go +++ b/src/pkg/dashboard/api_repo_item_decision_test.go @@ -450,3 +450,19 @@ func TestRepoItemDecisionWordingNeverSaysSweep(t *testing.T) { } } } + +// A Prow rejection of the relayed commands must not read as "approved" +// (hivecommons/hive#17475). +func TestRepoItemApproveResultSurfacesProwRejection(t *testing.T) { + out := &RowActionResult{} + repoItemApproveResult(out, 17359, repoItemApproveFacts{User: "alice", ProwRejected: true}) + if strings.HasPrefix(out.Outcome, "Approved #") || !strings.Contains(out.Outcome, "recorded in hive") { + t.Fatalf("outcome = %q, want the hive-only approval wording", out.Outcome) + } + if got := strings.Join(out.Posted, "\n"); !strings.Contains(got, "Prow rejected") || strings.Contains(got, "`/lgtm` posted") { + t.Fatalf("posted = %q, want Prow's rejection named", got) + } + if !strings.Contains(out.Next, "OWNERS reviewer") { + t.Fatalf("next = %q, want the OWNERS note", out.Next) + } +} diff --git a/src/pkg/github/issue_unpark_command_test.go b/src/pkg/github/issue_unpark_command_test.go index f9f8bda079..7106afd63c 100644 --- a/src/pkg/github/issue_unpark_command_test.go +++ b/src/pkg/github/issue_unpark_command_test.go @@ -56,6 +56,9 @@ type unparkServer struct { added map[int][]string // removed maps issue number to the labels removed from it. removed map[int][]string + // replyAfterPost maps issue number to comments that appear in its + // listing once a comment is POSTed on it (e.g. Prow's reply). + replyAfterPost map[int][]unparkWireComment // permissions maps login to the permission level the API reports. permissions map[string]string // roles maps login to the role_name the permission endpoint reports @@ -206,6 +209,11 @@ func newUnparkServer(t *testing.T, owner, repo string, issues []unparkWireIssue, } _ = json.NewDecoder(r.Body).Decode(&body) rec.posted[n] = append(rec.posted[n], body.Body) + if extra := rec.replyAfterPost[n]; len(extra) > 0 { + iss := byNum[n] + iss.Comments = append(append([]unparkWireComment(nil), iss.Comments...), extra...) + byNum[n] = iss + } _ = json.NewEncoder(w).Encode(map[string]any{"id": 9000 + len(rec.posted[n])}) case strings.HasPrefix(path, "issues/comments/") && r.Method == http.MethodPatch: diff --git a/src/pkg/github/pr_decision_operator.go b/src/pkg/github/pr_decision_operator.go index b239777715..ef60d74c63 100644 --- a/src/pkg/github/pr_decision_operator.go +++ b/src/pkg/github/pr_decision_operator.go @@ -88,6 +88,12 @@ type PullRequestApprovalEvent struct { // so posting again would only draw the same "NOT APPROVED" reply. The // hive's own approval record is unaffected. CommandsSkipped string + // ProwRejected is true when the `/lgtm` + `/approve` comments were + // posted and Prow answered them with a rejection ("restricted to + // collaborators" / "NOT APPROVED") before the approval reported. Prow's + // reply stays on the PR, so the next Approve takes the CommandsSkipped + // path (hivecommons/hive#17475). + ProwRejected bool // ReportingError preserves partial progress: the native approval stands, // but comments, labels or stage reporting must be retried/reconciled. ReportingError string @@ -101,6 +107,38 @@ const PRApprovalCommandsSkippedProw = "prow-rejects-bot-commands" // bot's `/lgtm` or `/approve` carries no weight on the repo. var prowCommandRejections = []string{"restricted to collaborators", "NOT APPROVED"} +// prowReplyWindow and prowReplyInterval bound how long an Approve on a +// Prow-managed PR waits for Prow to answer the commands it just relayed. +var ( + prowReplyWindow = 6 * time.Second + prowReplyInterval = 2 * time.Second +) + +// awaitProwRejection reads the PR's comments for Prow's rejection of the +// hive commands just posted: once when wait is false, otherwise polling +// every prowReplyInterval with a final read at the end of prowReplyWindow, +// since Prow answers a few seconds after the post. +func (c *Client) awaitProwRejection(ctx context.Context, owner, name string, number int, wait bool) bool { + deadline := time.Now().Add(prowReplyWindow) + for { + if rejected, err := c.prowRejectedHiveCommands(ctx, owner, name, number); err == nil && rejected { + return true + } + if !wait { + return false + } + remaining := time.Until(deadline) + if remaining <= 0 { + return false + } + select { + case <-ctx.Done(): + return false + case <-time.After(min(prowReplyInterval, remaining)): + } + } +} + // prowRejectedHiveCommands reports whether the Prow bot has replied to one of the hive's earlier dashboard approval commands on the // PR with a Prow rejection (dibs#510: `/lgtm` → "changing LGTM is // restricted to collaborators", `/approve` → "[APPROVALNOTIFIER] This PR is @@ -473,6 +511,7 @@ func (c *Client) reportOperatorPullRequestApproval(ctx context.Context, owner, n if c.logger != nil { c.logger.Info("pull request approval commands posted by dashboard decision", "repo", event.Repo, "pr", number, "actor", actor, "head", head) } + event.ProwRejected = c.awaitProwRejection(ctx, owner, name, number, event.ProwManaged) } return ctx.Err() } diff --git a/src/pkg/github/pr_decision_operator_test.go b/src/pkg/github/pr_decision_operator_test.go index e13726f50e..8e3b53e517 100644 --- a/src/pkg/github/pr_decision_operator_test.go +++ b/src/pkg/github/pr_decision_operator_test.go @@ -390,6 +390,41 @@ func TestRecordOperatorPullRequestApprovalSkipsCommandsProwRejected(t *testing.T } } +// On a Prow repo where rejection has not been observed yet, Prow answers the +// relayed commands after they are posted. The approval event reports that +// reply instead of leaving the board at "approved" (hivecommons/hive#17475). +func TestRecordOperatorPullRequestApprovalReportsProwRejectionAfterPost(t *testing.T) { + oldWindow, oldInterval := prowReplyWindow, prowReplyInterval + prowReplyWindow, prowReplyInterval = time.Second, 10*time.Millisecond + t.Cleanup(func() { prowReplyWindow, prowReplyInterval = oldWindow, oldInterval }) + at := time.Date(2026, 10, 29, 0, 32, 0, 0, time.UTC).Format(time.RFC3339) + own := unparkWireComment{ID: 9001, Body: renderPRApprovalCommandComment("/lgtm", "clubanderson", liveDecisionHead), User: wireUser{Login: "hive[bot]"}, CreatedAt: at, UpdatedAt: at} + reply := unparkWireComment{ID: 9100, Body: "@hive[bot]: changing LGTM is restricted to collaborators\n\nIn response to [this](https://github.com/org/repo/pull/1#issuecomment-9001):", User: wireUser{Login: "kubestellar-prow[bot]"}, CreatedAt: at, UpdatedAt: at} + for _, tc := range []struct { + name string + labels []string + after []unparkWireComment + want bool + }{ + {"prow rejects", []string{"size/XS"}, []unparkWireComment{own, reply}, true}, + {"prow silent", []string{"size/XS"}, []unparkWireComment{own}, false}, + } { + t.Run(tc.name, func(t *testing.T) { + pr := hiveParkedPR() + pr.Labels = append(pr.Labels, wireLabel{Name: tc.labels[0]}) + c, rec := newUnparkPRClient(t, []unparkWireIssue{pr}, map[string]string{"clubanderson": "admin"}) + rec.replyAfterPost = map[int][]unparkWireComment{liveDecisionPR: tc.after} + event, err := c.RecordOperatorPullRequestApprovalWith(context.Background(), "org/repo", liveDecisionPR, "clubanderson", PullRequestApprovalOptions{HumanChangeRequests: review.NewHumanChangeRequests("")}) + if err != nil { + t.Fatalf("approval: %v", err) + } + if event.ProwRejected != tc.want || event.CommandsSkipped != "" || len(rec.posted[liveDecisionPR]) != len(PRApprovalCommands) { + t.Fatalf("event = %+v, want ProwRejected %v with commands posted", event, tc.want) + } + }) + } +} + // TestRecordOperatorPullRequestDecisionCarriesDirectionToContributorHive: // a direction on a contributor's clanker PR (author/clanker) is accepted // and carried to that contributor's hive — recorded against the head as a