Skip to content
Open
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: 1 addition & 1 deletion .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ builds:
ldflags:
- "-s"
- "-w"
- "-X github.com/open-policy-agent/conftest/internal/commands.version={{.Version}}"
- "-X github.com/open-policy-agent/conftest/internal/version.Version={{.Version}}"

archives:
- name_template: >-
Expand Down
8 changes: 2 additions & 6 deletions internal/commands/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"os"
"strings"

"github.com/open-policy-agent/conftest/internal/version"
"github.com/open-policy-agent/conftest/plugin"

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

// These values are set at build time
var (
version = ""
)

// NewDefaultCommand creates the default command
func NewDefaultCommand() *cobra.Command {
cmd := cobra.Command{
Expand Down Expand Up @@ -97,7 +93,7 @@ func newCommandFromPlugin(ctx context.Context, p *plugin.Plugin) *cobra.Command
}

func createVersionString() string {
return fmt.Sprintf("Conftest: %s\nOPA: %s\n", version, opaversion.Version)
return fmt.Sprintf("Conftest: %s\nOPA: %s\n", version.Version, opaversion.Version)
}

func readInConfig() error {
Expand Down
4 changes: 4 additions & 0 deletions internal/version/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package version

// Version is the version of conftest. It is overridden by ldflags during releases.
var Version = "dev"
20 changes: 9 additions & 11 deletions output/sarif.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"path/filepath"
"strings"

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

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

for _, result := range results {
Expand Down Expand Up @@ -164,23 +168,17 @@ func (s *SARIF) Output(results CheckResults) error {
}
}

// Add run metadata
exitCode := 0
exitDesc := exitNoViolations
if results.HasFailure() {
exitCode = 1
exitDesc = exitViolations
} else if results.HasWarning() {
exitDesc = exitWarnings
}

successful := true
invocation := sarif.NewInvocation()
invocation.ExecutionSuccessful = &successful
invocation.ExitCode = &exitCode
invocation.ExitCodeDescription = &exitDesc

run.Invocations = []*sarif.Invocation{invocation}
run.AddInvocations(sarif.NewInvocation().
WithExecutionSuccess(true).
WithExitCode(results.ExitCode()).
WithExitCodeDescription(exitDesc))

// Add the run to the report
report.AddRun(run)
Expand Down
36 changes: 22 additions & 14 deletions output/sarif_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"testing"

"github.com/google/go-cmp/cmp"
"github.com/open-policy-agent/conftest/internal/version"
)

func TestSARIF_Output(t *testing.T) {
Expand All @@ -25,8 +26,9 @@ func TestSARIF_Output(t *testing.T) {
{
"tool": map[string]any{
"driver": map[string]any{
"informationUri": "https://github.com/open-policy-agent/conftest",
"name": "conftest",
"informationUri": toolURI,
"name": toolName,
"version": version.Version,
"rules": []any{},
},
},
Expand Down Expand Up @@ -66,8 +68,9 @@ func TestSARIF_Output(t *testing.T) {
{
"tool": map[string]any{
"driver": map[string]any{
"informationUri": "https://github.com/open-policy-agent/conftest",
"name": "conftest",
"informationUri": toolURI,
"name": toolName,
"version": version.Version,
"rules": []map[string]any{
{
"id": "main/deny",
Expand Down Expand Up @@ -135,8 +138,9 @@ func TestSARIF_Output(t *testing.T) {
{
"tool": map[string]any{
"driver": map[string]any{
"informationUri": "https://github.com/open-policy-agent/conftest",
"name": "conftest",
"informationUri": toolURI,
"name": toolName,
"version": version.Version,
"rules": []map[string]any{
{
"id": "main/warn",
Expand Down Expand Up @@ -203,8 +207,9 @@ func TestSARIF_Output(t *testing.T) {
{
"tool": map[string]any{
"driver": map[string]any{
"informationUri": "https://github.com/open-policy-agent/conftest",
"name": "conftest",
"informationUri": toolURI,
"name": toolName,
"version": version.Version,
"rules": []map[string]any{
{
"id": "main/allow",
Expand Down Expand Up @@ -290,8 +295,9 @@ func TestSARIF_Output(t *testing.T) {
{
"tool": map[string]any{
"driver": map[string]any{
"informationUri": "https://github.com/open-policy-agent/conftest",
"name": "conftest",
"informationUri": toolURI,
"name": toolName,
"version": version.Version,
"rules": []map[string]any{
{
"id": "main/skip",
Expand Down Expand Up @@ -366,8 +372,9 @@ func TestSARIF_Output(t *testing.T) {
{
"tool": map[string]any{
"driver": map[string]any{
"informationUri": "https://github.com/open-policy-agent/conftest",
"name": "conftest",
"informationUri": toolURI,
"name": toolName,
"version": version.Version,
"rules": []map[string]any{
{
"id": "main/deny",
Expand Down Expand Up @@ -445,8 +452,9 @@ func TestSARIF_Output(t *testing.T) {
{
"tool": map[string]any{
"driver": map[string]any{
"informationUri": "https://github.com/open-policy-agent/conftest",
"name": "conftest",
"informationUri": toolURI,
"name": toolName,
"version": version.Version,
"rules": []map[string]any{
{
"id": "main/success",
Expand Down
Loading