Skip to content

Commit 2d5b9e1

Browse files
refactor: changed git report message (#285)
**Proposed Changes** * The message field in the reports of git scans used to have too much git information that has now been removed in order to make the message clearer. The removed information is still accessible in the artifactLocation field of the report. **Checklist** - [ ] I covered my changes with tests. - [ ] I Updated the documentation that is affected by my changes: - [ ] Change in the CLI arguments - [ ] Change in the configuration file I submit this contribution under the Apache-2.0 license.
1 parent fc68586 commit 2d5b9e1

3 files changed

Lines changed: 66 additions & 5 deletions

File tree

lib/reporting/report_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ var (
8484
// sarif results
8585
result1Sarif = Results{
8686
Message: Message{
87-
Text: messageText(result1.RuleID, result1.Source),
87+
Text: createMessageText(result1.RuleID, result1.Source),
8888
},
8989
RuleId: ruleID1,
9090
Locations: []Locations{
@@ -115,7 +115,7 @@ var (
115115
}
116116
result2Sarif = Results{
117117
Message: Message{
118-
Text: messageText(result2.RuleID, result2.Source),
118+
Text: createMessageText(result2.RuleID, result2.Source),
119119
},
120120
RuleId: ruleID2,
121121
Locations: []Locations{
@@ -146,7 +146,7 @@ var (
146146
}
147147
result3Sarif = Results{
148148
Message: Message{
149-
Text: messageText(result3.RuleID, result3.Source),
149+
Text: createMessageText(result3.RuleID, result3.Source),
150150
},
151151
RuleId: ruleID1,
152152
Locations: []Locations{

lib/reporting/sarif.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,15 @@ func hasNoResults(report *Report) bool {
6868
return len(report.Results) == 0
6969
}
7070

71-
func messageText(ruleName string, filePath string) string {
71+
func createMessageText(ruleName string, filePath string) string {
72+
// maintain only the filename if the scan target is git
73+
if strings.HasPrefix(filePath, "git show ") {
74+
filePathParts := strings.SplitN(filePath, ":", 2)
75+
if len(filePathParts) == 2 {
76+
filePath = filePathParts[1]
77+
}
78+
}
79+
7280
return fmt.Sprintf("%s has detected secret for file %s.", ruleName, filePath)
7381
}
7482

@@ -85,7 +93,7 @@ func getResults(report *Report) []Results {
8593
for _, secret := range secrets {
8694
r := Results{
8795
Message: Message{
88-
Text: messageText(secret.RuleID, secret.Source),
96+
Text: createMessageText(secret.RuleID, secret.Source),
8997
},
9098
RuleId: secret.RuleID,
9199
Locations: getLocation(secret),

lib/reporting/sarif_test.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package reporting
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
func TestCreateMessageText(t *testing.T) {
11+
ruleName := "Test Rule"
12+
messagePrefix := ruleName + " has detected secret for file %s."
13+
14+
tests := []struct {
15+
Name string
16+
FilePath string
17+
ExpectedMessage string
18+
}{
19+
{
20+
Name: "Filesystem file name",
21+
FilePath: "folder/filename.txt",
22+
ExpectedMessage: fmt.Sprintf(messagePrefix, "folder/filename.txt"),
23+
},
24+
{
25+
Name: "Simple git filename",
26+
FilePath: "git show 1a9f3c87b4d029f54e8c72d8b11a78f6a3c29d2e:folder/filename.txt",
27+
ExpectedMessage: fmt.Sprintf(messagePrefix, "folder/filename.txt"),
28+
},
29+
{
30+
Name: "Broken git file name with no commit hash",
31+
FilePath: "git show folder/filename.txt",
32+
ExpectedMessage: fmt.Sprintf(messagePrefix, "git show folder/filename.txt"),
33+
},
34+
{
35+
Name: "Git file name with one colon character",
36+
FilePath: "git show d8e914f06d8d4494bd4f9ab2a2c9c88f78ef25ad:folder/filename:secondpart.txt",
37+
ExpectedMessage: fmt.Sprintf(messagePrefix, "folder/filename:secondpart.txt"),
38+
},
39+
{
40+
Name: "Git file name with multiple colon character",
41+
FilePath: "git show a73b5cf94f0b29e1cc6e71a092f6b8ebc1d0e002:folder:secondfolderpart/filename:secondpart.txt",
42+
ExpectedMessage: fmt.Sprintf(messagePrefix, "folder:secondfolderpart/filename:secondpart.txt"),
43+
},
44+
}
45+
46+
for _, tt := range tests {
47+
t.Run(tt.Name, func(t *testing.T) {
48+
message := createMessageText(ruleName, tt.FilePath)
49+
fmt.Printf("%v", message)
50+
assert.Equal(t, tt.ExpectedMessage, message)
51+
})
52+
}
53+
}

0 commit comments

Comments
 (0)