Skip to content

Commit

Permalink
Merge pull request cloudflare#641 from cloudflare/issue-609
Browse files Browse the repository at this point in the history
Add exclude option to ci block
  • Loading branch information
prymitive authored Jun 8, 2023
2 parents 799202c + 71945eb commit 87088f0
Show file tree
Hide file tree
Showing 11 changed files with 185 additions and 21 deletions.
9 changes: 7 additions & 2 deletions cmd/pint/ci.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ func actionCI(c *cli.Context) error {
includeRe = append(includeRe, regexp.MustCompile("^"+pattern+"$"))
}

excludeRe := []*regexp.Regexp{}
for _, pattern := range meta.cfg.CI.Exclude {
excludeRe = append(excludeRe, regexp.MustCompile("^"+pattern+"$"))
}

meta.cfg.CI = detectCI(meta.cfg.CI)
baseBranch := meta.cfg.CI.BaseBranch
if c.String(baseBranchFlag) != "" {
Expand All @@ -85,10 +90,10 @@ func actionCI(c *cli.Context) error {

var entries []discovery.Entry
if c.Bool(devFlag) {
finder := discovery.NewGitBranchFinder(git.RunGit, includeRe, baseBranch, meta.cfg.CI.MaxCommits, meta.cfg.Parser.CompileRelaxed())
finder := discovery.NewGitBranchFinder(git.RunGit, includeRe, excludeRe, baseBranch, meta.cfg.CI.MaxCommits, meta.cfg.Parser.CompileRelaxed())
entries, err = finder.Find()
} else {
finder := discovery.NewGitBlameFinder(git.RunGit, includeRe, baseBranch, meta.cfg.CI.MaxCommits, meta.cfg.Parser.CompileRelaxed())
finder := discovery.NewGitBlameFinder(git.RunGit, includeRe, excludeRe, baseBranch, meta.cfg.CI.MaxCommits, meta.cfg.Parser.CompileRelaxed())
entries, err = finder.Find()
}
if err != nil {
Expand Down
46 changes: 46 additions & 0 deletions cmd/pint/tests/0139_ci_exclude.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
mkdir testrepo
cd testrepo
exec git init --initial-branch=main .

cp ../src/.pint.hcl .
env GIT_AUTHOR_NAME=pint
env GIT_AUTHOR_EMAIL=pint@example.com
env GIT_COMMITTER_NAME=pint
env GIT_COMMITTER_EMAIL=pint@example.com
exec git add .
exec git commit -am 'import rules and config'

exec git checkout -b v1
cp ../src/a.yml a.yml
exec git add a.yml
exec git commit -am 'v1'

exec git checkout -b v2
cp ../src/b.yml b.yml
exec git add b.yml
exec git commit -am 'v2'

pint.ok -l debug --no-color ci
! stdout .
stderr 'level=debug msg="Got branch information" base=main current=v2'
stderr 'level=debug msg="Found commit to scan" commit=.*'
stderr 'level=debug msg="Found commit to scan" commit=.*'
stderr 'level=debug msg="Git file change" change=A commit=.* path=a.yml'
stderr 'level=debug msg="Git file change" change=A commit=.* path=b.yml'
stderr 'level=debug msg="Skipping file due to include/exclude rules" path=a.yml'
stderr 'level=debug msg="Skipping file due to include/exclude rules" path=b.yml'

-- src/a.yml --
- record: rule1
expr: sum(foo) bi()
-- src/b.yml --
- record: rule1
expr: sum(foo) bi()
-- src/.pint.hcl --
ci {
baseBranch = "main"
exclude = [".*.yml"]
}
parser {
relaxed = [".*"]
}
47 changes: 47 additions & 0 deletions cmd/pint/tests/0140_ci_include_exclude.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
mkdir testrepo
cd testrepo
exec git init --initial-branch=main .

cp ../src/.pint.hcl .
env GIT_AUTHOR_NAME=pint
env GIT_AUTHOR_EMAIL=pint@example.com
env GIT_COMMITTER_NAME=pint
env GIT_COMMITTER_EMAIL=pint@example.com
exec git add .
exec git commit -am 'import rules and config'

exec git checkout -b v1
cp ../src/a.yml a.yml
exec git add a.yml
exec git commit -am 'v1'

exec git checkout -b v2
cp ../src/b.yml b.yml
exec git add b.yml
exec git commit -am 'v2'

pint.ok -l debug --no-color ci
! stdout .
stderr 'level=debug msg="Got branch information" base=main current=v2'
stderr 'level=debug msg="Found commit to scan" commit=.*'
stderr 'level=debug msg="Found commit to scan" commit=.*'
stderr 'level=debug msg="Git file change" change=A commit=.* path=a.yml'
stderr 'level=debug msg="Git file change" change=A commit=.* path=b.yml'
stderr 'level=debug msg="Skipping file due to include/exclude rules" path=a.yml'
stderr 'level=debug msg="Skipping file due to include/exclude rules" path=b.yml'

-- src/a.yml --
- record: rule1
expr: sum(foo) bi()
-- src/b.yml --
- record: rule1
expr: sum(foo) bi()
-- src/.pint.hcl --
ci {
baseBranch = "main"
include = [".*.yml"]
exclude = [".*.yml"]
}
parser {
relaxed = [".*"]
}
4 changes: 4 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## v0.44.0

### Added

- Added `exclude` option to `ci` config block - #609.

### Fixed

- Fixed `alerts/annotation` check regexp matching - #613.
Expand Down
4 changes: 4 additions & 0 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,17 @@ Syntax:
```js
ci {
include = [ "(.*)", ... ]
exclude = [ "(.*)", ... ]
maxCommits = 20
baseBranch = "master"
}
```

- `include` - list of file patterns to check when running checks. Only files
matching those regexp rules will be checked, other modified files will be ignored.
- `exclude` - list of file patterns to ignore when running checks.
This option takes precedence over `include`, so if a file path matches both
`include` & `exclude` patterns it will be excluded.
- `maxCommits` - by default pint will try to find all commits on the current branch,
this requires full git history to be present, if we have a shallow clone this
might fail to find only current branch commits and give us a huge list.
Expand Down
8 changes: 8 additions & 0 deletions internal/config/ci.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

type CI struct {
Include []string `hcl:"include,optional" json:"include,omitempty"`
Exclude []string `hcl:"exclude,optional" json:"exclude,omitempty"`
MaxCommits int `hcl:"maxCommits,optional" json:"maxCommits,omitempty"`
BaseBranch string `hcl:"baseBranch,optional" json:"baseBranch,omitempty"`
}
Expand All @@ -22,5 +23,12 @@ func (ci CI) validate() error {
return err
}
}

for _, pattern := range ci.Exclude {
_, err := regexp.Compile(pattern)
if err != nil {
return err
}
}
return nil
}
14 changes: 14 additions & 0 deletions internal/config/ci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ func TestCISettings(t *testing.T) {
Include: []string{"foo/.+"},
},
},
{
conf: CI{
MaxCommits: 1,
BaseBranch: "main",
Exclude: []string{"foo/.+"},
},
},
{
conf: CI{
MaxCommits: -5,
Expand All @@ -41,6 +48,13 @@ func TestCISettings(t *testing.T) {
},
err: errors.New("error parsing regexp: invalid nested repetition operator: `++`"),
},
{
conf: CI{
MaxCommits: 20,
Exclude: []string{".+++"},
},
err: errors.New("error parsing regexp: invalid nested repetition operator: `++`"),
},
}

