Skip to content

Commit

Permalink
Merge pull request #1052 from cloudflare/file/disable
Browse files Browse the repository at this point in the history
Run all checks when file/disable is removed
  • Loading branch information
prymitive authored Aug 5, 2024
2 parents ed96a7e + 975d0d1 commit cf9d8b3
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 1 deletion.
1 change: 1 addition & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

- [promql/vector_matching](checks/promql/vector_matching.md) will now report more details, including which Prometheus server reports problems and which part of the query is the issue.
- GitHub report code was refactored, it should behave as before.
- When running `pint ci` pint will now run all checks on unmodified rules when a `file/disable` comment was removed.

### Fixed

Expand Down
13 changes: 12 additions & 1 deletion internal/discovery/git_branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"fmt"
"log/slog"
"sort"
"strings"

"golang.org/x/exp/slices"
Expand Down Expand Up @@ -275,7 +276,7 @@ func matchEntries(before, after []Entry) (ml []matchedEntry) {
if !matched && a.Rule.Name() != "" && a.Rule.IsIdentical(b.Rule) {
m.before = b
m.hasBefore = true
m.isIdentical = true
m.isIdentical = isEntryIdentical(b, a)
m.wasMoved = a.Path.Name != b.Path.Name
matched = true
slog.Debug(
Expand Down Expand Up @@ -317,6 +318,16 @@ func matchEntries(before, after []Entry) (ml []matchedEntry) {
return ml
}

func isEntryIdentical(b, a Entry) bool {
if !slices.Equal(sort.StringSlice(b.DisabledChecks), sort.StringSlice(a.DisabledChecks)) {
slog.Debug("List of disabled checks was modified",
slog.Any("before", sort.StringSlice(b.DisabledChecks)),
slog.Any("after", sort.StringSlice(a.DisabledChecks)))
return false
}
return true
}

func findRulesByName(entries []Entry, name string, typ parser.RuleType) (nomatch, match []Entry) {
for _, entry := range entries {
if entry.PathError == nil && entry.Rule.Type() == typ && entry.Rule.Name() == name {
Expand Down
56 changes: 56 additions & 0 deletions internal/discovery/git_branch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1071,6 +1071,62 @@ groups:
},
},
},
{
title: "file/disable comment removed",
setup: func(t *testing.T) {
commitFile(t, "rules.yml", `
# pint file/disable promql/series
- alert: rule1
expr: sum(foo) by(job)
- alert: rule2
expr: sum(foo) by(job)
- alert: rule3
expr: sum(foo) by(job)
`, "v1")

_, err := git.RunGit("checkout", "-b", "v2")
require.NoError(t, err, "git checkout v2")

commitFile(t, "rules.yml", `
- alert: rule1
expr: sum(foo) by(job)
- alert: rule2
expr: sum(foo) by(job)
- alert: rule3
expr: sum(foo) by(job)
`, "v2")
},
finder: discovery.NewGitBranchFinder(git.RunGit, git.NewPathFilter(includeAll, nil, includeAll), "main", 4),
entries: []discovery.Entry{
{
State: discovery.Modified,
Path: discovery.Path{
Name: "rules.yml",
SymlinkTarget: "rules.yml",
},
Rule: mustParse(3, "- alert: rule1\n expr: sum(foo) by(job)\n"),
},
{
State: discovery.Modified,
Path: discovery.Path{
Name: "rules.yml",
SymlinkTarget: "rules.yml",
},
Rule: mustParse(5, "- alert: rule2\n expr: sum(foo) by(job)\n"),
},
{
State: discovery.Modified,
Path: discovery.Path{
Name: "rules.yml",
SymlinkTarget: "rules.yml",
},
Rule: mustParse(7, "- alert: rule3\n expr: sum(foo) by(job)\n"),
},
},
},
}

for _, tc := range testCases {
Expand Down

0 comments on commit cf9d8b3

Please sign in to comment.