fix adding/removing team reviewers with gh pr edit
#3701
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes #3705
The issue:
According to the documentation of
gh pr edit
, the--add-reviewer
and--remove-reviewer
flags accept team users with the following format:myorg/team-slug
.If the PR doesn't have any team user specified in the "reviewers" section, the existing code works.
But if the PR already has a team user specified in the "reviewers" section, the
gh pr edit 1 --add-reviewer myorg/other-team
command fails with the error message:'' not found
.Identifying the root cause:
This error is returned by the TeamsToIDs function in
api/queries_repo.go
, because one element innames
is an empty string.Going further in debugging this, I found out that the empty string was generated by the Logins function in
api/queries_pr.go
.The Logins function was using the RequestedReviewer.Login attribute for both normal users and team users. But teams don't have a Login field (see Team graphql reference). So, when parsing an existing PR with a team as reviewer, the Logins function returns a list with an empty string element.
Fix proposal:
This fix updates the graphql query to get the slug and the organization login in case of a team element, and it adds a check on the typename in Logins to build the login for a team concatenating the org login and the team slug. That way the team login can be compared to values in --add-reviewer, --remove-reviewer flags and in the metadata of the repo.
Testing:
I added a simple unit test for the Logins function. I also built the gh command with the changes and manually tried the
gh pr edit
command with different combinations of the--add-reviewer
and--remove-reviewer
flags.Thank you in advance for your inputs on this PR. Please let me know in case changes to the fix would be needed.