-
Notifications
You must be signed in to change notification settings - Fork 4
/
review.go
632 lines (578 loc) · 15.8 KB
/
review.go
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
package main
import (
"bufio"
"bytes"
"context"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"os/exec"
"regexp"
"sort"
"strconv"
"strings"
"sync"
"text/tabwriter"
"time"
"github.com/fatih/color"
"github.com/google/go-github/github"
)
type commitComments map[string]fileComments
type fileComments map[string]lineComments
type lineComments map[int][]*github.PullRequestComment
func (c commitComments) get(commit string, file string, line int) []*github.PullRequestComment {
if files, ok := c[commit]; ok {
if lines, ok := files[file]; ok {
if comments, ok := lines[line]; ok {
return comments
}
}
}
return nil
}
func (c commitComments) put(comment *github.PullRequestComment) {
commit := *comment.CommitID
file := *comment.Path
if comment.Position == nil {
// Outdated comment
return
}
line := *comment.Position
if _, ok := c[commit]; !ok {
c[commit] = make(fileComments)
}
if _, ok := c[commit][file]; !ok {
c[commit][file] = make(lineComments)
}
c[commit][file][line] = append(c[commit][file][line], comment)
}
// topLevelComment represents either a review comment or an issue comment.
type topLevelComment struct {
body string
author string
createdAt time.Time
// Only for reviews
state string
commitID string
}
type topLevelComments []topLevelComment
func (c topLevelComments) Len() int { return len(c) }
func (c topLevelComments) Swap(i, j int) { c[i], c[j] = c[j], c[i] }
func (c topLevelComments) Less(i, j int) bool { return c[i].createdAt.Before(c[j].createdAt) }
func makeReviewTemplate(ctx context.Context, n int) string {
log.Printf("Fetching details for PR %d", n)
var wg sync.WaitGroup
wg.Add(6)
var pr *github.PullRequest
go func() {
start := time.Now()
var err error
pr, _, err = client.PullRequests.Get(ctx, projectOwner, projectRepo, n)
if err != nil {
log.Fatal(fmt.Errorf("getting pr: %v", err))
}
wg.Done()
log.Printf("Fetched pr in %v", time.Now().Sub(start))
}()
var diffStat strings.Builder
writer := tabwriter.NewWriter(&diffStat, 10, 4, 4, ' ', 0)
go func() {
opt := &github.ListOptions{PerPage: 200}
for {
files, resp, err := client.PullRequests.ListFiles(ctx, projectOwner, projectRepo, n, opt)
if err != nil {
log.Fatal(fmt.Errorf("getting pr files: %v", err))
}
for _, file := range files {
fmt.Fprintf(writer, "%s\t\t+%d\t-%d\n", file.GetFilename(), file.GetAdditions(), file.GetDeletions())
}
opt.Page = resp.NextPage
if opt.Page == 0 {
break
}
}
if err := writer.Flush(); err != nil {
panic(err)
}
wg.Done()
}()
diffBuf := bytes.NewBuffer(make([]byte, 0, 1024))
go func() {
commits, _, err := client.PullRequests.ListCommits(ctx, projectOwner, projectRepo, n, &github.ListOptions{})
if err != nil {
log.Fatal(fmt.Errorf("getting pr commits: %v", err))
}
for _, ghCommit := range commits {
raw, _, err := client.Repositories.GetCommitRaw(ctx, projectOwner, projectRepo, ghCommit.GetSHA(),
github.RawOptions{Type: github.Diff},
)
if err != nil {
log.Fatal(fmt.Errorf("getting pr commits: %v", err))
}
commit := ghCommit.GetCommit()
fmt.Fprintf(diffBuf, `
commit %s
Author: %s <%s>
Date: %s
`,
ghCommit.GetSHA(),
commit.GetAuthor().GetName(),
commit.GetAuthor().GetEmail(),
commit.Author.GetDate().Format(time.RubyDate),
)
message := commit.GetMessage()
for _, line := range strings.Split(message, "\n") {
diffBuf.WriteString(" ")
diffBuf.WriteString(line)
diffBuf.WriteByte('\n')
}
diffBuf.WriteByte('\n')
diffBuf.WriteString(raw)
}
wg.Done()
}()
reviews := make([]*github.PullRequestReview, 0, 10)
go func() {
start := time.Now()
for page := 1; ; {
list, resp, err := client.PullRequests.ListReviews(ctx, projectOwner, projectRepo, n, &github.ListOptions{
Page: page,
PerPage: 100,
})
if err != nil {
log.Fatal(fmt.Errorf("invoking list reviews: %v", err))
}
reviews = append(reviews, list...)
if resp.NextPage < page {
break
}
page = resp.NextPage
}
wg.Done()
log.Printf("Fetched reviews in %v", time.Now().Sub(start))
}()
issueComments := make([]*github.IssueComment, 0, 10)
go func() {
start := time.Now()
for page := 1; ; {
list, resp, err := client.Issues.ListComments(ctx, projectOwner, projectRepo, n, &github.IssueListCommentsOptions{
ListOptions: github.ListOptions{
Page: page,
PerPage: 100,
},
})
if err != nil {
log.Fatal(fmt.Errorf("invoking list issue comments: %v", err))
}
issueComments = append(issueComments, list...)
if resp.NextPage < page {
break
}
page = resp.NextPage
}
log.Printf("Fetched issue comments in %v", time.Now().Sub(start))
wg.Done()
}()
reviewComments := make(commitComments)
go func() {
start := time.Now()
for page := 1; ; {
list, resp, err := client.PullRequests.ListComments(ctx, projectOwner, projectRepo, n, &github.PullRequestListCommentsOptions{
ListOptions: github.ListOptions{
Page: page,
PerPage: 100,
},
})
if err != nil {
log.Fatal(fmt.Errorf("invoking list issue comments: %v", err))
}
for _, comment := range list {
reviewComments.put(comment)
}
if resp.NextPage < page {
break
}
page = resp.NextPage
}
log.Printf("Fetched review comments in %v", time.Now().Sub(start))
wg.Done()
}()
wg.Wait()
topLevelComments := make(topLevelComments, 0, len(reviews)+len(issueComments))
for _, r := range reviews {
topLevelComments = append(topLevelComments, topLevelComment{
body: getString(r.Body),
createdAt: getTime(r.SubmittedAt),
author: getUserLogin(r.User),
state: getString(r.State),
commitID: getString(r.CommitID),
})
}
for _, c := range issueComments {
topLevelComments = append(topLevelComments, topLevelComment{
body: getString(c.Body),
createdAt: getTime(c.CreatedAt),
author: getUserLogin(c.User),
})
}
sort.Sort(topLevelComments)
buf := bytes.NewBuffer(make([]byte, 0, 1024))
printPR(ctx, buf, pr, diffStat.String(), topLevelComments)
commit := ""
file := ""
num := 0
foundFirstHunk := false
// Parse the `git diff` output, output line-by-line to the review template,
// and insert inline comments where they're supposed to go.
for _, line := range strings.SplitAfter(diffBuf.String(), "\n") {
if line == "" {
break
}
buf.WriteString(line)
line = strings.TrimRight(line, "\n")
// Process commit header.
commitMatches := commitStart.FindStringSubmatch(line)
if len(commitMatches) > 1 {
foundFirstHunk = false
commit = commitMatches[1]
continue
}
// Process diff header. This means we're in a diff until wee see another
// diff or commit marker.
if strings.HasPrefix(line, diffStart) {
foundFirstHunk = false
continue
}
// Process file header.
fileMatches := fileStart.FindStringSubmatch(line)
if len(fileMatches) > 1 {
file = fileMatches[1]
continue
}
// Process first hunk header.
if !foundFirstHunk {
if strings.HasPrefix(line, hunkStart) {
foundFirstHunk = true
num = 0
}
continue
}
num++
if comments := reviewComments.get(commit, file, num); comments != nil {
fmt.Fprintf(buf, "%s\n", inlineStartMarker)
for _, comment := range comments {
fmt.Fprintf(buf, "* Comment by @%s (%s)", getUserLogin(comment.User), getTime(comment.CreatedAt).Format(timeFormat))
if comment.InReplyTo == nil {
fmt.Fprintf(buf, " thread %d", *comment.ID)
}
buf.WriteString("\n")
fmt.Fprintf(buf, "*\t%s\n", wrap(*comment.Body, "*\t"))
}
fmt.Fprintf(buf, "%s\n", inlineEndMarker)
}
}
f, err := ioutil.TempFile("", "re-edit-")
if err != nil {
log.Fatal(err)
}
if err := ioutil.WriteFile(f.Name(), buf.Bytes(), 0666); err != nil {
log.Fatal(err)
}
filename := f.Name()
f.Close()
return filename
}
const timeFormat = "2006-01-02 15:04:05"
var (
topLevelStartMarker = "# ------ BEGIN TOP-LEVEL REVIEW COMMENTS ----- #"
topLevelEndMarker = "# ------ END OF TOP-LEVEL REVIEW COMMENTS ----- #"
inlineStartMarker = strings.Repeat("*", 79) + "v"
inlineEndMarker = strings.Repeat("*", 79) + "^"
)
func printPR(ctx context.Context, w *bytes.Buffer, pr *github.PullRequest,
diffstat string, comments topLevelComments) error {
// Fool tpope/vim-git's filetype detector for Git commit messages
fmt.Fprint(w, "commit 0000000000000000000000000000000000000000\n")
fmt.Fprintf(w, "Author: %s <>\n", getUserLogin(pr.User))
fmt.Fprintf(w, "Date: %s\n", getTime(pr.CreatedAt).Format(timeFormat))
fmt.Fprintf(w, "Title: %s\n", getString(pr.Title))
fmt.Fprintf(w, "State: %s\n", getString(pr.State))
if pr.MergedAt != nil {
fmt.Fprintf(w, "Merged: %s\n", getTime(pr.MergedAt).Format(timeFormat))
}
if pr.ClosedAt != nil {
fmt.Fprintf(w, "Closed: %s\n", getTime(pr.ClosedAt).Format(timeFormat))
}
fmt.Fprintf(w, "URL: https://github.com/%s/%s/pull/%d\n\n", projectOwner, projectRepo, getInt(pr.Number))
fmt.Fprint(w, diffstat)
fmt.Fprintf(w, "\nCreated by %s (%s)\n", getUserLogin(pr.User), getTime(pr.CreatedAt).Format(timeFormat))
if pr.Body != nil {
text := strings.TrimSpace(*pr.Body)
if text != "" {
fmt.Fprintf(w, "\n\t%s\n", wrap(text, "\t"))
}
}
for _, com := range comments {
text := strings.TrimSpace(com.body)
if text == "" {
continue
}
if strings.Contains(text, "<!-- Reviewable:start -->") {
// Don't print "This change is Reviewable" message
continue
}
if strings.Contains(text, "<!-- Sent from Reviewable.io -->") {
// TODO(jordan) parse Reviewable comments into inlie comments.
}
action := "Comment"
switch com.state {
case reviewApprove:
action = "Approved"
case reviewRequestChanges:
action = "Changes requested"
case reviewPending:
action = "Draft comment"
}
fmt.Fprintf(w, "\n%s by %s (%s)\n", action, com.author, com.createdAt.Format(timeFormat))
fmt.Fprintf(w, "\n\t%s\n", wrap(text, "\t"))
}
fmt.Fprint(w, "\n")
fmt.Fprintf(w, `
# Add top-level review comments by typing between the marker lines below.
# Don't modify the markers!
%s
%s
# Add ordinary review comments by typing on a new line below the line of the
# diff you'd like to comment on. Comments may not begin with the special
# characters <space>, +, -, @, or *.
#
# Pre-existing comments are prefixed with *.
`, topLevelStartMarker, topLevelEndMarker)
return nil
}
var (
reviewApprove = "APPROVE"
reviewRequestChanges = "REQUEST_CHANGES"
reviewComment = "COMMENT"
reviewPending = "PENDING"
)
func review(prNum int, filename string) *github.PullRequestReviewRequest {
defer os.Remove(filename)
stdin := bufio.NewReader(os.Stdin)
editReview := true
var request *github.PullRequestReviewRequest
for {
if editReview {
request = parseFileUntilSuccess(filename)
}
editReview = true
fmt.Printf("Submit this review [y,a,r,d,s,p,e,q,?]? ")
text, err := stdin.ReadString('\n')
if err != nil && err != io.EOF {
log.Fatal(err)
} else if err == io.EOF {
exitHappy()
}
switch text[0] {
case 'y':
request.Event = &reviewComment
return request
case 'a':
request.Event = &reviewApprove
return request
case 'r':
request.Event = &reviewRequestChanges
return request
case 'd':
request.Event = nil
return request
case 's':
cpCmd := exec.Command("cp", filename, fmt.Sprintf("%d.redraft", prNum))
err := cpCmd.Run()
if err != nil {
log.Fatal(err)
}
exitHappy("Saved draft as", fmt.Sprintf("%d.redraft", prNum))
case 'p':
editReview = false
fmt.Println(request)
continue
case 'e':
continue
case 'q':
exitHappy()
case '?':
fallthrough
default:
editReview = false
color.Set(color.FgRed, color.Bold)
fmt.Println("y - submit comments")
fmt.Println("a - submit and approve")
fmt.Println("r - submit and request changes")
fmt.Println("d - publish as draft")
fmt.Println("s - save review locally and quit; resume with re <pr> resume")
fmt.Println("p - preview review")
fmt.Println("e - edit review")
fmt.Println("q - quit; abandon review")
fmt.Println("? - print help")
color.Unset()
continue
}
}
}
func parseFileUntilSuccess(filename string) *github.PullRequestReviewRequest {
stdin := bufio.NewReader(os.Stdin)
for {
updated, err := editFile(filename)
if err == nil {
request, err := parseFile(updated)
if err == nil {
return request
}
}
fmt.Printf("error parsing file: %s\n", err)
fmt.Printf("edit again? [Y]/q ")
text, err := stdin.ReadString('\n')
if err != nil && err != io.EOF {
log.Fatal(err)
} else if err == io.EOF {
exitHappy()
}
text = strings.TrimRight(text, "\n")
if text == "y" || text == "Y" || text == "" {
continue
}
if text == "q" {
exitHappy()
}
}
}
var commitStart = regexp.MustCompile(`^commit (.*)$`)
var diffStart = `diff --git `
var fileStart = regexp.MustCompile(`^\+\+\+ b\/(.*)$`)
var hunkStart = `@@`
var threadId = regexp.MustCompile(`^\* Comment by @\w+ \([^\)]+\) thread (\d+)$`)
func parseFile(b []byte) (*github.PullRequestReviewRequest, error) {
dat := string(b)
commit := ""
file := ""
num := 0
foundFirstHunk := false
commentStart := -1
lastCommentStart := -1
topLevelCommentStart := 0
lastInlineCommentId := 0
reviews := []*github.PullRequestReviewRequest{
&github.PullRequestReviewRequest{},
}
review := reviews[0]
off := 0
for _, line := range strings.SplitAfter(dat, "\n") {
lastCommentStart = commentStart
commentStart = -1
if line == "" {
break
}
off += len(line)
line = strings.TrimRight(line, "\n")
// Process top level comments.
if line == topLevelStartMarker {
topLevelCommentStart = off
continue
} else if line == topLevelEndMarker {
topLevelCommentEnd := off - len(line) - 2
if topLevelCommentEnd > topLevelCommentStart {
body := string(dat[topLevelCommentStart:topLevelCommentEnd])
body += "\n<!-- review by re -->"
review.Body = &body
}
topLevelCommentStart = 0
continue
} else if topLevelCommentStart != 0 {
continue
}
if line == inlineStartMarker {
continue
} else if line == inlineEndMarker {
lastInlineCommentId = 0
continue
}
threadIdMatches := threadId.FindStringSubmatch(line)
if len(threadIdMatches) > 1 {
var err error
lastInlineCommentId, err = strconv.Atoi(threadIdMatches[1])
if err != nil {
return nil, err
}
continue
}
// Process commit header.
commitMatches := commitStart.FindStringSubmatch(line)
if len(commitMatches) > 1 {
foundFirstHunk = false
commit = commitMatches[1]
review.CommitID = &commit
continue
}
// Process diff header. This means we're in a diff until wee see another
// diff or commit marker.
if strings.HasPrefix(line, diffStart) {
foundFirstHunk = false
continue
}
// Process file header.
fileMatches := fileStart.FindStringSubmatch(line)
if len(fileMatches) > 1 {
file = fileMatches[1]
continue
}
// Process first hunk header.
if !foundFirstHunk {
if strings.HasPrefix(line, hunkStart) {
foundFirstHunk = true
num = 0
}
continue
}
if len(line) == 0 {
// Empty line. Skip.
continue
}
// Process special diff first-chars.
switch line[0] {
case '+', '-', ' ', '@':
num++
continue
case '*', '\t':
// Old comment
continue
}
// We found a comment!
commentStart = lastCommentStart
if commentStart == -1 {
commentStart = off - len(line) - 1
comment := makeDraftReviewComment(file, num)
if lastInlineCommentId != 0 {
/* TODO(jordan) figure out how to send raft replies
cId := lastInlineCommentId
comment.InReplyTo = &cId
comment.Path = nil
comment.Position = nil
*/
}
review.Comments = append(review.Comments, comment)
}
c := review.Comments[len(review.Comments)-1]
body := dat[commentStart : off-1]
c.Body = &body
}
return review, nil
}
func makeDraftReviewComment(path string, position int) *github.DraftReviewComment {
return &github.DraftReviewComment{
Path: &path,
Position: &position,
}
}