-
Notifications
You must be signed in to change notification settings - Fork 15
/
gomodcheck.go
104 lines (88 loc) · 2.85 KB
/
gomodcheck.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
/*
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 gomodcheck
import (
"context"
"os"
"path/filepath"
"strings"
"github.com/qiniu/reviewbot/internal/linters"
"github.com/qiniu/reviewbot/internal/lintersutil"
"github.com/qiniu/x/xlog"
"golang.org/x/mod/modfile"
)
var lintName = "gomodcheck"
func init() {
linters.RegisterPullRequestHandler(lintName, goModCheckHandler)
linters.RegisterLinterLanguages(lintName, []string{".go", ".mod"})
}
func goModCheckHandler(ctx context.Context, a linters.Agent) error {
log := lintersutil.FromContext(ctx)
parsedOutput, err := goModCheckOutput(log, a)
if err != nil {
log.Errorf("gomodchecks parse output failed: %v", err)
return err
}
return linters.Report(ctx, a, parsedOutput)
}
func goModCheckOutput(log *xlog.Logger, a linters.Agent) (map[string][]linters.LinterOutput, error) {
output := make(map[string][]linters.LinterOutput)
for _, file := range a.Provider.GetFiles(nil) {
fName := file
if !strings.HasSuffix(fName, "go.mod") {
continue
}
goModPath := filepath.Join(a.RepoDir, fName)
file, err := os.ReadFile(goModPath)
if err != nil {
log.Errorf("Error opening %s: %s", goModPath, err)
return output, err
}
mod, err := modfile.Parse("go.mod", file, nil)
if err != nil {
log.Errorf("Error parsing %s: %s", goModPath, err)
return output, err
}
for _, replace := range mod.Replace {
if !strings.HasPrefix(replace.New.Path, "../") {
continue
}
parsePath := filepath.Join(filepath.Dir(goModPath), replace.New.Path)
isSub, err := isSubdirectory(a.RepoDir, parsePath)
if err != nil {
log.Errorf("failed to compare whether A is a subdirectory of B : %v", err)
}
if !isSub {
output[fName] = append(output[fName], linters.LinterOutput{
File: fName,
Line: replace.Syntax.Start.Line,
Column: replace.Syntax.Start.LineRune,
Message: "cross-repository local replacement are not allowed[reviewbot]\nfor more information see https://github.com/qiniu/reviewbot/issues/275",
})
}
}
}
return output, nil
}
// isSubdirectory reports whether the string b is subdirectory of a.
func isSubdirectory(a, b string) (bool, error) {
absA, err := filepath.Abs(a)
if err != nil {
return false, err
}
absB, err := filepath.Abs(b)
if err != nil {
return false, err
}
return strings.HasPrefix(absB, absA), nil
}