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

Config default #54

Closed
wants to merge 2 commits into from
Closed
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
29 changes: 23 additions & 6 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,46 @@ import (
)

// Config maps org or repo to LinterConfig
//type Config map[string]map[string]Linter

type Config map[string]map[string]Linter

type Linter struct {
// Enable is whether to enable this linter, if false, linter still run but not report.
Enable bool `json:"enable,omitempty"`
WorkDir string `json:"workDir,omitempty"`
Command string `json:"command,omitempty"`
Args []string `json:"args,omitempty"`
Enable *bool `json:"enable,omitempty" yaml:"enable,omitempty"`
WorkDir string `json:"workDir,omitempty" yaml:"workDir,omitempty"`
Command string `json:"command,omitempty" yaml:"command,omitempty"`
Args []string `json:"args,omitempty" yaml:"args,omitempty"`
}

// NewConfig returns a new Config.
func NewConfig(conf string) (Config, error) {
var c Config
f, err := os.ReadFile(conf)
if err != nil {
return nil, err
}

c := Config{}
if err = yaml.Unmarshal(f, &c); err != nil {
return nil, err
}

// TODO: 应该默认开启staticcheck?
defaultEnable := true
for _, v := range c {
for _, val := range v {
if val.Enable == nil {
val.Enable = &defaultEnable
}
}
}

if len(c) == 0 {
c["qbox"] = map[string]Linter{
"staticcheck": {Enable: &defaultEnable},
"govet": {Enable: &defaultEnable},
"luacheck": {Enable: &defaultEnable},
}
}

return c, nil
}
Expand Down
17 changes: 14 additions & 3 deletions config/config.yaml
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
# This is the default config file, you can override it by creating a config.yaml in the same directory
# by default, all linters are enabled if you don't specify. You can disable them by setting enable to false
# example1: disable staticcheck for org
# qbox:
#qbox:
# staticcheck:
# enable: false
#
# govet:
# enable: true
# luacheck:
# enable: true
# workDir: "nginx/Lua"
# #command: luacheck

#
# example2: disable staticcheck for repo
# qbox/kodo:
# staticcheck:
Expand All @@ -16,4 +23,8 @@ qbox/kodo: # github repository name, which will override the settings in qbox or
workDir: "src/qiniu.com/kodo"
command: staticcheck
args: ["-debug.no-compile-errors=true", "./..."]
run_if_changed: ["src/qiniu.com/kodo/.*\\.go$"] # only run if changed files match the regex
run_if_changed: ["src/qiniu.com/kodo/.*\\.go$"] # only run if changed files match the regex
govet:
enable: false
luacheck:
enable: false
67 changes: 60 additions & 7 deletions config/config_test.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,72 @@
package config

import (
"fmt"
"os"
"path"
"testing"

"github.com/stretchr/testify/assert"
)

