-
Notifications
You must be signed in to change notification settings - Fork 15
/
gofmt.go
242 lines (216 loc) · 7.36 KB
/
gofmt.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
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/*
Copyright 2024 Qiniu Cloud (qiniu.com).
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package gofmt
import (
"bytes"
"context"
"errors"
"os/exec"
"regexp"
"strconv"
"strings"
"github.com/qiniu/reviewbot/config"
"github.com/qiniu/reviewbot/internal/linters"
"github.com/qiniu/reviewbot/internal/lintersutil"
"github.com/qiniu/x/log"
"github.com/qiniu/x/xlog"
)
var lintName = "gofmt"
func init() {
linters.RegisterPullRequestHandler(lintName, gofmtHandler)
linters.RegisterLinterLanguages(lintName, []string{".go"})
}
func gofmtHandler(ctx context.Context, a linters.Agent) error {
log := lintersutil.FromContext(ctx)
if linters.IsEmpty(a.LinterConfig.Args...) {
a.LinterConfig.Args = append([]string{}, "-d", "./")
}
// Since GitHub's check run feature does not have the suggestion functionality, GitHub PR review is fixed used to display gofmt reports.
// Details: https://github.com/qiniu/reviewbot/issues/166
a.LinterConfig.ReportFormat = config.GithubPRReview
executor, err := NewgofmtExecutor(a.LinterConfig.WorkDir)
if err != nil {
log.Errorf("init gofmt executor failed: %v", err)
return err
}
output, err := executor.Run(log, a.LinterConfig.Args...)
if err != nil {
log.Errorf("gofmt run failed: %v", err)
return err
}
parsedOutput, err := executor.Parse(log, output)
if err != nil {
log.Errorf("gofmt parse output failed: %v", err)
return err
}
return linters.Report(ctx, a, parsedOutput)
}
type Gofmt struct {
dir string
gofmt string
execute func(dir, command string, args ...string) ([]byte, []byte, error)
}
func NewgofmtExecutor(dir string) (linters.Linter, error) {
log.Infof("gofmt executor init")
g, err := exec.LookPath("gofmt")
if err != nil {
return nil, err
}
return &Gofmt{
dir: dir,
gofmt: g,
execute: func(dir, command string, args ...string) ([]byte, []byte, error) {
c := exec.Command(command, args...)
c.Dir = dir
log.Printf("final command: %v \n", c)
if c.Stdout != nil {
return nil, nil, errors.New("exec: Stdout already set")
}
if c.Stderr != nil {
return nil, nil, errors.New("exec: Stderr already set")
}
var stdoutBuffer bytes.Buffer
var stderrBuffer bytes.Buffer
c.Stdout = &stdoutBuffer
c.Stderr = &stderrBuffer
err := c.Run()
return stdoutBuffer.Bytes(), stderrBuffer.Bytes(), err
},
}, nil
}
func (g *Gofmt) Run(log *xlog.Logger, args ...string) ([]byte, error) {
stdoutput, stderr, err := g.execute(g.dir, g.gofmt, args...)
if err != nil {
log.Warnf("gofmt run with status: %v, mark and continue", err)
if stderr != nil {
log.Warnf("gofmt run cause stderr: %s, mark and continue", stderr)
}
return stdoutput, nil
} else {
log.Infof("gofmt running succeeded")
}
return stdoutput, nil
}
func (g *Gofmt) Parse(log *xlog.Logger, output []byte) (map[string][]linters.LinterOutput, error) {
log.Infof("gofmt output is being parsed")
return formatGofmtOutput(output)
}
// Go Doc Comments Info: https://tip.golang.org/doc/comment
func formatGofmtOutput(output []byte) (map[string][]linters.LinterOutput, error) {
result := make(map[string][]linters.LinterOutput)
lines := strings.Split(string(output), "\n")
// map[$filename]map[$diffname][]string
fileErr := make(map[string]map[string][]string)
// filename eg. test/test.go
var filename string
// diffname eg. @@ -19,5 +19,5 @@
var diffname string
for _, line := range lines {
if strings.HasPrefix(line, "diff") {
fields := strings.Fields(line)
if len(fields) >= 3 {
filename = fields[2]
}
fileErr[filename] = map[string][]string{}
} else if filename != "" {
if strings.HasPrefix(line, "@@") {
diffname = line
fileErr[filename][diffname] = []string{}
} else if diffname != "" && !strings.HasPrefix(line, "-") {
fileErr[filename][diffname] = append(fileErr[filename][diffname], line)
}
}
}
type eachDiffInfo struct {
// diffLineCount is means that how many diff lines
diffLineCount int
// firstDiffLine refers to the first line of diff lines
firstDiffLine int
// fixedMessage refers to the diff fixed lines
fixedMessage string
// tmpLine refers to the line immediately following the blank line.
tmpLine string
}
regexpDiff := regexp.MustCompile(`-(\d+),(\d+)`)
for filename, errmsgs := range fileErr {
for diffname, errmsg := range errmsgs {
//"diffStartLine" refers to the starting line of a diff context, indicating the beginning position of a diff block.
var diffStartLine int64
match := regexpDiff.FindStringSubmatch(diffname)
if len(match) > 2 {
diffStartLine, _ = strconv.ParseInt(match[1], 10, 64)
}
var currentInfo eachDiffInfo
for i, line := range errmsg {
if strings.HasPrefix(line, "+") {
if currentInfo.firstDiffLine == 0 {
currentInfo.firstDiffLine = i + 1
}
currentInfo.diffLineCount++
currentInfo.fixedMessage += strings.TrimLeft(line, "+") + "\n"
if strings.TrimLeft(line, "+") == "" && i+1 < len(errmsg) {
// If trimLeft line is only a blank line, temporarily store tmpLine.
currentInfo.tmpLine = errmsg[i+1]
}
} else {
if currentInfo.fixedMessage != "" {
// If fixedMessage is either just a blank line or ends with a blank line.
// In order to display the GitHub suggestion comment correctly, it should be appended with tmpLine.
if currentInfo.fixedMessage == "\n" || strings.HasSuffix(currentInfo.fixedMessage, "\n\n") {
currentInfo.fixedMessage += currentInfo.tmpLine + "\n"
}
finalMessage := " Is your code not properly formatted? Here are some suggestions below\n```suggestion\n" + currentInfo.fixedMessage + "```"
addGofmtOutput(result, filename, diffStartLine, int64(currentInfo.firstDiffLine), int64(currentInfo.diffLineCount), finalMessage)
}
currentInfo = eachDiffInfo{}
}
}
}
}
return result, nil
}
func addGofmtOutput(result map[string][]linters.LinterOutput, filename string, diffStartLine, firstDiffLine, diffLineCount int64, message string) {
var output *linters.LinterOutput
// If diffLineCount==1, set a single-line comment on GitHub; otherwise, set a multi-line comment.
if diffLineCount == 1 {
output = &linters.LinterOutput{
File: filename,
Line: int(diffStartLine + firstDiffLine - 1),
Column: int(diffLineCount),
Message: message,
}
} else {
output = &linters.LinterOutput{
File: filename,
Line: int(diffStartLine + firstDiffLine + diffLineCount - 2),
Column: int(diffLineCount),
Message: message,
StartLine: int(diffStartLine + firstDiffLine - 1),
}
}
if outs, ok := result[output.File]; !ok {
result[output.File] = []linters.LinterOutput{*output}
} else {
// remove duplicate
var existed bool
for _, v := range outs {
if v.File == output.File && v.Line == output.Line && v.Column == output.Column && v.Message == output.Message {
existed = true
break
}
}
if !existed {
result[output.File] = append(result[output.File], *output)
}
}
}