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.
7576func 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.
0 commit comments