From 4bda00049e30eaf5bfd1900c94d86b386c2e832e Mon Sep 17 00:00:00 2001 From: Jerome Brown Date: Tue, 3 Sep 2024 07:44:26 +1200 Subject: [PATCH] chore: Update to owenrumney/go-sarif/v2 (#2111) --- formatter/sarif.go | 22 ++++---- formatter/sarif_test.go | 111 ++++++++++++++++++++++++---------------- go.mod | 2 +- go.sum | 4 +- 4 files changed, 81 insertions(+), 58 deletions(-) diff --git a/formatter/sarif.go b/formatter/sarif.go index 75c7a646e..1a988b7bb 100644 --- a/formatter/sarif.go +++ b/formatter/sarif.go @@ -6,7 +6,7 @@ import ( "path/filepath" "github.com/hashicorp/hcl/v2" - "github.com/owenrumney/go-sarif/sarif" + "github.com/owenrumney/go-sarif/v2/sarif" sdk "github.com/terraform-linters/tflint-plugin-sdk/tflint" "github.com/terraform-linters/tflint/tflint" ) @@ -17,7 +17,7 @@ func (f *Formatter) sarifPrint(issues tflint.Issues, appErr error) { panic(initErr) } - run := sarif.NewRun("tflint", "https://github.com/terraform-linters/tflint") + run := sarif.NewRunWithInformationURI("tflint", "https://github.com/terraform-linters/tflint") version := tflint.Version.String() run.Tool.Driver.Version = &version @@ -25,7 +25,9 @@ func (f *Formatter) sarifPrint(issues tflint.Issues, appErr error) { report.AddRun(run) for _, issue := range issues { - rule := run.AddRule(issue.Rule.Name()).WithHelpURI(issue.Rule.Link()).WithDescription("") + rule := run.AddRule(issue.Rule.Name()). + WithHelpURI(issue.Rule.Link()). + WithDescription("") var level string switch issue.Rule.Severity() { @@ -55,16 +57,16 @@ func (f *Formatter) sarifPrint(issues tflint.Issues, appErr error) { } } - result := run.AddResult(rule.ID). + result := run.CreateResultForRule(rule.ID). WithLevel(level). WithMessage(sarif.NewTextMessage(issue.Message)) if location != nil { - result.WithLocation(sarif.NewLocationWithPhysicalLocation(location)) + result.AddLocation(sarif.NewLocationWithPhysicalLocation(location)) } } - errRun := sarif.NewRun("tflint-errors", "https://github.com/terraform-linters/tflint") + errRun := sarif.NewRunWithInformationURI("tflint-errors", "https://github.com/terraform-linters/tflint") errRun.Tool.Driver.Version = &version report.AddRun(errRun) @@ -105,15 +107,15 @@ func (f *Formatter) sarifAddErrors(errRun *sarif.Run, err error) { WithEndColumn(diag.Subject.End.Column), ) - errRun.AddResult(diag.Summary). + errRun.CreateResultForRule(diag.Summary). WithLevel(fromHclSeverity(diag.Severity)). - WithLocation(sarif.NewLocationWithPhysicalLocation(location)). - WithMessage(sarif.NewTextMessage(diag.Detail)) + WithMessage(sarif.NewTextMessage(diag.Detail)). + AddLocation(sarif.NewLocationWithPhysicalLocation(location)) } return } - errRun.AddResult("application_error"). + errRun.CreateResultForRule("application_error"). WithLevel("error"). WithMessage(sarif.NewTextMessage(err.Error())) } diff --git a/formatter/sarif_test.go b/formatter/sarif_test.go index 4291453f2..4b3d6102e 100644 --- a/formatter/sarif_test.go +++ b/formatter/sarif_test.go @@ -26,14 +26,15 @@ func Test_sarifPrint(t *testing.T) { Issues: tflint.Issues{}, Stdout: fmt.Sprintf(`{ "version": "2.1.0", - "$schema": "https://json.schemastore.org/sarif-2.1.0-rtm.5.json", + "$schema": "https://raw.githubusercontent.com/oasis-tcs/sarif-spec/main/sarif-2.1/schema/sarif-schema-2.1.0.json", "runs": [ { "tool": { "driver": { + "informationUri": "https://github.com/terraform-linters/tflint", "name": "tflint", - "version": "%s", - "informationUri": "https://github.com/terraform-linters/tflint" + "rules": [], + "version": "%s" } }, "results": [] @@ -41,9 +42,10 @@ func Test_sarifPrint(t *testing.T) { { "tool": { "driver": { + "informationUri": "https://github.com/terraform-linters/tflint", "name": "tflint-errors", - "version": "%s", - "informationUri": "https://github.com/terraform-linters/tflint" + "rules": [], + "version": "%s" } }, "results": [] @@ -66,14 +68,13 @@ func Test_sarifPrint(t *testing.T) { }, Stdout: fmt.Sprintf(`{ "version": "2.1.0", - "$schema": "https://json.schemastore.org/sarif-2.1.0-rtm.5.json", + "$schema": "https://raw.githubusercontent.com/oasis-tcs/sarif-spec/main/sarif-2.1/schema/sarif-schema-2.1.0.json", "runs": [ { "tool": { "driver": { - "name": "tflint", - "version": "%s", "informationUri": "https://github.com/terraform-linters/tflint", + "name": "tflint", "rules": [ { "id": "test_rule", @@ -82,12 +83,14 @@ func Test_sarifPrint(t *testing.T) { }, "helpUri": "https://github.com" } - ] + ], + "version": "%s" } }, "results": [ { "ruleId": "test_rule", + "ruleIndex": 0, "level": "error", "message": { "text": "test" @@ -113,9 +116,10 @@ func Test_sarifPrint(t *testing.T) { { "tool": { "driver": { + "informationUri": "https://github.com/terraform-linters/tflint", "name": "tflint-errors", - "version": "%s", - "informationUri": "https://github.com/terraform-linters/tflint" + "rules": [], + "version": "%s" } }, "results": [] @@ -138,14 +142,13 @@ func Test_sarifPrint(t *testing.T) { }, Stdout: fmt.Sprintf(`{ "version": "2.1.0", - "$schema": "https://json.schemastore.org/sarif-2.1.0-rtm.5.json", + "$schema": "https://raw.githubusercontent.com/oasis-tcs/sarif-spec/main/sarif-2.1/schema/sarif-schema-2.1.0.json", "runs": [ { "tool": { "driver": { - "name": "tflint", - "version": "%s", "informationUri": "https://github.com/terraform-linters/tflint", + "name": "tflint", "rules": [ { "id": "test_rule", @@ -154,12 +157,14 @@ func Test_sarifPrint(t *testing.T) { }, "helpUri": "https://github.com" } - ] + ], + "version": "%s" } }, "results": [ { "ruleId": "test_rule", + "ruleIndex": 0, "level": "error", "message": { "text": "test" @@ -185,9 +190,10 @@ func Test_sarifPrint(t *testing.T) { { "tool": { "driver": { + "informationUri": "https://github.com/terraform-linters/tflint", "name": "tflint-errors", - "version": "%s", - "informationUri": "https://github.com/terraform-linters/tflint" + "rules": [], + "version": "%s" } }, "results": [] @@ -210,14 +216,13 @@ func Test_sarifPrint(t *testing.T) { }, Stdout: fmt.Sprintf(`{ "version": "2.1.0", - "$schema": "https://json.schemastore.org/sarif-2.1.0-rtm.5.json", + "$schema": "https://raw.githubusercontent.com/oasis-tcs/sarif-spec/main/sarif-2.1/schema/sarif-schema-2.1.0.json", "runs": [ { "tool": { "driver": { - "name": "tflint", - "version": "%s", "informationUri": "https://github.com/terraform-linters/tflint", + "name": "tflint", "rules": [ { "id": "test_rule", @@ -226,12 +231,14 @@ func Test_sarifPrint(t *testing.T) { }, "helpUri": "https://github.com" } - ] + ], + "version": "%s" } }, "results": [ { "ruleId": "test_rule", + "ruleIndex": 0, "level": "error", "message": { "text": "test" @@ -257,9 +264,10 @@ func Test_sarifPrint(t *testing.T) { { "tool": { "driver": { + "informationUri": "https://github.com/terraform-linters/tflint", "name": "tflint-errors", - "version": "%s", - "informationUri": "https://github.com/terraform-linters/tflint" + "rules": [], + "version": "%s" } }, "results": [] @@ -282,14 +290,13 @@ func Test_sarifPrint(t *testing.T) { }, Stdout: fmt.Sprintf(`{ "version": "2.1.0", - "$schema": "https://json.schemastore.org/sarif-2.1.0-rtm.5.json", + "$schema": "https://raw.githubusercontent.com/oasis-tcs/sarif-spec/main/sarif-2.1/schema/sarif-schema-2.1.0.json", "runs": [ { "tool": { "driver": { - "name": "tflint", - "version": "%s", "informationUri": "https://github.com/terraform-linters/tflint", + "name": "tflint", "rules": [ { "id": "test_rule", @@ -298,12 +305,14 @@ func Test_sarifPrint(t *testing.T) { }, "helpUri": "https://github.com" } - ] + ], + "version": "%s" } }, "results": [ { "ruleId": "test_rule", + "ruleIndex": 0, "level": "error", "message": { "text": "test" @@ -329,9 +338,10 @@ func Test_sarifPrint(t *testing.T) { { "tool": { "driver": { + "informationUri": "https://github.com/terraform-linters/tflint", "name": "tflint-errors", - "version": "%s", - "informationUri": "https://github.com/terraform-linters/tflint" + "rules": [], + "version": "%s" } }, "results": [] @@ -353,14 +363,13 @@ func Test_sarifPrint(t *testing.T) { Error: fmt.Errorf("Failed to work; %w", errors.New("I don't feel like working")), Stdout: fmt.Sprintf(`{ "version": "2.1.0", - "$schema": "https://json.schemastore.org/sarif-2.1.0-rtm.5.json", + "$schema": "https://raw.githubusercontent.com/oasis-tcs/sarif-spec/main/sarif-2.1/schema/sarif-schema-2.1.0.json", "runs": [ { "tool": { "driver": { - "name": "tflint", - "version": "%s", "informationUri": "https://github.com/terraform-linters/tflint", + "name": "tflint", "rules": [ { "id": "test_rule", @@ -369,12 +378,14 @@ func Test_sarifPrint(t *testing.T) { }, "helpUri": "https://github.com" } - ] + ], + "version": "%s" } }, "results": [ { "ruleId": "test_rule", + "ruleIndex": 0, "level": "error", "message": { "text": "test" @@ -394,14 +405,16 @@ func Test_sarifPrint(t *testing.T) { { "tool": { "driver": { + "informationUri": "https://github.com/terraform-linters/tflint", "name": "tflint-errors", - "version": "%s", - "informationUri": "https://github.com/terraform-linters/tflint" + "rules": [], + "version": "%s" } }, "results": [ { "ruleId": "application_error", + "ruleIndex": 18446744073709551615, "level": "error", "message": { "text": "Failed to work; I don't feel like working" @@ -431,14 +444,15 @@ func Test_sarifPrint(t *testing.T) { ), Stdout: fmt.Sprintf(`{ "version": "2.1.0", - "$schema": "https://json.schemastore.org/sarif-2.1.0-rtm.5.json", + "$schema": "https://raw.githubusercontent.com/oasis-tcs/sarif-spec/main/sarif-2.1/schema/sarif-schema-2.1.0.json", "runs": [ { "tool": { "driver": { + "informationUri": "https://github.com/terraform-linters/tflint", "name": "tflint", - "version": "%s", - "informationUri": "https://github.com/terraform-linters/tflint" + "rules": [], + "version": "%s" } }, "results": [] @@ -446,14 +460,16 @@ func Test_sarifPrint(t *testing.T) { { "tool": { "driver": { + "informationUri": "https://github.com/terraform-linters/tflint", "name": "tflint-errors", - "version": "%s", - "informationUri": "https://github.com/terraform-linters/tflint" + "rules": [], + "version": "%s" } }, "results": [ { "ruleId": "summary", + "ruleIndex": 18446744073709551615, "level": "warning", "message": { "text": "detail" @@ -501,14 +517,15 @@ func Test_sarifPrint(t *testing.T) { ), Stdout: fmt.Sprintf(`{ "version": "2.1.0", - "$schema": "https://json.schemastore.org/sarif-2.1.0-rtm.5.json", + "$schema": "https://raw.githubusercontent.com/oasis-tcs/sarif-spec/main/sarif-2.1/schema/sarif-schema-2.1.0.json", "runs": [ { "tool": { "driver": { + "informationUri": "https://github.com/terraform-linters/tflint", "name": "tflint", - "version": "%s", - "informationUri": "https://github.com/terraform-linters/tflint" + "rules": [], + "version": "%s" } }, "results": [] @@ -516,14 +533,16 @@ func Test_sarifPrint(t *testing.T) { { "tool": { "driver": { + "informationUri": "https://github.com/terraform-linters/tflint", "name": "tflint-errors", - "version": "%s", - "informationUri": "https://github.com/terraform-linters/tflint" + "rules": [], + "version": "%s" } }, "results": [ { "ruleId": "application_error", + "ruleIndex": 18446744073709551615, "level": "error", "message": { "text": "an error occurred" @@ -531,6 +550,7 @@ func Test_sarifPrint(t *testing.T) { }, { "ruleId": "application_error", + "ruleIndex": 18446744073709551615, "level": "error", "message": { "text": "failed" @@ -538,6 +558,7 @@ func Test_sarifPrint(t *testing.T) { }, { "ruleId": "summary", + "ruleIndex": 18446744073709551615, "level": "warning", "message": { "text": "detail" diff --git a/go.mod b/go.mod index 486718c3a..b5b9a129d 100644 --- a/go.mod +++ b/go.mod @@ -20,7 +20,7 @@ require ( github.com/jstemmer/go-junit-report v1.0.0 github.com/mattn/go-colorable v0.1.13 github.com/mitchellh/go-homedir v1.1.0 - github.com/owenrumney/go-sarif v1.1.1 + github.com/owenrumney/go-sarif/v2 v2.3.3 github.com/sourcegraph/go-lsp v0.0.0-20200429204803-219e11d77f5d github.com/sourcegraph/jsonrpc2 v0.2.0 github.com/spf13/afero v1.11.0 diff --git a/go.sum b/go.sum index a0ec2a127..6ec626071 100644 --- a/go.sum +++ b/go.sum @@ -434,8 +434,8 @@ github.com/mitchellh/go-wordwrap v1.0.0 h1:6GlHJ/LTGMrIJbwgdqdl2eEH8o+Exx/0m8ir9 github.com/mitchellh/go-wordwrap v1.0.0/go.mod h1:ZXFpozHsX6DPmq2I0TCekCxypsnAUbP2oI0UX1GXzOo= github.com/oklog/run v1.0.0 h1:Ru7dDtJNOyC66gQ5dQmaCa0qIsAUFY3sFpK1Xk8igrw= github.com/oklog/run v1.0.0/go.mod h1:dlhp/R75TPv97u0XWUtDeV/lRKWPKSdTuV0TZvrmrQA= -github.com/owenrumney/go-sarif v1.1.1 h1:QNObu6YX1igyFKhdzd7vgzmw7XsWN3/6NMGuDzBgXmE= -github.com/owenrumney/go-sarif v1.1.1/go.mod h1:dNDiPlF04ESR/6fHlPyq7gHKmrM0sHUvAGjsoh8ZH0U= +github.com/owenrumney/go-sarif/v2 v2.3.3 h1:ubWDJcF5i3L/EIOER+ZyQ03IfplbSU1BLOE26uKQIIU= +github.com/owenrumney/go-sarif/v2 v2.3.3/go.mod h1:MSqMMx9WqlBSY7pXoOZWgEsVB4FDNfhcaXDA1j6Sr+w= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=