Repository navigation
Expand file tree
/
Copy pathpull_request_comments.v
More file actions
197 lines (181 loc) · 7.42 KB
/
Copy pathpull_request_comments.v
File metadata and controls
197 lines (181 loc) · 7.42 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
module main
import json2
import time
const review_comment_fields = 'id body createdAt path line originalLine url author { login } ' + reaction_group_fields
const pull_request_discussion_fields = r'
totalCommentsCount
comments(first: $first, after: $cursor) @include(if: $loadComments) {
totalCount
pageInfo { hasNextPage endCursor }
nodes { ' + issue_comment_fields + ' }
}
reviews(first: $first, after: $reviewCursor, states: [APPROVED, CHANGES_REQUESTED, COMMENTED, DISMISSED]) @include(if: $loadReviews) {
pageInfo { hasNextPage endCursor }
nodes {
id body submittedAt state author { login }
' + reaction_group_fields + '
comments(first: 100) {
pageInfo { hasNextPage endCursor }
nodes { ' + review_comment_fields + ' }
}
}
}'
const pull_request_comments_query = r'query PullRequestComments($owner: String!, $name: String!, $number: Int!, $cursor: String, $reviewCursor: String, $first: Int!, $loadComments: Boolean!, $loadReviews: Boolean!) {
repository(owner: $owner, name: $name) {
issue: pullRequest(number: $number) {
__typename number' + pull_request_discussion_fields + '
}
}
}'
const pull_request_detail_query = r'query PullRequestDetail($owner: String!, $name: String!, $number: Int!, $cursor: String, $reviewCursor: String, $first: Int!, $loadComments: Boolean!, $loadReviews: Boolean!) {
repository(owner: $owner, name: $name) {
issue: pullRequest(number: $number) {
__typename number title body url state isDraft mergeable createdAt updatedAt
baseRefName headRefName headRefOid additions deletions changedFiles
mergedAt mergedBy { login }
author { login }
labels(first: 20) { nodes { name color } }
assignees(first: 20) { nodes { login } }
milestone { title }
' + reaction_group_fields + pull_request_discussion_fields + '
}
}
}'
const review_comments_query = r'query ReviewComments($review: ID!, $cursor: String!) {
node(id: $review) {
... on PullRequestReview {
comments(first: 100, after: $cursor) {
pageInfo { hasNextPage endCursor }
nodes { ' + review_comment_fields + ' }
}
}
}
}'
// The two connections have unrelated cursors and can finish on different pages.
struct PrDiscussionCursor {
comments string
reviews string
comments_done bool
reviews_done bool
}
struct ReviewCommentPage {
review string
cursor string
}
fn pr_discussion_cursor(source string) PrDiscussionCursor {
if source == '' { return PrDiscussionCursor{} }
return json2.decode[PrDiscussionCursor](source) or { PrDiscussionCursor{ comments: source } }
}
fn decode_pr_discussion(item json2.Any, detail IssueDetail) !IssueDetail {
reviews := json_field(item, 'reviews')
mut comments := detail.comments.clone()
mut pages := []ReviewCommentPage{}
for review in json_array(json_field(reviews, 'nodes')) {
id := json_text(json_field(review, 'id'))
if id == '' { return error('GitHub returned a review without an ID.') }
body := json_text(json_field(review, 'body'))
reaction_groups := decode_reaction_groups(review)
// A blank review is a container for inline comments, not a blank card.
if body.trim_space() != '' || reaction_groups.len > 0 {
comments << IssueComment{
id: id
body: body
created_at: json_text(json_field(review, 'submittedAt'))
author: User{ login: json_text(json_field(json_field(review, 'author'), 'login')) }
kind: .review
state: json_text(json_field(review, 'state'))
reaction_groups: reaction_groups
}
}
connection := json_field(review, 'comments')
if 'nodes' !in json_object(connection) {
return error('GitHub’s response did not contain review comments.')
}
comments = merge_comments(comments, decode_comment_nodes(connection, .inline_review)!)
page_info := json_field(connection, 'pageInfo')
if json_field(page_info, 'hasNextPage').bool() {
cursor := json_text(json_field(page_info, 'endCursor'))
if cursor == '' {
return error('GitHub returned review comments without a pagination cursor.')
}
pages << ReviewCommentPage{ review: id, cursor: cursor }
}
}
review_info := json_field(reviews, 'pageInfo')
cursor := PrDiscussionCursor{
comments: detail.cursor
comments_done: !detail.has_next
reviews: json_text(json_field(review_info, 'endCursor'))
reviews_done: !json_field(review_info, 'hasNextPage').bool()
}
return IssueDetail{
...detail
comments: sort_discussion_comments(comments)
has_next: !cursor.comments_done || !cursor.reviews_done
cursor: json2.encode(cursor)
source_count: detail.source_count + json_array(json_field(reviews, 'nodes')).len
review_pages: pages
}
}
fn review_comments_args(page ReviewCommentPage) []string {
return ['api', '--hostname', 'github.com', 'graphql', '-f', 'query=${review_comments_query}',
'-f', 'review=${page.review}', '-f', 'cursor=${page.cursor}']
}
fn fetch_pr_review_comments(request DetailRequest, detail IssueDetail) !IssueDetail {
mut comments := detail.comments.clone()
for pending in detail.review_pages {
mut page := pending
for {
response := decode_detail_response(run_gh_with_cancel(review_comments_args(page), request.cancel)!)!
connection := json_field(json_field(json_field(response, 'data'), 'node'), 'comments')
if 'nodes' !in json_object(connection) {
return error('GitHub’s response did not contain review comments.')
}
comments = merge_comments(comments, decode_comment_nodes(connection, .inline_review)!)
info := json_field(connection, 'pageInfo')
if !json_field(info, 'hasNextPage').bool() { break }
cursor := json_text(json_field(info, 'endCursor'))
if cursor == '' || cursor == page.cursor {
return error('GitHub’s review comment pagination did not advance.')
}
page = ReviewCommentPage{ ...page, cursor: cursor }
}
}
return IssueDetail{ ...detail, comments: sort_discussion_comments(comments), review_pages: []ReviewCommentPage{} }
}
fn sort_discussion_comments(source []IssueComment) []IssueComment {
mut comments := source.clone()
comments.sort_with_compare(compare_discussion_comments)
return comments
}
fn compare_discussion_comments(a &IssueComment, b &IssueComment) int {
if a.created_at < b.created_at { return -1 }
if a.created_at > b.created_at { return 1 }
// Put a review summary before inline comments submitted at the same instant.
if a.kind == .review && b.kind != .review { return -1 }
if b.kind == .review && a.kind != .review { return 1 }
if a.id < b.id { return -1 }
if a.id > b.id { return 1 }
return 0
}
fn (comment IssueComment) action(now time.Time) string {
action := if comment.kind == .review {
match comment.state {
'APPROVED' { 'approved' }
'CHANGES_REQUESTED' { 'requested changes' }
'DISMISSED' { 'reviewed (dismissed)' }
else { 'reviewed' }
}
} else {
'commented'
}
return '${action} ${relative_time(comment.created_at, now)}'
}
fn (comment IssueComment) display_body() string {
body := if comment.body.trim_space() == '' { 'This comment has no text.' } else { comment.body }
if comment.kind != .inline_review || comment.path == '' { return body }
location := comment.path + if comment.line > 0 { ':${comment.line}' } else { '' }
label := location.replace('\\', '\\\\').replace('[', '\\[').replace(']', '\\]')
context := if comment.url != '' { '[${label}](${comment.url})' } else { label }
return 'Comment on ${context}\n\n${body}'
}