From a85e340d81140d5f08f78612d3232ab7b6468d98 Mon Sep 17 00:00:00 2001 From: jichangjun Date: Mon, 2 Sep 2024 19:30:37 +0800 Subject: [PATCH] fix(gomodcheck): use modfile --- go.mod | 2 +- internal/linters/go/gomodcheck/gomodcheck.go | 52 +++++++------------- 2 files changed, 20 insertions(+), 34 deletions(-) diff --git a/go.mod b/go.mod index ddc0135..311ca5c 100644 --- a/go.mod +++ b/go.mod @@ -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 ) @@ -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 diff --git a/internal/linters/go/gomodcheck/gomodcheck.go b/internal/linters/go/gomodcheck/gomodcheck.go index cafac2c..705e5df 100644 --- a/internal/linters/go/gomodcheck/gomodcheck.go +++ b/internal/linters/go/gomodcheck/gomodcheck.go @@ -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" @@ -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