forked from reviewdog/reviewdog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
doghouse.go
344 lines (314 loc) · 10.3 KB
/
doghouse.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
package server
import (
"bytes"
"context"
"fmt"
"net/http"
"strings"
"time"
"github.com/google/go-github/v39/github"
"github.com/androidjp/reviewdog/diff"
"github.com/androidjp/reviewdog/doghouse"
"github.com/androidjp/reviewdog/filter"
"github.com/androidjp/reviewdog/proto/rdf"
"github.com/androidjp/reviewdog/service/github/githubutils"
)
// GitHub check runs API cannot handle too large requests.
// Set max number of filtered findings to be shown in check-run summary.
// ERROR:
// https://api.github.com/repos/easymotion/vim-easymotion/check-runs: 422
// Invalid request.
// Only 65535 characters are allowed; 250684 were supplied. []
const maxFilteredFinding = 150
// > The Checks API limits the number of annotations to a maximum of 50 per API
// > request.
// https://developer.github.com/v3/checks/runs/#output-object
const maxAnnotationsPerRequest = 50
type Checker struct {
req *doghouse.CheckRequest
gh checkerGitHubClientInterface
}
func NewChecker(req *doghouse.CheckRequest, gh *github.Client) *Checker {
return &Checker{req: req, gh: &checkerGitHubClient{Client: gh}}
}
func (ch *Checker) Check(ctx context.Context) (*doghouse.CheckResponse, error) {
var filediffs []*diff.FileDiff
if ch.req.PullRequest != 0 {
var err error
filediffs, err = ch.pullRequestDiff(ctx, ch.req.PullRequest)
if err != nil {
return nil, fmt.Errorf("fail to parse diff: %w", err)
}
}
results := annotationsToDiagnostics(ch.req.Annotations)
filterMode := ch.req.FilterMode
//lint:ignore SA1019 Need to support OutsideDiff for backward compatibility.
if ch.req.PullRequest == 0 || ch.req.OutsideDiff {
// If it's not Pull Request run, do not filter results by diff regardless
// of the filter mode.
filterMode = filter.ModeNoFilter
}
filtered := filter.FilterCheck(results, filediffs, 1, "", filterMode)
check, err := ch.createCheck(ctx)
if err != nil {
// If this error is StatusForbidden (403) here, it means reviewdog is
// running on GitHub Actions and has only read permission (because it's
// running for Pull Requests from forked repository). If the token itself
// is invalid, reviewdog should return an error earlier (e.g. when reading
// Pull Requests diff), so it should be ok not to return error here and
// return results instead.
if err, ok := err.(*github.ErrorResponse); ok && err.Response.StatusCode == http.StatusForbidden {
return &doghouse.CheckResponse{CheckedResults: filtered}, nil
}
return nil, fmt.Errorf("failed to create check: %w", err)
}
checkRun, conclusion, err := ch.postCheck(ctx, check.GetID(), filtered)
if err != nil {
return nil, fmt.Errorf("failed to post result: %w", err)
}
res := &doghouse.CheckResponse{
ReportURL: checkRun.GetHTMLURL(),
Conclusion: conclusion,
}
return res, nil
}
func (ch *Checker) postCheck(ctx context.Context, checkID int64, checks []*filter.FilteredDiagnostic) (*github.CheckRun, string, error) {
var annotations []*github.CheckRunAnnotation
for _, c := range checks {
if !c.ShouldReport {
continue
}
annotations = append(annotations, ch.toCheckRunAnnotation(c))
}
if len(annotations) > 0 {
if err := ch.postAnnotations(ctx, checkID, annotations); err != nil {
return nil, "", fmt.Errorf("failed to post annotations: %w", err)
}
}
conclusion := "success"
if len(annotations) > 0 {
conclusion = ch.conclusion()
}
opt := github.UpdateCheckRunOptions{
Name: ch.checkName(),
Status: github.String("completed"),
Conclusion: github.String(conclusion),
CompletedAt: &github.Timestamp{Time: time.Now()},
Output: &github.CheckRunOutput{
Title: github.String(ch.checkTitle()),
Summary: github.String(ch.summary(checks)),
},
}
checkRun, err := ch.gh.UpdateCheckRun(ctx, ch.req.Owner, ch.req.Repo, checkID, opt)
if err != nil {
return nil, "", err
}
return checkRun, conclusion, nil
}
func (ch *Checker) createCheck(ctx context.Context) (*github.CheckRun, error) {
opt := github.CreateCheckRunOptions{
Name: ch.checkName(),
HeadSHA: ch.req.SHA,
Status: github.String("in_progress"),
}
return ch.gh.CreateCheckRun(ctx, ch.req.Owner, ch.req.Repo, opt)
}
func (ch *Checker) postAnnotations(ctx context.Context, checkID int64, annotations []*github.CheckRunAnnotation) error {
opt := github.UpdateCheckRunOptions{
Name: ch.checkName(),
Output: &github.CheckRunOutput{
Title: github.String(ch.checkTitle()),
Summary: github.String(""), // Post summary with the last request.
Annotations: annotations[:min(maxAnnotationsPerRequest, len(annotations))],
},
}
if _, err := ch.gh.UpdateCheckRun(ctx, ch.req.Owner, ch.req.Repo, checkID, opt); err != nil {
return err
}
if len(annotations) > maxAnnotationsPerRequest {
return ch.postAnnotations(ctx, checkID, annotations[maxAnnotationsPerRequest:])
}
return nil
}
func (ch *Checker) checkName() string {
if ch.req.Name != "" {
return ch.req.Name
}
return "reviewdog"
}
func (ch *Checker) checkTitle() string {
if name := ch.checkName(); name != "reviewdog" {
return fmt.Sprintf("reviewdog [%s] report", name)
}
return "reviewdog report"
}
// https://developer.github.com/v3/checks/runs/#parameters-1
func (ch *Checker) conclusion() string {
switch strings.ToLower(ch.req.Level) {
case "info", "warning":
return "neutral"
}
return "failure"
}
// https://developer.github.com/v3/checks/runs/#annotations-object
func (ch *Checker) annotationLevel(s rdf.Severity) string {
switch s {
case rdf.Severity_ERROR:
return "failure"
case rdf.Severity_WARNING:
return "warning"
case rdf.Severity_INFO:
return "notice"
default:
return ch.reqAnnotationLevel()
}
}
func (ch *Checker) reqAnnotationLevel() string {
switch strings.ToLower(ch.req.Level) {
case "info":
return "notice"
case "warning":
return "warning"
case "failure":
return "failure"
}
return "failure"
}
func (ch *Checker) summary(checks []*filter.FilteredDiagnostic) string {
var lines []string
lines = append(lines, "reported by [reviewdog](https://github.com/androidjp/reviewdog) :dog:")
var findings []*filter.FilteredDiagnostic
var filteredFindings []*filter.FilteredDiagnostic
for _, c := range checks {
if c.ShouldReport {
findings = append(findings, c)
} else {
filteredFindings = append(filteredFindings, c)
}
}
lines = append(lines, ch.summaryFindings("Findings", findings)...)
lines = append(lines, ch.summaryFindings("Filtered Findings", filteredFindings)...)
return strings.Join(lines, "\n")
}
func (ch *Checker) summaryFindings(name string, checks []*filter.FilteredDiagnostic) []string {
var lines []string
lines = append(lines, "<details>")
lines = append(lines, fmt.Sprintf("<summary>%s (%d)</summary>", name, len(checks)))
lines = append(lines, "")
for i, c := range checks {
if i >= maxFilteredFinding {
lines = append(lines, "... (Too many findings. Dropped some findings)")
break
}
lines = append(lines, githubutils.LinkedMarkdownDiagnostic(
ch.req.Owner, ch.req.Repo, ch.req.SHA, c.Diagnostic))
}
lines = append(lines, "</details>")
return lines
}
func (ch *Checker) toCheckRunAnnotation(c *filter.FilteredDiagnostic) *github.CheckRunAnnotation {
loc := c.Diagnostic.GetLocation()
startLine := int(loc.GetRange().GetStart().GetLine())
endLine := int(loc.GetRange().GetEnd().GetLine())
if endLine == 0 {
endLine = startLine
}
a := &github.CheckRunAnnotation{
Path: github.String(loc.GetPath()),
StartLine: github.Int(startLine),
EndLine: github.Int(endLine),
AnnotationLevel: github.String(ch.annotationLevel(c.Diagnostic.Severity)),
Message: github.String(c.Diagnostic.GetMessage()),
Title: github.String(ch.buildTitle(c)),
}
// Annotations only support start_column and end_column on the same line.
if startLine == endLine {
if s, e := loc.GetRange().GetStart().GetColumn(), loc.GetRange().GetEnd().GetColumn(); s != 0 && e != 0 {
a.StartColumn = github.Int(int(s))
a.EndColumn = github.Int(int(e))
}
}
if s := c.Diagnostic.GetOriginalOutput(); s != "" {
a.RawDetails = github.String(s)
}
return a
}
func (ch *Checker) buildTitle(c *filter.FilteredDiagnostic) string {
var sb strings.Builder
toolName := c.Diagnostic.GetSource().GetName()
if toolName == "" {
toolName = ch.req.Name
}
if toolName != "" {
sb.WriteString(fmt.Sprintf("[%s] ", toolName))
}
loc := c.Diagnostic.GetLocation()
sb.WriteString(loc.GetPath())
if startLine := int(loc.GetRange().GetStart().GetLine()); startLine > 0 {
sb.WriteString(fmt.Sprintf("#L%d", startLine))
if endLine := int(loc.GetRange().GetEnd().GetLine()); startLine < endLine {
sb.WriteString(fmt.Sprintf("-L%d", endLine))
}
}
if code := c.Diagnostic.GetCode().GetValue(); code != "" {
if url := c.Diagnostic.GetCode().GetUrl(); url != "" {
sb.WriteString(fmt.Sprintf(" <%s>(%s)", code, url))
} else {
sb.WriteString(fmt.Sprintf(" <%s>", code))
}
}
return sb.String()
}
func (ch *Checker) pullRequestDiff(ctx context.Context, pr int) ([]*diff.FileDiff, error) {
d, err := ch.rawPullRequestDiff(ctx, pr)
if err != nil {
return nil, err
}
filediffs, err := diff.ParseMultiFile(bytes.NewReader(d))
if err != nil {
return nil, fmt.Errorf("fail to parse diff: %w", err)
}
return filediffs, nil
}
func (ch *Checker) rawPullRequestDiff(ctx context.Context, pr int) ([]byte, error) {
d, err := ch.gh.GetPullRequestDiff(ctx, ch.req.Owner, ch.req.Repo, pr)
if err != nil {
return nil, err
}
return d, nil
}
func annotationsToDiagnostics(as []*doghouse.Annotation) []*rdf.Diagnostic {
ds := make([]*rdf.Diagnostic, 0, len(as))
for _, a := range as {
ds = append(ds, annotationToDiagnostic(a))
}
return ds
}
func annotationToDiagnostic(a *doghouse.Annotation) *rdf.Diagnostic {
if a.Diagnostic != nil {
return a.Diagnostic
}
// Old reviewdog CLI doesn't have the Diagnostic field.
return &rdf.Diagnostic{
Location: &rdf.Location{
//lint:ignore SA1019 use deprecated fields because of backward compatibility.
Path: a.Path,
Range: &rdf.Range{
Start: &rdf.Position{
//lint:ignore SA1019 use deprecated fields because of backward compatibility.
Line: int32(a.Line),
},
},
},
//lint:ignore SA1019 use deprecated fields because of backward compatibility.
Message: a.Message,
//lint:ignore SA1019 use deprecated fields because of backward compatibility.
OriginalOutput: a.RawMessage,
}
}
func min(x, y int) int {
if x > y {
return y
}
return x
}