-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpull_requests.go
More file actions
371 lines (322 loc) · 9.59 KB
/
pull_requests.go
File metadata and controls
371 lines (322 loc) · 9.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
package main
import (
"context"
"fmt"
"github.com/google/go-github/v71/github"
"github.com/shurcooL/githubv4"
)
const (
reviewThreadPageSize = 50
threadCommentPageSize = 50
)
// FetchPullRequestReviews returns a map keyed by pull request ID containing all reviews for each PR.
func (l *GithubReposPlugin) FetchPullRequestReviews(ctx context.Context, repo *github.Repository, prs []*github.PullRequest) (map[int64][]*github.PullRequestReview, error) {
reviewsByPR := make(map[int64][]*github.PullRequestReview, len(prs))
if repo == nil {
return reviewsByPR, nil
}
owner := repo.GetOwner().GetLogin()
name := repo.GetName()
for _, pr := range prs {
if pr == nil {
continue
}
prKey := pullRequestKey(pr)
opts := &github.ListOptions{PerPage: 100, Page: 1}
var reviews []*github.PullRequestReview
for {
batch, resp, err := l.githubClient.PullRequests.ListReviews(ctx, owner, name, pr.GetNumber(), opts)
if err != nil {
return nil, err
}
reviews = append(reviews, batch...)
if resp == nil || resp.NextPage == 0 {
break
}
opts.Page = resp.NextPage
}
reviewsByPR[prKey] = reviews
}
return reviewsByPR, nil
}
// ProcessOpenPullRequests nests reviews and their threads beneath each pull request.
func ProcessOpenPullRequests(prs []*github.PullRequest, reviews map[int64][]*github.PullRequestReview, reviewThreads map[int64][]*PullRequestReviewThread) []*OpenPullRequest {
openPRs := make([]*OpenPullRequest, 0, len(prs))
for _, pr := range prs {
if pr == nil {
continue
}
prKey := pullRequestKey(pr)
reviewList := reviews[prKey]
openReviews := make([]*PullRequestReview, 0, len(reviewList))
for _, review := range reviewList {
if review == nil {
continue
}
reviewID := review.GetID()
openReviews = append(openReviews, &PullRequestReview{
PullRequestReview: review,
Threads: reviewThreads[reviewID],
})
}
openPRs = append(openPRs, &OpenPullRequest{
PullRequest: pr,
Reviews: openReviews,
})
}
return openPRs
}
// GatherReviewsAndComments orchestrates fetching reviews and comments before producing the enriched slice.
func (l *GithubReposPlugin) GatherReviewsAndComments(ctx context.Context, repo *github.Repository, prs []*github.PullRequest) ([]*OpenPullRequest, error) {
if len(prs) == 0 {
return nil, nil
}
reviewsByPR, err := l.FetchPullRequestReviews(ctx, repo, prs)
if err != nil {
l.Logger.Error("failed to fetch pull request reviews", "error", err)
return ProcessOpenPullRequests(prs, nil, nil), nil
}
threadsByReview, err := l.FetchReviewThreads(ctx, prs, reviewsByPR)
if err != nil {
l.Logger.Error("failed to fetch pull request review threads", "error", err)
return ProcessOpenPullRequests(prs, reviewsByPR, nil), nil
}
return ProcessOpenPullRequests(prs, reviewsByPR, threadsByReview), nil
}
func pullRequestKey(pr *github.PullRequest) int64 {
if pr == nil {
return 0
}
if id := pr.GetID(); id != 0 {
return id
}
return int64(pr.GetNumber())
}
func (l *GithubReposPlugin) FetchReviewThreads(ctx context.Context, prs []*github.PullRequest, reviewsByPR map[int64][]*github.PullRequestReview) (map[int64][]*PullRequestReviewThread, error) {
threadsByReview := make(map[int64][]*PullRequestReviewThread)
if l.graphqlClient == nil {
return threadsByReview, nil
}
for _, pr := range prs {
if pr == nil {
continue
}
prNodeID := pr.GetNodeID()
if prNodeID == "" {
continue
}
prKey := pullRequestKey(pr)
reviewList := reviewsByPR[prKey]
if len(reviewList) == 0 {
continue
}
reviewNodeToID := make(map[string]int64, len(reviewList))
for _, review := range reviewList {
if review == nil {
continue
}
nodeID := review.GetNodeID()
if nodeID == "" {
continue
}
reviewNodeToID[nodeID] = review.GetID()
}
if len(reviewNodeToID) == 0 {
continue
}
threadList, err := l.fetchThreadsForPullRequest(ctx, prNodeID)
if err != nil {
return nil, err
}
if len(threadList) == 0 {
continue
}
for _, thread := range threadList {
if thread == nil {
continue
}
reviewsForThread := make(map[int64]struct{})
for _, comment := range thread.Comments {
if comment == nil || comment.ReviewID == "" {
continue
}
if reviewID, ok := reviewNodeToID[comment.ReviewID]; ok {
reviewsForThread[reviewID] = struct{}{}
}
}
if len(reviewsForThread) == 0 {
continue
}
for reviewID := range reviewsForThread {
threadsByReview[reviewID] = append(threadsByReview[reviewID], thread)
}
}
}
return threadsByReview, nil
}
func (l *GithubReposPlugin) fetchThreadsForPullRequest(ctx context.Context, prNodeID string) ([]*PullRequestReviewThread, error) {
var threads []*PullRequestReviewThread
var threadCursor *githubv4.String
for {
var query struct {
Node struct {
PullRequest struct {
ReviewThreads struct {
Nodes []reviewThreadNode
PageInfo struct {
HasNextPage githubv4.Boolean
EndCursor githubv4.String
}
} `graphql:"reviewThreads(first: $threadsFirst, after: $threadsCursor)"`
} `graphql:"... on PullRequest"`
} `graphql:"node(id: $pullRequestID)"`
}
variables := map[string]interface{}{
"pullRequestID": githubv4.ID(prNodeID),
"threadsFirst": githubv4.Int(reviewThreadPageSize),
"threadsCursor": threadCursor,
"commentsFirst": githubv4.Int(threadCommentPageSize),
}
if err := l.graphqlClient.Query(ctx, &query, variables); err != nil {
return nil, err
}
threadConnection := query.Node.PullRequest.ReviewThreads
for _, node := range threadConnection.Nodes {
threadNode := node
if bool(node.Comments.PageInfo.HasNextPage) {
additional, err := l.fetchAdditionalThreadComments(ctx, node.ID, node.Comments.PageInfo.EndCursor)
if err != nil {
return nil, err
}
threadNode.Comments.Nodes = append(threadNode.Comments.Nodes, additional...)
}
threads = append(threads, convertGraphQLThread(threadNode))
}
if !bool(threadConnection.PageInfo.HasNextPage) {
break
}
next := threadConnection.PageInfo.EndCursor
threadCursor = new(githubv4.String)
*threadCursor = next
}
return threads, nil
}
func (l *GithubReposPlugin) fetchAdditionalThreadComments(ctx context.Context, threadID githubv4.ID, startCursor githubv4.String) ([]reviewThreadCommentNode, error) {
var allComments []reviewThreadCommentNode
cursor := new(githubv4.String)
*cursor = startCursor
for {
var query struct {
Node struct {
PullRequestReviewThread struct {
Comments struct {
Nodes []reviewThreadCommentNode
PageInfo struct {
HasNextPage githubv4.Boolean
EndCursor githubv4.String
}
} `graphql:"comments(first: $commentsFirst, after: $commentsCursor)"`
} `graphql:"... on PullRequestReviewThread"`
} `graphql:"node(id: $threadID)"`
}
variables := map[string]interface{}{
"threadID": threadID,
"commentsFirst": githubv4.Int(threadCommentPageSize),
"commentsCursor": cursor,
}
if err := l.graphqlClient.Query(ctx, &query, variables); err != nil {
return nil, err
}
commentConnection := query.Node.PullRequestReviewThread.Comments
allComments = append(allComments, commentConnection.Nodes...)
if !bool(commentConnection.PageInfo.HasNextPage) {
break
}
next := commentConnection.PageInfo.EndCursor
cursor = new(githubv4.String)
*cursor = next
}
return allComments, nil
}
type reviewThreadNode struct {
ID githubv4.ID
IsResolved githubv4.Boolean
Comments struct {
Nodes []reviewThreadCommentNode
PageInfo struct {
HasNextPage githubv4.Boolean
EndCursor githubv4.String
}
} `graphql:"comments(first: $commentsFirst)"`
}
type reviewThreadCommentNode struct {
ID githubv4.ID
Body githubv4.String
URL githubv4.URI
Author struct{ Login githubv4.String } `graphql:"author"`
DiffHunk githubv4.String
Path githubv4.String
Position *githubv4.Int
OriginalPosition *githubv4.Int
ReplyTo *struct {
ID githubv4.ID
} `graphql:"replyTo"`
PullRequestReview *struct {
ID githubv4.ID
} `graphql:"pullRequestReview"`
CreatedAt githubv4.DateTime
UpdatedAt githubv4.DateTime
}
func convertGraphQLThread(node reviewThreadNode) *PullRequestReviewThread {
thread := &PullRequestReviewThread{
ID: idToString(node.ID),
IsResolved: bool(node.IsResolved),
Comments: make([]*PullRequestReviewThreadComment, 0, len(node.Comments.Nodes)),
}
for _, commentNode := range node.Comments.Nodes {
thread.Comments = append(thread.Comments, convertGraphQLThreadComment(commentNode))
}
return thread
}
func convertGraphQLThreadComment(node reviewThreadCommentNode) *PullRequestReviewThreadComment {
comment := &PullRequestReviewThreadComment{
ID: idToString(node.ID),
Body: string(node.Body),
URL: node.URL.String(),
DiffHunk: string(node.DiffHunk),
Path: string(node.Path),
}
if node.Position != nil {
pos := int(*node.Position)
comment.Position = &pos
}
if node.OriginalPosition != nil {
pos := int(*node.OriginalPosition)
comment.OriginalPosition = &pos
}
if node.ReplyTo != nil {
if replyTo := idToString(node.ReplyTo.ID); replyTo != "" {
comment.ReplyToID = replyTo
}
}
if node.PullRequestReview != nil {
comment.ReviewID = idToString(node.PullRequestReview.ID)
}
created := node.CreatedAt.Time
comment.CreatedAt = &created
updated := node.UpdatedAt.Time
comment.UpdatedAt = &updated
return comment
}
func idToString(id githubv4.ID) string {
switch v := id.(type) {
case nil:
return ""
case string:
return v
case fmt.Stringer:
return v.String()
default:
return fmt.Sprintf("%v", v)
}
}