diff --git a/changelog.d/changed-16075-skip-pending-review-requests.md b/changelog.d/changed-16075-skip-pending-review-requests.md new file mode 100644 index 0000000000..58cae851c8 --- /dev/null +++ b/changelog.d/changed-16075-skip-pending-review-requests.md @@ -0,0 +1 @@ +- The `request_review` relay no longer re-requests reviewers or teams whose review request on the pull request is still pending, so they are not pinged again for the same ask ([#16075](https://github.com/hivecommons/hive/issues/16075)). diff --git a/src/docs/hive-open-issue.md b/src/docs/hive-open-issue.md index abd4b69832..63ae3f77fd 100644 --- a/src/docs/hive-open-issue.md +++ b/src/docs/hive-open-issue.md @@ -292,7 +292,12 @@ the PR and audits it as `agent_review_requested` with the repository and number ([#9587](https://github.com/hivecommons/hive/issues/9587)). It is the audited alternative to `gh pr edit --add-reviewer`. Logins and team slugs are checked before any GitHub call; an impossible name, or more than 15 reviewers in total, -is quarantined as malformed rather than retried. +is quarantined as malformed rather than retried. Users and teams whose review +request on the PR is still pending are skipped rather than re-pinged; they are +listed in the `.result.json` as `reviewers_already_pending` / +`team_reviewers_already_pending`, and when everyone named is already pending +no GitHub call is made (see +[notifications.md › Review requests](notifications.md#review-requests)). ## It is asynchronous, by design diff --git a/src/docs/notifications.md b/src/docs/notifications.md index 78272d1a7b..35989f6d17 100644 --- a/src/docs/notifications.md +++ b/src/docs/notifications.md @@ -219,6 +219,19 @@ Operator-defined [hooks](hooks.md) with the `notify` action send through the sam The legacy shell scripts in `bin/` also use `bin/notify.sh` for events such as stale agents, rate limits, backend switches, and kick status when those scripts are deployed. Those scripts read environment variables (`NTFY_TOPIC`, `NTFY_SERVER`, `SLACK_WEBHOOK`, `DISCORD_WEBHOOK`) rather than the `notifications:` YAML block. +## Review requests + +An agent's `request_review` relay (`hive-open-issue request-review`, or +`gh pr edit --add-reviewer`) skips any user or team whose review request on +the PR is still pending: asking again would only re-ping them for the same +unresolved ask. The skipped names are reported in the request result as +`reviewers_already_pending` / `team_reviewers_already_pending` and in the +audit entry as `already_pending`; when everyone named is already pending, no +GitHub call is made. Someone who has already submitted a review is no longer +pending, so they can still be asked again for a new head. If the pending list +cannot be read, everyone named is requested, so a needed review is never +dropped. + ## Mention hygiene Text the hive copies into a body it authors (issue/PR titles, quoted comments, diff --git a/src/pkg/github/issue_request_review.go b/src/pkg/github/issue_request_review.go index 6d624f6338..b094fbb28a 100644 --- a/src/pkg/github/issue_request_review.go +++ b/src/pkg/github/issue_request_review.go @@ -99,6 +99,46 @@ func reviewRequestShapeError(reviewers, teams []string) string { return "" } +// dropPendingReviewers removes the users and teams whose review request on +// the PR is still pending (#16075 §4): asking them again re-pings them for the +// same unresolved ask. Someone who has already reviewed is no longer pending +// and can still be re-requested for a new head. If the pending list cannot be +// read, everyone is kept, because a newly needed review must never be dropped. +func (c *Client) dropPendingReviewers(ctx context.Context, repo string, number int, reviewers, teams []string) (keepUsers, keepTeams, pendingUsers, pendingTeams []string) { + owner, repoName := c.splitRepo(repo) + pending, _, err := c.client.PullRequests.ListReviewers(ctx, owner, repoName, number, &gh.ListOptions{PerPage: 100}) + if err != nil || pending == nil { + if err != nil && c.logger != nil { + c.logger.Warn("request_review: could not read pending review requests; requesting everyone", + "repo", repo, "number", number, "error", err) + } + return reviewers, teams, nil, nil + } + users := map[string]bool{} + for _, u := range pending.Users { + users[strings.ToLower(u.GetLogin())] = true + } + slugs := map[string]bool{} + for _, t := range pending.Teams { + slugs[strings.ToLower(t.GetSlug())] = true + } + for _, r := range reviewers { + if users[strings.ToLower(r)] { + pendingUsers = append(pendingUsers, r) + } else { + keepUsers = append(keepUsers, r) + } + } + for _, t := range teams { + if slugs[strings.ToLower(t)] { + pendingTeams = append(pendingTeams, t) + } else { + keepTeams = append(keepTeams, t) + } + } + return keepUsers, keepTeams, pendingUsers, pendingTeams +} + // RequestReviewers asks users and/or teams to review a pull request. An empty // request is a no-op. func (c *Client) RequestReviewers(ctx context.Context, repo string, number int, reviewers, teams []string) error { diff --git a/src/pkg/github/issue_request_review_test.go b/src/pkg/github/issue_request_review_test.go index 50d12f0188..576eb9e5aa 100644 --- a/src/pkg/github/issue_request_review_test.go +++ b/src/pkg/github/issue_request_review_test.go @@ -211,3 +211,112 @@ func TestRequestReviewersNoop(t *testing.T) { t.Errorf("empty request should be a no-op, got %v", err) } } + +// newPendingReviewMockServer is newReviewRequestMockServer whose GET of the +// PR's requested reviewers reports pendingJSON. +func newPendingReviewMockServer(t *testing.T, pendingJSON string, calls *int, reviewers, teams *[]string) *httptest.Server { + t.Helper() + post := newReviewRequestMockServer(t, calls, reviewers, teams) + t.Cleanup(post.Close) + return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.Method == "GET" && strings.HasSuffix(r.URL.Path, "/requested_reviewers") { + w.Header().Set("Content-Type", "application/json") + _, _ = io.WriteString(w, pendingJSON) + return + } + post.Config.Handler.ServeHTTP(w, r) + })) +} + +// Asking a reviewer whose request is still pending would only re-ping them +// for the same unresolved ask (#16075 §4): they are skipped and reported, and +// only the new reviewers are requested. +func TestIssueRequestWatcher_RequestReviewSkipsPendingReviewers(t *testing.T) { + var calls int + var reviewers, teams []string + srv := newPendingReviewMockServer(t, `{"users":[{"login":"Alice"}],"teams":[{"slug":"core"}]}`, &calls, &reviewers, &teams) + defer srv.Close() + c := issueTestClient(t, srv.URL) + recs := captureAudit(c) + dir := withIssueDir(t) + + reqPath, err := WriteIssueRequest(dir, IssueRequest{ + Kind: "request_review", Repo: "o/r", Number: 42, Agent: "scanner", + Reviewers: []string{"alice", "bob"}, + TeamReviewers: []string{"o/core", "docs"}, + }) + if err != nil { + t.Fatal(err) + } + c.ProcessIssueRequestsOnce(context.Background()) + + if calls != 1 { + t.Fatalf("reviewer API calls = %d, want 1", calls) + } + if got := strings.Join(reviewers, ","); got != "bob" { + t.Errorf("reviewers requested = %q, want %q (pending alice skipped)", got, "bob") + } + if got := strings.Join(teams, ","); got != "docs" { + t.Errorf("teams requested = %q, want %q (pending core skipped)", got, "docs") + } + resp := readIssueResultFile(t, reqPath) + if !resp.OK { + t.Fatalf("result = %+v, want ok", resp) + } + if strings.Join(resp.ReviewersAlreadyPending, ",") != "alice" || strings.Join(resp.TeamReviewersAlreadyPending, ",") != "core" { + t.Errorf("result does not report who was already pending: %+v", resp) + } + if strings.Join(resp.ReviewersRequested, ",") != "bob" || strings.Join(resp.TeamReviewersRequested, ",") != "docs" { + t.Errorf("result does not report who was asked: %+v", resp) + } + rec, ok := findAudit(*recs, AuditActionAgentReviewRequested) + if !ok { + t.Fatalf("review request was not audited; records: %+v", *recs) + } + if !strings.Contains(rec.Detail, "already_pending=alice core") { + t.Errorf("audit detail = %q, want the skipped pending reviewers", rec.Detail) + } +} + +// When every named reviewer is already pending, no request is sent at all and +// the relay still succeeds. +func TestIssueRequestWatcher_RequestReviewAllPendingIsNoop(t *testing.T) { + var calls int + srv := newPendingReviewMockServer(t, `{"users":[{"login":"alice"}],"teams":[]}`, &calls, nil, nil) + defer srv.Close() + c := issueTestClient(t, srv.URL) + dir := withIssueDir(t) + + reqPath, err := WriteIssueRequest(dir, IssueRequest{ + Kind: "request_review", Repo: "o/r", Number: 42, Agent: "scanner", Reviewers: []string{"@alice"}, + }) + if err != nil { + t.Fatal(err) + } + c.ProcessIssueRequestsOnce(context.Background()) + + if calls != 0 { + t.Fatalf("reviewer API calls = %d, want 0 (alice is already pending)", calls) + } + resp := readIssueResultFile(t, reqPath) + if !resp.OK || len(resp.ReviewersRequested) != 0 || strings.Join(resp.ReviewersAlreadyPending, ",") != "alice" { + t.Errorf("result = %+v, want ok with alice reported as already pending", resp) + } + if _, err := os.Stat(reqPath); !os.IsNotExist(err) { + t.Error("a fulfilled request_review request should be consumed") + } +} + +// A failed pending-reviewer lookup never drops a review request: everyone +// named is still asked. +func TestDropPendingReviewersFailsOpen(t *testing.T) { + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusInternalServerError) + })) + defer srv.Close() + c := issueTestClient(t, srv.URL) + users, teams, pu, pt := c.dropPendingReviewers(context.Background(), "o/r", 42, []string{"alice"}, []string{"core"}) + if strings.Join(users, ",") != "alice" || strings.Join(teams, ",") != "core" || pu != nil || pt != nil { + t.Errorf("dropPendingReviewers on error = %v %v %v %v, want everyone kept", users, teams, pu, pt) + } +} diff --git a/src/pkg/github/issue_request_watcher.go b/src/pkg/github/issue_request_watcher.go index 2efb5846e7..22891c6277 100644 --- a/src/pkg/github/issue_request_watcher.go +++ b/src/pkg/github/issue_request_watcher.go @@ -188,6 +188,11 @@ type IssueResponse struct { // "request_review" request asked, after normalization. ReviewersRequested []string `json:"reviewers_requested,omitempty"` TeamReviewersRequested []string `json:"team_reviewers_requested,omitempty"` + // ReviewersAlreadyPending and TeamReviewersAlreadyPending report who was + // skipped because their review request on the PR is still pending, so + // asking again would only re-ping them for the same ask. + ReviewersAlreadyPending []string `json:"reviewers_already_pending,omitempty"` + TeamReviewersAlreadyPending []string `json:"team_reviewers_already_pending,omitempty"` // ParentLinked reports that the created issue was linked as a GitHub // sub-issue of req.Parent. ParentLinkError carries the (non-fatal) reason // when a requested link did not succeed; the issue is still created OK. @@ -743,12 +748,16 @@ func (c *Client) handleOneIssueRequest(ctx context.Context, path string, nowFn f case "request_review": reviewers := normalizeReviewers(req.Reviewers) teams := normalizeTeamReviewers(req.TeamReviewers) + var pendingUsers, pendingTeams []string + reviewers, teams, pendingUsers, pendingTeams = c.dropPendingReviewers(ctx, req.Repo, req.Number, reviewers, teams) err = c.RequestReviewers(ctx, req.Repo, req.Number, reviewers, teams) if err == nil { resp.OK = true resp.Number = req.Number resp.ReviewersRequested = reviewers resp.TeamReviewersRequested = teams + resp.ReviewersAlreadyPending = pendingUsers + resp.TeamReviewersAlreadyPending = pendingTeams } case "close": closedPR, err = c.closeIssue(ctx, req.Repo, req.Number, IssueCloseOptions{OverrideReason: req.OverrideReason}) @@ -899,6 +908,7 @@ func (c *Client) handleOneIssueRequest(ctx context.Context, path string, nowFn f extra = []string{ "reviewers", strings.Join(resp.ReviewersRequested, " "), "team_reviewers", strings.Join(resp.TeamReviewersRequested, " "), + "already_pending", strings.Join(append(append([]string{}, resp.ReviewersAlreadyPending...), resp.TeamReviewersAlreadyPending...), " "), } } if closedPR {