-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathgolangci_lint.go
433 lines (367 loc) · 12.8 KB
/
golangci_lint.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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
/*
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 golangcilint
import (
"context"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/qiniu/reviewbot/config"
"github.com/qiniu/reviewbot/internal/lint"
"github.com/qiniu/reviewbot/internal/util"
"github.com/qiniu/x/log"
"github.com/qiniu/x/xlog"
)
// refer to https://golangci-lint.run/
var lintName = "golangci-lint"
func init() {
lint.RegisterPullRequestHandler(lintName, golangciLintHandler)
lint.RegisterLinterLanguages(lintName, []string{".go", ".mod", ".sum"})
}
func golangciLintHandler(ctx context.Context, a lint.Agent) error {
log := util.FromContext(ctx)
var goModDirs []string
if len(a.LinterConfig.Command) == 0 || (len(a.LinterConfig.Command) == 1 && a.LinterConfig.Command[0] == lintName) {
// Default mode, automatically find the go.mod path in current repo
goModDirs = findGoModDirs(a)
log.Infof("find go.mod in dirs: %v", goModDirs)
// Default mode, automatically apply parameters.
a = argsApply(log, a)
} else if a.LinterConfig.ConfigPath != "" {
// Custom mode, only apply golangci-lint configuration if necessary.
path := golangciConfigApply(log, a)
log.Infof("golangci-lint config prepared: %v", path)
}
log.Infof("golangci-lint run config: %v", a.LinterConfig)
// When the go.mod file is not found, set GO111MODULE=off, so that golangci does not run through gomod.
if len(goModDirs) == 0 {
a.LinterConfig.Env = append(a.LinterConfig.Env, "GO111MODULE=off")
return lint.GeneralHandler(ctx, log, a, lint.ExecRun, parser)
}
// if the workDir is not specified, align it with the directory of the root-level go.mod file.
if a.LinterConfig.WorkDir == a.RepoDir && len(goModDirs) > 0 {
topDir := topLevelGoModDir(goModDirs)
log.Infof("align workDir with the directory of the root-level go.mod file: %v", topDir)
a.LinterConfig.WorkDir = topDir
}
a.LinterConfig.Modifier = newGoModTidyBuilder(a.LinterConfig.Modifier, goModDirs)
a.LinterConfig.Modifier = newGitConfigModifier(a.LinterConfig.Modifier, a.Provider)
return lint.GeneralHandler(ctx, log, a, lint.ExecRun, parser)
}
type gitConfigModifier struct {
prev config.Modifier
provider lint.Provider
}
func newGitConfigModifier(prev config.Modifier, provider lint.Provider) config.Modifier {
return &gitConfigModifier{
prev: prev,
provider: provider,
}
}
func (g *gitConfigModifier) Modify(cfg *config.Linter) (*config.Linter, error) {
base, err := g.prev.Modify(cfg)
if err != nil {
return nil, err
}
newCfg := base
token, err := g.provider.GetToken()
if err != nil {
return nil, err
}
info := g.provider.GetProviderInfo()
var gitUsername string
switch info.Platform {
case config.GitHub:
// see https://docs.github.com/zh/apps/creating-github-apps/writing-code-for-a-github-app/building-ci-checks-with-a-github-app#add-code-to-clone-a-repository
gitUsername = "x-access-token"
case config.GitLab:
// see https://docs.gitlab.com/ee/api/oauth2.html#access-git-over-https-with-access-token
gitUsername = "oauth2"
}
// delete the old git config
deleteOldConfigCmd := fmt.Sprintf(`git config --global -l | grep "url.*%s" | grep -v "${ACCESS_TOKEN}" | sed 's/=.*//' | while read key; do git config --global --unset "$key"; done`, gitUsername)
// get the current git config
currentConfigCmd := fmt.Sprintf("git config --global -l | grep \"url.*%s\" || true", gitUsername)
// add the new git config
addNewConfigCmd := fmt.Sprintf(`git config --global "url.https://%s:${ACCESS_TOKEN}@%s/.insteadOf" "git@%s:"
git config --global "url.https://%s:${ACCESS_TOKEN}@%s/.insteadOf" "https://%s/"`,
gitUsername, info.Host, info.Host,
gitUsername, info.Host, info.Host)
args := []string{
fmt.Sprintf(`
configure_git() {
# delete the old git config
%s || return 1
currentConfig=$(%s) || return 1
if ! echo "$currentConfig" | grep -q "${ACCESS_TOKEN}" || [ -z "$currentConfig" ]; then
# add the new git config
%s || return 1
fi
return 0
}
max_retries=5
retry_delay=1
i=1
while [ $i -le $max_retries ]; do
if configure_git; then
break
else
sleep $retry_delay
fi
i=$((i+1))
done
if [ $i -gt $max_retries ]; then
echo "WARNING: failed to configure git globally after $max_retries retries"
fi
`,
deleteOldConfigCmd,
currentConfigCmd,
addNewConfigCmd,
),
}
// add \n to the end of each command
args = append(args, "\n")
newCfg.Args = append(args, base.Args...)
newCfg.Env = append(newCfg.Env, "ACCESS_TOKEN="+token)
return newCfg, nil
}
type goModTidyModifier struct {
prev config.Modifier
goModDirs []string
}
func newGoModTidyBuilder(prev config.Modifier, goModDirs []string) config.Modifier {
return &goModTidyModifier{
prev: prev,
goModDirs: goModDirs,
}
}
func (b *goModTidyModifier) Modify(cfg *config.Linter) (*config.Linter, error) {
base, err := b.prev.Modify(cfg)
if err != nil {
return nil, err
}
newCfg := base
args := []string{}
for _, dir := range b.goModDirs {
args = append(args, fmt.Sprintf("cd %s > /dev/null && go mod tidy && cd - > /dev/null \n", dir))
}
newCfg.Args = append(args, base.Args...)
return newCfg, nil
}
func parser(log *xlog.Logger, output []byte) (map[string][]lint.LinterOutput, []string) {
log.Infof("golangci-lint output: %s", output)
trainer := func(o lint.LinterOutput) (*lint.LinterOutput, []string) {
// Perhaps it may not be precise enough?
// refer: https://golangci-lint.run/usage/linters/
if strings.Contains(o.Message, "(typecheck)") {
unexpected := fmt.Sprintf("%s:%d:%d: %s", o.File, o.Line, o.Column, o.Message)
return nil, []string{strings.TrimSpace(unexpected)}
}
return &o, []string{}
}
unexpected := make([]string, 0)
rawResults, rawUnexpected := lint.ParseV2(log, output, trainer)
for _, ex := range rawUnexpected {
// skip the warning level log
// example: level=warning msg="[linters_context] copyloopvar: this linter is disabled because the Go version (1.18) of your project is lower than Go 1.22"
// the warning level log is not a real lint error, so we need to skip it
if strings.Contains(ex, "level=warning") {
continue
}
// skip the go download log
if strings.Contains(ex, "go: downloading") || strings.Contains(ex, "go: finding") {
continue
}
// skip the docker artifact log flag
if strings.Contains(ex, "---artifacts-") {
continue
}
// skip the git config error since it will retry 5 times
if strings.Contains(ex, "unable to read config file '/root/.gitconfig'") {
continue
}
unexpected = append(unexpected, strings.TrimSpace(ex))
}
return rawResults, unexpected
}
// argsApply is used to set the default parameters for golangci-lint
// see: ./docs/website/docs/component/go/golangci-lint
func argsApply(log *xlog.Logger, a lint.Agent) lint.Agent {
config := a.LinterConfig
if len(config.Command) == 0 || len(config.Command) > 1 || config.Command[0] != lintName {
return a
}
legacyArgs := config.Args
switch {
case len(legacyArgs) == 0:
legacyArgs = []string{}
case len(legacyArgs) > 0 && legacyArgs[0] != "run":
return a
default:
legacyArgs = legacyArgs[1:]
}
newArgs := []string{"run"}
var (
timeoutFlag bool
parallelFlag bool
outFormatFlag bool
printFlag bool
configFlag bool
concurrencyFlag bool
)
for _, arg := range legacyArgs {
switch {
case strings.HasPrefix(arg, "--timeout"):
timeoutFlag = true
case strings.HasPrefix(arg, "--allow-parallel-runners"):
parallelFlag = true
case strings.HasPrefix(arg, "--out-format"):
outFormatFlag = true
case strings.HasPrefix(arg, "--print-issued-lines"):
printFlag = true
case strings.HasPrefix(arg, "--config"):
configFlag = true
case strings.HasPrefix(arg, "--concurrency"):
concurrencyFlag = true
}
newArgs = append(newArgs, arg)
}
if !timeoutFlag {
newArgs = append(newArgs, "--timeout=15m0s")
}
if !parallelFlag {
newArgs = append(newArgs, "--allow-parallel-runners=true")
}
if !outFormatFlag {
newArgs = append(newArgs, "--out-format=line-number")
}
if !printFlag {
newArgs = append(newArgs, "--print-issued-lines=false")
}
if !concurrencyFlag {
newArgs = append(newArgs, "--concurrency=8")
}
if !configFlag && config.ConfigPath != "" {
config.ConfigPath = golangciConfigApply(log, a)
newArgs = append(newArgs, "--config", config.ConfigPath)
}
config.Args = newArgs
a.LinterConfig = config
return a
}
// golangciConfigApply is used to get the config file path based on rules as below:
// 1. if the config file exists in current directory, return its absolute path.
// 2. if the config file exists in the workDir directory, return its absolute path.
// 3. if the config file exists in the repo linter ConfigPath.
func golangciConfigApply(log *xlog.Logger, a lint.Agent) string {
// refer to https://golangci-lint.run/usage/configuration/
// the default config file name is .golangci.yml, .golangci.yaml, .golangci.json, .golangci.toml
golangciConfigFiles := []string{".golangci.yml", ".golangci.yaml", ".golangci.json", ".golangci.toml"}
// if the config file exists in the current directory, return its absolute path
for _, file := range golangciConfigFiles {
if path, exist := util.FileExists(file); exist {
return path
}
}
// if the config file exists in the workDir directory, return its absolute path
if a.LinterConfig.WorkDir != "" {
for _, file := range golangciConfigFiles {
if path, exist := util.FileExists(a.LinterConfig.WorkDir + "/" + file); exist {
return path
}
}
}
if a.LinterConfig.ConfigPath == "" {
return ""
}
path, exist := util.FileExists(a.LinterConfig.ConfigPath)
if !exist {
// if the config file is not found, these probably not configured.
// here we still return the original config path which may let user know the config is wrong.
return a.LinterConfig.ConfigPath
}
// WorkDir is the working directory of the linter, it is a temporary directory
currentDir := a.LinterConfig.WorkDir
// if the config file is outside the repo, copy it to the repo
if !strings.Contains(path, currentDir) {
// copy the config file to the repo
data, err := os.ReadFile(path)
if err != nil {
log.Warnf("failed to read config file: %v, err: %v", path, err)
return ""
}
targetFile := filepath.Join(currentDir, filepath.Base(path))
if err := os.WriteFile(targetFile, data, 0o600); err != nil {
log.Warnf("failed to write config file: %v ,err: %v", targetFile, err)
return ""
}
log.Infof("copy config file from %v to %v", path, targetFile)
return targetFile
}
return path
}
// findGoModDirs aims to find the dirs where go.mod files exist in the current repo based on the changed files.
func findGoModDirs(a lint.Agent) []string {
// it means WorkDir is specified via the config file probably, so we don't need to find go.mod
if a.LinterConfig.WorkDir != a.RepoDir {
log.Infof("WorkDir does not match the repo dir, so we don't need to find go.mod. WorkDir: %v, RepoDir: %v", a.LinterConfig.WorkDir, a.RepoDir)
return []string{}
}
var goModDirs []string
dirs := extractDirs(a.Provider.GetFiles(nil))
for _, dir := range dirs {
goModFile := filepath.Join(a.RepoDir, dir, "go.mod")
if _, exist := util.FileExists(goModFile); exist {
goModDirs = append(goModDirs, filepath.Join(a.RepoDir, dir))
}
}
return goModDirs
}
func extractDirs(files []string) []string {
directorySet := make(map[string]bool)
for _, file := range files {
if filepath.Ext(file) != ".go" && filepath.Ext(file) != ".mod" && filepath.Ext(file) != ".sum" {
continue
}
dir := filepath.Dir(file)
// Split the directory path into individual directories
dirs := strings.Split(dir, string(filepath.Separator))
prefix := "."
// root directory
directorySet[prefix] = true
for _, d := range dirs {
prefix = filepath.Join(prefix, d)
// current directory
directorySet[prefix] = true
}
}
directories := make([]string, 0, len(directorySet))
for dir := range directorySet {
directories = append(directories, dir)
}
return directories
}
// topLevelGoModDir is used to find the root-level go.mod directory.
func topLevelGoModDir(gomodDirs []string) string {
rootLevelGoModDir := gomodDirs[0]
minDepth := strings.Count(rootLevelGoModDir, string(filepath.Separator))
for i := 1; i < len(gomodDirs); i++ {
depth := strings.Count(gomodDirs[i], string(filepath.Separator))
if depth < minDepth {
minDepth = depth
rootLevelGoModDir = gomodDirs[i]
}
}
return rootLevelGoModDir
}