Skip to content

Commit

Permalink
Merge pull request #1 from owenrumney/initial_dev
Browse files Browse the repository at this point in the history
Update the version and schema
  • Loading branch information
owenrumney authored Oct 23, 2020
2 parents 17977a7 + 847640d commit 33247f8
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 52 deletions.
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,14 @@
# go-sarif
Go library for sarif - Static Analysis Results Interchange Format

## What?

SARIF is the Static Analysis Results Interchange Format, this project seeks to provide a simple interface to generate reports in the SARIF format.

## What prompted this?
One of the projects I like to contribute to is [tfsec](https://tfsec.dev) - this is a static analysis tool for Terraform which produces output in many formats. Generating SARIF reports is missing functionality and felt like it warranted being moved out to a project of its own.

## More information about SARIF
For more information about SARIF, you can visit the [Oasis Open](https://www.oasis-open.org/committees/tc_home.php?wg_abbrev=sarif) site.

## Usage

4 changes: 2 additions & 2 deletions models/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package models

type Run struct {
Tool *Tool `json:"tool"`
Artifacts []*Location `json:"artifacts"`
Results []*Result `json:"results"`
Artifacts []*Location `json:"artifacts,omitempty"`
Results []*Result `json:"results,omitempty"`
}

func CreateRun(tool *Tool) *Run {
Expand Down
6 changes: 3 additions & 3 deletions models/tool.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ type Tool struct {
}

type Driver struct {
Name string `json:"name"`
InformationUri string `json:"informationUri"`
Rules []*Rule
Name string `json:"name"`
InformationUri string `json:"informationUri"`
Rules []*Rule `json:"rules,omitempty"`
}

type Rule struct {
Expand Down
30 changes: 0 additions & 30 deletions sarif.go

This file was deleted.

53 changes: 53 additions & 0 deletions sarif/sarif.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package sarif

import (
"encoding/json"
"errors"
"fmt"
"github.com/owenrumney/go-sarif/models"
"io"
)

var versions = map[string]string{
"2.1.0": "http://json.schemastore.org/sarif-2.1.0-rtm.4",
}

type SarifReport struct {
Version string `json:"version"`
Schema string `json:"$schema"`
Runs []*models.Run `json:"runs"`
}

func New(version string) (*SarifReport, error) {
schema, err := getVersionSchema(version)
if err != nil {
return nil, err
}
return &SarifReport{
Version: version,
Schema: schema,
Runs: []*models.Run{},
}, nil
}

func getVersionSchema(version string) (string, error) {
for ver, schema := range versions {
if ver == version {
return schema, nil
}
}
return "", errors.New(fmt.Sprintf("version [%s] is not supported", version))
}

func (sarif *SarifReport) AddRun(run *models.Run) {
sarif.Runs = append(sarif.Runs, run)
}

func (sarif *SarifReport) Write(w io.Writer) error {
marshal, err := json.Marshal(sarif)
if err != nil {
return err
}
_, err = w.Write(marshal)
return err
}
45 changes: 31 additions & 14 deletions test/sarif_stage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,53 @@ package test
import (
"bytes"
"github.com/magiconair/properties/assert"
go_sarif "github.com/owenrumney/go-sarif"
"github.com/owenrumney/go-sarif/models"
"github.com/owenrumney/go-sarif/sarif"
"testing"
)

type sarifHarness struct {
type sarifTest struct {
t *testing.T
sarifReport *go_sarif.SarifReport
sarifReport *sarif.SarifReport
content string
}

func (h *sarifHarness) a_new_sarif_report() {
h.sarifReport = go_sarif.New()
func (st *sarifTest) a_new_sarif_report() {
report, err := sarif.New("2.1.0")
if err != nil {
panic(err)
}
st.sarifReport = report
}

func (h *sarifHarness) the_report_is_written_to_string() {
func (st *sarifTest) the_report_is_written_to_string() {
buf := new(bytes.Buffer)
err := h.sarifReport.Write(buf)
err := st.sarifReport.Write(buf)
if err != nil {
h.t.Error(err)
st.t.Error(err)
}
h.content = buf.String()
st.content = buf.String()
}

func (h *sarifHarness) content_should_be(expected string) {
assert.Equal(h.t, h.content, expected)
func (st *sarifTest) content_should_be(expected string) {
assert.Equal(st.t, st.content, expected)
}

func CreateNewSarifHarness(t *testing.T) (*sarifHarness, *sarifHarness, *sarifHarness) {
s := &sarifHarness{
func CreateNewSarifHarness(t *testing.T) (*sarifTest, *sarifTest, *sarifTest) {
sarifTest := &sarifTest{
t: t,
}
return s, s, s
return sarifTest, sarifTest, sarifTest
}

func (st *sarifTest) and() *sarifTest {
return st
}

func (st *sarifTest) a_driver_is_added() *sarifTest {
driver := models.CreateDriver("ESLint", "https://eslint.org")
tool := models.CreateTool(driver)
run := models.CreateRun(tool)
st.sarifReport.AddRun(run)
return st
}
17 changes: 15 additions & 2 deletions test/sarif_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,23 @@ package test

import "testing"

func Test_create_new_sarif_file(t *testing.T) {
func Test_create_new_a_new_empty_sarif_file(t *testing.T) {
given, when, then := CreateNewSarifHarness(t)
expected := `{"runs":[]}`

expected := `{"version":"2.1.0","$schema":"http://json.schemastore.org/sarif-2.1.0-rtm.4","runs":[]}`

given.a_new_sarif_report()
when.the_report_is_written_to_string()
then.content_should_be(expected)
}

func Test_create_new_a_new_sarif_file_with_a_driver(t *testing.T) {
given, when, then := CreateNewSarifHarness(t)

expected := `{"version":"2.1.0","$schema":"http://json.schemastore.org/sarif-2.1.0-rtm.4","runs":[{"tool":{"driver":{"name":"ESLint","informationUri":"https://eslint.org"}}}]}`

given.a_new_sarif_report()
when.a_driver_is_added().
and().the_report_is_written_to_string()
then.content_should_be(expected)
}

0 comments on commit 33247f8

Please sign in to comment.