Quellcode durchsuchen

fix: keep an issue's first position and log it once when it has two severities

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
jamesread vor 1 Tag
Ursprung
Commit
ad828bea56

+ 3 - 1
service/internal/configissues/health.go

@@ -176,8 +176,10 @@ func uniqueIssueViews(stored []health.IssueView) []health.IssueView {
 			out = append(out, view)
 			continue
 		}
+		// Only the severity is raised, so the issue keeps the position of the
+		// first source it was found in.
 		if severityRank(view.Severity) > severityRank(out[position].Severity) {
-			out[position] = view
+			out[position].Severity = view.Severity
 		}
 	}
 	return out

+ 31 - 0
service/internal/configissues/health_test.go

@@ -225,3 +225,34 @@ func TestIssueInTwoSourcesIsListedWithTheHighestSeverity(t *testing.T) {
 		assert.Equal(t, []Issue{failure}, List())
 	})
 }
+
+func TestDuplicateIssueIsLoggedOnceAsAnError(t *testing.T) {
+	resetIssues(t)
+	hook := captureLogs(t)
+
+	warning := testIssue(CodeConfigDecode, "bad value")
+	failure := warning
+	failure.Severity = SeverityError
+
+	Replace([]Issue{warning, failure})
+	Replace([]Issue{warning, failure})
+
+	entries := issueLogEntries(hook)
+	require.Len(t, entries, 1, "a duplicate issue must be logged once, and not again on an unchanged rebuild")
+	assert.Equal(t, log.ErrorLevel, entries[0].Level)
+}
+
+func TestIssueRaisedToAnErrorKeepsItsFirstPosition(t *testing.T) {
+	resetIssues(t)
+
+	warning := testIssue(CodeConfigDecode, "bad value")
+	failure := warning
+	failure.Severity = SeverityError
+
+	ReplaceLoadIssues([]Issue{warning})
+	ReportRuntime(testIssue(CodeDeprecatedAPICall, "runtime"))
+	Replace([]Issue{failure})
+
+	assert.Equal(t, []string{"bad value", "runtime"}, messages(List()))
+	assert.Equal(t, SeverityError, List()[0].Severity)
+}

+ 10 - 6
service/internal/configissues/store.go

@@ -121,7 +121,7 @@ func setSource(sourceID string, next []Issue) {
 	}
 
 	refreshSnapshot()
-	logNewIssues(before, next)
+	logNewIssues(before, copyCurrentSeverities(), next)
 }
 
 func raise(sourceID string, issue Issue) {
@@ -143,7 +143,7 @@ func raise(sourceID string, issue Issue) {
 	}
 
 	refreshSnapshot()
-	logNewIssues(before, []Issue{issue})
+	logNewIssues(before, copyCurrentSeverities(), []Issue{issue})
 }
 
 // refreshSnapshot rebuilds the list returned by List after a change, so that
@@ -182,14 +182,18 @@ func copyCurrentSeverities() map[string]string {
 // logNewIssues logs issues that were not open before, or whose severity changed,
 // in the order they were collected, so startup and reloads do not repeat the
 // same warnings.
-func logNewIssues(before map[string]string, issues []Issue) {
+// An issue is logged when the severity it is now listed with is new, and that
+// severity is the one it was reported with. The same problem reported as both a
+// warning and an error is listed once as an error, so only the error is logged,
+// and an unchanged rebuild logs nothing.
+func logNewIssues(before map[string]string, after map[string]string, issues []Issue) {
 	for _, issue := range issues {
 		key := issueKey(issue)
-		severity := string(healthSeverity(issue.Severity))
-		if before[key] == severity {
+		listed := after[key]
+		if listed == before[key] || listed != string(healthSeverity(issue.Severity)) {
 			continue
 		}
-		before[key] = severity
+		before[key] = listed
 		logNewIssue(issue)
 	}
 }