Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(code): return exit code 3 for unsupported projects [CLI-309] #5264

Merged
merged 4 commits into from
May 29, 2024
Merged
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
12 changes: 10 additions & 2 deletions cliv2/cmd/cliv2/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,15 @@ func getErrorFromWorkFlowData(engine workflow.Engine, data []workflow.Data) erro
}
}

dataErrors := data[i].GetErrorList()
for _, dataError := range dataErrors {
if dataError.ErrorCode == "SNYK-CODE-0006" {
return errors.Join(dataError, &cli_errors.ErrorWithExitCode{
ExitCode: constants.SNYK_EXIT_CODE_UNSUPPORTED_PROJECTS,
})
}
}

return nil
}
}
Expand Down Expand Up @@ -395,9 +404,8 @@ func handleError(err error) HandleError {

func displayError(err error, userInterface ui.UserInterface, config configuration.Configuration) {
if err != nil {
var exitCode *cli_errors.ErrorWithExitCode
_, isExitError := err.(*exec.ExitError)
isErrorWithCode := errors.As(err, &exitCode)
_, isErrorWithCode := err.(*cli_errors.ErrorWithExitCode)
if isExitError || isErrorWithCode {
return
}
Expand Down
43 changes: 42 additions & 1 deletion cliv2/cmd/cliv2/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (

"github.com/golang/mock/gomock"
"github.com/rs/zerolog"
"github.com/snyk/error-catalog-golang-public/code"
"github.com/snyk/error-catalog-golang-public/snyk_errors"
"github.com/snyk/go-application-framework/pkg/configuration"
localworkflows "github.com/snyk/go-application-framework/pkg/local_workflows"
"github.com/snyk/go-application-framework/pkg/local_workflows/content_type"
Expand Down Expand Up @@ -263,7 +265,7 @@ func Test_getErrorFromWorkFlowData(t *testing.T) {
assert.Equal(t, constants.SNYK_EXIT_CODE_VULNERABILITIES_FOUND, expectedError.ExitCode)
})

t.Run("workflow with empty testing findings", func(t *testing.T) {
t.Run("workflow with zero count test summary", func(t *testing.T) {
workflowId := workflow.NewWorkflowIdentifier("output")
workflowIdentifier := workflow.NewTypeIdentifier(workflowId, "output")
d, err := json.Marshal(json_schemas.TestSummary{
Expand All @@ -280,6 +282,45 @@ func Test_getErrorFromWorkFlowData(t *testing.T) {
err = getErrorFromWorkFlowData(engine, []workflow.Data{data})
assert.Nil(t, err)
})

t.Run("workflow with empty test summary and unsupported error annotation", func(t *testing.T) {
workflowId := workflow.NewWorkflowIdentifier("output")
workflowIdentifier := workflow.NewTypeIdentifier(workflowId, "output")
d, err := json.Marshal(json_schemas.NewTestSummary("sast"))
assert.Nil(t, err)
data := workflow.NewData(workflowIdentifier, content_type.TEST_SUMMARY, d)
expectedCodeErr := code.NewUnsupportedProjectError("")
data.AddError(expectedCodeErr)
err = getErrorFromWorkFlowData(engine, []workflow.Data{data})

var actualError *clierrors.ErrorWithExitCode
var actualSnykCatalogError snyk_errors.Error
assert.ErrorAs(t, err, &actualError)
assert.ErrorAs(t, err, &actualSnykCatalogError)

assert.Equal(t, expectedCodeErr, actualSnykCatalogError)
assert.Equal(t, constants.SNYK_EXIT_CODE_UNSUPPORTED_PROJECTS, actualError.ExitCode)
})

t.Run("workflow with empty testing and misc error annotation", func(t *testing.T) {
workflowId := workflow.NewWorkflowIdentifier("output")
workflowIdentifier := workflow.NewTypeIdentifier(workflowId, "output")
d, err := json.Marshal(json_schemas.TestSummary{
Results: []json_schemas.TestSummaryResult{{
Severity: "critical",
Total: 0,
Open: 0,
Ignored: 0,
}},
Artifacts: 0,
Type: "sast",
})
assert.Nil(t, err)
data := workflow.NewData(workflowIdentifier, content_type.TEST_SUMMARY, d)
data.AddError(code.NewAnalysisFileCountLimitExceededError(""))
err = getErrorFromWorkFlowData(engine, []workflow.Data{data})
assert.NoError(t, err)
})
}

func Test_runWorkflowAndProcessData(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions cliv2/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ require (
github.com/snyk/cli-extension-iac-rules v0.0.0-20240422133948-ae17a4306672
github.com/snyk/cli-extension-sbom v0.0.0-20240523084359-a2830fadb001
github.com/snyk/container-cli v0.0.0-20240322120441-6d9b9482f9b1
github.com/snyk/go-application-framework v0.0.0-20240528170035-ab13975bf74d
github.com/snyk/error-catalog-golang-public v0.0.0-20240527112826-2b77438d25f1
github.com/snyk/go-application-framework v0.0.0-20240529115017-973f3ab671e4
github.com/snyk/go-httpauth v0.0.0-20240307114523-1f5ea3f55c65
github.com/snyk/snyk-iac-capture v0.6.5
github.com/snyk/snyk-ls v0.0.0-20240524141328-ca2e2955918a
Expand Down Expand Up @@ -150,7 +151,6 @@ require (
github.com/shirou/gopsutil v3.21.11+incompatible // indirect
github.com/skeema/knownhosts v1.2.2 // indirect
github.com/snyk/code-client-go v1.6.1 // indirect
github.com/snyk/error-catalog-golang-public v0.0.0-20240425141803-2516e42296c3 // indirect
github.com/snyk/policy-engine v0.30.11 // indirect
github.com/sourcegraph/conc v0.3.0 // indirect
github.com/sourcegraph/go-lsp v0.0.0-20240223163137-f80c5dd31dfd // indirect
Expand Down
8 changes: 4 additions & 4 deletions cliv2/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -729,10 +729,10 @@ github.com/snyk/code-client-go v1.6.1 h1:wCCcsTLsk5ZAUwwAHiU35/hmmYzJ5lgFbcJ1fOk
github.com/snyk/code-client-go v1.6.1/go.mod h1:Kkr7pQc8ItsBZSYd6A1S4r4VHO6HNyTWZsqi18sAtwQ=
github.com/snyk/container-cli v0.0.0-20240322120441-6d9b9482f9b1 h1:9RKY9NdX5DrJAoVXDP0JiqrXT+4Nb9NH8pjEcA0NsLA=
github.com/snyk/container-cli v0.0.0-20240322120441-6d9b9482f9b1/go.mod h1:38w+dcAQp9eG3P5t2eNS9eG0reut10AeJjLv5lJ5lpM=
github.com/snyk/error-catalog-golang-public v0.0.0-20240425141803-2516e42296c3 h1:ZUaY5LIVGQ0GScf1SsaqvUxaiGbBKgBBLsQUgB4Zx5o=
github.com/snyk/error-catalog-golang-public v0.0.0-20240425141803-2516e42296c3/go.mod h1:Ytttq7Pw4vOCu9NtRQaOeDU2dhBYUyNBe6kX4+nIIQ4=
github.com/snyk/go-application-framework v0.0.0-20240528170035-ab13975bf74d h1:OChBTTRBxQe455GQ0Sbyh6KybHCkk6bQltMCdURUeYc=
github.com/snyk/go-application-framework v0.0.0-20240528170035-ab13975bf74d/go.mod h1:ZMl89wenxt+xaRtHvI2uvWwVJjFPv3b5qzT3w+batgQ=
github.com/snyk/error-catalog-golang-public v0.0.0-20240527112826-2b77438d25f1 h1:49X/bTeiWdi+DrkTbTSw5BePpQ6LiucIt++/Z+MB95U=
github.com/snyk/error-catalog-golang-public v0.0.0-20240527112826-2b77438d25f1/go.mod h1:Ytttq7Pw4vOCu9NtRQaOeDU2dhBYUyNBe6kX4+nIIQ4=
github.com/snyk/go-application-framework v0.0.0-20240529115017-973f3ab671e4 h1:5xMDDbXOLhF8ga48Xs388Wk3LhldGW6Hl1VXimw1It8=
github.com/snyk/go-application-framework v0.0.0-20240529115017-973f3ab671e4/go.mod h1:1T6hnPUgT0lJPO0g3VjwvW0fCKDcgst8bgXs+wCfmW8=
github.com/snyk/go-httpauth v0.0.0-20240307114523-1f5ea3f55c65 h1:CEQuYv0Go6MEyRCD3YjLYM2u3Oxkx8GpCpFBd4rUTUk=
github.com/snyk/go-httpauth v0.0.0-20240307114523-1f5ea3f55c65/go.mod h1:88KbbvGYlmLgee4OcQ19yr0bNpXpOr2kciOthaSzCAg=
github.com/snyk/policy-engine v0.30.11 h1:wUy5LMar2vccMbNM62MSBRdjAQAhAbIm7aNXXO+g2tk=
Expand Down
1 change: 1 addition & 0 deletions cliv2/internal/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package constants
const SNYK_EXIT_CODE_OK = 0
const SNYK_EXIT_CODE_VULNERABILITIES_FOUND = 1
const SNYK_EXIT_CODE_ERROR = 2
const SNYK_EXIT_CODE_UNSUPPORTED_PROJECTS = 3
const SNYK_EXIT_CODE_EX_UNAVAILABLE = 69
const SNYK_INTEGRATION_NAME = "CLI_V1_PLUGIN"
const SNYK_INTEGRATION_NAME_ENV = "SNYK_INTEGRATION_NAME"
Expand Down
Loading