Skip to content

Commit 7de1f99

Browse files
authored
feat: Add location to SARIF output (#1207)
Signed-off-by: James Alseth <james@jalseth.me>
1 parent ff4e456 commit 7de1f99

File tree

2 files changed

+22
-20
lines changed

2 files changed

+22
-20
lines changed

output/sarif.go

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"io"
66
"path/filepath"
7+
"strconv"
78
"strings"
89

910
"github.com/open-policy-agent/conftest/internal/version"
@@ -74,19 +75,10 @@ func addRuleIndex(run *sarif.Run, ruleID string, result Result, indices map[stri
7475
// addRule adds a new rule to the SARIF run with the given ID and result metadata.
7576
func addRule(run *sarif.Run, ruleID string, result Result) {
7677
desc := getRuleDescription(ruleID)
77-
rule := run.AddRule(ruleID).
78+
run.AddRule(ruleID).
7879
WithDescription(desc).
79-
WithShortDescription(&sarif.MultiformatMessageString{
80-
Text: &desc,
81-
})
82-
83-
if result.Metadata != nil {
84-
props := sarif.NewPropertyBag()
85-
for k, v := range result.Metadata {
86-
props.Add(k, v)
87-
}
88-
rule.WithProperties(props.Properties)
89-
}
80+
WithProperties(result.Metadata).
81+
WithShortDescription(sarif.NewMultiformatMessageString(desc))
9082
}
9183

9284
// addResult adds a result to the SARIF run
@@ -97,18 +89,20 @@ func addResult(run *sarif.Run, result Result, namespace, ruleType, level, fileNa
9789
idx = addRuleIndex(run, ruleID, result, indices)
9890
}
9991

92+
location := sarif.NewPhysicalLocation()
93+
if loc := result.Location; loc != nil {
94+
line, _ := strconv.Atoi(loc.Line.String())
95+
location.ArtifactLocation = sarif.NewSimpleArtifactLocation(filepath.ToSlash(loc.File))
96+
location.Region = sarif.NewRegion().WithStartLine(line).WithEndLine(line)
97+
} else {
98+
location.ArtifactLocation = sarif.NewSimpleArtifactLocation(filepath.ToSlash(fileName))
99+
}
100+
100101
run.CreateResultForRule(ruleID).
101102
WithRuleIndex(idx).
102103
WithLevel(level).
103104
WithMessage(sarif.NewTextMessage(result.Message)).
104-
AddLocation(
105-
sarif.NewLocationWithPhysicalLocation(
106-
sarif.NewPhysicalLocation().
107-
WithArtifactLocation(
108-
sarif.NewSimpleArtifactLocation(filepath.ToSlash(fileName)),
109-
),
110-
),
111-
)
105+
AddLocation(sarif.NewLocationWithPhysicalLocation(location))
112106
}
113107

114108
// Output outputs the results in SARIF format.

output/sarif_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,10 @@ func TestSARIF_Output(t *testing.T) {
124124
Warnings: []Result{
125125
{
126126
Message: "test warning",
127+
Location: &Location{
128+
File: "test.yaml",
129+
Line: json.Number("123"),
130+
},
127131
Metadata: map[string]any{
128132
"foo": "bar",
129133
},
@@ -175,6 +179,10 @@ func TestSARIF_Output(t *testing.T) {
175179
"artifactLocation": map[string]any{
176180
"uri": "test.yaml",
177181
},
182+
"region": map[string]any{
183+
"startLine": float64(123),
184+
"endLine": float64(123),
185+
},
178186
},
179187
},
180188
},

0 commit comments

Comments
 (0)