Skip to content

Commit d192c8e

Browse files
authored
✨ Add score to SARIF for all results (#1694)
* add score * fix unit tests
1 parent 3818dbe commit d192c8e

File tree

7 files changed

+21
-16
lines changed

7 files changed

+21
-16
lines changed

pkg/sarif.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -510,14 +510,18 @@ func filterOutDetailType(details []checker.CheckDetail, t checker.DetailType) []
510510
return ret
511511
}
512512

513+
func messageWithScore(msg string, score int) string {
514+
return fmt.Sprintf("score is %d: %s", score, msg)
515+
}
516+
513517
func createDefaultLocationMessage(check *checker.CheckResult, score int) string {
514518
details := filterOutDetailType(check.Details2, checker.DetailInfo)
515519
s, b := detailsToString(details, log.WarnLevel)
516520
if b {
517521
// Warning: GitHub UX needs a single `\n` to turn it into a `<br>`.
518-
return fmt.Sprintf("score is %d: %s:\n%s", score, check.Reason, s)
522+
return messageWithScore(fmt.Sprintf("%s:\n%s", check.Reason, s), score)
519523
}
520-
return fmt.Sprintf("score is %d: %s", score, check.Reason)
524+
return messageWithScore(check.Reason, score)
521525
}
522526

523527
// AsSARIF outputs ScorecardResult in SARIF 2.1.0 format.
@@ -603,7 +607,8 @@ func (r *ScorecardResult) AsSARIF(showDetails bool, logLevel log.Level,
603607
} else {
604608
for _, loc := range locs {
605609
// Use the location's message (check's detail's message) as message.
606-
cr := createSARIFCheckResult(RuleIndex, sarifCheckID, loc.Message.Text, &loc)
610+
msg := messageWithScore(loc.Message.Text, check.Score)
611+
cr := createSARIFCheckResult(RuleIndex, sarifCheckID, msg, &loc)
607612
run.Results = append(run.Results, cr)
608613
}
609614
}

pkg/testdata/check1.sarif

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
"ruleId": "CheckNameID",
4848
"ruleIndex": 0,
4949
"message": {
50-
"text": "warn message\nClick Remediation section below to solve this issue"
50+
"text": "score is 5: warn message\nClick Remediation section below to solve this issue"
5151
},
5252
"locations": [
5353
{

pkg/testdata/check2.sarif

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
"ruleId": "CheckNameID",
4848
"ruleIndex": 0,
4949
"message": {
50-
"text": "warn message\nClick Remediation section below to solve this issue"
50+
"text": "score is 0: warn message\nClick Remediation section below to solve this issue"
5151
},
5252
"locations": [
5353
{

pkg/testdata/check3.sarif

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@
104104
"ruleId": "CheckNameID",
105105
"ruleIndex": 0,
106106
"message": {
107-
"text": "warn message\nClick Remediation section below to solve this issue"
107+
"text": "score is 0: warn message\nClick Remediation section below to solve this issue"
108108
},
109109
"locations": [
110110
{
@@ -128,7 +128,7 @@
128128
"ruleId": "CheckName2ID",
129129
"ruleIndex": 1,
130130
"message": {
131-
"text": "warn message\nClick Remediation section below to solve this issue"
131+
"text": "score is 0: warn message\nClick Remediation section below to solve this issue"
132132
},
133133
"locations": [
134134
{

pkg/testdata/check4.sarif

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@
104104
"ruleId": "CheckNameID",
105105
"ruleIndex": 0,
106106
"message": {
107-
"text": "warn message\nClick Remediation section below to solve this issue"
107+
"text": "score is 0: warn message\nClick Remediation section below to solve this issue"
108108
},
109109
"locations": [
110110
{
@@ -128,7 +128,7 @@
128128
"ruleId": "CheckName2ID",
129129
"ruleIndex": 1,
130130
"message": {
131-
"text": "warn message\nClick Remediation section below to solve this issue"
131+
"text": "score is 0: warn message\nClick Remediation section below to solve this issue"
132132
},
133133
"locations": [
134134
{

pkg/testdata/check7.sarif

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@
104104
"ruleId": "CheckNameID",
105105
"ruleIndex": 0,
106106
"message": {
107-
"text": "warn message\nClick Remediation section below to solve this issue"
107+
"text": "score is 0: warn message\nClick Remediation section below to solve this issue"
108108
},
109109
"locations": [
110110
{

pkg/testdata/check8.sarif

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@
7676
"ruleId": "CheckNameID",
7777
"ruleIndex": 0,
7878
"message": {
79-
"text": "warn message\nClick Remediation section below to solve this issue"
79+
"text": "score is 5: warn message\nClick Remediation section below to solve this issue"
8080
},
8181
"locations": [
8282
{
@@ -103,7 +103,7 @@
103103
"ruleId": "CheckNameID",
104104
"ruleIndex": 0,
105105
"message": {
106-
"text": "warn message\nClick Remediation section below to solve this issue"
106+
"text": "score is 5: warn message\nClick Remediation section below to solve this issue"
107107
},
108108
"locations": [
109109
{
@@ -130,7 +130,7 @@
130130
"ruleId": "CheckName5ID",
131131
"ruleIndex": 1,
132132
"message": {
133-
"text": "warn message\nClick Remediation section below to solve this issue"
133+
"text": "score is 8: warn message\nClick Remediation section below to solve this issue"
134134
},
135135
"locations": [
136136
{
@@ -157,7 +157,7 @@
157157
"ruleId": "CheckName5ID",
158158
"ruleIndex": 1,
159159
"message": {
160-
"text": "warn message\nClick Remediation section below to solve this issue"
160+
"text": "score is 8: warn message\nClick Remediation section below to solve this issue"
161161
},
162162
"locations": [
163163
{
@@ -229,7 +229,7 @@
229229
"ruleId": "CheckName6ID",
230230
"ruleIndex": 0,
231231
"message": {
232-
"text": "warn message\nClick Remediation section below to solve this issue"
232+
"text": "score is 9: warn message\nClick Remediation section below to solve this issue"
233233
},
234234
"locations": [
235235
{
@@ -301,7 +301,7 @@
301301
"ruleId": "CheckName4ID",
302302
"ruleIndex": 0,
303303
"message": {
304-
"text": "warn message\nClick Remediation section below to solve this issue"
304+
"text": "score is 5: warn message\nClick Remediation section below to solve this issue"
305305
},
306306
"locations": [
307307
{

0 commit comments

Comments
 (0)