@@ -27,9 +27,8 @@ type SarifReport struct {
2727}
2828
2929type Run struct {
30- Tool Tool `json:"tool"`
31- Results []Result `json:"results"`
32- Invocations []Invocation `json:"invocations,omitempty"`
30+ Tool Tool `json:"tool"`
31+ Results []Result `json:"results"`
3332}
3433
3534type Tool struct {
@@ -81,89 +80,18 @@ type ArtifactLocation struct {
8180type Region struct {
8281 StartLine int `json:"startLine"`
8382 StartColumn int `json:"startColumn"`
84- EndLine int `json:"endLine"`
85- EndColumn int `json:"endColumn"`
86- }
87-
88- type Invocation struct {
89- ExecutionSuccessful bool `json:"executionSuccessful"`
90- ExitCode int `json:"exitCode"`
91- ExitSignalName string `json:"exitSignalName"`
92- ExitSignalNumber int `json:"exitSignalNumber"`
93- Stderr Artifact `json:"stderr"`
94- }
95-
96- type Artifact struct {
97- Text string `json:"text"`
9883}
9984
10085type MessageText struct {
10186 Text string `json:"text"`
10287}
10388
104- // ReadSarifFile reads a SARIF file and returns its contents
105- func ReadSarifFile (file string ) (SarifReport , error ) {
106- data , err := os .ReadFile (file )
107- if err != nil {
108- return SarifReport {}, fmt .Errorf ("failed to read SARIF file: %w" , err )
109- }
110-
111- var sarif SarifReport
112- if err := json .Unmarshal (data , & sarif ); err != nil {
113- return SarifReport {}, fmt .Errorf ("failed to parse SARIF file: %w" , err )
114- }
115-
116- return sarif , nil
117- }
118-
119- // WriteSarifFile writes a SARIF report to a file
120- func WriteSarifFile (sarif SarifReport , outputFile string ) error {
121- out , err := os .Create (outputFile )
122- if err != nil {
123- return fmt .Errorf ("failed to create output file: %w" , err )
124- }
125- defer out .Close ()
126-
127- encoder := json .NewEncoder (out )
128- encoder .SetIndent ("" , " " )
129- if err := encoder .Encode (sarif ); err != nil {
130- return fmt .Errorf ("failed to write SARIF: %w" , err )
131- }
132-
133- return nil
134- }
135-
136- // AddErrorRun adds an error run to an existing SARIF report
137- func AddErrorRun (sarif * SarifReport , toolName string , errorMessage string ) {
138- errorRun := Run {
139- Tool : Tool {
140- Driver : Driver {
141- Name : toolName ,
142- Version : "1.0.0" ,
143- },
144- },
145- Invocations : []Invocation {
146- {
147- ExecutionSuccessful : false ,
148- ExitCode : 1 ,
149- ExitSignalName : "error" ,
150- ExitSignalNumber : 1 ,
151- Stderr : Artifact {
152- Text : errorMessage ,
153- },
154- },
155- },
156- Results : []Result {},
157- }
158- sarif .Runs = append (sarif .Runs , errorRun )
159- }
160-
16189// ConvertPylintToSarif converts Pylint JSON output to SARIF format
16290func ConvertPylintToSarif (pylintOutput []byte ) []byte {
16391 var issues []PylintIssue
16492 if err := json .Unmarshal (pylintOutput , & issues ); err != nil {
165- // If parsing fails, return empty SARIF report with error
166- return createEmptySarifReportWithError ( err . Error () )
93+ // If parsing fails, return empty SARIF report
94+ return createEmptySarifReport ( )
16795 }
16896
16997 // Create SARIF report
@@ -180,17 +108,6 @@ func ConvertPylintToSarif(pylintOutput []byte) []byte {
180108 },
181109 },
182110 Results : make ([]Result , 0 , len (issues )),
183- Invocations : []Invocation {
184- {
185- ExecutionSuccessful : true , // Pylint ran successfully if we got here
186- ExitCode : 0 ,
187- ExitSignalName : "" ,
188- ExitSignalNumber : 0 ,
189- Stderr : Artifact {
190- Text : "" ,
191- },
192- },
193- },
194111 },
195112 },
196113 }
@@ -244,33 +161,6 @@ func getSarifLevel(pylintType string) string {
244161
245162// createEmptySarifReport creates an empty SARIF report in case of errors
246163func createEmptySarifReport () []byte {
247- emptyReport := SarifReport {
248- Version : "2.1.0" ,
249- Schema : "https://raw.githubusercontent.com/oasis-tcs/sarif-spec/master/Schemata/sarif-schema-2.1.0.json" ,
250- Runs : []Run {
251- {
252- Tool : Tool {
253- Driver : Driver {
254- Name : "Pylint" ,
255- Version : "3.3.6" ,
256- InformationURI : "https://pylint.org" ,
257- },
258- },
259- Results : []Result {},
260- Invocations : []Invocation {},
261- },
262- },
263- }
264- sarifData , err := json .MarshalIndent (emptyReport , "" , " " )
265- if err != nil {
266- fmt .Fprintf (os .Stderr , "Error marshaling empty SARIF report: %v\n " , err )
267- return []byte ("{}" )
268- }
269- return sarifData
270- }
271-
272- // createEmptySarifReportWithError creates an empty SARIF report with error information
273- func createEmptySarifReportWithError (errorMessage string ) []byte {
274164 emptyReport := SarifReport {
275165 Version : "2.1.0" ,
276166 Schema : "https://raw.githubusercontent.com/oasis-tcs/sarif-spec/master/Schemata/sarif-schema-2.1.0.json" ,
@@ -284,25 +174,10 @@ func createEmptySarifReportWithError(errorMessage string) []byte {
284174 },
285175 },
286176 Results : []Result {},
287- Invocations : []Invocation {
288- {
289- ExecutionSuccessful : false ,
290- ExitCode : 1 ,
291- ExitSignalName : "error" ,
292- ExitSignalNumber : 1 ,
293- Stderr : Artifact {
294- Text : errorMessage ,
295- },
296- },
297- },
298177 },
299178 },
300179 }
301- sarifData , err := json .MarshalIndent (emptyReport , "" , " " )
302- if err != nil {
303- fmt .Fprintf (os .Stderr , "Error marshaling SARIF report with error: %v\n " , err )
304- return []byte ("{}" )
305- }
180+ sarifData , _ := json .MarshalIndent (emptyReport , "" , " " )
306181 return sarifData
307182}
308183
0 commit comments