-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add SARIF as a reporter option (#166)
* cmd/validator/validator_test.go: Fix typo. * Add SARIF reporter cmd/validator/validator.go: Add option to select reporter type as SARIF. pkg/reporter/sarif_reporter.go: Create SARIF report. https://sarifweb.azurewebsites.net/ * Add tests for SARIF reporter. cmd/validator/validator_test.go: Test for --reporter=sarif flag. pkg/reporter/reporter_test.go: Test for SARIF report. * Add test for SARIF reporter writer. * cmd/validator/validator.go: Satisfy goreportcard. * Fix SARIF validation errors * Combine groupOutput check for junit and sarif. * Add groupby test for sarif reporter. * make version, schema, driver name and infoUri constants and move them outside. * Make gocyclo happy
- Loading branch information
1 parent
405d9dd
commit 5193c9c
Showing
5 changed files
with
323 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
package reporter | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
const SARIFVersion = "2.1.0" | ||
const SARIFSchema = "https://docs.oasis-open.org/sarif/sarif/v2.1.0/errata01/os/schemas/sarif-schema-2.1.0.json" | ||
const DriverName = "config-file-validator" | ||
const DriverInfoURI = "https://github.com/Boeing/config-file-validator" | ||
const DriverVersion = "1.7.1" | ||
|
||
type SARIFReporter struct { | ||
outputDest string | ||
} | ||
|
||
type SARIFLog struct { | ||
Version string `json:"version"` | ||
Schema string `json:"$schema"` | ||
Runs []runs `json:"runs"` | ||
} | ||
|
||
type runs struct { | ||
Tool tool `json:"tool"` | ||
Results []result `json:"results"` | ||
} | ||
|
||
type tool struct { | ||
Driver driver `json:"driver"` | ||
} | ||
|
||
type driver struct { | ||
Name string `json:"name"` | ||
InfoURI string `json:"informationUri"` | ||
Version string `json:"version"` | ||
} | ||
|
||
type result struct { | ||
Kind string `json:"kind"` | ||
Level string `json:"level"` | ||
Message message `json:"message"` | ||
Locations []location `json:"locations"` | ||
} | ||
|
||
type message struct { | ||
Text string `json:"text"` | ||
} | ||
|
||
type location struct { | ||
PhysicalLocation physicalLocation `json:"physicalLocation"` | ||
} | ||
|
||
type physicalLocation struct { | ||
ArtifactLocation artifactLocation `json:"artifactLocation"` | ||
} | ||
|
||
type artifactLocation struct { | ||
URI string `json:"uri"` | ||
} | ||
|
||
func NewSARIFReporter(outputDest string) *SARIFReporter { | ||
return &SARIFReporter{ | ||
outputDest: outputDest, | ||
} | ||
} | ||
|
||
func createSARIFReport(reports []Report) (*SARIFLog, error) { | ||
var log SARIFLog | ||
|
||
n := len(reports) | ||
|
||
log.Version = SARIFVersion | ||
log.Schema = SARIFSchema | ||
|
||
log.Runs = make([]runs, 1) | ||
runs := &log.Runs[0] | ||
|
||
runs.Tool.Driver.Name = DriverName | ||
runs.Tool.Driver.InfoURI = DriverInfoURI | ||
runs.Tool.Driver.Version = DriverVersion | ||
|
||
runs.Results = make([]result, n) | ||
|
||
for i, report := range reports { | ||
if strings.Contains(report.FilePath, "\\") { | ||
report.FilePath = strings.ReplaceAll(report.FilePath, "\\", "/") | ||
} | ||
|
||
result := &runs.Results[i] | ||
if !report.IsValid { | ||
result.Kind = "fail" | ||
result.Level = "error" | ||
result.Message.Text = report.ValidationError.Error() | ||
} else { | ||
result.Kind = "pass" | ||
result.Level = "none" | ||
result.Message.Text = "No errors detected" | ||
} | ||
|
||
result.Locations = make([]location, 1) | ||
location := &result.Locations[0] | ||
|
||
location.PhysicalLocation.ArtifactLocation.URI = "file:///" + report.FilePath | ||
} | ||
|
||
return &log, nil | ||
} | ||
|
||
func (sr SARIFReporter) Print(reports []Report) error { | ||
report, err := createSARIFReport(reports) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
sarifBytes, err := json.MarshalIndent(report, "", " ") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
sarifBytes = append(sarifBytes, '\n') | ||
|
||
if len(reports) > 0 && !reports[0].IsQuiet { | ||
fmt.Print(string(sarifBytes)) | ||
} | ||
|
||
if sr.outputDest != "" { | ||
return outputBytesToFile(sr.outputDest, "result", "sarif", sarifBytes) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
{ | ||
"version": "2.1.0", | ||
"$schema": "https://docs.oasis-open.org/sarif/sarif/v2.1.0/errata01/os/schemas/sarif-schema-2.1.0.json", | ||
"runs": [ | ||
{ | ||
"tool": { | ||
"driver": { | ||
"name": "config-file-validator", | ||
"informationUri": "https://github.com/Boeing/config-file-validator", | ||
"version": "1.7.1" | ||
} | ||
}, | ||
"results": [ | ||
{ | ||
"kind": "pass", | ||
"level": "none", | ||
"message": { | ||
"text": "No errors detected" | ||
}, | ||
"locations": [ | ||
{ | ||
"physicalLocation": { | ||
"artifactLocation": { | ||
"uri": "file:///test/output/example/good.json" | ||
} | ||
} | ||
} | ||
] | ||
} | ||
] | ||
} | ||
] | ||
} |