Skip to content

Commit ff4e456

Browse files
authored
fix(sarif): Include conftest version number (#1206)
Previously, the SARIF validator [0] raised an error due to the missing version. [0] https://sarifweb.azurewebsites.net/Validation Signed-off-by: James Alseth <james@jalseth.me>
1 parent ac3146f commit ff4e456

File tree

5 files changed

+38
-32
lines changed

5 files changed

+38
-32
lines changed

.goreleaser.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ builds:
2020
ldflags:
2121
- "-s"
2222
- "-w"
23-
- "-X github.com/open-policy-agent/conftest/internal/commands.version={{.Version}}"
23+
- "-X github.com/open-policy-agent/conftest/internal/version.Version={{.Version}}"
2424

2525
archives:
2626
- name_template: >-

internal/commands/default.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"os"
99
"strings"
1010

11+
"github.com/open-policy-agent/conftest/internal/version"
1112
"github.com/open-policy-agent/conftest/plugin"
1213

1314
"github.com/spf13/cobra"
@@ -19,11 +20,6 @@ import (
1920
_ "github.com/open-policy-agent/conftest/builtins"
2021
)
2122

22-
// These values are set at build time
23-
var (
24-
version = ""
25-
)
26-
2723
// NewDefaultCommand creates the default command
2824
func NewDefaultCommand() *cobra.Command {
2925
cmd := cobra.Command{
@@ -97,7 +93,7 @@ func newCommandFromPlugin(ctx context.Context, p *plugin.Plugin) *cobra.Command
9793
}
9894

9995
func createVersionString() string {
100-
return fmt.Sprintf("Conftest: %s\nOPA: %s\n", version, opaversion.Version)
96+
return fmt.Sprintf("Conftest: %s\nOPA: %s\n", version.Version, opaversion.Version)
10197
}
10298

10399
func readInConfig() error {

internal/version/version.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package version
2+
3+
// Version is the version of conftest. It is overridden by ldflags during releases.
4+
var Version = "dev"

output/sarif.go

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"path/filepath"
77
"strings"
88

9+
"github.com/open-policy-agent/conftest/internal/version"
910
"github.com/open-policy-agent/opa/v1/tester"
1011
"github.com/owenrumney/go-sarif/v2/sarif"
1112
)
@@ -117,7 +118,10 @@ func (s *SARIF) Output(results CheckResults) error {
117118
return fmt.Errorf("create sarif report: %w", err)
118119
}
119120

120-
run := sarif.NewRunWithInformationURI(toolName, toolURI)
121+
// SARIF versions must start with a number, so we remove the "v" prefix.
122+
toolVersion := strings.TrimPrefix(version.Version, "v")
123+
driver := sarif.NewVersionedDriver(toolName, toolVersion).WithInformationURI(toolURI)
124+
run := sarif.NewRun(sarif.Tool{Driver: driver})
121125
indices := make(map[string]int)
122126

123127
for _, result := range results {
@@ -164,23 +168,17 @@ func (s *SARIF) Output(results CheckResults) error {
164168
}
165169
}
166170

167-
// Add run metadata
168-
exitCode := 0
169171
exitDesc := exitNoViolations
170172
if results.HasFailure() {
171-
exitCode = 1
172173
exitDesc = exitViolations
173174
} else if results.HasWarning() {
174175
exitDesc = exitWarnings
175176
}
176177

177-
successful := true
178-
invocation := sarif.NewInvocation()
179-
invocation.ExecutionSuccessful = &successful
180-
invocation.ExitCode = &exitCode
181-
invocation.ExitCodeDescription = &exitDesc
182-
183-
run.Invocations = []*sarif.Invocation{invocation}
178+
run.AddInvocations(sarif.NewInvocation().
179+
WithExecutionSuccess(true).
180+
WithExitCode(results.ExitCode()).
181+
WithExitCodeDescription(exitDesc))
184182

185183
// Add the run to the report
186184
report.AddRun(run)

output/sarif_test.go

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"testing"
77

88
"github.com/google/go-cmp/cmp"
9+
"github.com/open-policy-agent/conftest/internal/version"
910
)
1011

1112
func TestSARIF_Output(t *testing.T) {
@@ -25,8 +26,9 @@ func TestSARIF_Output(t *testing.T) {
2526
{
2627
"tool": map[string]any{
2728
"driver": map[string]any{
28-
"informationUri": "https://github.com/open-policy-agent/conftest",
29-
"name": "conftest",
29+
"informationUri": toolURI,
30+
"name": toolName,
31+
"version": version.Version,
3032
"rules": []any{},
3133
},
3234
},
@@ -66,8 +68,9 @@ func TestSARIF_Output(t *testing.T) {
6668
{
6769
"tool": map[string]any{
6870
"driver": map[string]any{
69-
"informationUri": "https://github.com/open-policy-agent/conftest",
70-
"name": "conftest",
71+
"informationUri": toolURI,
72+
"name": toolName,
73+
"version": version.Version,
7174
"rules": []map[string]any{
7275
{
7376
"id": "main/deny",
@@ -135,8 +138,9 @@ func TestSARIF_Output(t *testing.T) {
135138
{
136139
"tool": map[string]any{
137140
"driver": map[string]any{
138-
"informationUri": "https://github.com/open-policy-agent/conftest",
139-
"name": "conftest",
141+
"informationUri": toolURI,
142+
"name": toolName,
143+
"version": version.Version,
140144
"rules": []map[string]any{
141145
{
142146
"id": "main/warn",
@@ -203,8 +207,9 @@ func TestSARIF_Output(t *testing.T) {
203207
{
204208
"tool": map[string]any{
205209
"driver": map[string]any{
206-
"informationUri": "https://github.com/open-policy-agent/conftest",
207-
"name": "conftest",
210+
"informationUri": toolURI,
211+
"name": toolName,
212+
"version": version.Version,
208213
"rules": []map[string]any{
209214
{
210215
"id": "main/allow",
@@ -290,8 +295,9 @@ func TestSARIF_Output(t *testing.T) {
290295
{
291296
"tool": map[string]any{
292297
"driver": map[string]any{
293-
"informationUri": "https://github.com/open-policy-agent/conftest",
294-
"name": "conftest",
298+
"informationUri": toolURI,
299+
"name": toolName,
300+
"version": version.Version,
295301
"rules": []map[string]any{
296302
{
297303
"id": "main/skip",
@@ -366,8 +372,9 @@ func TestSARIF_Output(t *testing.T) {
366372
{
367373
"tool": map[string]any{
368374
"driver": map[string]any{
369-
"informationUri": "https://github.com/open-policy-agent/conftest",
370-
"name": "conftest",
375+
"informationUri": toolURI,
376+
"name": toolName,
377+
"version": version.Version,
371378
"rules": []map[string]any{
372379
{
373380
"id": "main/deny",
@@ -445,8 +452,9 @@ func TestSARIF_Output(t *testing.T) {
445452
{
446453
"tool": map[string]any{
447454
"driver": map[string]any{
448-
"informationUri": "https://github.com/open-policy-agent/conftest",
449-
"name": "conftest",
455+
"informationUri": toolURI,
456+
"name": toolName,
457+
"version": version.Version,
450458
"rules": []map[string]any{
451459
{
452460
"id": "main/success",

0 commit comments

Comments
 (0)