X Tutup
Skip to content

Commit b9b1079

Browse files
committed
Display reviews when viewing pull requests
1 parent dcf5a27 commit b9b1079

17 files changed

+994
-359
lines changed

api/queries_comments.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,3 +132,51 @@ func CommentCreate(client *Client, repoHost string, params CommentCreateInput) (
132132

133133
return mutation.AddComment.CommentEdge.Node.URL, nil
134134
}
135+
136+
func commentsFragment() string {
137+
return `comments(last: 1) {
138+
nodes {
139+
author {
140+
login
141+
}
142+
authorAssociation
143+
body
144+
createdAt
145+
includesCreatedEdit
146+
` + reactionGroupsFragment() + `
147+
}
148+
totalCount
149+
}`
150+
}
151+
152+
func (c Comment) AuthorLogin() string {
153+
return c.Author.Login
154+
}
155+
156+
func (c Comment) Association() string {
157+
return c.AuthorAssociation
158+
}
159+
160+
func (c Comment) Content() string {
161+
return c.Body
162+
}
163+
164+
func (c Comment) Created() time.Time {
165+
return c.CreatedAt
166+
}
167+
168+
func (c Comment) IsEdited() bool {
169+
return c.IncludesCreatedEdit
170+
}
171+
172+
func (c Comment) Reactions() ReactionGroups {
173+
return c.ReactionGroups
174+
}
175+
176+
func (c Comment) Status() string {
177+
return ""
178+
}
179+
180+
func (c Comment) Link() string {
181+
return ""
182+
}

api/queries_pr.go

Lines changed: 17 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,6 @@ import (
1515
"github.com/shurcooL/githubv4"
1616
)
1717

