-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathreview.go
123 lines (102 loc) · 2.72 KB
/
review.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 review
import (
"bufio"
"fmt"
"io"
"strconv"
"strings"
"golang.org/x/build/gerrit"
)
const (
ErrNoProblemsFound ErrorString = "no problems to convert into review comments"
ErrSplitLine ErrorString = "failed to split line to location and description"
ErrSplitLocation ErrorString = "failed split location to filename and position"
)
type ErrorString string
func (e ErrorString) Error() string { return string(e) }
type Problem struct {
FileName string
Description string
Position
}
type Position struct {
Line int
Column int
}
type ParseError struct {
LineNumber int
Err error
}
func (e *ParseError) Error() string {
return fmt.Sprintf("could not parse line: %d, %s", e.LineNumber, e.Err)
}
// LinesToReviewComments reads linter reports and converts those to Gerrit Review Comments.
// Key in returned map is file name and value all associated problems as comments.
func LinesToReviewComments(r io.Reader) (comments map[string][]gerrit.CommentInput, err error) {
comments = map[string][]gerrit.CommentInput{}
scanner := bufio.NewScanner(r)
lineNumber := 0
for scanner.Scan() {
lineNumber++
line := scanner.Text()
if strings.HasPrefix(line, "#") {
continue
}
problem, err := parseLine(line)
if err != nil {
return nil, &ParseError{LineNumber: lineNumber, Err: err}
}
if _, ok := comments[problem.FileName]; !ok {
comments[problem.FileName] = []gerrit.CommentInput{}
}
comments[problem.FileName] = append(comments[problem.FileName], gerrit.CommentInput{
Line: problem.Line,
Message: problem.Description,
})
}
if err := scanner.Err(); err != nil {
return nil, err
}
if len(comments) == 0 {
return nil, ErrNoProblemsFound
}
return comments, nil
}
func parseLine(line string) (Problem, error) {
report := Problem{}
parts := strings.SplitN(line, " ", 2)
if len(parts) != 2 {
return report, ErrSplitLine
}
locParts := strings.SplitAfter(parts[0], ".go")
if len(locParts) < 2 {
return Problem{}, ErrSplitLocation
}
report.FileName = strings.Join(locParts[0:len(locParts)-1], "")
pos, err := parsePosition(locParts[len(locParts)-1])
if err != nil {
return report, err
}
report.Position = pos
report.Description = parts[1]
return report, nil
}
func parsePosition(text string) (pos Position, err error) {
parts := strings.FieldsFunc(text, func(c rune) bool { return c == ':' })
switch {
case len(parts) > 1:
i, err := strconv.Atoi(parts[1])
if err != nil {
return pos, fmt.Errorf("expected column number but got: %s", parts[1])
}
pos.Column = i
fallthrough
case len(parts) > 0:
i, err := strconv.Atoi(parts[0])
if err != nil {
return pos, fmt.Errorf("expected line number but got: %s", parts[0])
}
pos.Line = i
}
return pos, nil
}