-
-
Notifications
You must be signed in to change notification settings - Fork 398
/
github.go
210 lines (189 loc) 路 5.99 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
package github
import (
"context"
"fmt"
"path/filepath"
"strings"
"sync"
"github.com/google/go-github/v28/github"
"github.com/reviewdog/reviewdog"
"github.com/reviewdog/reviewdog/service/github/githubutils"
"github.com/reviewdog/reviewdog/service/serviceutil"
)
var _ reviewdog.CommentService = &GitHubPullRequest{}
var _ reviewdog.DiffService = &GitHubPullRequest{}
const maxCommentsPerRequest = 30
// GitHubPullRequest is a comment and diff service for GitHub PullRequest.
//
// API:
// https://developer.github.com/v3/pulls/comments/#create-a-comment
// POST /repos/:owner/:repo/pulls/:number/comments
type GitHubPullRequest struct {
cli *github.Client
owner string
repo string
pr int
sha string
muComments sync.Mutex
postComments []*reviewdog.Comment
postedcs serviceutil.PostedComments
// wd is working directory relative to root of repository.
wd string
}
// NewGitHubPullRequest returns a new GitHubPullRequest service.
// GitHubPullRequest service needs git command in $PATH.
func NewGitHubPullRequest(cli *github.Client, owner, repo string, pr int, sha string) (*GitHubPullRequest, error) {
workDir, err := serviceutil.GitRelWorkdir()
if err != nil {
return nil, fmt.Errorf("GitHubPullRequest needs 'git' command: %v", err)
}
return &GitHubPullRequest{
cli: cli,
owner: owner,
repo: repo,
pr: pr,
sha: sha,
wd: workDir,
}, nil
}
// Post accepts a comment and holds it. Flush method actually posts comments to
// GitHub in parallel.
func (g *GitHubPullRequest) Post(_ context.Context, c *reviewdog.Comment) error {
c.Path = filepath.ToSlash(filepath.Join(g.wd, c.Path))
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 *GitHubPullRequest) 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 *GitHubPullRequest) postAsReviewComment(ctx context.Context) error {
comments := make([]*github.DraftReviewComment, 0, len(g.postComments))
remaining := make([]*reviewdog.Comment, 0)
for _, c := range g.postComments {
if g.postedcs.IsPosted(c, c.LnumDiff) {
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://developer.github.com/v3/#abuse-rate-limits
if len(comments) >= maxCommentsPerRequest {
remaining = append(remaining, c)
continue
}
cbody := serviceutil.CommentBody(c)
comments = append(comments, &github.DraftReviewComment{
Path: &c.Path,
Position: &c.LnumDiff,
Body: &cbody,
})
}
if len(comments) == 0 {
return nil
}
// TODO(haya14busa): it might be useful to report overview results by "body"
// field.
review := &github.PullRequestReviewRequest{
CommitID: &g.sha,
Event: github.String("COMMENT"),
Comments: comments,
Body: github.String(g.remainingCommentsSummary(remaining)),
}
_, _, err := g.cli.PullRequests.CreateReview(ctx, g.owner, g.repo, g.pr, review)
return err
}
func (g *GitHubPullRequest) remainingCommentsSummary(remaining []*reviewdog.Comment) string {
perTool := make(map[string][]*reviewdog.Comment)
for _, c := range remaining {
perTool[c.ToolName] = append(perTool[c.ToolName], c)
}
var sb strings.Builder
for tool, comments := range perTool {
sb.WriteString("Remaining comments which cannot be posted as a review comment to avoid GitHub abuse Rate Limit\n")
sb.WriteString("\n")
sb.WriteString("<details>\n")
sb.WriteString(fmt.Sprintf("<summary>%s</summary>\n", tool))
sb.WriteString("\n")
for _, c := range comments {
sb.WriteString(githubutils.LinkedMarkdownCheckResult(g.owner, g.repo, g.sha, c.CheckResult))
sb.WriteString("\n")
}
sb.WriteString("</details>\n")
}
return sb.String()
}
func (g *GitHubPullRequest) setPostedComment(ctx context.Context) error {
g.postedcs = make(serviceutil.PostedComments)
cs, err := g.comment(ctx)
if err != nil {
return err
}
for _, c := range cs {
if c.Position == nil || c.Path == nil || c.Body == nil {
// skip resolved comments. Or comments which do not have "path" nor
// "body".
continue
}
g.postedcs.AddPostedComment(c.GetPath(), c.GetPosition(), c.GetBody())
}
return nil
}
// Diff returns a diff of PullRequest.
func (g *GitHubPullRequest) Diff(ctx context.Context) ([]byte, error) {
opt := github.RawOptions{Type: github.Diff}
d, _, err := g.cli.PullRequests.GetRaw(ctx, g.owner, g.repo, g.pr, opt)
if err != nil {
return nil, err
}
return []byte(d), nil
}
// Strip returns 1 as a strip of git diff.
func (g *GitHubPullRequest) Strip() int {
return 1
}
func (g *GitHubPullRequest) 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
}