Skip to content

Commit

Permalink
Add logical operators to checks to chain different rules
Browse files Browse the repository at this point in the history
Signed-off-by: Marco Bersani <tachetefogo@gmail.com>
  • Loading branch information
B3rs committed Jan 18, 2024
1 parent 18b51d3 commit 5c385be
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 5 deletions.
5 changes: 3 additions & 2 deletions config.example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ interval: 10

providers:
# Static list of projects to check
static:
static:
- name: "standards-insights"
url: https://github.com/qonto/standards-insights.git
Expand Down Expand Up @@ -68,12 +67,14 @@ checks:
rules:
- go-version-latest
- name: go-main
operator: and
labels:
category: misc
severity: minor
owner: backend
rules:
- go-main
- go-version-latest
- name: grep-test
labels:
category: misc
Expand All @@ -90,7 +91,7 @@ rules:
- name: go-version-latest
files:
- path: "go.mod"
contains: "go 1.20"
contains: "go 1.21"
- name: go-main
files:
- path: "main.go"
Expand Down
11 changes: 8 additions & 3 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,14 @@ type GrepRule struct {
}

type Check struct {
Name string `validate:"required"`
Labels map[string]string
Rules []string `validate:"required,min=1"`
Name string `validate:"required"`
Labels map[string]string
Operator string `validate:"oneof=and or"`
Rules []string `validate:"required,min=1"`
}

func (c Check) IsAND() bool {
return c.Operator == "" || c.Operator == "and"
}

type Group struct {
Expand Down
31 changes: 31 additions & 0 deletions pkg/checker/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,21 @@ import (
)

func (c *Checker) executeCheck(ctx context.Context, check config.Check, project project.Project) aggregates.CheckResult {
if check.IsAND() {
return c.executeANDCheck(ctx, check, project)
} else {
return c.executeORCheck(ctx, check, project)
}
}

func (c *Checker) executeANDCheck(ctx context.Context, check config.Check, project project.Project) aggregates.CheckResult {
success := true
checkResult := aggregates.CheckResult{
Name: check.Name,
Labels: check.Labels,
Results: []ruleraggregates.RuleResult{},
}

for _, rule := range check.Rules {
ruleResult := c.ruler.Execute(ctx, rule, project)
if !ruleResult.Success {
Expand All @@ -31,3 +40,25 @@ func (c *Checker) executeCheck(ctx context.Context, check config.Check, project
checkResult.Success = success
return checkResult
}

func (c *Checker) executeORCheck(ctx context.Context, check config.Check, project project.Project) aggregates.CheckResult {
success := false
checkResult := aggregates.CheckResult{
Name: check.Name,
Labels: check.Labels,
Results: []ruleraggregates.RuleResult{},
}

for _, rule := range check.Rules {
ruleResult := c.ruler.Execute(ctx, rule, project)
if ruleResult.Success {
success = true
c.logger.Debug(fmt.Sprintf("rule %s successful on project %s for check %s", rule, project.Name, check.Name))
} else {
c.logger.Debug(fmt.Sprintf("rule %s failed on project %s for check %s", rule, project.Name, check.Name))
}
checkResult.Results = append(checkResult.Results, ruleResult)
}
checkResult.Success = success
return checkResult
}

0 comments on commit 5c385be

Please sign in to comment.