/
github.go
254 lines (219 loc) 路 6.37 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
package reviewdog
import (
"fmt"
"log"
"os/exec"
"sync"
"golang.org/x/sync/errgroup"
"github.com/google/go-github/github"
)
var _ = github.ScopeAdminOrg
var _ CommentService = &GitHubPullRequest{}
var _ DiffService = &GitHubPullRequest{}
// `path` to `position`(Lnum for new file) to comment `body`s
type postedcomments map[string]map[int][]string
// IsPosted returns true if a given comment has been posted in GitHub already,
// otherwise returns false. It sees comments with same path, same position,
// and same body as same comments.
func (p postedcomments) IsPosted(c *Comment) bool {
if _, ok := p[c.Path]; !ok {
return false
}
bodys, ok := p[c.Path][c.LnumDiff]
if !ok {
return false
}
for _, body := range bodys {
if body == commentBody(c) {
return true
}
}
return false
}
// 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 {
postComments []*Comment
cli *github.Client
owner string
repo string
pr int
sha string
postedcs postedcomments
muFlash sync.Mutex
}
// NewGitHubPullReqest returns a new GitHubPullRequest service.
func NewGitHubPullReqest(cli *github.Client, owner, repo string, pr int, sha string) *GitHubPullRequest {
return &GitHubPullRequest{
cli: cli,
owner: owner,
repo: repo,
pr: pr,
sha: sha,
}
}
// Post accepts a comment and holds it. Flash method actually posts comments to
// GitHub in parallel.
func (g *GitHubPullRequest) Post(c *Comment) error {
g.muFlash.Lock()
defer g.muFlash.Unlock()
g.postComments = append(g.postComments, c)
return nil
}
const bodyPrefix = `<sub>reported by [reviewdog](https://github.com/haya14busa/reviewdog) :dog:</sub>`
func commentBody(c *Comment) string {
tool := ""
if c.ToolName != "" {
tool = fmt.Sprintf("**[%s]** ", c.ToolName)
}
return tool + bodyPrefix + "\n" + c.Body
}
var githubAPIHost = "api.github.com"
// Flash posts comments which has not been posted yet.
func (g *GitHubPullRequest) Flash() error {
g.muFlash.Lock()
defer g.muFlash.Unlock()
if err := g.setPostedComment(); err != nil {
return err
}
// TODO(haya14busa,#58): remove host check when GitHub Enterprise supports
// Pull Request API.
if g.cli.BaseURL.Host == githubAPIHost {
return g.postAsReviewComment()
}
return g.postCommentsForEach()
}
func (g *GitHubPullRequest) postAsReviewComment() error {
comments := make([]*ReviewComment, 0, len(g.postComments))
for _, c := range g.postComments {
if g.postedcs.IsPosted(c) {
continue
}
cbody := commentBody(c)
comments = append(comments, &ReviewComment{
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.
event := "COMMENT"
review := &Review{Event: &event, Comments: comments}
_, _, err := g.CreateReview(g.owner, g.repo, g.pr, review)
if err != nil {
return err
}
return nil
}
func (g *GitHubPullRequest) postCommentsForEach() error {
var eg errgroup.Group
for _, c := range g.postComments {
comment := c
if g.postedcs.IsPosted(comment) {
continue
}
eg.Go(func() error {
body := commentBody(comment)
prcomment := &github.PullRequestComment{
CommitID: &g.sha,
Body: &body,
Path: &comment.Path,
Position: &comment.LnumDiff,
}
_, _, err := g.cli.PullRequests.CreateComment(g.owner, g.repo, g.pr, prcomment)
return err
})
}
return eg.Wait()
}
func (g *GitHubPullRequest) setPostedComment() error {
g.postedcs = make(postedcomments)
cs, err := g.comment()
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
}
path := *c.Path
pos := *c.Position
body := *c.Body
if _, ok := g.postedcs[path]; !ok {
g.postedcs[path] = make(map[int][]string)
}
if _, ok := g.postedcs[path][pos]; !ok {
g.postedcs[path][pos] = make([]string, 0)
}
g.postedcs[path][pos] = append(g.postedcs[path][pos], body)
}
return nil
}
// Diff returns a diff of PullRequest. It runs `git diff` locally instead of
// diff_url of GitHub Pull Request because diff of diff_url is not suited for
// comment API in a sense that diff of diff_url is equivalent to
// `git diff --no-renames`, we want diff which is equivalent to
// `git diff --find-renames`.
func (g *GitHubPullRequest) Diff() ([]byte, error) {
pr, _, err := g.cli.PullRequests.Get(g.owner, g.repo, g.pr)
if err != nil {
return nil, err
}
return exec.Command("git", "diff", "--find-renames", *pr.Base.SHA, g.sha).Output()
}
// Strip returns 1 as a strip of git diff.
func (g *GitHubPullRequest) Strip() int {
return 1
}
func (g *GitHubPullRequest) comment() ([]*github.PullRequestComment, error) {
comments, _, err := g.cli.PullRequests.ListComments(g.owner, g.repo, g.pr, nil)
if err != nil {
return nil, err
}
return comments, nil
}
// ---
// GitHub PullRequest Review API Implementation
// ref: https://github.com/google/go-github/issues/495
const (
mediaTypePullRequestReview = "application/vnd.github.black-cat-preview+json"
)
// Review represents a pull request review.
type Review struct {
Body *string `json:"body,omitempty"`
Event *string `json:"event,omitempty"`
Comments []*ReviewComment `json:"comments,omitempty"`
}
// ReviewComment represents draft review comments.
type ReviewComment struct {
Path *string `json:"path,omitempty"`
Position *int `json:"position,omitempty"`
Body *string `json:"body,omitempty"`
}
// CreateReview creates a new review comment on the specified pull request.
//
// GitHub API docs: https://developer.github.com/v3/pulls/reviews/#create-a-pull-request-review
func (g *GitHubPullRequest) CreateReview(owner, repo string, number int, review *Review) (*github.PullRequestReview, *github.Response, error) {
u := fmt.Sprintf("repos/%v/%v/pulls/%d/reviews", owner, repo, number)
req, err := g.cli.NewRequest("POST", u, review)
if err != nil {
return nil, nil, err
}
req.Header.Set("Accept", mediaTypePullRequestReview)
r := new(github.PullRequestReview)
resp, err := g.cli.Do(req, r)
if err != nil {
log.Printf("GitHub Review API error: %v", err)
return nil, resp, err
}
return r, resp, err
}