Skip to content

Commit c08271b

Browse files
authored
Merge pull request #1365 from cloudflare/bb
Don't report annotations from unmodified rules
2 parents f29b5b9 + 498520d commit c08271b

File tree

5 files changed

+108
-2
lines changed

5 files changed

+108
-2
lines changed

docs/changelog.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22

3+
## v0.71.8
4+
5+
### Fixed
6+
7+
- Don't try to report problems to BitBucket for unmodified rules.
8+
39
## v0.71.7
410

511
### Fixed

internal/discovery/git_branch_test.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1218,6 +1218,55 @@ groups:
12181218
},
12191219
},
12201220
},
1221+
{
1222+
title: "rule comment modified",
1223+
setup: func(t *testing.T) {
1224+
commitFile(t, "rules.yml", `
1225+
groups:
1226+
- name: v1
1227+
rules:
1228+
- record: up:count
1229+
pint disable promql/series(up)
1230+
expr: sum(up)
1231+
`, "v1")
1232+
1233+
_, err := git.RunGit("checkout", "-b", "v2")
1234+
require.NoError(t, err, "git checkout v2")
1235+
1236+
commitFile(t, "rules.yml", `
1237+
groups:
1238+
- name: v2
1239+
rules:
1240+
- record: up:count
1241+
# pint disable promql/series(up)
1242+
expr: sum(up)
1243+
`, "v2")
1244+
},
1245+
finder: discovery.NewGitBranchFinder(git.RunGit, git.NewPathFilter(includeAll, nil, nil), "main", 4, parser.PrometheusSchema, model.UTF8Validation, nil),
1246+
entries: []discovery.Entry{
1247+
{
1248+
State: discovery.Added,
1249+
Path: discovery.Path{
1250+
Name: "rules.yml",
1251+
SymlinkTarget: "rules.yml",
1252+
},
1253+
ModifiedLines: []int{6},
1254+
Rule: mustParse(4, " - record: up:count\n # pint disable promql/series(up)\n expr: sum(up)\n"),
1255+
},
1256+
{
1257+
State: discovery.Removed,
1258+
Path: discovery.Path{
1259+
Name: "rules.yml",
1260+
SymlinkTarget: "rules.yml",
1261+
},
1262+
PathError: parser.ParseError{
1263+
Err: errors.New("xxx"),
1264+
Line: 6,
1265+
},
1266+
ModifiedLines: []int{3, 6},
1267+
},
1268+
},
1269+
},
12211270
}
12221271

12231272
for _, tc := range testCases {

internal/reporter/bitbucket.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,5 @@ func moveReportedLine(report Report) (reported, original int) {
155155
if reported < 0 {
156156
reported = 0
157157
}
158-
159158
return reported, original
160159
}

internal/reporter/bitbucket_api.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,12 @@ func (bb bitBucketAPI) createReport(summary Summary, commit string) error {
375375
func (bb bitBucketAPI) createAnnotations(summary Summary, commit string) error {
376376
annotations := make([]BitBucketAnnotation, 0, len(summary.reports))
377377
for _, report := range summary.reports {
378-
annotations = append(annotations, reportToAnnotation(report))
378+
ann := reportToAnnotation(report)
379+
if !slices.Contains(report.ModifiedLines, ann.Line) {
380+
slog.Warn("Annotation for unmodified line, skipping", slog.String("path", ann.Path), slog.Int("line", ann.Line))
381+
continue
382+
}
383+
annotations = append(annotations, ann)
379384
}
380385

381386
if len(annotations) == 0 {

internal/reporter/bitbucket_test.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1935,6 +1935,53 @@ func TestBitBucketReporter(t *testing.T) {
19351935
return nil
19361936
},
19371937
},
1938+
{
1939+
description: "annotation on unmodified lines",
1940+
gitCmd: fakeGit,
1941+
reports: []reporter.Report{
1942+
{
1943+
Path: discovery.Path{
1944+
SymlinkTarget: "foo.txt",
1945+
Name: "foo.txt",
1946+
},
1947+
ModifiedLines: []int{},
1948+
Rule: mockRules[1],
1949+
Problem: checks.Problem{
1950+
Lines: diags.LineRange{
1951+
First: 1,
1952+
Last: 1,
1953+
},
1954+
Reporter: "mock",
1955+
Summary: "line is not part of the diff",
1956+
Severity: checks.Bug,
1957+
},
1958+
},
1959+
},
1960+
report: reporter.BitBucketReport{
1961+
Reporter: "Prometheus rule linter",
1962+
Title: "pint v0.0.0",
1963+
Details: reporter.BitBucketDescription,
1964+
Link: "https://cloudflare.github.io/pint/",
1965+
Result: "FAIL",
1966+
Data: []reporter.BitBucketReportData{
1967+
{Title: "Number of rules parsed", Type: reporter.NumberType, Value: float64(0)},
1968+
{Title: "Number of rules checked", Type: reporter.NumberType, Value: float64(0)},
1969+
{Title: "Number of problems found", Type: reporter.NumberType, Value: float64(1)},
1970+
{Title: "Number of offline checks", Type: reporter.NumberType, Value: float64(0)},
1971+
{Title: "Number of online checks", Type: reporter.NumberType, Value: float64(0)},
1972+
{Title: "Checks duration", Type: reporter.DurationType, Value: float64(0)},
1973+
},
1974+
},
1975+
annotations: reporter.BitBucketAnnotations{
1976+
Annotations: []reporter.BitBucketAnnotation{},
1977+
},
1978+
errorHandler: func(err error) error {
1979+
if err != nil {
1980+
return fmt.Errorf("Unpexpected error: %w", err)
1981+
}
1982+
return nil
1983+
},
1984+
},
19381985
}
19391986

19401987
for _, tc := range testCases {

0 commit comments

Comments
 (0)