Skip to content

Commit

Permalink
fix: more fixes from testing
Browse files Browse the repository at this point in the history
  • Loading branch information
didroe committed Nov 22, 2023
1 parent 178da62 commit 28e20fd
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 36 deletions.
3 changes: 2 additions & 1 deletion internal/commands/artifact/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
29 changes: 14 additions & 15 deletions internal/commands/process/gitrepository/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ import (

type Context struct {
RootDir string
Branch,
CurrentBranch,
DefaultBranch,
BaseBranch string
CommitHash,
CurrentCommitHash,
BaseCommitHash string
OriginURL string
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand Down Expand Up @@ -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,
Expand All @@ -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) {
Expand Down Expand Up @@ -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(
Expand Down
24 changes: 12 additions & 12 deletions internal/flag/repository_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,23 @@ 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
},
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
Expand Down Expand Up @@ -114,8 +114,8 @@ var (

type RepositoryOptions struct {
OriginURL string
CurrentBranch string
CurrentCommit string
Branch string
Commit string
DefaultBranch string
DiffBaseBranch string
DiffBaseCommit string
Expand All @@ -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),
Expand Down
2 changes: 1 addition & 1 deletion internal/report/output/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
14 changes: 7 additions & 7 deletions internal/report/output/saas/saas.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 == "" {
Expand All @@ -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 == "" {
Expand All @@ -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,
Expand Down

0 comments on commit 28e20fd

Please sign in to comment.