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

Feature/import sarif file #680

Closed
wants to merge 3 commits into from
Closed
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
59 changes: 59 additions & 0 deletions internal/commands/import.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package commands

import (
"github.com/MakeNowJust/heredoc"

Check failure on line 4 in internal/commands/import.go

View workflow job for this annotation

GitHub Actions / lint

import 'github.com/MakeNowJust/heredoc' is not allowed from list 'main' (depguard)
clierrors "github.com/checkmarx/ast-cli/internal/errors"
commonParams "github.com/checkmarx/ast-cli/internal/params"
"github.com/checkmarx/ast-cli/internal/wrappers"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)

func NewImportCommand(uploadsWrapper wrappers.UploadsWrapper) *cobra.Command {
cmd := &cobra.Command{
Use: "import",
Short: "Import scan results",
Long: "Import a SARIF file or a ZIP file containing SARIF file/s.",
Annotations: map[string]string{
"command:doc": heredoc.Doc(
`
https://checkmarx.com/resource/documents/en/34965-68625-checkmarx-one-cli-commands.html
`,
),
},
RunE: runImportCommand(uploadsWrapper),
}

cmd.PersistentFlags().String(commonParams.ImportFileType, "", "The type of the imported file (SARIF or ZIP containing SARIF files)")
cmd.PersistentFlags().String(commonParams.ImportFilePath, "", "The local path of the imported file")

return cmd
}

func runImportCommand(wrapper wrappers.UploadsWrapper) func(cmd *cobra.Command, args []string) error {
return func(cmd *cobra.Command, args []string) error {
importFileType, err := cmd.Flags().GetString(commonParams.ImportFileType)
if err != nil {
return err
}
importFilePath, err := cmd.Flags().GetString(commonParams.ImportFilePath)
if err != nil {
return err
}

if importFileType == "" || importFilePath == "" {
return errors.Errorf(clierrors.MissingImportFlags)
}
_, err = importFile(importFileType, importFilePath)
if err != nil {
return err
}

return nil
}
}

func importFile(fileType string, path string) (string, error) {

Check failure on line 56 in internal/commands/import.go

View workflow job for this annotation

GitHub Actions / lint

paramTypeCombine: func(fileType string, path string) (string, error) could be replaced with func(fileType, path string) (string, error) (gocritic)
// returns importId as string
return "", nil
}
24 changes: 24 additions & 0 deletions internal/commands/import_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//go:build !integration

package commands

import (
"testing"

cliErrors "github.com/checkmarx/ast-cli/internal/errors"
"gotest.tools/assert"
)

func TestImport_ImportSarifFileWithCorrectFlags_CreateImportSuccessfully(t *testing.T) {
execCmdNilAssertion(t, "import", "--import-file-type", "my-type", "--import-file-path", "my-path")
}

func TestImport_ImportSarifFileMissingImportFileType_CreateImportReturnsErrorWithCorrectMessage(t *testing.T) {
err := execCmdNotNilAssertion(t, "import", "--import-file-type", "", "--import-file-path", "my-path")
assert.Assert(t, err.Error() == cliErrors.MissingImportFlags)
}

func TestImport_ImportSarifFileMissingImportFilePath_CreateImportReturnsErrorWithCorrectMessage(t *testing.T) {
err := execCmdNotNilAssertion(t, "import", "--import-file-type", "my-type", "--import-file-path", "")
assert.Assert(t, err.Error() == cliErrors.MissingImportFlags)
}
4 changes: 4 additions & 0 deletions internal/commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,9 @@ func NewAstCLI(
accessManagementWrapper,
)
projectCmd := NewProjectCommand(applicationsWrapper, projectsWrapper, groupsWrapper, accessManagementWrapper)

importCmd := NewImportCommand(uploadsWrapper)

resultsCmd := NewResultsCommand(
resultsWrapper,
scansWrapper,
Expand Down Expand Up @@ -203,6 +206,7 @@ func NewAstCLI(
utilsCmd,
configCmd,
chatCmd,
importCmd,
)

rootCmd.SilenceErrors = true
Expand Down
9 changes: 0 additions & 9 deletions internal/errors/application-errors.go

This file was deleted.

7 changes: 7 additions & 0 deletions internal/errors/cli-errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package clierrors

const (
ApplicationDoesntExistOrNoPermission = "Provided application does not exist or user has no permission to the application"
MissingImportFlags = "importFileType and importFilePath are required"
FailedToGetApplication = "Failed to get application"
)

Check failure on line 7 in internal/errors/cli-errors.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofmt`-ed with `-s` (gofmt)
2 changes: 2 additions & 0 deletions internal/params/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ const (
IgnorePolicyFlag = "ignore-policy"
SourceDirFilterFlag = "file-filter"
SourceDirFilterFlagSh = "f"
ImportFileType = "import-file-type"
ImportFilePath = "import-file-path"
IncludeFilterFlag = "file-include"
IncludeFilterFlagSh = "i"
ProjectIDFlag = "project-id"
Expand Down
2 changes: 1 addition & 1 deletion test/integration/util_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ func executeCmdWithTimeOutNilAssertion(
func executeWithTimeout(cmd *cobra.Command, timeout time.Duration, args ...string) error {

args = append(args, flag(params.RetryFlag), "3", flag(params.RetryDelayFlag), "5")
args = appendProxyArgs(args)
//args = appendProxyArgs(args)
cmd.SetArgs(args)

ctx, cancel := context.WithTimeout(context.Background(), timeout)
Expand Down
Loading