Skip to content

Commit

Permalink
Add --generate-commit-name flag
Browse files Browse the repository at this point in the history
  • Loading branch information
ProgHaj committed Nov 20, 2024
1 parent 560541c commit 542dc97
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
4 changes: 4 additions & 0 deletions internal/cmd/scan/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -68,6 +69,7 @@ const (
TagCommitAsReleaseFlag = "tag-commit-as-release"
TagCommitAsReleaseEnv = "TAG_COMMIT_AS_RELEASE"
ExperimentalFlag = "experimental"
GenerateCommitName = "generate-commit-name"
)

var scanCmdError error
Expand All @@ -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")
Expand Down Expand Up @@ -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),
Expand Down
11 changes: 11 additions & 0 deletions internal/scan/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"os"
"path/filepath"
"time"

"github.com/debricked/cli/internal/callgraph"
"github.com/debricked/cli/internal/callgraph/config"
Expand Down Expand Up @@ -60,6 +61,7 @@ type DebrickedOptions struct {
VersionHint bool
RepositoryName string
CommitName string
GenerateCommitName bool
BranchName string
CommitAuthor string
RepositoryUrl string
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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
Expand Down
38 changes: 38 additions & 0 deletions internal/scan/scanner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -718,6 +723,39 @@ func TestScanWithFingerprintNoEnterprise(t *testing.T) {
assert.Contains(t, cwd, path)
}

func TestScanWithGeneratedCommitName(t *testing.T) {
clientMock := testdata.NewDebClientMock()
addMockedFormatsResponse(clientMock, "yarn\\.lock")
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")
Expand Down

0 comments on commit 542dc97

Please sign in to comment.