forked from reviewdog/reviewdog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreviewdog.go
123 lines (104 loc) · 3 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
119
120
121
122
123
package reviewdog
import (
"bytes"
"context"
"fmt"
"io"
"os"
"github.com/reviewdog/reviewdog/diff"
)
// 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
c CommentService
d DiffService
}
// NewReviewdog returns a new Reviewdog.
func NewReviewdog(toolname string, p Parser, c CommentService, d DiffService) *Reviewdog {
return &Reviewdog{p: p, c: c, d: d, toolname: toolname}
}
func RunFromResult(ctx context.Context, c CommentService, results []*CheckResult,
filediffs []*diff.FileDiff, strip int, toolname string) error {
return (&Reviewdog{c: c, toolname: toolname}).runFromResult(ctx, results, filediffs, strip)
}
// CheckResult represents a checked result of static analysis tools.
// :h error-file-format
type CheckResult struct {
Path string // relative file path
Lnum int // line number
Col int // column number (1 <tab> == 1 character column)
Message string // error message
Lines []string // Original error lines (often one line)
}
// Parser is an interface which parses compilers, linters, or any tools
// results.
type Parser interface {
Parse(r io.Reader) ([]*CheckResult, error)
}
// Comment represents a reported result as a comment.
type Comment struct {
*CheckResult
Body string
LnumDiff int
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 []*CheckResult,
filediffs []*diff.FileDiff, strip int) error {
wd, err := os.Getwd()
if err != nil {
return err
}
checks := FilterCheck(results, filediffs, strip, wd)
for _, check := range checks {
if !check.InDiff {
continue
}
comment := &Comment{
CheckResult: check.CheckResult,
Body: check.Message, // TODO: format message
LnumDiff: check.LnumDiff,
ToolName: w.toolname,
}
if err := w.c.Post(ctx, comment); err != nil {
return err
}
}
if bulk, ok := w.c.(BulkCommentService); ok {
return bulk.Flush(ctx)
}
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: %v", err)
}
d, err := w.d.Diff(ctx)
if err != nil {
return fmt.Errorf("fail to get diff: %v", err)
}
filediffs, err := diff.ParseMultiFile(bytes.NewReader(d))
if err != nil {
return fmt.Errorf("fail to parse diff: %v", err)
}
return w.runFromResult(ctx, results, filediffs, w.d.Strip())
}