18-
type PullRequestReviewState int
19-
20-
const (
21-
ReviewApprove PullRequestReviewState = iota
22-
ReviewRequestChanges
23-
ReviewComment
24-
)
25-
26-
type PullRequestReviewInput struct {
27-
Body string
28-
State PullRequestReviewState
29-
}
30-
3118
type PullRequestsPayload struct {
3219
ViewerCreated PullRequestAndTotalCount
3320
ReviewRequested PullRequestAndTotalCount
@@ -103,14 +90,6 @@ type PullRequest struct {
10390
}
10491
TotalCount int
10592
}
106-
Reviews struct {
107-
Nodes []struct {
108-
Author struct {
109-
Login string
110-
}
111-
State string
112-
}
113-
}
11493
Assignees struct {
11594
Nodes []struct {
11695
Login string
@@ -139,6 +118,7 @@ type PullRequest struct {
139118
}
140119
Comments Comments
141120
ReactionGroups ReactionGroups
121+
Reviews PullRequestReviews
142122
}
143123

144124
type NotFoundError struct {
@@ -220,6 +200,18 @@ func (pr *PullRequest) ChecksStatus() (summary PullRequestChecksStatus) {
220200
return
221201
}
222202

203+
func (pr *PullRequest) DisplayableReviews() PullRequestReviews {
204+
published := []PullRequestReview{}
205+
for _, prr := range pr.Reviews.Nodes {
206+
//Dont display pending reviews
207+
//Dont display commenting reviews without top level comment body
208+
if prr.State != "PENDING" && !(prr.State == "COMMENTED" && prr.Body == "") {
209+
published = append(published, prr)
210+
}
211+
}
212+
return PullRequestReviews{Nodes: published, TotalCount: len(published)}
213+
}
214+
223215
func (c Client) PullRequestDiff(baseRepo ghrepo.Interface, prNumber int) (io.ReadCloser, error) {
224216
url := fmt.Sprintf("%srepos/%s/pulls/%d",
225217
ghinstance.RESTPrefix(baseRepo.RepoHost()), ghrepo.FullName(baseRepo), prNumber)
@@ -570,15 +562,6 @@ func PullRequestByNumber(client *Client, repo ghrepo.Interface, number int) (*Pu
570562
}
571563
totalCount
572564
}
573-
reviews(last: 100) {
574-
nodes {
575-
author {
576-
login
577-
}
578-
state
579-
}
580-
totalCount
581-
}
582565
assignees(first: 100) {
583566
nodes {
584567
login
@@ -605,30 +588,8 @@ func PullRequestByNumber(client *Client, repo ghrepo.Interface, number int) (*Pu
605588
milestone{
606589
title
607590
}
608-
comments(last: 1) {
609-
nodes {
610-
author {
611-
login
612-
}
613-
authorAssociation
614-
body
615-
createdAt
616-
includesCreatedEdit
617-
reactionGroups {
618-
content
619-
users {
620-
totalCount
621-
}
622-
}
623-
}
624-
totalCount
625-
}
626-
reactionGroups {
627-
content
628-
users {
629-
totalCount
630-
}
631-
}
591+
` + commentsFragment() + `
592+
` + reactionGroupsFragment() + `
632593
}
633594
}
634595
}`
@@ -703,15 +664,6 @@ func PullRequestForBranch(client *Client, repo ghrepo.Interface, baseBranch, hea
703664
}
704665
totalCount
705666
}
706-
reviews(last: 100) {
707-
nodes {
708-
author {
709-
login
710-
}
711-
state
712-
}
713-
totalCount
714-
}
715667
assignees(first: 100) {
716668
nodes {
717669
login
@@ -738,30 +690,8 @@ func PullRequestForBranch(client *Client, repo ghrepo.Interface, baseBranch, hea
738690
milestone{
739691
title
740692
}
741-
comments(last: 1) {
742-
nodes {
743-
author {
744-
login
745-
}
746-
authorAssociation
747-
body
748-
createdAt
749-
includesCreatedEdit
750-
reactionGroups {
751-
content
752-
users {
753-
totalCount
754-
}
755-
}
756-
}
757-
totalCount
758-
}
759-
reactionGroups {
760-
content
761-
users {
762-
totalCount
763-
}
764-
}
693+
` + commentsFragment() + `
694+
` + reactionGroupsFragment() + `
765695
}
766696
}
767697
}
@@ -906,34 +836,6 @@ func isBlank(v interface{}) bool {
906836
}
907837
}
908838

909-
func AddReview(client *Client, repo ghrepo.Interface, pr *PullRequest, input *PullRequestReviewInput) error {
910-
var mutation struct {
911-
AddPullRequestReview struct {
912-
ClientMutationID string
913-
} `graphql:"addPullRequestReview(input:$input)"`
914-
}
915-
916-
state := githubv4.PullRequestReviewEventComment
917-
switch input.State {
918-
case ReviewApprove:
919-
state = githubv4.PullRequestReviewEventApprove
920-
case ReviewRequestChanges:
921-
state = githubv4.PullRequestReviewEventRequestChanges
922-
}
923-
924-
body := githubv4.String(input.Body)
925-
variables := map[string]interface{}{
926-
"input": githubv4.AddPullRequestReviewInput{
927-
PullRequestID: pr.ID,
928-
Event: &state,
929-
Body: &body,
930-
},
931-
}
932-
933-
gql := graphQLClient(client.http, repo.RepoHost())
934-
return gql.MutateNamed(context.Background(), "PullRequestReviewAdd", &mutation, variables)
935-
}
936-
937839
func PullRequestList(client *Client, repo ghrepo.Interface, vars map[string]interface{}, limit int) (*PullRequestAndTotalCount, error) {
938840
type prBlock struct {
939841
Edges []struct {

api/queries_pr_review.go

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
package api
2+
3+
import (
4+
"context"
5+
"time"
6+
7+
"github.com/cli/cli/internal/ghrepo"
8+
"github.com/shurcooL/githubv4"
9+
)
10+
11+
type PullRequestReviewState int
12+
13+
const (
14+
ReviewApprove PullRequestReviewState = iota
15+
ReviewRequestChanges
16+
ReviewComment
17+
)
18+
19+
type PullRequestReviewInput struct {
20+
Body string
21+
State PullRequestReviewState
22+
}
23+
24+
type PullRequestReviews struct {
25+
Nodes []PullRequestReview
26+
PageInfo PageInfo
27+
TotalCount int
28+
}
29+
30+
type PullRequestReview struct {
31+
Author Author
32+
AuthorAssociation string
33+
Body string
34+
CreatedAt time.Time
35+
IncludesCreatedEdit bool
36+
ReactionGroups ReactionGroups
37+
State string
38+
URL string
39+
}
40+
41+
func AddReview(client *Client, repo ghrepo.Interface, pr *PullRequest, input *PullRequestReviewInput) error {
42+
var mutation struct {
43+
AddPullRequestReview struct {
44+
ClientMutationID string
45+
} `graphql:"addPullRequestReview(input:$input)"`
46+
}
47+
48+
state := githubv4.PullRequestReviewEventComment
49+
switch input.State {
50+
case ReviewApprove:
51+
state = githubv4.PullRequestReviewEventApprove
52+
case ReviewRequestChanges:
53+
state = githubv4.PullRequestReviewEventRequestChanges
54+
}
55+
56+
body := githubv4.String(input.Body)
57+
variables := map[string]interface{}{
58+
"input": githubv4.AddPullRequestReviewInput{
59+
PullRequestID: pr.ID,
60+
Event: &state,
61+
Body: &body,
62+
},
63+
}
64+
65+
gql := graphQLClient(client.http, repo.RepoHost())
66+
return gql.MutateNamed(context.Background(), "PullRequestReviewAdd", &mutation, variables)
67+
}
68+
69+
func ReviewsForPullRequest(client *Client, repo ghrepo.Interface, pr *PullRequest) (*PullRequestReviews, error) {
70+
type response struct {
71+
Repository struct {
72+
PullRequest struct {
73+
Reviews PullRequestReviews `graphql:"reviews(first: 100, after: $endCursor)"`
74+
} `graphql:"pullRequest(number: $number)"`
75+
} `graphql:"repository(owner: $owner, name: $repo)"`
76+
}
77+
78+
variables := map[string]interface{}{
79+
"owner": githubv4.String(repo.RepoOwner()),
80+
"repo": githubv4.String(repo.RepoName()),
81+
"number": githubv4.Int(pr.Number),
82+
"endCursor": (*githubv4.String)(nil),
83+
}
84+
85+
gql := graphQLClient(client.http, repo.RepoHost())
86+
87+
var reviews []PullRequestReview
88+
for {
89+
var query response
90+
err := gql.QueryNamed(context.Background(), "ReviewsForPullRequest", &query, variables)
91+
if err != nil {
92+
return nil, err
93+
}
94+
95+
reviews = append(reviews, query.Repository.PullRequest.Reviews.Nodes...)
96+
if !query.Repository.PullRequest.Reviews.PageInfo.HasNextPage {
97+
break
98+
}
99+
variables["endCursor"] = githubv4.String(query.Repository.PullRequest.Reviews.PageInfo.EndCursor)
100+
}
101+
102+
return &PullRequestReviews{Nodes: reviews, TotalCount: len(reviews)}, nil
103+
}
104+
105+
func (prr PullRequestReview) AuthorLogin() string {
106+
return prr.Author.Login
107+
}
108+
109+
func (prr PullRequestReview) Association() string {
110+
return prr.AuthorAssociation
111+
}
112+
113+
func (prr PullRequestReview) Content() string {
114+
return prr.Body
115+
}
116+
117+
func (prr PullRequestReview) Created() time.Time {
118+
return prr.CreatedAt
119+
}
120+
121+
func (prr PullRequestReview) IsEdited() bool {
122+
return prr.IncludesCreatedEdit
123+
}
124+
125+
func (prr PullRequestReview) Reactions() ReactionGroups {
126+
return prr.ReactionGroups
127+
}
128+
129+
func (prr PullRequestReview) Status() string {
130+
return prr.State
131+
}
132+
133+
func (prr PullRequestReview) Link() string {
134+
return prr.URL
135+
}

api/reaction_groups.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,12 @@ var reactionEmoji = map[string]string{
2929
"ROCKET": "\U0001f680",
3030
"EYES": "\U0001f440",
3131
}
32+
33+
func reactionGroupsFragment() string {
34+
return `reactionGroups {
35+
content
36+
users {
37+
totalCount
38+
}
39+
}`
40+
}

0 commit comments

Comments
 (0)
X Tutup