From 28e20fd197f3914de5bcf0da2a0e40d61dddab2d Mon Sep 17 00:00:00 2001 From: David Roe Date: Wed, 22 Nov 2023 11:00:04 +0000 Subject: [PATCH] fix: more fixes from testing --- internal/commands/artifact/run.go | 3 +- .../commands/process/gitrepository/context.go | 29 +++++++++---------- internal/flag/repository_flags.go | 24 +++++++-------- internal/report/output/output.go | 2 +- internal/report/output/saas/saas.go | 14 ++++----- 5 files changed, 36 insertions(+), 36 deletions(-) diff --git a/internal/commands/artifact/run.go b/internal/commands/artifact/run.go index 357243b59..acd3cbf15 100644 --- a/internal/commands/artifact/run.go +++ b/internal/commands/artifact/run.go @@ -101,7 +101,8 @@ func NewRunner( log.Debug().Msgf("creating report %s", path) if _, err := os.Stat(completedPath); err == nil { - if !scanSettings.Scan.Force { + // diff can't use the cache because the base branch scan data is not in the report + if !scanSettings.Scan.Force && !scanSettings.Scan.Diff { // force is not set, and we are not running a diff scan r.reuseDetection = true log.Debug().Msgf("reuse detection for %s", path) diff --git a/internal/commands/process/gitrepository/context.go b/internal/commands/process/gitrepository/context.go index 8e3ad309d..30731ca1c 100644 --- a/internal/commands/process/gitrepository/context.go +++ b/internal/commands/process/gitrepository/context.go @@ -19,9 +19,11 @@ import ( type Context struct { RootDir string + Branch, CurrentBranch, DefaultBranch, BaseBranch string + CommitHash, CurrentCommitHash, BaseCommitHash string OriginURL string @@ -43,7 +45,7 @@ func NewContext(options *flag.Options) (*Context, error) { return nil, err } - currentBranch, err := getCurrentBranch(options, rootDir) + currentBranch, err := git.GetCurrentBranch(rootDir) if err != nil { return nil, fmt.Errorf("error getting current branch name: %w", err) } @@ -58,7 +60,7 @@ func NewContext(options *flag.Options) (*Context, error) { return nil, fmt.Errorf("error getting base branch name: %w", err) } - currentCommitHash, err := getCurrentCommitHash(options, rootDir) + currentCommitHash, err := git.GetCurrentCommit(rootDir) if err != nil { return nil, fmt.Errorf("error getting current commit hash: %w", err) } @@ -94,9 +96,11 @@ func NewContext(options *flag.Options) (*Context, error) { context := &Context{ RootDir: rootDir, + Branch: getBranch(options, currentBranch), CurrentBranch: currentBranch, DefaultBranch: defaultBranch, BaseBranch: baseBranch, + CommitHash: getCommitHash(options, currentCommitHash), CurrentCommitHash: currentCommitHash, BaseCommitHash: baseCommitHash, OriginURL: originURL, @@ -114,17 +118,12 @@ func NewContext(options *flag.Options) (*Context, error) { return context, nil } -func getCurrentBranch(options *flag.Options, rootDir string) (string, error) { - if options.CurrentBranch != "" { - return options.CurrentBranch, nil +func getBranch(options *flag.Options, currentBranch string) string { + if options.Branch != "" { + return options.Branch } - name, err := git.GetCurrentBranch(rootDir) - if err != nil { - return "", err - } - - return name, err + return currentBranch } func getDefaultBranch(options *flag.Options, rootDir string) (string, error) { @@ -155,12 +154,12 @@ func getBaseBranch(options *flag.Options, defaultBranch string) (string, error) ) } -func getCurrentCommitHash(options *flag.Options, rootDir string) (string, error) { - if options.CurrentCommit != "" { - return options.CurrentCommit, nil +func getCommitHash(options *flag.Options, currentCommitHash string) string { + if options.Commit != "" { + return options.Commit } - return git.GetCurrentCommit(rootDir) + return currentCommitHash } func getBaseCommitHash( diff --git a/internal/flag/repository_flags.go b/internal/flag/repository_flags.go index a1121a564..ae483efd5 100644 --- a/internal/flag/repository_flags.go +++ b/internal/flag/repository_flags.go @@ -17,11 +17,11 @@ var ( DisableInConfig: true, Hide: true, }) - CurrentBranchFlag = RepositoryFlagGroup.add(Flag{ - Name: "current-branch", - ConfigName: "repository.current-branch", + BranchFlag = RepositoryFlagGroup.add(Flag{ + Name: "branch", + ConfigName: "repository.branch", Value: "", - Usage: "The name of the current branch.", + Usage: "The name of the branch being scanned.", EnvironmentVariables: []string{ "CURRENT_BRANCH", // legacy "CI_COMMIT_REF_NAME", // gitlab @@ -29,11 +29,11 @@ var ( DisableInConfig: true, Hide: true, }) - CurrentCommitFlag = RepositoryFlagGroup.add(Flag{ - Name: "current-commit", - ConfigName: "repository.current-commit", + CommitFlag = RepositoryFlagGroup.add(Flag{ + Name: "commit", + ConfigName: "repository.commit", Value: "", - Usage: "The hash of the current commit.", + Usage: "The hash of the commit being scanned.", EnvironmentVariables: []string{ "SHA", // legacy "CI_COMMIT_SHA", // gitlab @@ -114,8 +114,8 @@ var ( type RepositoryOptions struct { OriginURL string - CurrentBranch string - CurrentCommit string + Branch string + Commit string DefaultBranch string DiffBaseBranch string DiffBaseCommit string @@ -127,8 +127,8 @@ type RepositoryOptions struct { func (repositoryFlagGroup) SetOptions(options *Options, args []string) error { options.RepositoryOptions = RepositoryOptions{ OriginURL: getString(RepositoryURLFlag), - CurrentBranch: getString(CurrentBranchFlag), - CurrentCommit: getString(CurrentCommitFlag), + Branch: getString(BranchFlag), + Commit: getString(CommitFlag), DefaultBranch: getString(DefaultBranchFlag), DiffBaseBranch: getString(DiffBaseBranchFlag), DiffBaseCommit: getString(DiffBaseCommitFlag), diff --git a/internal/report/output/output.go b/internal/report/output/output.go index f1b450d84..bf9da13ca 100644 --- a/internal/report/output/output.go +++ b/internal/report/output/output.go @@ -63,7 +63,7 @@ func GetData( if err = security.AddReportData(data, config, baseBranchFindings, report.HasFiles); err != nil { return nil, err } - err = saas.GetReport(data, config, gitContext, true) + err = saas.GetReport(data, config, gitContext, false) case flag.ReportPrivacy: err = privacy.AddReportData(data, config) case flag.ReportStats: diff --git a/internal/report/output/saas/saas.go b/internal/report/output/saas/saas.go index f48fb3bf2..1f94777db 100644 --- a/internal/report/output/saas/saas.go +++ b/internal/report/output/saas/saas.go @@ -168,10 +168,10 @@ func getMeta( } var messages []string - if gitContext.CurrentBranch == "" { + if gitContext.Branch == "" { messages = append(messages, - "Couldn't determine the current branch of the repository. "+ - "Please set the 'BEARER_CURRENT_BRANCH' environment variable.", + "Couldn't determine the name of the branch being scanned. "+ + "Please set the 'BEARER_BRANCH' environment variable.", ) } if gitContext.DefaultBranch == "" { @@ -180,10 +180,10 @@ func getMeta( "Please set the 'BEARER_DEFAULT_BRANCH' environment variable.", ) } - if gitContext.CurrentCommitHash == "" { + if gitContext.CommitHash == "" { messages = append(messages, "Couldn't determine the hash of the current commit of the repository. "+ - "Please set the 'BEARER_CURRENT_COMMIT' environment variable.", + "Please set the 'BEARER_COMMIT' environment variable.", ) } if gitContext.OriginURL == "" { @@ -205,8 +205,8 @@ func getMeta( FullName: gitContext.FullName, URL: gitContext.OriginURL, Target: config.Scan.Target, - SHA: gitContext.CurrentCommitHash, - CurrentBranch: gitContext.CurrentBranch, + SHA: gitContext.CommitHash, + CurrentBranch: gitContext.Branch, DefaultBranch: gitContext.DefaultBranch, DiffBaseBranch: gitContext.BaseBranch, BearerRulesVersion: config.BearerRulesVersion,