-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathdummy.go
More file actions
133 lines (109 loc) · 2.72 KB
/
dummy.go
File metadata and controls
133 lines (109 loc) · 2.72 KB
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
package dummy
import (
"bytes"
"context"
"fmt"
"github.com/src-d/lookout"
"gopkg.in/src-d/go-git.v4/utils/binary"
)
type Analyzer struct {
Version string
DataClient *lookout.DataClient
RequestUAST bool
}
var _ lookout.AnalyzerServer = &Analyzer{}
func (a *Analyzer) NotifyReviewEvent(ctx context.Context, e *lookout.ReviewEvent) (
*lookout.EventResponse, error) {
changes, err := a.DataClient.GetChanges(ctx, &lookout.ChangesRequest{
Base: &e.CommitRevision.Base,
Head: &e.CommitRevision.Head,
WantContents: true,
WantUAST: a.RequestUAST,
})
if err != nil {
return nil, err
}
resp := &lookout.EventResponse{AnalyzerVersion: a.Version}
for changes.Next() {
change := changes.Change()
resp.Comments = append(resp.Comments, a.lineIncrease(change)...)
resp.Comments = append(resp.Comments, a.maxLineWidth(change)...)
if a.RequestUAST {
resp.Comments = append(resp.Comments, a.noUAST(change)...)
}
}
if err := changes.Err(); err != nil {
return nil, err
}
return resp, nil
}
func (a *Analyzer) NotifyPushEvent(ctx context.Context, e *lookout.PushEvent) (*lookout.EventResponse, error) {
return &lookout.EventResponse{
AnalyzerVersion: a.Version,
Comments: []*lookout.Comment{
{Text: fmt.Sprintf(
"dummy comment for push event: %s -> %s (%d commits)",
e.CommitRevision.Base,
e.CommitRevision.Head,
e.Commits,
)},
},
}, nil
}
func (a *Analyzer) lineIncrease(ch *lookout.Change) []*lookout.Comment {
if a.isBinary(ch.Head) || a.isBinary(ch.Base) {
return nil
}
diff := a.countLines(ch.Head) - a.countLines(ch.Base)
if diff <= 0 {
return nil
}
return []*lookout.Comment{{
File: ch.Head.Path,
Line: 0,
Text: fmt.Sprintf("The file has increased in %d lines.", diff),
}}
}
func (a *Analyzer) maxLineWidth(ch *lookout.Change) []*lookout.Comment {
if ch.Head == nil {
return nil
}
lines := bytes.Split(ch.Head.Content, []byte("\n"))
var comments []*lookout.Comment
for i, line := range lines {
if len(line) > 80 {
comments = append(comments, &lookout.Comment{
File: ch.Head.Path,
Line: int32(i + 1),
Text: "This line exceeded 80 bytes.",
})
}
}
return comments
}
func (a *Analyzer) noUAST(ch *lookout.Change) []*lookout.Comment {
if ch.Head == nil {
return nil
}
if ch.Head.UAST == nil {
return []*lookout.Comment{{
File: ch.Head.Path,
Line: 0,
Text: fmt.Sprintf("The file doesn't have UAST."),
}}
}
return nil
}
func (a *Analyzer) isBinary(f *lookout.File) bool {
if f == nil {
return false
}
ok, err := binary.IsBinary(bytes.NewReader(f.Content))
return err != nil || ok
}
func (a *Analyzer) countLines(f *lookout.File) int {
if f == nil {
return 0
}
return bytes.Count(f.Content, []byte("\n"))
}