Skip to content

Commit 90bfa62

Browse files
BagToadCopilot
andcommitted
Exclude current user from suggested reviewers in gh pr create
Query the viewer login in SuggestedReviewerActorsForRepo and pre-seed the seen map so the current user is filtered out of collaborator results. You cannot review your own PR. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 08c7a4c commit 90bfa62

2 files changed

Lines changed: 40 additions & 1 deletion

File tree

api/queries_pr_review.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,9 @@ func SuggestedReviewerActors(client *Client, repo ghrepo.Interface, prID string,
556556
// Returns the candidates, a MoreResults count, and an error.
557557
func SuggestedReviewerActorsForRepo(client *Client, repo ghrepo.Interface, query string) ([]ReviewerCandidate, int, error) {
558558
type responseData struct {
559+
Viewer struct {
560+
Login string
561+
}
559562
Repository struct {
560563
// HACK: There's no repo-level API to check Copilot reviewer eligibility,
561564
// so we piggyback on an open PR's suggestedReviewerActors to detect
@@ -610,8 +613,13 @@ func SuggestedReviewerActorsForRepo(client *Client, repo ghrepo.Interface, query
610613
return nil, 0, err
611614
}
612615

613-
// Build candidates using cascading quota logic
616+
// Build candidates using cascading quota logic.
617+
// Pre-seed seen with the current user to exclude them from results
618+
// since you cannot review your own PR.
614619
seen := make(map[string]bool)
620+
if result.Viewer.Login != "" {
621+
seen[result.Viewer.Login] = true
622+
}
615623
var candidates []ReviewerCandidate
616624
const baseQuota = 5
617625

api/queries_pr_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,7 @@ func mockReviewerResponseForRepoWithCopilot(collabs, teams, totalCollabs, totalT
390390

391391
return fmt.Sprintf(`{
392392
"data": {
393+
"viewer": {"login": "testuser"},
393394
"repository": {
394395
%s,
395396
"collaborators": {"nodes": [%s]},
@@ -500,6 +501,36 @@ func TestSuggestedReviewerActorsForRepo(t *testing.T) {
500501
expectedLogins: []string{"c1", "c2", "c3", "OWNER/team1", "OWNER/team2"},
501502
expectedMore: 10,
502503
},
504+
{
505+
name: "viewer excluded from collaborators",
506+
httpStubs: func(reg *httpmock.Registry) {
507+
// c1 matches the viewer login "testuser" won't be in this fixture,
508+
// but we can craft a response where the viewer login matches a collaborator.
509+
reg.Register(
510+
httpmock.GraphQL(`query SuggestedReviewerActorsForRepo\b`),
511+
httpmock.StringResponse(`{
512+
"data": {
513+
"viewer": {"login": "c2"},
514+
"repository": {
515+
"pullRequests": {"nodes": []},
516+
"collaborators": {"nodes": [
517+
{"login": "c1", "name": "C1"},
518+
{"login": "c2", "name": "C2"},
519+
{"login": "c3", "name": "C3"}
520+
]},
521+
"collaboratorsTotalCount": {"totalCount": 3}
522+
},
523+
"organization": {
524+
"teams": {"nodes": []},
525+
"teamsTotalCount": {"totalCount": 0}
526+
}
527+
}
528+
}`))
529+
},
530+
expectedCount: 2,
531+
expectedLogins: []string{"c1", "c3"},
532+
expectedMore: 3,
533+
},
503534
}
504535

505536
for _, tt := range tests {

0 commit comments

Comments
 (0)