for _, tc := range testCases {
Expand Down
12 changes: 11 additions & 1 deletion internal/discovery/git_blame.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ import (
func NewGitBlameFinder(
gitCmd git.CommandRunner,
include []*regexp.Regexp,
exclude []*regexp.Regexp,
baseBranch string,
maxCommits int,
relaxed []*regexp.Regexp,
) GitBlameFinder {
return GitBlameFinder{
gitCmd: gitCmd,
include: include,
exclude: exclude,
baseBranch: baseBranch,
maxCommits: maxCommits,
relaxed: relaxed,
Expand All @@ -30,6 +32,7 @@ func NewGitBlameFinder(
type GitBlameFinder struct {
gitCmd git.CommandRunner
include []*regexp.Regexp
exclude []*regexp.Regexp
baseBranch string
maxCommits int
relaxed []*regexp.Regexp
Expand Down Expand Up @@ -166,15 +169,22 @@ func (f GitBlameFinder) Find() (entries []Entry, err error) {
}

func (f GitBlameFinder) isPathAllowed(path string) bool {
if len(f.include) == 0 {
if len(f.include) == 0 && len(f.exclude) == 0 {
return true
}

for _, pattern := range f.exclude {
if pattern.MatchString(path) {
return false
}
}

for _, pattern := range f.include {
if pattern.MatchString(path) {
return true
}
}

return false
}

Expand Down
10 changes: 10 additions & 0 deletions internal/discovery/git_blame_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ func TestGitBlameFinder(t *testing.T) {
return nil, fmt.Errorf("mock error")
},
nil,
nil,
"main",
0,
nil,
Expand All @@ -104,6 +105,7 @@ func TestGitBlameFinder(t *testing.T) {
}
},
nil,
nil,
"main",
0,
nil,
Expand All @@ -128,6 +130,7 @@ func TestGitBlameFinder(t *testing.T) {
}
},
nil,
nil,
"main",
0,
[]*regexp.Regexp{regexp.MustCompile(".*")},
Expand All @@ -154,6 +157,7 @@ func TestGitBlameFinder(t *testing.T) {
}
},
nil,
nil,
"main",
0,
[]*regexp.Regexp{regexp.MustCompile(".*")},
Expand Down Expand Up @@ -185,6 +189,7 @@ func TestGitBlameFinder(t *testing.T) {
}
},
nil,
nil,
"main",
0,
[]*regexp.Regexp{regexp.MustCompile(".*")},
Expand Down Expand Up @@ -218,6 +223,7 @@ func TestGitBlameFinder(t *testing.T) {
}
},
nil,
nil,
"main",
0,
[]*regexp.Regexp{regexp.MustCompile(".*")},
Expand Down Expand Up @@ -330,6 +336,7 @@ R090 foo/c2c.yml c2c.yml
regexp.MustCompile("^foo/.*"),
regexp.MustCompile("^c.*.yml$"),
},
nil,
"main",
0,
[]*regexp.Regexp{regexp.MustCompile(".*")},
Expand Down Expand Up @@ -382,6 +389,7 @@ R090 foo/c2c.yml c2c.yml
}
},
nil,
nil,
"main",
0,
nil,
Expand Down Expand Up @@ -410,6 +418,7 @@ R090 foo/c2c.yml c2c.yml
}
},
nil,
nil,
"main",
0,
[]*regexp.Regexp{regexp.MustCompile(".*")},
Expand All @@ -436,6 +445,7 @@ R090 foo/c2c.yml c2c.yml
}
},
nil,
nil,
"main",
0,
[]*regexp.Regexp{regexp.MustCompile(".*")},
Expand Down
12 changes: 11 additions & 1 deletion internal/discovery/git_branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@ import (
func NewGitBranchFinder(
gitCmd git.CommandRunner,
include []*regexp.Regexp,
exclude []*regexp.Regexp,
baseBranch string,
maxCommits int,
relaxed []*regexp.Regexp,
) GitBranchFinder {
return GitBranchFinder{
gitCmd: gitCmd,
include: include,
exclude: exclude,
baseBranch: baseBranch,
maxCommits: maxCommits,
relaxed: relaxed,
Expand All @@ -33,6 +35,7 @@ func NewGitBranchFinder(
type GitBranchFinder struct {
gitCmd git.CommandRunner
include []*regexp.Regexp
exclude []*regexp.Regexp
baseBranch string
maxCommits int
relaxed []*regexp.Regexp
Expand Down Expand Up @@ -152,15 +155,22 @@ func (f GitBranchFinder) Find() (entries []Entry, err error) {
}

func (f GitBranchFinder) isPathAllowed(path string) bool {
if len(f.include) == 0 {
if len(f.include) == 0 && len(f.exclude) == 0 {
return true
}

for _, pattern := range f.exclude {
if pattern.MatchString(path) {
return false
}
}

for _, pattern := range f.include {
if pattern.MatchString(path) {
return true
}
}

return false
}

Expand Down
Loading

0 comments on commit 87088f0

Please sign in to comment.