-
-
Notifications
You must be signed in to change notification settings - Fork 398
/
github.go
487 lines (438 loc) 路 14.9 KB
/
github.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
package github
import (
"context"
"errors"
"fmt"
"log"
"net/http"
"os"
"os/exec"
"path/filepath"
"strings"
"sync"
"github.com/google/go-github/v60/github"
"github.com/reviewdog/reviewdog"
"github.com/reviewdog/reviewdog/cienv"
"github.com/reviewdog/reviewdog/proto/rdf"
"github.com/reviewdog/reviewdog/service/commentutil"
"github.com/reviewdog/reviewdog/service/github/githubutils"
"github.com/reviewdog/reviewdog/service/serviceutil"
)
var _ reviewdog.CommentService = (*PullRequest)(nil)
var _ reviewdog.DiffService = (*PullRequest)(nil)
const maxCommentsPerRequest = 30
const (
invalidSuggestionPre = "<details><summary>reviewdog suggestion error</summary>"
invalidSuggestionPost = "</details>"
)
func isPermissionError(err error) bool {
var githubErr *github.ErrorResponse
if !errors.As(err, &githubErr) {
return false
}
status := githubErr.Response.StatusCode
return status == http.StatusForbidden || status == http.StatusNotFound
}
// PullRequest is a comment and diff service for GitHub PullRequest.
//
// API:
//
// https://docs.github.com/en/rest/pulls/comments?apiVersion=2022-11-28#create-a-review-comment-for-a-pull-request
// POST /repos/:owner/:repo/pulls/:number/comments
type PullRequest struct {
cli *github.Client
owner string
repo string
pr int
sha string
muComments sync.Mutex
postComments []*reviewdog.Comment
logWriter *githubutils.GitHubActionLogWriter
fallbackToLog bool
postedcs commentutil.PostedComments
// wd is working directory relative to root of repository.
wd string
}
// NewGitHubPullRequest returns a new PullRequest service.
// PullRequest service needs git command in $PATH.
//
// The GitHub Token generated by GitHub Actions may not have the necessary permissions.
// For example, in the case of a PR from a forked repository, or when write permission is prohibited in the repository settings [1].
//
// In such a case, the service will fallback to GitHub Actions workflow commands [2].
//
// [1]: https://docs.github.com/en/actions/security-guides/automatic-token-authentication#permissions-for-the-github_token
// [2]: https://docs.github.com/en/actions/reference/workflow-commands-for-github-actions
func NewGitHubPullRequest(cli *github.Client, owner, repo string, pr int, sha, level string) (*PullRequest, error) {
workDir, err := serviceutil.GitRelWorkdir()
if err != nil {
return nil, fmt.Errorf("PullRequest needs 'git' command: %w", err)
}
return &PullRequest{
cli: cli,
owner: owner,
repo: repo,
pr: pr,
sha: sha,
logWriter: githubutils.NewGitHubActionLogWriter(level),
wd: workDir,
}, nil
}
// Post accepts a comment and holds it. Flush method actually posts comments to
// GitHub in parallel.
func (g *PullRequest) Post(_ context.Context, c *reviewdog.Comment) error {
c.Result.Diagnostic.GetLocation().Path = filepath.ToSlash(filepath.Join(g.wd,
c.Result.Diagnostic.GetLocation().GetPath()))
g.muComments.Lock()
defer g.muComments.Unlock()
g.postComments = append(g.postComments, c)
return nil
}
// Flush posts comments which has not been posted yet.
func (g *PullRequest) Flush(ctx context.Context) error {
g.muComments.Lock()
defer g.muComments.Unlock()
if err := g.setPostedComment(ctx); err != nil {
return err
}
return g.postAsReviewComment(ctx)
}
func (g *PullRequest) postAsReviewComment(ctx context.Context) error {
if g.fallbackToLog {
// we don't have permission to post a review comment.
// Fallback to GitHub Actions log as report.
for _, c := range g.postComments {
if err := g.logWriter.Post(ctx, c); err != nil {
return err
}
}
return g.logWriter.Flush(ctx)
}
postComments := g.postComments
g.postComments = nil
rawComments := make([]*reviewdog.Comment, 0, len(postComments))
reviewComments := make([]*github.DraftReviewComment, 0, len(postComments))
remaining := make([]*reviewdog.Comment, 0)
for _, c := range postComments {
if !c.Result.InDiffContext {
// GitHub Review API cannot report results outside diff. If it's running
// in GitHub Actions, fallback to GitHub Actions log as report.
if cienv.IsInGitHubAction() {
if err := g.logWriter.Post(ctx, c); err != nil {
return err
}
}
continue
}
body := buildBody(c)
if g.postedcs.IsPosted(c, githubCommentLine(c), body) {
// it's already posted. skip it.
continue
}
// Only posts maxCommentsPerRequest comments per 1 request to avoid spammy
// review comments. An example GitHub error if we don't limit the # of
// review comments.
//
// > 403 You have triggered an abuse detection mechanism and have been
// > temporarily blocked from content creation. Please retry your request
// > again later.
// https://docs.github.com/en/rest/overview/resources-in-the-rest-api?apiVersion=2022-11-28#rate-limiting
if len(reviewComments) >= maxCommentsPerRequest {
remaining = append(remaining, c)
continue
}
reviewComments = append(reviewComments, buildDraftReviewComment(c, body))
}
if err := g.logWriter.Flush(ctx); err != nil {
return err
}
if len(reviewComments) > 0 {
// send review comments to GitHub.
review := &github.PullRequestReviewRequest{
CommitID: &g.sha,
Event: github.String("COMMENT"),
Comments: reviewComments,
Body: github.String(g.remainingCommentsSummary(remaining)),
}
_, _, err := g.cli.PullRequests.CreateReview(ctx, g.owner, g.repo, g.pr, review)
if err != nil {
log.Printf("reviewdog: failed to post a review comment: %v", err)
// GitHub returns 403 or 404 if we don't have permission to post a review comment.
// fallback to log message in this case.
if isPermissionError(err) && cienv.IsInGitHubAction() {
goto FALLBACK
}
return err
}
}
return nil
FALLBACK:
// fallback to GitHub Actions log as report.
fmt.Fprintln(os.Stderr, `reviewdog: This GitHub Token doesn't have write permission of Review API [1],
so reviewdog will report results via logging command [2] and create annotations similar to
github-pr-check reporter as a fallback.
[1]: https://docs.github.com/en/actions/reference/events-that-trigger-workflows#pull_request_target
[2]: https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions`)
g.fallbackToLog = true
for _, c := range rawComments {
if err := g.logWriter.Post(ctx, c); err != nil {
return err
}
}
return g.logWriter.Flush(ctx)
}
// Document: https://docs.github.com/en/rest/reference/pulls#create-a-review-comment-for-a-pull-request
func buildDraftReviewComment(c *reviewdog.Comment, body string) *github.DraftReviewComment {
loc := c.Result.Diagnostic.GetLocation()
startLine, endLine := githubCommentLineRange(c)
r := &github.DraftReviewComment{
Path: github.String(loc.GetPath()),
Side: github.String("RIGHT"),
Body: github.String(body),
Line: github.Int(endLine),
}
// GitHub API: Start line must precede the end line.
if startLine < endLine {
r.StartSide = github.String("RIGHT")
r.StartLine = github.Int(startLine)
}
return r
}
// line represents end line if it's a multiline comment in GitHub, otherwise
// it's start line.
// Document: https://docs.github.com/en/rest/reference/pulls#create-a-review-comment-for-a-pull-request
func githubCommentLine(c *reviewdog.Comment) int {
if !c.Result.InDiffContext {
return 0
}
_, end := githubCommentLineRange(c)
return end
}
func githubCommentLineRange(c *reviewdog.Comment) (start int, end int) {
// Prefer first suggestion line range to diagnostic location if available so
// that reviewdog can post code suggestion as well when the line ranges are
// different between the diagnostic location and its suggestion.
if c.Result.FirstSuggestionInDiffContext && len(c.Result.Diagnostic.GetSuggestions()) > 0 {
s := c.Result.Diagnostic.GetSuggestions()[0]
startLine := s.GetRange().GetStart().GetLine()
endLine := s.GetRange().GetEnd().GetLine()
if endLine == 0 {
endLine = startLine
}
return int(startLine), int(endLine)
}
loc := c.Result.Diagnostic.GetLocation()
startLine := loc.GetRange().GetStart().GetLine()
endLine := loc.GetRange().GetEnd().GetLine()
if endLine == 0 {
endLine = startLine
}
return int(startLine), int(endLine)
}
func (g *PullRequest) remainingCommentsSummary(remaining []*reviewdog.Comment) string {
if len(remaining) == 0 {
return ""
}
perTool := make(map[string][]*reviewdog.Comment)
for _, c := range remaining {
perTool[c.ToolName] = append(perTool[c.ToolName], c)
}
var sb strings.Builder
sb.WriteString("Remaining comments which cannot be posted as a review comment to avoid GitHub Rate Limit\n")
sb.WriteString("\n")
for tool, comments := range perTool {
sb.WriteString("<details>\n")
sb.WriteString(fmt.Sprintf("<summary>%s</summary>\n", tool))
sb.WriteString("\n")
for _, c := range comments {
sb.WriteString(githubutils.LinkedMarkdownDiagnostic(g.owner, g.repo, g.sha, c.Result.Diagnostic))
sb.WriteString("\n")
}
sb.WriteString("</details>\n")
}
return sb.String()
}
// setPostedComment get posted comments from GitHub.
func (g *PullRequest) setPostedComment(ctx context.Context) error {
g.postedcs = make(commentutil.PostedComments)
cs, err := g.comment(ctx)
if err != nil {
return err
}
for _, c := range cs {
if c.Line == nil || c.Path == nil || c.Body == nil || c.SubjectType == nil {
continue
}
var line int
if c.GetSubjectType() == "line" {
line = c.GetLine()
}
g.postedcs.AddPostedComment(c.GetPath(), line, c.GetBody())
}
return nil
}
// Diff returns a diff of PullRequest.
func (g *PullRequest) Diff(ctx context.Context) ([]byte, error) {
opt := github.RawOptions{Type: github.Diff}
d, resp, err := g.cli.PullRequests.GetRaw(ctx, g.owner, g.repo, g.pr, opt)
if err != nil {
if resp != nil && resp.StatusCode == http.StatusNotAcceptable && g.checkInstallGitCommand() {
log.Print("fallback to use git command")
return g.diffUsingGitCommand(ctx)
}
return nil, err
}
return []byte(d), nil
}
// checkInstallGitCommand checks if git command is installed.
func (g *PullRequest) checkInstallGitCommand() bool {
_, err := exec.Command("git", "-v").CombinedOutput()
return err == nil
}
// diffUsingGitCommand returns a diff of PullRequest using git command.
func (g *PullRequest) diffUsingGitCommand(ctx context.Context) ([]byte, error) {
pr, _, err := g.cli.PullRequests.Get(ctx, g.owner, g.repo, g.pr)
if err != nil {
return nil, err
}
head := pr.GetHead()
headSha := head.GetSHA()
commitsComparison, _, err := g.cli.Repositories.CompareCommits(ctx, g.owner, g.repo, headSha, pr.GetBase().GetSHA(), nil)
if err != nil {
return nil, err
}
mergeBaseSha := commitsComparison.GetMergeBaseCommit().GetSHA()
if os.Getenv("REVIEWDOG_SKIP_GIT_FETCH") != "true" {
for _, sha := range []string{mergeBaseSha, headSha} {
_, err := exec.Command("git", "fetch", "--depth=1", head.GetRepo().GetHTMLURL(), sha).CombinedOutput()
if err != nil {
return nil, fmt.Errorf("failed to run git fetch: %w", err)
}
}
}
bytes, err := exec.Command("git", "diff", "--find-renames", mergeBaseSha, headSha).CombinedOutput()
if err != nil {
return nil, fmt.Errorf("failed to run git diff: %w", err)
}
return bytes, nil
}
// Strip returns 1 as a strip of git diff.
func (g *PullRequest) Strip() int {
return 1
}
func (g *PullRequest) comment(ctx context.Context) ([]*github.PullRequestComment, error) {
// https://developer.github.com/v3/guides/traversing-with-pagination/
opts := &github.PullRequestListCommentsOptions{
ListOptions: github.ListOptions{
PerPage: 100,
},
}
comments, err := listAllPullRequestsComments(ctx, g.cli, g.owner, g.repo, g.pr, opts)
if err != nil {
return nil, err
}
return comments, nil
}
func listAllPullRequestsComments(ctx context.Context, cli *github.Client,
owner, repo string, pr int, opts *github.PullRequestListCommentsOptions) ([]*github.PullRequestComment, error) {
comments, resp, err := cli.PullRequests.ListComments(ctx, owner, repo, pr, opts)
if err != nil {
return nil, err
}
if resp.NextPage == 0 {
return comments, nil
}
newOpts := &github.PullRequestListCommentsOptions{
ListOptions: github.ListOptions{
Page: resp.NextPage,
PerPage: opts.PerPage,
},
}
restComments, err := listAllPullRequestsComments(ctx, cli, owner, repo, pr, newOpts)
if err != nil {
return nil, err
}
return append(comments, restComments...), nil
}
func buildBody(c *reviewdog.Comment) string {
cbody := commentutil.MarkdownComment(c)
if suggestion := buildSuggestions(c); suggestion != "" {
cbody += "\n" + suggestion
}
return cbody
}
func buildSuggestions(c *reviewdog.Comment) string {
var sb strings.Builder
for _, s := range c.Result.Diagnostic.GetSuggestions() {
txt, err := buildSingleSuggestion(c, s)
if err != nil {
sb.WriteString(invalidSuggestionPre + err.Error() + invalidSuggestionPost + "\n")
continue
}
sb.WriteString(txt)
sb.WriteString("\n")
}
return sb.String()
}
func buildSingleSuggestion(c *reviewdog.Comment, s *rdf.Suggestion) (string, error) {
start := s.GetRange().GetStart()
startLine := int(start.GetLine())
end := s.GetRange().GetEnd()
endLine := int(end.GetLine())
if endLine == 0 {
endLine = startLine
}
gStart, gEnd := githubCommentLineRange(c)
if startLine != gStart || endLine != gEnd {
return "", fmt.Errorf("GitHub comment range and suggestion line range must be same. L%d-L%d v.s. L%d-L%d",
gStart, gEnd, startLine, endLine)
}
if start.GetColumn() > 0 || end.GetColumn() > 0 {
return buildNonLineBasedSuggestion(c, s)
}
txt := s.GetText()
backticks := commentutil.GetCodeFenceLength(txt)
var sb strings.Builder
sb.Grow(backticks + len("suggestion\n") + len(txt) + len("\n") + backticks)
commentutil.WriteCodeFence(&sb, backticks)
sb.WriteString("suggestion\n")
if txt != "" {
sb.WriteString(txt)
sb.WriteString("\n")
}
commentutil.WriteCodeFence(&sb, backticks)
return sb.String(), nil
}
func buildNonLineBasedSuggestion(c *reviewdog.Comment, s *rdf.Suggestion) (string, error) {
sourceLines := c.Result.SourceLines
if len(sourceLines) == 0 {
return "", errors.New("source lines are not available")
}
start := s.GetRange().GetStart()
end := s.GetRange().GetEnd()
startLineContent, err := getSourceLine(sourceLines, int(start.GetLine()))
if err != nil {
return "", err
}
endLineContent, err := getSourceLine(sourceLines, int(end.GetLine()))
if err != nil {
return "", err
}
txt := startLineContent[:max(start.GetColumn()-1, 0)] + s.GetText() + endLineContent[max(end.GetColumn()-1, 0):]
backticks := commentutil.GetCodeFenceLength(txt)
var sb strings.Builder
sb.Grow(backticks + len("suggestion\n") + len(txt) + len("\n") + backticks)
commentutil.WriteCodeFence(&sb, backticks)
sb.WriteString("suggestion\n")
sb.WriteString(txt)
sb.WriteString("\n")
commentutil.WriteCodeFence(&sb, backticks)
return sb.String(), nil
}
func getSourceLine(sourceLines map[int]string, line int) (string, error) {
lineContent, ok := sourceLines[line]
if !ok {
return "", fmt.Errorf("source line (L=%d) is not available for this suggestion", line)
}
return lineContent, nil
}