-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathdummy.go
More file actions
176 lines (145 loc) · 3.76 KB
/
dummy.go
File metadata and controls
176 lines (145 loc) · 3.76 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
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
package dummy
import (
"bytes"
"context"
"fmt"
"strings"
"github.com/src-d/lookout"
"gopkg.in/src-d/go-git.v4/utils/binary"
"gopkg.in/src-d/lookout-sdk.v0/pb"
)
type Analyzer struct {
Version string
DataClient *lookout.DataClient
RequestUAST bool
RequestFilesPush bool
}
var _ lookout.AnalyzerServer = &Analyzer{}
func (a *Analyzer) NotifyReviewEvent(ctx context.Context, e *pb.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,
IncludePattern: ".*",
ExcludePattern: "^should-never-match$",
})
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.maxLineLen(change.Head)...)
if a.RequestUAST {
resp.Comments = append(resp.Comments, a.hasUAST(change.Head)...)
resp.Comments = append(resp.Comments, a.language(change.Head)...)
}
}
if err := changes.Err(); err != nil {
return nil, err
}
return resp, nil
}
func (a *Analyzer) NotifyPushEvent(ctx context.Context, e *pb.PushEvent) (*lookout.EventResponse, error) {
resp := &lookout.EventResponse{AnalyzerVersion: a.Version}
if !a.RequestFilesPush {
return resp, nil
}
files, err := a.DataClient.GetFiles(ctx, &lookout.FilesRequest{
Revision: &e.CommitRevision.Head,
ExcludeVendored: true,
WantContents: true,
WantUAST: a.RequestUAST,
IncludePattern: ".*",
ExcludePattern: "^should-never-match$",
})
if err != nil {
return nil, err
}
for files.Next() {
file := files.File()
resp.Comments = append(resp.Comments, a.maxLineLen(file)...)
if a.RequestUAST {
resp.Comments = append(resp.Comments, a.hasUAST(file)...)
resp.Comments = append(resp.Comments, a.language(file)...)
}
}
if err := files.Err(); err != nil {
return nil, err
}
return resp, 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),
}}
}
const maxLineLength = 120
func (a *Analyzer) maxLineLen(file *lookout.File) []*lookout.Comment {
if file == nil || a.isBinary(file) {
return nil
}
lines := strings.Split(string(file.Content), "\n")
var comments []*lookout.Comment
for i, line := range lines {
if len(line) > maxLineLength {
comments = append(comments, &lookout.Comment{
File: file.Path,
Line: int32(i + 1),
Text: fmt.Sprintf("This line exceeded %d chars.", maxLineLength),
})
}
}
return comments
}
func (a *Analyzer) hasUAST(file *lookout.File) []*lookout.Comment {
if file == nil {
return nil
}
var text string
if file.UAST == nil {
text = "The file doesn't have UAST."
} else {
text = "The file has UAST."
}
return []*lookout.Comment{{
File: file.Path,
Line: 0,
Text: text,
}}
}
func (a *Analyzer) language(file *lookout.File) []*lookout.Comment {
if file == nil {
return nil
}
return []*lookout.Comment{{
File: file.Path,
Line: 0,
Text: fmt.Sprintf("The file has language detected: %q", file.Language),
}}
}
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"))
}