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

Add ci.minSeverity #1108

Closed
wants to merge 1 commit 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
2 changes: 1 addition & 1 deletion cmd/pint/bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,6 @@ rule {

b.ResetTimer()
for n := 0; n < b.N; n++ {
_, _ = checkRules(ctx, 10, false, gen, cfg, entries)
_, _ = checkRules(ctx, "", 10, false, gen, cfg, entries)
}
}
2 changes: 1 addition & 1 deletion cmd/pint/ci.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func actionCI(c *cli.Context) error {

slog.Debug("Generated all Prometheus servers", slog.Int("count", gen.Count()))

summary, err := checkRules(ctx, meta.workers, meta.isOffline, gen, meta.cfg, entries)
summary, err := checkRules(ctx, "ci", meta.workers, meta.isOffline, gen, meta.cfg, entries)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/pint/lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func actionLint(c *cli.Context) error {
return err
}

summary, err := checkRules(ctx, meta.workers, meta.isOffline, gen, meta.cfg, entries)
summary, err := checkRules(ctx, "lint", meta.workers, meta.isOffline, gen, meta.cfg, entries)
if err != nil {
return err
}
Expand Down
21 changes: 19 additions & 2 deletions cmd/pint/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const (
This usually means that it's missing some required fields.`
)

func checkRules(ctx context.Context, workers int, isOffline bool, gen *config.PrometheusGenerator, cfg config.Config, entries []discovery.Entry) (summary reporter.Summary, err error) {
func checkRules(ctx context.Context, cmd string, workers int, isOffline bool, gen *config.PrometheusGenerator, cfg config.Config, entries []discovery.Entry) (summary reporter.Summary, err error) {
if isOffline {
slog.Info("Offline mode, skipping Prometheus discovery")
} else {
Expand Down Expand Up @@ -127,9 +127,26 @@ func checkRules(ctx context.Context, workers int, isOffline bool, gen *config.Pr
defer close(jobs)
}()

shouldReport := func(sev checks.Severity) bool {
return true
}

if cmd == "ci" {
minSev, err := checks.ParseSeverity(cfg.CI.MinSeverity)
if err != nil {
minSev = checks.Warning
}
shouldReport = func(sev checks.Severity) bool {
return sev >= minSev
}
}

for result := range results {
summary.Report(result)
if shouldReport(result.Problem.Severity) {
summary.Report(result)
}
}

summary.SortReports()
summary.Duration = time.Since(start)
summary.TotalEntries = len(entries)
Expand Down
2 changes: 1 addition & 1 deletion cmd/pint/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ func (c *problemCollector) scan(ctx context.Context, workers int, isOffline bool
return err
}

s, err := checkRules(ctx, workers, isOffline, gen, c.cfg, entries)
s, err := checkRules(ctx, "watch", workers, isOffline, gen, c.cfg, entries)
if err != nil {
return err
}
Expand Down
11 changes: 9 additions & 2 deletions internal/config/ci.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,23 @@ package config

import (
"errors"
"fmt"

"github.com/cloudflare/pint/internal/checks"
)

type CI struct {
BaseBranch string `hcl:"baseBranch,optional" json:"baseBranch,omitempty"`
MaxCommits int `hcl:"maxCommits,optional" json:"maxCommits,omitempty"`
BaseBranch string `hcl:"baseBranch,optional" json:"baseBranch,omitempty"`
MaxCommits int `hcl:"maxCommits,optional" json:"maxCommits,omitempty"`
MinSeverity string `hcl:"minSeverity,optional" json:"minSeverity,omitempty"`
}

func (ci CI) validate() error {
if ci.MaxCommits <= 0 {
return errors.New("maxCommits cannot be <= 0")
}
if _, err := checks.ParseSeverity(ci.MinSeverity); err != nil {
return fmt.Errorf("invalid minSeverity: %w", err)
}
return nil
}
5 changes: 3 additions & 2 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,8 +222,9 @@ func getContext() *hcl.EvalContext {
func Load(path string, failOnMissing bool) (cfg Config, fromFile bool, err error) {
cfg = Config{
CI: &CI{
MaxCommits: 20,
BaseBranch: "master",
MaxCommits: 20,
BaseBranch: "master",
MinSeverity: "info",
},
Parser: &Parser{},
Checks: &Checks{
Expand Down