Skip to content

Commit

Permalink
fix(gomodcheck): use modfile
Browse files Browse the repository at this point in the history
  • Loading branch information
CarlJi committed Sep 2, 2024
1 parent 4338e56 commit a85e340
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 34 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ require (
github.com/qiniu/x v1.13.10
github.com/sirupsen/logrus v1.9.3
github.com/stretchr/testify v1.9.0
golang.org/x/mod v0.10.0
sigs.k8s.io/prow v0.0.0-20230209194617-a36077c30491
sigs.k8s.io/yaml v1.4.0
)
Expand Down Expand Up @@ -48,7 +49,6 @@ require (
go.opentelemetry.io/otel/metric v1.29.0 // indirect
go.opentelemetry.io/otel/sdk v1.29.0 // indirect
go.opentelemetry.io/otel/trace v1.29.0 // indirect
golang.org/x/mod v0.10.0 // indirect
golang.org/x/sys v0.24.0 // indirect
golang.org/x/tools v0.9.3 // indirect
google.golang.org/protobuf v1.34.2 // indirect
Expand Down
52 changes: 19 additions & 33 deletions internal/linters/go/gomodcheck/gomodcheck.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
package gomodcheck

import (
"bufio"
"context"
"os"
"regexp"

"path/filepath"
"strings"

"github.com/qiniu/reviewbot/config"
"github.com/qiniu/reviewbot/internal/linters"

"github.com/qiniu/x/xlog"
"golang.org/x/mod/modfile"
)

var lintName = "gomodcheck"
Expand All @@ -36,45 +33,34 @@ func goModCheckHandler(ctx context.Context, a linters.Agent) error {
func goModCheckOutput(log *xlog.Logger, a linters.Agent) (map[string][]linters.LinterOutput, error) {
output := make(map[string][]linters.LinterOutput)
for _, file := range a.PullRequestChangedFiles {
if filepath.Ext(file.GetFilename()) != ".mod" {
fName := file.GetFilename()
if !strings.HasSuffix(fName, "go.mod") {
continue
}

replaceRegex := regexp.MustCompile(`^replace\s+([^\s]+)\s+=>\s+([^\s]+)`)
goModPath := filepath.Join(a.RepoDir, file.GetFilename())
file, err := os.Open(goModPath)
goModPath := filepath.Join(a.RepoDir, fName)
file, err := os.ReadFile(goModPath)
if err != nil {
log.Errorf("Error opening %s: %s", goModPath, err)
continue
return output, err
}
defer file.Close()

scanner := bufio.NewScanner(file)
lineNumber := 0
filename := strings.TrimPrefix(goModPath, a.RepoDir+"/")
msg := "It is not recommended to use `replace ../xxx` to specify dependency "

for scanner.Scan() {
lineNumber++
line := scanner.Text()
if matches := replaceRegex.FindStringSubmatch(line); len(matches) > 0 {
replacementPath := matches[2]
if strings.HasPrefix(replacementPath, "../") {
output[filename] = append(output[filename], linters.LinterOutput{
File: filename,
Line: lineNumber,
Column: 1,
Message: msg,
})
}
}
mod, err := modfile.Parse("go.mod", file, nil)
if err != nil {
log.Errorf("Error parsing %s: %s", goModPath, err)
return output, err
}

if err := scanner.Err(); err != nil {
log.Errorf("Error reading go.mod: %s", err)
continue
for _, replace := range mod.Replace {
if strings.HasPrefix(replace.New.Path, "../") {
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
Expand Down

0 comments on commit a85e340

Please sign in to comment.