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

fix: skip linter without languages related #201

Merged
merged 2 commits into from
Jun 24, 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
1 change: 1 addition & 0 deletions config/linters-config/.golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@ linters:
- exhaustruct # seems too arbitrary
- execinquery # deprecated
- gomnd # deprecated
- funlen # seems too arbitrary

32 changes: 32 additions & 0 deletions internal/linters/filters.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@
return results, nil
}

// LinterRelated checks if the linter is related to the PR.
// Each linter has a list of languages that it supports and the file extensions are used to determine
// whether the linter is related to the PR.
func LinterRelated(linterName string, a Agent) bool {
exts := exts(a.PullRequestChangedFiles)
return languageRelated(linterName, exts)
}

// cleanLintResults cleans the file path in lint results.
// It removes the workdir prefix from the file path.
func cleanLintResults(workdir string, lintResults map[string][]LinterOutput) map[string][]LinterOutput {
Expand Down Expand Up @@ -143,3 +151,27 @@

return found
}

// exts returns the file extensions of the changes.
// file extensions are used to determine whether the linter is related to the PR.
func exts(changes []*github.CommitFile) map[string]bool {
var exts = make(map[string]bool)
for _, change := range changes {

Check warning on line 159 in internal/linters/filters.go

View check run for this annotation

qiniu-x / golangci-lint

internal/linters/filters.go#L159

ranges should only be cuddled with assignments used in the iteration (wsl)
ext := filepath.Ext(change.GetFilename())
if ext == "" {
continue

Check warning on line 162 in internal/linters/filters.go

View check run for this annotation

Codecov / codecov/patch

internal/linters/filters.go#L162

Added line #L162 was not covered by tests
}
exts[ext] = true
}
return exts
}

func languageRelated(linterName string, exts map[string]bool) bool {
langs := Languages(linterName)
for _, language := range langs {
if language == "*" || exts[language] {
return true
}
}
return false
}
76 changes: 76 additions & 0 deletions internal/linters/filters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,3 +197,79 @@ func TestFilters(t *testing.T) {
})
}
}

func TestLinterRelated(t *testing.T) {
tcs := []struct {
name string
linter string
a Agent
langs []string
expected bool
}{
{
name: "related",
linter: "golangci-lint",
a: Agent{
PullRequestChangedFiles: []*github.CommitFile{
{
Filename: github.String(".testdata/xxx_1.go"),
},
},
},
langs: []string{".go"},
expected: true,
},
{
name: "not related",
linter: "golangci-lint",
a: Agent{
PullRequestChangedFiles: []*github.CommitFile{
{
Filename: github.String("abc.sh"),
},
},
},
langs: []string{".go"},
expected: false,
},
{
name: "related",
linter: "git-flow",
a: Agent{
PullRequestChangedFiles: []*github.CommitFile{
{
Filename: github.String("abc.sh"),
},
},
},
langs: []string{"*"},
expected: true,
},
{
name: "related",
linter: "git-flow",
a: Agent{
PullRequestChangedFiles: []*github.CommitFile{
{
Filename: github.String("main.c"),
},
{
Filename: github.String("main.cpp"),
},
},
},
langs: []string{".c", ".go"},
expected: true,
},
}

for _, tc := range tcs {
t.Run(tc.name, func(t *testing.T) {
RegisterLinterLanguages(tc.linter, tc.langs)
actual := LinterRelated(tc.linter, tc.a)
if actual != tc.expected {
t.Errorf("expected %v, got %v, linter: %v, PR: %v", tc.expected, actual, tc.linter, tc.a.PullRequestChangedFiles)
}
})
}
}
29 changes: 0 additions & 29 deletions internal/linters/linters.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"fmt"
"os"
"os/exec"
"path/filepath"
"regexp"
"strconv"
"strings"
Expand Down Expand Up @@ -180,13 +179,6 @@ func Report(log *xlog.Logger, a Agent, lintResults map[string][]LinterOutput) er

log.Infof("[%s] found %d files with valid %d linter errors related to this PR %d (%s) \n", linterName, len(lintResults), countLinterErrors(lintResults), num, orgRepo)

// only not report when there is no lint errors and the linter is not related to the PR
// see https://github.com/qiniu/reviewbot/issues/108#issuecomment-2042217108
if len(lintResults) == 0 && !languageRelated(linterName, exts(a.PullRequestChangedFiles)) {
log.Debugf("[%s] no lint errors found and not languages related, skip report", linterName)
return nil
}

if len(lintResults) > 0 {
metric.IncIssueCounter(orgRepo, linterName, a.PullRequestEvent.PullRequest.GetHTMLURL(), a.PullRequestEvent.GetPullRequest().GetHead().GetSHA(), float64(countLinterErrors(lintResults)))
}
Expand Down Expand Up @@ -380,27 +372,6 @@ func IsEmpty(args ...string) bool {
return true
}

func exts(changes []*github.CommitFile) map[string]bool {
var exts = make(map[string]bool)
for _, change := range changes {
ext := filepath.Ext(change.GetFilename())
if ext == "" {
continue
}
exts[ext] = true
}
return exts
}

func languageRelated(linterName string, exts map[string]bool) bool {
for _, language := range Languages(linterName) {
if exts[language] {
return true
}
}
return false
}

func countLinterErrors(lintResults map[string][]LinterOutput) int {
var count int
for _, outputs := range lintResults {
Expand Down
5 changes: 5 additions & 0 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,11 @@ func (s *Server) handle(log *xlog.Logger, ctx context.Context, event *github.Pul
PullRequestChangedFiles: pullRequestAffectedFiles,
}

if !linters.LinterRelated(name, agent) {
log.Infof("[%s] linter is not related to the PR, skipping", name)
continue
}

if err := fn(log, agent); err != nil {
log.Errorf("failed to run linter: %v", err)
// continue to run other linters
Expand Down
Loading