Skip to content

Commit

Permalink
✨ Add score to SARIF for all results (#1694)
Browse files Browse the repository at this point in the history
* add score

* fix unit tests
  • Loading branch information
laurentsimon authored Mar 3, 2022
1 parent 3818dbe commit d192c8e
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 16 deletions.
11 changes: 8 additions & 3 deletions pkg/sarif.go
Original file line number Diff line number Diff line change
Expand Up @@ -510,14 +510,18 @@ func filterOutDetailType(details []checker.CheckDetail, t checker.DetailType) []
return ret
}

func messageWithScore(msg string, score int) string {
return fmt.Sprintf("score is %d: %s", score, msg)
}

func createDefaultLocationMessage(check *checker.CheckResult, score int) string {
details := filterOutDetailType(check.Details2, checker.DetailInfo)
s, b := detailsToString(details, log.WarnLevel)
if b {
// Warning: GitHub UX needs a single `\n` to turn it into a `<br>`.
return fmt.Sprintf("score is %d: %s:\n%s", score, check.Reason, s)
return messageWithScore(fmt.Sprintf("%s:\n%s", check.Reason, s), score)
}
return fmt.Sprintf("score is %d: %s", score, check.Reason)
return messageWithScore(check.Reason, score)
}

// AsSARIF outputs ScorecardResult in SARIF 2.1.0 format.
Expand Down Expand Up @@ -603,7 +607,8 @@ func (r *ScorecardResult) AsSARIF(showDetails bool, logLevel log.Level,
} else {
for _, loc := range locs {
// Use the location's message (check's detail's message) as message.
cr := createSARIFCheckResult(RuleIndex, sarifCheckID, loc.Message.Text, &loc)
msg := messageWithScore(loc.Message.Text, check.Score)
cr := createSARIFCheckResult(RuleIndex, sarifCheckID, msg, &loc)
run.Results = append(run.Results, cr)
}
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/testdata/check1.sarif
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
"ruleId": "CheckNameID",
"ruleIndex": 0,
"message": {
"text": "warn message\nClick Remediation section below to solve this issue"
"text": "score is 5: warn message\nClick Remediation section below to solve this issue"
},
"locations": [
{
Expand Down
2 changes: 1 addition & 1 deletion pkg/testdata/check2.sarif
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
"ruleId": "CheckNameID",
"ruleIndex": 0,
"message": {
"text": "warn message\nClick Remediation section below to solve this issue"
"text": "score is 0: warn message\nClick Remediation section below to solve this issue"
},
"locations": [
{
Expand Down
4 changes: 2 additions & 2 deletions pkg/testdata/check3.sarif
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@
"ruleId": "CheckNameID",
"ruleIndex": 0,
"message": {
"text": "warn message\nClick Remediation section below to solve this issue"
"text": "score is 0: warn message\nClick Remediation section below to solve this issue"
},
"locations": [
{
Expand All @@ -128,7 +128,7 @@
"ruleId": "CheckName2ID",
"ruleIndex": 1,
"message": {
"text": "warn message\nClick Remediation section below to solve this issue"
"text": "score is 0: warn message\nClick Remediation section below to solve this issue"
},
"locations": [
{
Expand Down
4 changes: 2 additions & 2 deletions pkg/testdata/check4.sarif
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@
"ruleId": "CheckNameID",
"ruleIndex": 0,
"message": {
"text": "warn message\nClick Remediation section below to solve this issue"
"text": "score is 0: warn message\nClick Remediation section below to solve this issue"
},
"locations": [
{
Expand All @@ -128,7 +128,7 @@
"ruleId": "CheckName2ID",
"ruleIndex": 1,
"message": {
"text": "warn message\nClick Remediation section below to solve this issue"
"text": "score is 0: warn message\nClick Remediation section below to solve this issue"
},
"locations": [
{
Expand Down
2 changes: 1 addition & 1 deletion pkg/testdata/check7.sarif
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@
"ruleId": "CheckNameID",
"ruleIndex": 0,
"message": {
"text": "warn message\nClick Remediation section below to solve this issue"
"text": "score is 0: warn message\nClick Remediation section below to solve this issue"
},
"locations": [
{
Expand Down
12 changes: 6 additions & 6 deletions pkg/testdata/check8.sarif
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
"ruleId": "CheckNameID",
"ruleIndex": 0,
"message": {
"text": "warn message\nClick Remediation section below to solve this issue"
"text": "score is 5: warn message\nClick Remediation section below to solve this issue"
},
"locations": [
{
Expand All @@ -103,7 +103,7 @@
"ruleId": "CheckNameID",
"ruleIndex": 0,
"message": {
"text": "warn message\nClick Remediation section below to solve this issue"
"text": "score is 5: warn message\nClick Remediation section below to solve this issue"
},
"locations": [
{
Expand All @@ -130,7 +130,7 @@
"ruleId": "CheckName5ID",
"ruleIndex": 1,
"message": {
"text": "warn message\nClick Remediation section below to solve this issue"
"text": "score is 8: warn message\nClick Remediation section below to solve this issue"
},
"locations": [
{
Expand All @@ -157,7 +157,7 @@
"ruleId": "CheckName5ID",
"ruleIndex": 1,
"message": {
"text": "warn message\nClick Remediation section below to solve this issue"
"text": "score is 8: warn message\nClick Remediation section below to solve this issue"
},
"locations": [
{
Expand Down Expand Up @@ -229,7 +229,7 @@
"ruleId": "CheckName6ID",
"ruleIndex": 0,
"message": {
"text": "warn message\nClick Remediation section below to solve this issue"
"text": "score is 9: warn message\nClick Remediation section below to solve this issue"
},
"locations": [
{
Expand Down Expand Up @@ -301,7 +301,7 @@
"ruleId": "CheckName4ID",
"ruleIndex": 0,
"message": {
"text": "warn message\nClick Remediation section below to solve this issue"
"text": "score is 5: warn message\nClick Remediation section below to solve this issue"
},
"locations": [
{
Expand Down

0 comments on commit d192c8e

Please sign in to comment.