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

Add --generate-commit-name flag #282

Merged
merged 4 commits into from
Nov 22, 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
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 generateCommitName 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"
GenerateCommitNameFlag = "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(&generateCommitName, GenerateCommitNameFlag, false, "auto-generate a commit name if no commit hash is found (in -c or env)")
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(GenerateCommitNameFlag),
BranchName: viper.GetString(BranchFlag),
CommitAuthor: viper.GetString(CommitAuthorFlag),
RepositoryUrl: viper.GetString(RepositoryUrlFlag),
Expand Down
14 changes: 14 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 @@ -105,6 +107,7 @@ func (dScanner *DebrickedScanner) Scan(o IOptions) error {

debug.Log("Mapping environment variables...", dOptions.Debug)
MapEnvToOptions(&dOptions, e)
UpdatedEmptyCommitName(&dOptions)

if err := SetWorkingDirectory(&dOptions); err != nil {
return err
Expand Down Expand Up @@ -332,6 +335,17 @@ func SetWorkingDirectory(d *DebrickedOptions) error {
return nil
}

func UpdatedEmptyCommitName(o *DebrickedOptions) {
if o.GenerateCommitName && o.CommitName == "" {
debug.Log("No commit name set, generating commit name", o.Debug)
o.CommitName = GenerateCommitNameTimestamp()
}
}

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
67 changes: 67 additions & 0 deletions internal/scan/scanner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,40 @@ var cases = []struct {
},
}

func TestUpdateEmptyCommitName(t *testing.T) {
opts := DebrickedOptions{
CommitName: "",
GenerateCommitName: true,
}

UpdatedEmptyCommitName(&opts)
assert.Contains(t, opts.CommitName, "generated-")
}

func TestUpdateEmptyCommitNameNotEmpty(t *testing.T) {
opts := DebrickedOptions{
CommitName: "test",
GenerateCommitName: true,
}

UpdatedEmptyCommitName(&opts)
assert.Equal(t, opts.CommitName, "test")
}

func TestUpdateEmptyCommitNameNoGenerateCommitNameSet(t *testing.T) {
opts := DebrickedOptions{
CommitName: "",
}

UpdatedEmptyCommitName(&opts)
assert.Empty(t, opts.CommitName)
}

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 +752,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")
Expand Down
Loading