-
Notifications
You must be signed in to change notification settings - Fork 7.4k
gh pr edit: Only fetch org teams for reviewers when required
#11835
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Switches pull request reviewer add/remove operations from GraphQL to the REST API, enabling separate add and remove calls for reviewers and teams. Refactors reviewer editing logic to avoid fetching organization teams unless required for interactive editing, improving performance for non-interactive flows. Updates tests and supporting code to reflect the new reviewer management and metadata fetching behavior.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR refactors the gh pr edit command to optimize team fetching behavior and switch from GraphQL to REST API for reviewer management. It improves performance by only fetching organization teams when actually needed and fixes a bug where PR authors could be incorrectly included in reviewer selections.
- Refactored reviewer API calls from GraphQL to REST endpoints
- Added conditional team fetching logic to avoid unnecessary API calls
- Fixed PR author inclusion bug in interactive reviewer selection
Reviewed Changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
| pkg/cmd/pr/shared/editable.go | Removed ReviewerIds method and optimized team fetching logic |
| pkg/cmd/pr/edit/edit_test.go | Updated test suite with new REST API mocks and conditional team fetching test cases |
| pkg/cmd/pr/edit/edit.go | Implemented REST API reviewer updates and PR author filtering |
| api/queries_pr.go | Added new REST API functions for adding/removing reviewers |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
babakks
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, bar a few necessary fixes (e.g. url.PathEscape).
Deleted the unused ghIds helper function and the associated githubv4 import from edit.go to clean up the codebase.
Updated AddPullRequestReviews and RemovePullRequestReviews to use url.PathEscape for repo owner and name in API paths. This ensures correct handling of special characters in repository identifiers.
Extracted logic for splitting reviewer identifiers into users and teams into a new helper function, partitionUsersAndTeams. Updated updatePullRequestReviews to use this function for both adding and removing reviewers, improving code clarity and maintainability. Also clarified comments regarding PR author handling.
Corrected '--tile' to '--title' in the error message shown when required flags are missing in non-interactive mode.
Eliminates unnecessary initialization of users and teams to empty slices in RemovePullRequestReviews. Also updates the request body struct to use 'omitempty' for reviewers and team_reviewers, ensuring empty fields are omitted from the JSON payload.
Swaps the argument order of the httpStubs functions in edit_test.go to match the expected (t *testing.T, reg *httpmock.Registry) signature. This improves consistency and prevents potential confusion or errors when calling these test helpers.
Co-authored-by: Babak K. Shandiz <[email protected]>
Co-authored-by: Babak K. Shandiz <[email protected]>
This comment was marked as spam.
This comment was marked as spam.
This comment was marked as spam.
This comment was marked as spam.
This MR contains the following updates: | Package | Update | Change | |---|---|---| | [cli/cli](https://github.com/cli/cli) | minor | `v2.81.0` -> `v2.82.0` | MR created with the help of [el-capitano/tools/renovate-bot](https://gitlab.com/el-capitano/tools/renovate-bot). **Proposed changes to behavior should be submitted there as MRs.** --- ### Release Notes <details> <summary>cli/cli (cli/cli)</summary> ### [`v2.82.0`](https://github.com/cli/cli/releases/tag/v2.82.0): GitHub CLI 2.82.0 [Compare Source](cli/cli@v2.81.0...v2.82.0) ##### ✨ Features - `gh pr edit`: Only fetch org teams for reviewers when required by [@​BagToad](https://github.com/BagToad) in [#​11835](cli/cli#11835) ##### 🐛 Fixes - fix(cache delete): report correct deleted count for key and key+ref deletions by [@​luxass](https://github.com/luxass) in [#​11838](cli/cli#11838) - `gh agent-task create`: Fix `--follow` not killing the progress indicator by [@​BagToad](https://github.com/BagToad) in [#​11879](cli/cli#11879) - `gh agent-task create`: Fix targetting upstream instead of default repo by [@​BagToad](https://github.com/BagToad) in [#​11896](cli/cli#11896) - Fix `auth login` and `auth refresh` to use UNIX socket by [@​babakks](https://github.com/babakks) in [#​11922](cli/cli#11922) **Full Changelog**: <cli/cli@v2.81.0...v2.82.0> </details> --- ### Configuration 📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined). 🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied. ♻ **Rebasing**: Whenever MR becomes conflicted, or you tick the rebase/retry checkbox. 🔕 **Ignore**: Close this MR and you won't be reminded about this update again. --- - [ ] <!-- rebase-check -->If you want to rebase/retry this MR, check this box --- This MR has been generated by [Renovate Bot](https://github.com/renovatebot/renovate). <!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiI0MS4xNDguNiIsInVwZGF0ZWRJblZlciI6IjQxLjE0OC42IiwidGFyZ2V0QnJhbmNoIjoibWFpbiIsImxhYmVscyI6WyJSZW5vdmF0ZSBCb3QiXX0=-->
Description
This pull request refactors how pull request reviewer updates are handled, switching from the GraphQL API to the REST API for adding and removing reviewers. It also improves the logic for managing reviewer lists, especially in non-interactive and interactive editing modes, and updates the test suite to cover these changes and edge cases.
Also fixes a bug where the PR author was included in the interactive list of reviewers. With GraphQL this selection would be ignored, but the REST API actually returns an error if the PR author is provided, so we need to fix that now to avoid confusion.
fixes #4844
Considerations
Moving to the REST API with this allows us to add/remove as separate operations, eliminating the need to fetch org teams and org team IDs. This is good, because it allows the
GITHUB_TOKENand other repo scoped authentication types to proceed with requesting a user review.However, there's a drawback that we now will require two operations, one for adding reviewers and one for removing.
I am fine with this trade-off because I suspect the typical use-case will only involve either requesting a reviewer or removing a reviewer as discrete operations. Meaning, in most cases we'll never actually perform two API calls. Nonetheless the risk is there.
This implementation also proposes that these two API requests be performed concurrently. I suspect this is optimal, but I am unsure of any sort of race-condition that could arise. In particular in the case of removing and adding the same reviewer, but that could be seen as user error.
Testing
This was tested in Actions to prove that it will allow requesting a user reviewer without requiring access to teams, meaning the
GITHUB_TOKENcould be used where currentlyghwill fail.Test workflow
You can see the first job is successful, which is what we're going for. We want the
GITHUB_TOKENto be able to request a review from a user as long as there isn't teams included in the review request.Note
This was also tested with a team reviewer already requested to prove that we will not fetch team reviewers beyond what the PR metadata gives us already through the non-interactive flow. Therein lies the key reason to switch to the REST API here.
Further Improvements
It should be noted that the REST API doesn't return a satisfying or useful error message when you provide it a team reviewer without having access to org teams, as in the case of the
GITHUB_TOKEN:Perhaps there is further work to be done in the future to improve this error messaging client-side.