Skip to content

Commit

Permalink
Merge pull request cli#3737 from cli/requested-reviewers-slug
Browse files Browse the repository at this point in the history
Fix how teams are displayed in requested reviewers
  • Loading branch information
mislav authored Jun 2, 2021
2 parents e65d956 + 1a980e7 commit 4960935
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 30 deletions.
16 changes: 13 additions & 3 deletions api/export_pr.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,20 @@ func (pr *PullRequest) ExportData(fields []string) *map[string]interface{} {
case "reviewRequests":
requests := make([]interface{}, 0, len(pr.ReviewRequests.Nodes))
for _, req := range pr.ReviewRequests.Nodes {
if req.RequestedReviewer.TypeName == "" {
continue
r := req.RequestedReviewer
switch r.TypeName {
case "User":
requests = append(requests, map[string]string{
"__typename": r.TypeName,
"login": r.Login,
})
case "Team":
requests = append(requests, map[string]string{
"__typename": r.TypeName,
"name": r.Name,
"slug": r.LoginOrSlug(),
})
}
requests = append(requests, req.RequestedReviewer)
}
data[f] = &requests
default:
Expand Down
35 changes: 20 additions & 15 deletions api/queries_pr.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,28 +150,33 @@ type PullRequestFile struct {

type ReviewRequests struct {
Nodes []struct {
RequestedReviewer struct {
TypeName string `json:"__typename"`
Login string `json:"login"`
Name string `json:"name"`
Slug string `json:"slug"`
Organization struct {
Login string `json:"login"`
}
}
RequestedReviewer RequestedReviewer
}
}

type RequestedReviewer struct {
TypeName string `json:"__typename"`
Login string `json:"login"`
Name string `json:"name"`
Slug string `json:"slug"`
Organization struct {
Login string `json:"login"`
} `json:"organization"`
}

func (r RequestedReviewer) LoginOrSlug() string {
if r.TypeName == teamTypeName {
return fmt.Sprintf("%s/%s", r.Organization.Login, r.Slug)
}
return r.Login
}

const teamTypeName = "Team"

func (r ReviewRequests) Logins() []string {
logins := make([]string, len(r.Nodes))
for i, a := range r.Nodes {
if a.RequestedReviewer.TypeName == teamTypeName {
logins[i] = fmt.Sprintf("%s/%s", a.RequestedReviewer.Organization.Login, a.RequestedReviewer.Slug)
} else {
logins[i] = a.RequestedReviewer.Login
}
for i, r := range r.Nodes {
logins[i] = r.RequestedReviewer.LoginOrSlug()
}
return logins
}
Expand Down
10 changes: 6 additions & 4 deletions pkg/cmd/pr/view/fixtures/prViewPreviewWithReviewersByNumber.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@
}
},
{
"requestedReviewer": {
"__typename": "Team",
"name": "Team 1"
}
"requestedReviewer": {
"__typename": "Team",
"name": "Team 1",
"slug": "team-1",
"organization": {"login": "my-org"}
}
},
{
"requestedReviewer": {
Expand Down
7 changes: 1 addition & 6 deletions pkg/cmd/pr/view/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,8 +294,6 @@ func prReviewerList(pr api.PullRequest, cs *iostreams.ColorScheme) string {
return reviewerList
}

const teamTypeName = "Team"

const ghostName = "ghost"

// parseReviewers parses given Reviews and ReviewRequests
Expand All @@ -317,10 +315,7 @@ func parseReviewers(pr api.PullRequest) []*reviewerState {

// Overwrite reviewer's state if a review request for the same reviewer exists.
for _, reviewRequest := range pr.ReviewRequests.Nodes {
name := reviewRequest.RequestedReviewer.Login
if reviewRequest.RequestedReviewer.TypeName == teamTypeName {
name = reviewRequest.RequestedReviewer.Name
}
name := reviewRequest.RequestedReviewer.LoginOrSlug()
reviewerStates[name] = &reviewerState{
Name: name,
State: requestedReviewState,
Expand Down
4 changes: 2 additions & 2 deletions pkg/cmd/pr/view/view_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ func TestPRView_Preview_nontty(t *testing.T) {
`milestone:\t\n`,
`additions:\t100\n`,
`deletions:\t10\n`,
`reviewers:\tDEF \(Commented\), def \(Changes requested\), ghost \(Approved\), hubot \(Commented\), xyz \(Approved\), 123 \(Requested\), Team 1 \(Requested\), abc \(Requested\)\n`,
`reviewers:\tDEF \(Commented\), def \(Changes requested\), ghost \(Approved\), hubot \(Commented\), xyz \(Approved\), 123 \(Requested\), abc \(Requested\), my-org\/team-1 \(Requested\)\n`,
`\*\*blueberries taste good\*\*`,
},
},
Expand Down Expand Up @@ -383,7 +383,7 @@ func TestPRView_Preview(t *testing.T) {
},
expectedOutputs: []string{
`Blueberries are from a fork`,
`Reviewers:.*DEF \(.*Commented.*\), def \(.*Changes requested.*\), ghost \(.*Approved.*\), hubot \(Commented\), xyz \(.*Approved.*\), 123 \(.*Requested.*\), Team 1 \(.*Requested.*\), abc \(.*Requested.*\)\n`,
`Reviewers: DEF \(Commented\), def \(Changes requested\), ghost \(Approved\), hubot \(Commented\), xyz \(Approved\), 123 \(Requested\), abc \(Requested\), my-org\/team-1 \(Requested\)`,
`blueberries taste good`,
`View this pull request on GitHub: https://github.com/OWNER/REPO/pull/12`,
},
Expand Down

0 comments on commit 4960935

Please sign in to comment.