func TestConfig(t *testing.T) {
repoConfig, err := NewConfig("config.yaml")
if err != nil {
t.Errorf("NewConfig() error = %v", err)
return
defaultTrue := true
defaultFalse := false
case1 := `
qbox:
staticcheck:
enable: false
luacheck:
enable: true
workDir: "nginx/Lua"
command: luacheck
`

exp1 := map[string]map[string]Linter{
"qbox": {
"staticcheck": {Enable: &defaultFalse},
"luacheck": {Enable: &defaultTrue, WorkDir: "nginx/Lua", Command: "luacheck"},
},
}

case2 := `
qbox:
luacheck:
enable: false
workDir: "nginx/Lua"
command: luacheck
`
exp2 := map[string]map[string]Linter{
"qbox": {
"luacheck": {Enable: &defaultFalse, WorkDir: "nginx/Lua", Command: "luacheck"},
},
}

case3 := ``
exp3 := map[string]map[string]Linter{
"qbox": {
"staticcheck": {Enable: &defaultTrue},
"govet": {Enable: &defaultTrue},
"luacheck": {Enable: &defaultTrue},
},
}

for k, c := range repoConfig {
fmt.Printf("%v: %v \n", k, c)
cs := map[string]map[string]map[string]Linter{
case1: exp1,
case2: exp2,
case3: exp3,
}

dir := os.TempDir()
defer os.RemoveAll(dir)

for k, v := range cs {
f := path.Join(dir, "t.yaml")
err := os.WriteFile(f, []byte(k), 666)
assert.NoError(t, err)

res, err := NewConfig(f)
assert.NoError(t, err)

assert.EqualValues(t, v, res)
}
}
2 changes: 1 addition & 1 deletion internal/linters/git-flow/commit-check/commit_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ import (
"bytes"
"context"
"fmt"
"github.com/reviewbot/config"
"regexp"
"strings"
"text/template"

"github.com/google/go-github/v57/github"
"github.com/qiniu/x/log"
"github.com/qiniu/x/xlog"
"github.com/reviewbot/config"
"github.com/reviewbot/internal/linters"
)

Expand Down
131 changes: 108 additions & 23 deletions internal/linters/git-flow/commit-check/commit_check_test.go
Original file line number Diff line number Diff line change
@@ -1,34 +1,119 @@
package commit_check

import (
"bytes"
"strings"
"testing"
"text/template"

"github.com/google/go-github/v57/github"
)

func TestRebaseSuggestionTmpl(t *testing.T) {
tmpl, err := template.New("rebase_suggestion").Parse(rebaseSuggestionTmpl)
if err != nil {
t.Fatal(err)
}
func TestRebaseCheckRule(t *testing.T) {

var buf bytes.Buffer
var rebaseSuggestion = RebaseSuggestion{
Author: "author",
Flag: rebaseSuggestionFlag,
TargetCommits: []string{"commit1", "commit2"},
}
err = tmpl.Execute(&buf, rebaseSuggestion)
if err != nil {
t.Fatal(err)
tcs := []struct {
title string
commits []*github.RepositoryCommit
expected string
}{
{
title: "filter merge commits",
commits: []*github.RepositoryCommit{
{
Commit: &github.Commit{
Message: github.String("feat: add feature 1"),
},
},
{
Commit: &github.Commit{
Message: github.String("Merge a into b"),
},
},
{
Commit: &github.Commit{
Message: github.String("fix: fix bug 2"),
},
},
{
Commit: &github.Commit{
Message: github.String("Merge xxx into xxx"),
},
},
},
expected: "git merge",
},
{
title: "filter duplicate commits",
commits: []*github.RepositoryCommit{
{
Commit: &github.Commit{
Message: github.String("feat: add feature 1"),
},
},
{
Commit: &github.Commit{
Message: github.String("feat: add feature 1"),
},
},
{
Commit: &github.Commit{
Message: github.String("fix: fix bug 2"),
},
},
},
expected: "duplicated",
},
{
title: "filter duplicate and merge commits",
commits: []*github.RepositoryCommit{
{
Commit: &github.Commit{
Message: github.String("feat: add feature 1"),
},
},
{
Commit: &github.Commit{
Message: github.String("feat: add feature 1"),
},
},
{
Commit: &github.Commit{
Message: github.String("Merge xxx into xxx"),
},
},
},
expected: "feat: add feature 1",
},
{
title: "filter duplicate and merge commits",
commits: []*github.RepositoryCommit{
{
Commit: &github.Commit{
Message: github.String("feat: add feature 1"),
},
},
{
Commit: &github.Commit{
Message: github.String("feat: add feature 2"),
},
},
},
expected: "",
},
}

t.Log(buf.String())
}
for _, tc := range tcs {
t.Run(tc.title, func(t *testing.T) {
comments, err := rebaseCheck(nil, tc.commits)
if err != nil {
t.Fatal(err)
}

func TestRebaseCheckRule(t *testing.T) {
var rule = RebaseCheckRule{
RebaseSuggestion: RebaseSuggestion
}
if tc.expected == "" && comments != "" {
t.Fatalf("expected %s, got %s", tc.expected, comments)
}

if !strings.Contains(comments, tc.expected) {
t.Fatalf("expected %s, got %s", tc.expected, comments)
}
})
}
}
}
2 changes: 1 addition & 1 deletion internal/linters/go/staticcheck/staticcheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package staticcheck

import (
"fmt"
"github.com/reviewbot/config"
"os/exec"
"path/filepath"
"regexp"
Expand All @@ -27,7 +28,6 @@ import (
"github.com/google/go-github/v57/github"
"github.com/qiniu/x/log"
"github.com/qiniu/x/xlog"
"github.com/reviewbot/config"
"github.com/reviewbot/internal/linters"
)

Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ import (
"errors"
"flag"
"fmt"
"github.com/reviewbot/config"
"net/http"
"os"

"github.com/google/go-github/v57/github"
"github.com/qiniu/x/log"
"github.com/reviewbot/config"
"github.com/sirupsen/logrus"
gitv2 "k8s.io/test-infra/prow/git/v2"

Expand Down
Loading
Loading