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>
This commit is contained in:
parent
08c7a4c207
commit
90bfa624c2
2 changed files with 40 additions and 1 deletions
|
|
@ -556,6 +556,9 @@ func SuggestedReviewerActors(client *Client, repo ghrepo.Interface, prID string,
|
|||
// Returns the candidates, a MoreResults count, and an error.
|
||||
func SuggestedReviewerActorsForRepo(client *Client, repo ghrepo.Interface, query string) ([]ReviewerCandidate, int, error) {
|
||||
type responseData struct {
|
||||
Viewer struct {
|
||||
Login string
|
||||
}
|
||||
Repository struct {
|
||||
// HACK: There's no repo-level API to check Copilot reviewer eligibility,
|
||||
// so we piggyback on an open PR's suggestedReviewerActors to detect
|
||||
|
|
@ -610,8 +613,13 @@ func SuggestedReviewerActorsForRepo(client *Client, repo ghrepo.Interface, query
|
|||
return nil, 0, err
|
||||
}
|
||||
|
||||
// Build candidates using cascading quota logic
|
||||
// Build candidates using cascading quota logic.
|
||||
// Pre-seed seen with the current user to exclude them from results
|
||||
// since you cannot review your own PR.
|
||||
seen := make(map[string]bool)
|
||||
if result.Viewer.Login != "" {
|
||||
seen[result.Viewer.Login] = true
|
||||
}
|
||||
var candidates []ReviewerCandidate
|
||||
const baseQuota = 5
|
||||
|
||||
|
|
|
|||
|
|
@ -390,6 +390,7 @@ func mockReviewerResponseForRepoWithCopilot(collabs, teams, totalCollabs, totalT
|
|||
|
||||
return fmt.Sprintf(`{
|
||||
"data": {
|
||||
"viewer": {"login": "testuser"},
|
||||
"repository": {
|
||||
%s,
|
||||
"collaborators": {"nodes": [%s]},
|
||||
|
|
@ -500,6 +501,36 @@ func TestSuggestedReviewerActorsForRepo(t *testing.T) {
|
|||
expectedLogins: []string{"c1", "c2", "c3", "OWNER/team1", "OWNER/team2"},
|
||||
expectedMore: 10,
|
||||
},
|
||||
{
|
||||
name: "viewer excluded from collaborators",
|
||||
httpStubs: func(reg *httpmock.Registry) {
|
||||
// c1 matches the viewer login "testuser" won't be in this fixture,
|
||||
// but we can craft a response where the viewer login matches a collaborator.
|
||||
reg.Register(
|
||||
httpmock.GraphQL(`query SuggestedReviewerActorsForRepo\b`),
|
||||
httpmock.StringResponse(`{
|
||||
"data": {
|
||||
"viewer": {"login": "c2"},
|
||||
"repository": {
|
||||
"pullRequests": {"nodes": []},
|
||||
"collaborators": {"nodes": [
|
||||
{"login": "c1", "name": "C1"},
|
||||
{"login": "c2", "name": "C2"},
|
||||
{"login": "c3", "name": "C3"}
|
||||
]},
|
||||
"collaboratorsTotalCount": {"totalCount": 3}
|
||||
},
|
||||
"organization": {
|
||||
"teams": {"nodes": []},
|
||||
"teamsTotalCount": {"totalCount": 0}
|
||||
}
|
||||
}
|
||||
}`))
|
||||
},
|
||||
expectedCount: 2,
|
||||
expectedLogins: []string{"c1", "c3"},
|
||||
expectedMore: 3,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue