Browse Source

fix: keep the error when the same configuration issue is also a warning

Issue keys leave severity out, so a warning and an error that are otherwise
identical share a key. Both de-duplication steps kept whichever came first,
so Diagnostics could show only the warning. They now keep the highest
severity, at the position the issue was first collected.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
jamesread 2 ngày trước cách đây
mục cha
commit
b2c3e64f13

+ 28 - 8
service/internal/configissues/health.go

@@ -69,20 +69,34 @@ func toHealthIssue(issue Issue) health.Issue {
 	}
 	}
 }
 }
 
 
+// toHealthIssues lists each problem once. When the same problem is collected
+// as both a warning and an error, the error is kept, at the position the
+// problem was first collected.
 func toHealthIssues(issues []Issue) []health.Issue {
 func toHealthIssues(issues []Issue) []health.Issue {
-	seen := make(map[string]bool, len(issues))
+	positions := make(map[string]int, len(issues))
 	out := make([]health.Issue, 0, len(issues))
 	out := make([]health.Issue, 0, len(issues))
 	for _, issue := range issues {
 	for _, issue := range issues {
 		converted := toHealthIssue(issue)
 		converted := toHealthIssue(issue)
-		if seen[converted.Key] {
+		position, seen := positions[converted.Key]
+		if !seen {
+			positions[converted.Key] = len(out)
+			out = append(out, converted)
 			continue
 			continue
 		}
 		}
-		seen[converted.Key] = true
-		out = append(out, converted)
+		if severityRank(string(converted.Severity)) > severityRank(string(out[position].Severity)) {
+			out[position] = converted
+		}
 	}
 	}
 	return out
 	return out
 }
 }
 
 
+func severityRank(severity string) int {
+	if severity == string(health.SeverityError) {
+		return 1
+	}
+	return 0
+}
+
 func fromHealthIssue(view health.IssueView) Issue {
 func fromHealthIssue(view health.IssueView) Issue {
 	return Issue{
 	return Issue{
 		Severity:     view.Severity,
 		Severity:     view.Severity,
@@ -141,14 +155,20 @@ func uniqueIssueViews(stored []health.IssueView) []health.IssueView {
 		return sourceRank(views[left].CheckID) < sourceRank(views[right].CheckID)
 		return sourceRank(views[left].CheckID) < sourceRank(views[right].CheckID)
 	})
 	})
 
 
-	seen := make(map[string]bool, len(views))
+	// The same problem can be open in more than one source, with a different
+	// severity in each. It is listed once, with the highest severity.
+	positions := make(map[string]int, len(views))
 	out := make([]health.IssueView, 0, len(views))
 	out := make([]health.IssueView, 0, len(views))
 	for _, view := range views {
 	for _, view := range views {
-		if seen[view.IssueKey] {
+		position, seen := positions[view.IssueKey]
+		if !seen {
+			positions[view.IssueKey] = len(out)
+			out = append(out, view)
 			continue
 			continue
 		}
 		}
-		seen[view.IssueKey] = true
-		out = append(out, view)
+		if severityRank(view.Severity) > severityRank(out[position].Severity) {
+			out[position] = view
+		}
 	}
 	}
 	return out
 	return out
 }
 }

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

@@ -189,3 +189,41 @@ func TestLongIssuesKeepOrderAndAreNotLoggedAgain(t *testing.T) {
 	assert.True(t, strings.HasPrefix(listed[1].Message, "b "), "long issues must keep collection order")
 	assert.True(t, strings.HasPrefix(listed[1].Message, "b "), "long issues must keep collection order")
 	assert.True(t, strings.HasPrefix(listed[2].Message, "a "), "long issues must keep collection order")
 	assert.True(t, strings.HasPrefix(listed[2].Message, "a "), "long issues must keep collection order")
 }
 }
+
+func TestDuplicateIssueKeepsTheError(t *testing.T) {
+	warning := testIssue(CodeConfigDecode, "bad value")
+	failure := warning
+	failure.Severity = SeverityError
+
+	orders := map[string][]Issue{
+		"warning first": {warning, failure},
+		"error first":   {failure, warning},
+	}
+	for name, issues := range orders {
+		t.Run(name, func(t *testing.T) {
+			resetIssues(t)
+			Replace(issues)
+			assert.Equal(t, []Issue{failure}, List())
+		})
+	}
+}
+
+func TestIssueInTwoSourcesIsListedWithTheHighestSeverity(t *testing.T) {
+	warning := testIssue(CodeConfigDecode, "bad value")
+	failure := warning
+	failure.Severity = SeverityError
+
+	t.Run("error reported after a load warning", func(t *testing.T) {
+		resetIssues(t)
+		ReplaceLoadIssues([]Issue{warning})
+		Report(failure)
+		assert.Equal(t, []Issue{failure}, List())
+	})
+
+	t.Run("warning reported after a load error", func(t *testing.T) {
+		resetIssues(t)
+		ReplaceLoadIssues([]Issue{failure})
+		Report(warning)
+		assert.Equal(t, []Issue{failure}, List())
+	})
+}