Skip to content

Commit

Permalink
sarif: add tool version, exclude empty range/position, slash paths (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
bendrucker authored Feb 13, 2023
1 parent db6a012 commit 602fa73
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 29 deletions.
48 changes: 28 additions & 20 deletions formatter/sarif.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package formatter
import (
"errors"
"fmt"
"path/filepath"

"github.com/hashicorp/hcl/v2"
"github.com/owenrumney/go-sarif/sarif"
Expand All @@ -17,6 +18,10 @@ func (f *Formatter) sarifPrint(issues tflint.Issues, appErr error) {
}

run := sarif.NewRun("tflint", "https://github.com/terraform-linters/tflint")

version := tflint.Version.String()
run.Tool.Driver.Version = &version

report.AddRun(run)

for _, issue := range issues {
Expand All @@ -34,39 +39,42 @@ func (f *Formatter) sarifPrint(issues tflint.Issues, appErr error) {
panic(fmt.Errorf("Unexpected lint type: %s", issue.Rule.Severity()))
}

endLine := issue.Range.End.Line
if endLine == 0 {
endLine = 1
}
endColumn := issue.Range.End.Column
if endColumn == 0 {
endColumn = 1
var location *sarif.PhysicalLocation
if issue.Range.Filename != "" {
location = sarif.NewPhysicalLocation().
WithArtifactLocation(sarif.NewSimpleArtifactLocation(filepath.ToSlash(issue.Range.Filename)))

if !issue.Range.Empty() {
location.WithRegion(
sarif.NewRegion().
WithStartLine(issue.Range.Start.Line).
WithStartColumn(issue.Range.Start.Column).
WithEndLine(issue.Range.Start.Column).
WithEndColumn(issue.Range.End.Column),
)
}
}

location := sarif.NewPhysicalLocation().
WithArtifactLocation(sarif.NewSimpleArtifactLocation(issue.Range.Filename)).
WithRegion(
sarif.NewRegion().
WithStartLine(issue.Range.Start.Line).
WithStartColumn(issue.Range.Start.Column).
WithEndLine(endLine).
WithEndColumn(endColumn),
)

run.AddResult(rule.ID).
result := run.AddResult(rule.ID).
WithLevel(level).
WithLocation(sarif.NewLocationWithPhysicalLocation(location)).
WithMessage(sarif.NewTextMessage(issue.Message))

if location != nil {
result.WithLocation(sarif.NewLocationWithPhysicalLocation(location))
}
}

errRun := sarif.NewRun("tflint-errors", "https://github.com/terraform-linters/tflint")
errRun.Tool.Driver.Version = &version

report.AddRun(errRun)

if appErr != nil {
var diags hcl.Diagnostics
if errors.As(appErr, &diags) {
for _, diag := range diags {
location := sarif.NewPhysicalLocation().
WithArtifactLocation(sarif.NewSimpleArtifactLocation(diag.Subject.Filename)).
WithArtifactLocation(sarif.NewSimpleArtifactLocation(filepath.ToSlash(diag.Subject.Filename))).
WithRegion(
sarif.NewRegion().
WithByteOffset(diag.Subject.Start.Byte).
Expand Down
92 changes: 83 additions & 9 deletions formatter/sarif_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import (
"bytes"
"errors"
"fmt"
"path/filepath"
"testing"

"github.com/google/go-cmp/cmp"
hcl "github.com/hashicorp/hcl/v2"
"github.com/terraform-linters/tflint/tflint"
"github.com/xeipuuv/gojsonschema"
Expand All @@ -29,6 +31,7 @@ func Test_sarifPrint(t *testing.T) {
"tool": {
"driver": {
"name": "tflint",
"version": "0.45.0",
"informationUri": "https://github.com/terraform-linters/tflint"
}
},
Expand All @@ -38,6 +41,7 @@ func Test_sarifPrint(t *testing.T) {
"tool": {
"driver": {
"name": "tflint-errors",
"version": "0.45.0",
"informationUri": "https://github.com/terraform-linters/tflint"
}
},
Expand Down Expand Up @@ -67,6 +71,7 @@ func Test_sarifPrint(t *testing.T) {
"tool": {
"driver": {
"name": "tflint",
"version": "0.45.0",
"informationUri": "https://github.com/terraform-linters/tflint",
"rules": [
{
Expand Down Expand Up @@ -108,6 +113,7 @@ func Test_sarifPrint(t *testing.T) {
"tool": {
"driver": {
"name": "tflint-errors",
"version": "0.45.0",
"informationUri": "https://github.com/terraform-linters/tflint"
}
},
Expand All @@ -117,19 +123,18 @@ func Test_sarifPrint(t *testing.T) {
}`,
},
{
Name: "Issues with SARIF-invalid position are output correctly",
Name: "issues in directories",
Issues: tflint.Issues{
{
Rule: &testRule{},
Message: "test",
Range: hcl.Range{
Filename: "test.tf",
Start: hcl.Pos{Line: 1, Column: 1},
End: hcl.Pos{Line: 0, Column: 0},
Filename: filepath.Join("test", "main.tf"),
Start: hcl.Pos{Line: 1, Column: 1, Byte: 0},
End: hcl.Pos{Line: 1, Column: 4, Byte: 3},
},
},
},
Error: fmt.Errorf("Failed to work; %w", errors.New("I don't feel like working")),
Stdout: `{
"version": "2.1.0",
"$schema": "https://json.schemastore.org/sarif-2.1.0-rtm.5.json",
Expand All @@ -138,6 +143,7 @@ func Test_sarifPrint(t *testing.T) {
"tool": {
"driver": {
"name": "tflint",
"version": "0.45.0",
"informationUri": "https://github.com/terraform-linters/tflint",
"rules": [
{
Expand All @@ -161,13 +167,78 @@ func Test_sarifPrint(t *testing.T) {
{
"physicalLocation": {
"artifactLocation": {
"uri": "test.tf"
"uri": "test/main.tf"
},
"region": {
"startLine": 1,
"startColumn": 1,
"endLine": 1,
"endColumn": 1
"endColumn": 4
}
}
}
]
}
]
},
{
"tool": {
"driver": {
"name": "tflint-errors",
"version": "0.45.0",
"informationUri": "https://github.com/terraform-linters/tflint"
}
},
"results": []
}
]
}`,
},
{
Name: "Issues with missing source positions",
Issues: tflint.Issues{
{
Rule: &testRule{},
Message: "test",
Range: hcl.Range{
Filename: "test.tf",
},
},
},
Error: fmt.Errorf("Failed to work; %w", errors.New("I don't feel like working")),
Stdout: `{
"version": "2.1.0",
"$schema": "https://json.schemastore.org/sarif-2.1.0-rtm.5.json",
"runs": [
{
"tool": {
"driver": {
"name": "tflint",
"version": "0.45.0",
"informationUri": "https://github.com/terraform-linters/tflint",
"rules": [
{
"id": "test_rule",
"shortDescription": {
"text": ""
},
"helpUri": "https://github.com"
}
]
}
},
"results": [
{
"ruleId": "test_rule",
"level": "error",
"message": {
"text": "test"
},
"locations": [
{
"physicalLocation": {
"artifactLocation": {
"uri": "test.tf"
}
}
}
Expand All @@ -179,6 +250,7 @@ func Test_sarifPrint(t *testing.T) {
"tool": {
"driver": {
"name": "tflint-errors",
"version": "0.45.0",
"informationUri": "https://github.com/terraform-linters/tflint"
}
},
Expand Down Expand Up @@ -220,6 +292,7 @@ func Test_sarifPrint(t *testing.T) {
"tool": {
"driver": {
"name": "tflint",
"version": "0.45.0",
"informationUri": "https://github.com/terraform-linters/tflint"
}
},
Expand All @@ -229,6 +302,7 @@ func Test_sarifPrint(t *testing.T) {
"tool": {
"driver": {
"name": "tflint-errors",
"version": "0.45.0",
"informationUri": "https://github.com/terraform-linters/tflint"
}
},
Expand Down Expand Up @@ -272,8 +346,8 @@ func Test_sarifPrint(t *testing.T) {

formatter.Print(tc.Issues, tc.Error, map[string][]byte{})

if stdout.String() != tc.Stdout {
t.Fatalf("Failed %s test: expected=%s, stdout=%s", tc.Name, tc.Stdout, stdout.String())
if diff := cmp.Diff(tc.Stdout, stdout.String()); diff != "" {
t.Fatalf("Failed %s test: %s", tc.Name, diff)
}

schemaLoader := gojsonschema.NewReferenceLoader("http://json.schemastore.org/sarif-2.1.0")
Expand Down

0 comments on commit 602fa73

Please sign in to comment.