diff --git a/internal/cmd/scan/scan.go b/internal/cmd/scan/scan.go index 6127c1ac..0a949f21 100644 --- a/internal/cmd/scan/scan.go +++ b/internal/cmd/scan/scan.go @@ -21,6 +21,7 @@ var callgraphGenerateTimeout int var callgraphUploadTimeout int var commitAuthor string var commitName string +var generateCommitNameBool bool var debug bool var exclusions = file.Exclusions() var inclusions = file.Exclusions() @@ -68,6 +69,7 @@ const ( TagCommitAsReleaseFlag = "tag-commit-as-release" TagCommitAsReleaseEnv = "TAG_COMMIT_AS_RELEASE" ExperimentalFlag = "experimental" + GenerateCommitName = "generate-commit-name" ) var scanCmdError error @@ -88,6 +90,7 @@ If the given path contains a git repository all flags but "integration" will be cmd.Flags().StringVarP(&repositoryName, RepositoryFlag, "r", "", "repository name") cmd.Flags().StringVarP(&commitName, CommitFlag, "c", "", "commit hash") + cmd.Flags().BoolVar(&generateCommitNameBool, GenerateCommitName, false, "auto-generate a commit name if flag is set") cmd.Flags().StringVarP(&branchName, BranchFlag, "b", "", "branch name") cmd.Flags().StringVarP(&commitAuthor, CommitAuthorFlag, "a", "", "commit author") cmd.Flags().StringVarP(&repositoryUrl, RepositoryUrlFlag, "u", "", "repository URL") @@ -239,6 +242,7 @@ func RunE(s *scan.IScanner) func(_ *cobra.Command, args []string) error { VersionHint: viper.GetBool(VersionHintFlag), RepositoryName: viper.GetString(RepositoryFlag), CommitName: viper.GetString(CommitFlag), + GenerateCommitName: viper.GetBool(GenerateCommitName), BranchName: viper.GetString(BranchFlag), CommitAuthor: viper.GetString(CommitAuthorFlag), RepositoryUrl: viper.GetString(RepositoryUrlFlag), diff --git a/internal/scan/scanner.go b/internal/scan/scanner.go index 5567ac19..af3919dc 100644 --- a/internal/scan/scanner.go +++ b/internal/scan/scanner.go @@ -6,6 +6,7 @@ import ( "fmt" "os" "path/filepath" + "time" "github.com/debricked/cli/internal/callgraph" "github.com/debricked/cli/internal/callgraph/config" @@ -60,6 +61,7 @@ type DebrickedOptions struct { VersionHint bool RepositoryName string CommitName string + GenerateCommitName bool BranchName string CommitAuthor string RepositoryUrl string @@ -103,6 +105,11 @@ func (dScanner *DebrickedScanner) Scan(o IOptions) error { e, _ := dScanner.ciService.Find() + if dOptions.GenerateCommitName { + debug.Log("Overwriting commit name with generated name", dOptions.Debug) + dOptions.CommitName = GenerateCommitNameTimestamp() + } + debug.Log("Mapping environment variables...", dOptions.Debug) MapEnvToOptions(&dOptions, e) @@ -332,6 +339,10 @@ func SetWorkingDirectory(d *DebrickedOptions) error { return nil } +func GenerateCommitNameTimestamp() string { + return fmt.Sprintf("generated-%d", time.Now().Unix()) +} + func MapEnvToOptions(o *DebrickedOptions, env env.Env) { if len(o.RepositoryName) == 0 { o.RepositoryName = env.Repository diff --git a/internal/scan/scanner_test.go b/internal/scan/scanner_test.go index cb63963c..e5a5288d 100644 --- a/internal/scan/scanner_test.go +++ b/internal/scan/scanner_test.go @@ -519,6 +519,11 @@ var cases = []struct { }, } +func TestGenerateCommitName(t *testing.T) { + res := GenerateCommitNameTimestamp() + assert.Contains(t, res, "generated-") +} + func TestMapEnvToOptions(t *testing.T) { for _, co := range cases { @@ -718,6 +723,39 @@ func TestScanWithFingerprintNoEnterprise(t *testing.T) { assert.Contains(t, cwd, path) } +func TestScanWithGeneratedCommitName(t *testing.T) { + clientMock := testdata.NewDebClientMock() + addMockedFormatsResponse(clientMock, "package\\.json") + addMockedFileUploadResponse(clientMock) + addMockedFinishResponse(clientMock, http.StatusNoContent) + addMockedStatusResponse(clientMock, http.StatusOK, 100) + + scanner := makeScanner(clientMock, nil, nil) + + cwd, _ := os.Getwd() + defer resetWd(t, cwd) + + path := testdataNpm + repositoryName := path + opts := DebrickedOptions{ + Path: path, + Resolve: false, + Fingerprint: false, + CallGraph: false, + Exclusions: nil, + Inclusions: nil, + RepositoryName: repositoryName, + GenerateCommitName: true, + BranchName: "", + RepositoryUrl: "", + IntegrationName: "", + } + err := scanner.Scan(opts) + assert.Nil(t, err) + cwd, _ = os.Getwd() + assert.Contains(t, cwd, path) +} + func TestScanWithCallgraph(t *testing.T) { if runtime.GOOS == windowsOS { t.Skipf("TestScan is skipped due to Windows env")