Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for sarif reports #108

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions cmd/validator/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,8 @@ func getReporter(reportType, outputDest *string) reporter.Reporter {
return reporter.NewJunitReporter(*outputDest)
case "json":
return reporter.NewJSONReporter(*outputDest)
case "sarif":
return reporter.NewSarifReporter(*outputDest)
default:
return reporter.StdoutReporter{}
}
Expand Down
145 changes: 145 additions & 0 deletions pkg/reporter/sarif_reporter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
package reporter

import (
"encoding/json"
"fmt"
)

type SarifReporter struct {
outputDest string
}

type driver struct {
Name string `json:"name"`
InformationURI string `json:"informationUri"`
}

type tool struct {
Driver driver `json:"driver"`
}
type artifactLocation struct {
URI string `json:"uri"`
}

type physicalLocation struct {
ArtifactLocation artifactLocation `json:"artifactLocation"`
}

type message struct {
Text string `json:"text"`
}

type location struct {
PhysicalLocation physicalLocation `json:"physicalLocation"`
}

type result struct {
Message message `json:"message"`
Locations []location `json:"locations,omitempty"`
Level string `json:"level"`
}

type run struct {
Tool tool `json:"tool"`
Results []result `json:"results"`
}

type sarifReport struct {
Version string `json:"version"`
Schema string `json:"$schema"`
Runs []run `json:"runs"`
}

func NewSarifReporter(outputDest string) *SarifReporter {
return &SarifReporter{
outputDest: outputDest,
}
}

func (sr SarifReporter) Print(reports []Report) error {
report := createSarifReport(reports)

jsonBytes, err := json.MarshalIndent(report, "", " ")
if err != nil {
return err
}

jsonBytes = append(jsonBytes, '\n')
fmt.Print(string(jsonBytes))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line looks like a left behind debug used for dev purpose


if sr.outputDest != "" {
return outputBytesToFile(sr.outputDest, "result", "json", jsonBytes)
}

return nil
}

func createSarifReport(reports []Report) *sarifReport {

results := getResultsFromReports(reports)

sr := &sarifReport{
Version: "2.1.0",
Schema: "http://json.schemastore.org/sarif-2.1.0-rtm.4",
Runs: []run{
{Tool: tool{
Driver: driver{
Name: "config-file-validator",
InformationURI: "https://github.com/Boeing/config-file-validator",
},
},
Results: results,
},
},
}

return sr
}

func getResultsFromReports(reports []Report) []result {
results := []result{}
failedValidations := 0
successValidations := 0

for _, report := range reports {
if report.IsValid {
successValidations++
continue
}
res := result{
Message: message{
Text: report.ValidationError.Error(),
},
Locations: []location{
{
PhysicalLocation: physicalLocation{
ArtifactLocation: artifactLocation{
URI: report.FilePath,
},
},
},
},
Level: "error",
}
results = append(results, res)
failedValidations++
}

successSummary := result{
Message: message{
Text: fmt.Sprintf("Tests Passed: %d", successValidations),
},
Level: "note",
}

failedSummary := result{
Message: message{
Text: fmt.Sprintf("Tests failed: %d", failedValidations),
},
Level: "error",
}

results = append(results, successSummary, failedSummary)

return results
}
Loading