Skip to content

Commit 9bdcf22

Browse files
authored
feat(AGI-162): compare severities quantitatively (#145)
1 parent 5e82c32 commit 9bdcf22

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

pkg/models/severity.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,32 @@ const (
77
SeverityLow = "LOW"
88
SeverityUnknown = "UNKNOWN"
99
)
10+
11+
var severityToInt = map[string]int{
12+
SeverityUnknown: 0,
13+
SeverityLow: 1,
14+
SeverityMedium: 2,
15+
SeverityHigh: 3,
16+
SeverityCritical: 4,
17+
}
18+
19+
func CompareSeverity(severity1, severity2 string) int {
20+
val1, ok1 := severityToInt[severity1]
21+
val2, ok2 := severityToInt[severity2]
22+
23+
if !ok1 && !ok2 {
24+
return 0
25+
} else if !ok1 {
26+
return -1
27+
} else if !ok2 {
28+
return 1
29+
}
30+
31+
if val1 < val2 {
32+
return -1
33+
} else if val1 > val2 {
34+
return 1
35+
} else {
36+
return 0
37+
}
38+
}

pkg/models/severity_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package models
2+
3+
import "testing"
4+
5+
func TestCompareSeverity(t *testing.T) {
6+
tests := []struct {
7+
severity1 string
8+
severity2 string
9+
expected int
10+
}{
11+
{"INVALID", "HIGH", -1},
12+
{"UNKNOWN", "CRITICAL", -1},
13+
{"LOW", "CRITICAL", -1},
14+
{"MEDIUM", "CRITICAL", -1},
15+
{"HIGH", "CRITICAL", -1},
16+
{"CRITICAL", "HIGH", 1},
17+
{"HIGH", "MEDIUM", 1},
18+
{"MEDIUM", "LOW", 1},
19+
{"LOW", "UNKNOWN", 1},
20+
{"CRITICAL", "UNKNOWN", 1},
21+
{"HIGH", "INVALID", 1},
22+
{"UNKNOWN", "UNKNOWN", 0},
23+
{"LOW", "LOW", 0},
24+
{"MEDIUM", "MEDIUM", 0},
25+
{"HIGH", "HIGH", 0},
26+
{"CRITICAL", "CRITICAL", 0},
27+
}
28+
29+
for _, test := range tests {
30+
result := CompareSeverity(test.severity1, test.severity2)
31+
if result != test.expected {
32+
t.Errorf("CompareSeverity(%q, %q) = %d; expected %d", test.severity1, test.severity2, result, test.expected)
33+
}
34+
}
35+
}

0 commit comments

Comments
 (0)