forked from reviewdog/reviewdog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reviewdog.go
118 lines (99 loc) · 3.08 KB
/
reviewdog.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
package reviewdog
import (
"bytes"
"context"
"fmt"
"io"
"os"
"github.com/reviewdog/reviewdog/diff"
"github.com/reviewdog/reviewdog/filter"
"github.com/reviewdog/reviewdog/parser"
"github.com/reviewdog/reviewdog/proto/rdf"
)
// Reviewdog represents review dog application which parses result of compiler
// or linter, get diff and filter the results by diff, and report filtered
// results.
type Reviewdog struct {
toolname string
p parser.Parser
c CommentService
d DiffService
filterMode filter.Mode
failOnError bool
}
// NewReviewdog returns a new Reviewdog.
func NewReviewdog(toolname string, p parser.Parser, c CommentService, d DiffService, filterMode filter.Mode, failOnError bool) *Reviewdog {
return &Reviewdog{p: p, c: c, d: d, toolname: toolname, filterMode: filterMode, failOnError: failOnError}
}
// RunFromResult creates a new Reviewdog and runs it with check results.
func RunFromResult(ctx context.Context, c CommentService, results []*rdf.Diagnostic,
filediffs []*diff.FileDiff, strip int, toolname string, filterMode filter.Mode, failOnError bool) error {
return (&Reviewdog{c: c, toolname: toolname, filterMode: filterMode, failOnError: failOnError}).runFromResult(ctx, results, filediffs, strip, failOnError)
}
// Comment represents a reported result as a comment.
type Comment struct {
Result *filter.FilteredDiagnostic
ToolName string
}
// CommentService is an interface which posts Comment.
type CommentService interface {
Post(context.Context, *Comment) error
}
// BulkCommentService posts comments all at once when Flush() is called.
// Flush() will be called at the end of reviewdog run.
type BulkCommentService interface {
CommentService
Flush(context.Context) error
}
// DiffService is an interface which get diff.
type DiffService interface {
Diff(context.Context) ([]byte, error)
Strip() int
}
func (w *Reviewdog) runFromResult(ctx context.Context, results []*rdf.Diagnostic,
filediffs []*diff.FileDiff, strip int, failOnError bool) error {
wd, err := os.Getwd()
if err != nil {
return err
}
checks := filter.FilterCheck(results, filediffs, strip, wd, w.filterMode)
hasViolations := false
for _, check := range checks {
if !check.ShouldReport {
continue
}
comment := &Comment{
Result: check,
ToolName: w.toolname,
}
if err := w.c.Post(ctx, comment); err != nil {
return err
}
hasViolations = true
}
if bulk, ok := w.c.(BulkCommentService); ok {
if err := bulk.Flush(ctx); err != nil {
return err
}
}
if failOnError && hasViolations {
return fmt.Errorf("input data has violations")
}
return nil
}
// Run runs Reviewdog application.
func (w *Reviewdog) Run(ctx context.Context, r io.Reader) error {
results, err := w.p.Parse(r)
if err != nil {
return fmt.Errorf("parse error: %w", err)
}
d, err := w.d.Diff(ctx)
if err != nil {
return fmt.Errorf("fail to get diff: %w", err)
}
filediffs, err := diff.ParseMultiFile(bytes.NewReader(d))
if err != nil {
return fmt.Errorf("fail to parse diff: %w", err)
}
return w.runFromResult(ctx, results, filediffs, w.d.Strip(), w.failOnError)
}