Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: new gomodcheck linter #308

Merged
merged 1 commit into from
Sep 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 81 additions & 0 deletions internal/linters/go/gomodcheck/gomodcheck.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
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"
)

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 := xlog.New(ctx.Value(config.EventGUIDKey).(string))
parsedOutput, err := goModCheckOutput(log, a)
if err != nil {
log.Errorf("gomodchecks parse output failed: %v", err)
return err
}
return linters.Report(log, 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.PullRequestChangedFiles {
if filepath.Ext(file.GetFilename()) != ".mod" {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

直接用 file.GetFilename() != "go.mod" 判断?

continue
}

replaceRegex := regexp.MustCompile(`^replace\s+([^\s]+)\s+=>\s+([^\s]+)`)
goModPath := filepath.Join(a.RepoDir, file.GetFilename())
file, err := os.Open(goModPath)
if err != nil {
log.Errorf("Error opening %s: %s", goModPath, err)
continue
}
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,
})
}
}
}

if err := scanner.Err(); err != nil {
log.Errorf("Error reading go.mod: %s", err)
continue
}

}

return output, nil
}
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import (
_ "github.com/qiniu/reviewbot/internal/linters/git-flow/commit-check"
_ "github.com/qiniu/reviewbot/internal/linters/go/gofmt"
_ "github.com/qiniu/reviewbot/internal/linters/go/golangci_lint"
_ "github.com/qiniu/reviewbot/internal/linters/go/gomodcheck"
_ "github.com/qiniu/reviewbot/internal/linters/java/pmdcheck"
_ "github.com/qiniu/reviewbot/internal/linters/java/stylecheck"
_ "github.com/qiniu/reviewbot/internal/linters/lua/luacheck"
Expand Down
Loading