X Tutup
Skip to content

Commit e758f30

Browse files
committed
Fix preloading of pr reviews, checks, and issue/pr comments
1 parent 51f7cbd commit e758f30

21 files changed

+383
-196
lines changed

api/export_pr.go

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,34 @@ func (pr *PullRequest) ExportData(fields []string) *map[string]interface{} {
4949
data[f] = nil
5050
}
5151
case "statusCheckRollup":
52-
if n := pr.Commits.Nodes; len(n) > 0 {
52+
if n := pr.StatusCheckRollup.Nodes; len(n) > 0 {
5353
data[f] = n[0].Commit.StatusCheckRollup.Contexts.Nodes
5454
} else {
5555
data[f] = nil
5656
}
57+
case "commits":
58+
commits := make([]interface{}, 0, len(pr.Commits.Nodes))
59+
for _, c := range pr.Commits.Nodes {
60+
commit := c.Commit
61+
authors := make([]interface{}, 0, len(commit.Authors.Nodes))
62+
for _, author := range commit.Authors.Nodes {
63+
authors = append(authors, map[string]interface{}{
64+
"name": author.Name,
65+
"email": author.Email,
66+
"id": author.User.ID,
67+
"login": author.User.Login,
68+
})
69+
}
70+
commits = append(commits, map[string]interface{}{
71+
"oid": commit.OID,
72+
"messageHeadline": commit.MessageHeadline,
73+
"messageBody": commit.MessageBody,
74+
"committedDate": commit.CommittedDate,
75+
"authoredDate": commit.AuthoredDate,
76+
"authors": authors,
77+
})
78+
}
79+
data[f] = commits
5780
case "comments":
5881
data[f] = pr.Comments.Nodes
5982
case "assignees":

api/export_pr_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ func TestPullRequest_ExportData(t *testing.T) {
129129
name: "status checks",
130130
fields: []string{"statusCheckRollup"},
131131
inputJSON: heredoc.Doc(`
132-
{ "commits": { "nodes": [
132+
{ "statusCheckRollup": { "nodes": [
133133
{ "commit": { "statusCheckRollup": { "contexts": { "nodes": [
134134
{
135135
"__typename": "CheckRun",

api/pull_request_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
func TestPullRequest_ChecksStatus(t *testing.T) {
1111
pr := PullRequest{}
1212
payload := `
13-
{ "commits": { "nodes": [{ "commit": {
13+
{ "statusCheckRollup": { "nodes": [{ "commit": {
1414
"statusCheckRollup": {
1515
"contexts": {
1616
"nodes": [

api/queries_comments.go

Lines changed: 4 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,17 @@ import (
44
"context"
55
"time"
66

7-
"github.com/cli/cli/internal/ghrepo"
87
"github.com/shurcooL/githubv4"
98
"github.com/shurcooL/graphql"
109
)
1110

1211
type Comments struct {
1312
Nodes []Comment
1413
TotalCount int
15-
PageInfo PageInfo
14+
PageInfo struct {
15+
HasNextPage bool
16+
EndCursor string
17+
}
1618
}
1719

1820
type Comment struct {
@@ -26,83 +28,6 @@ type Comment struct {
2628
ReactionGroups ReactionGroups `json:"reactionGroups"`
2729
}
2830

29-
type PageInfo struct {
30-
HasNextPage bool
31-
EndCursor string
32-
}
33-
34-
func CommentsForIssue(client *Client, repo ghrepo.Interface, issue *Issue) (*Comments, error) {
35-
type response struct {
36-
Repository struct {
37-
Issue struct {
38-
Comments Comments `graphql:"comments(first: 100, after: $endCursor)"`
39-
} `graphql:"issue(number: $number)"`
40-
} `graphql:"repository(owner: $owner, name: $repo)"`
41-
}
42-
43-
variables := map[string]interface{}{
44-
"owner": githubv4.String(repo.RepoOwner()),
45-
"repo": githubv4.String(repo.RepoName()),
46-
"number": githubv4.Int(issue.Number),
47-
"endCursor": (*githubv4.String)(nil),
48-
}
49-
50-
gql := graphQLClient(client.http, repo.RepoHost())
51-
52-
var comments []Comment
53-
for {
54-
var query response
55-
err := gql.QueryNamed(context.Background(), "CommentsForIssue", &query, variables)
56-
if err != nil {
57-
return nil, err
58-
}
59-
60-
comments = append(comments, query.Repository.Issue.Comments.Nodes...)
61-
if !query.Repository.Issue.Comments.PageInfo.HasNextPage {
62-
break
63-
}
64-
variables["endCursor"] = githubv4.String(query.Repository.Issue.Comments.PageInfo.EndCursor)
65-
}
66-
67-
return &Comments{Nodes: comments, TotalCount: len(comments)}, nil
68-
}
69-
70-
func CommentsForPullRequest(client *Client, repo ghrepo.Interface, pr *PullRequest) (*Comments, error) {
71-
type response struct {
72-
Repository struct {
73-
PullRequest struct {
74-
Comments Comments `graphql:"comments(first: 100, after: $endCursor)"`
75-
} `graphql:"pullRequest(number: $number)"`
76-
} `graphql:"repository(owner: $owner, name: $repo)"`
77-
}
78-
79-
variables := map[string]interface{}{
80-
"owner": githubv4.String(repo.RepoOwner()),
81-
"repo": githubv4.String(repo.RepoName()),
82-
"number": githubv4.Int(pr.Number),
83-
"endCursor": (*githubv4.String)(nil),
84-
}
85-
86-
gql := graphQLClient(client.http, repo.RepoHost())
87-
88-
var comments []Comment
89-
for {
90-
var query response
91-
err := gql.QueryNamed(context.Background(), "CommentsForPullRequest", &query, variables)
92-
if err != nil {
93-
return nil, err
94-
}
95-
96-
comments = append(comments, query.Repository.PullRequest.Comments.Nodes...)
97-
if !query.Repository.PullRequest.Comments.PageInfo.HasNextPage {
98-
break
99-
}
100-
variables["endCursor"] = githubv4.String(query.Repository.PullRequest.Comments.PageInfo.EndCursor)
101-
}
102-
103-
return &Comments{Nodes: comments, TotalCount: len(comments)}, nil
104-
}
105-
10631
type CommentCreateInput struct {
10732
Body string
10833
SubjectId string

api/queries_pr.go

Lines changed: 41 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,33 @@ type PullRequest struct {
7575
TotalCount int
7676
Nodes []PullRequestCommit
7777
}
78+
StatusCheckRollup struct {
79+
Nodes []struct {
80+
Commit struct {
81+
StatusCheckRollup struct {
82+
Contexts struct {
83+
Nodes []struct {
84+
TypeName string `json:"__typename"`
85+
Name string `json:"name"`
86+
Context string `json:"context,omitempty"`
87+
State string `json:"state,omitempty"`
88+
Status string `json:"status"`
89+
Conclusion string `json:"conclusion"`
90+
StartedAt time.Time `json:"startedAt"`
91+
CompletedAt time.Time `json:"completedAt"`
92+
DetailsURL string `json:"detailsUrl"`
93+
TargetURL string `json:"targetUrl,omitempty"`
94+
}
95+
PageInfo struct {
96+
HasNextPage bool
97+
EndCursor string
98+
}
99+
}
100+
}
101+
}
102+
}
103+
}
104+
78105
Assignees Assignees
79106
Labels Labels
80107
ProjectCards ProjectCards
@@ -89,6 +116,7 @@ type PRRepository struct {
89116
Name string
90117
}
91118

119+
// Commit loads just the commit SHA and nothing else
92120
type Commit struct {
93121
OID string `json:"oid"`
94122
}
@@ -97,25 +125,20 @@ type PullRequestCommit struct {
97125
Commit PullRequestCommitCommit
98126
}
99127

100-
// PullRequestCommitCommit is like "Commit" but with StatusCheckRollup
128+
// PullRequestCommitCommit contains full information about a commit
101129
type PullRequestCommitCommit struct {
102-
Oid string
103-
StatusCheckRollup struct {
104-
Contexts struct {
105-
Nodes []struct {
106-
TypeName string `json:"__typename"`
107-
Name string `json:"name"`
108-
Context string `json:"context,omitempty"`
109-
State string `json:"state,omitempty"`
110-
Status string `json:"status"`
111-
Conclusion string `json:"conclusion"`
112-
StartedAt time.Time `json:"startedAt"`
113-
CompletedAt time.Time `json:"completedAt"`
114-
DetailsURL string `json:"detailsUrl"`
115-
TargetURL string `json:"targetUrl,omitempty"`
116-
}
130+
OID string `json:"oid"`
131+
Authors struct {
132+
Nodes []struct {
133+
Name string
134+
Email string
135+
User GitHubUser
117136
}
118137
}
138+
MessageHeadline string
139+
MessageBody string
140+
CommittedDate time.Time
141+
AuthoredDate time.Time
119142
}
120143

121144
type PullRequestFile struct {
@@ -132,7 +155,6 @@ type ReviewRequests struct {
132155
Name string `json:"name"`
133156
}
134157
}
135-
TotalCount int
136158
}
137159

138160
func (r ReviewRequests) Logins() []string {
@@ -189,10 +211,10 @@ type PullRequestChecksStatus struct {
189211
}
190212

191213
func (pr *PullRequest) ChecksStatus() (summary PullRequestChecksStatus) {
192-
if len(pr.Commits.Nodes) == 0 {
214+
if len(pr.StatusCheckRollup.Nodes) == 0 {
193215
return
194216
}
195-
commit := pr.Commits.Nodes[0].Commit
217+
commit := pr.StatusCheckRollup.Nodes[0].Commit
196218
for _, c := range commit.StatusCheckRollup.Contexts.Nodes {
197219
state := c.State // StatusContext
198220
if state == "" {

api/queries_pr_review.go

Lines changed: 5 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,11 @@ type PullRequestReviewInput struct {
2222
}
2323

2424
type PullRequestReviews struct {
25-
Nodes []PullRequestReview
26-
PageInfo PageInfo
25+
Nodes []PullRequestReview
26+
PageInfo struct {
27+
HasNextPage bool
28+
EndCursor string
29+
}
2730
TotalCount int
2831
}
2932

@@ -66,42 +69,6 @@ func AddReview(client *Client, repo ghrepo.Interface, pr *PullRequest, input *Pu
6669
return gql.MutateNamed(context.Background(), "PullRequestReviewAdd", &mutation, variables)
6770
}
6871

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-
10572
func (prr PullRequestReview) AuthorLogin() string {
10673
return prr.Author.Login
10774
}

0 commit comments

Comments
 (0)
X Tutup