diff --git a/scanrepository/scanrepository.go b/scanrepository/scanrepository.go index b509e11a4..e768f25be 100644 --- a/scanrepository/scanrepository.go +++ b/scanrepository/scanrepository.go @@ -4,12 +4,13 @@ import ( "context" "errors" "fmt" - "github.com/jfrog/frogbot/v2/packageupdaters" "os" "path/filepath" "regexp" "strings" + "github.com/jfrog/frogbot/v2/packageupdaters" + "github.com/go-git/go-git/v5" biutils "github.com/jfrog/build-info-go/utils" @@ -44,16 +45,17 @@ var supportedAutoFixTechnologies = []techutils.Technology{ type ScanRepositoryCmd struct { outputwriter.OutputWriter - dryRun bool - dryRunRepoPath string - scanDetails *utils.ScanDetails - baseWd string - gitManager *utils.GitManager - projectTech []techutils.Technology - updaters map[techutils.Technology]packageupdaters.PackageUpdater - customTemplates utils.CustomTemplates - XrayVersion string - XscVersion string + dryRun bool + dryRunRepoPath string + scanDetails *utils.ScanDetails + baseWd string + workingDirectory string + gitManager *utils.GitManager + projectTech []techutils.Technology + updaters map[techutils.Technology]packageupdaters.PackageUpdater + customTemplates utils.CustomTemplates + XrayVersion string + XscVersion string } func (sr *ScanRepositoryCmd) Run(repository utils.Repository, client vcsclient.VcsClient) (err error) { @@ -118,6 +120,7 @@ func (sr *ScanRepositoryCmd) setCommandPrerequisites(repository *utils.Repositor SetJfrogVersions(sr.XrayVersion, sr.XscVersion). SetResultsContext(repositoryCloneUrl, repository.Params.JFrogPlatform.JFrogProjectKey, false). SetConfigProfile(repository.Params.ConfigProfile) + sr.workingDirectory = repository.Params.WorkingDirectory sr.OutputWriter = repository.OutputWriter sr.OutputWriter.SetSizeLimit(client) @@ -201,7 +204,9 @@ func (sr *ScanRepositoryCmd) uploadResultsToGithubDashboardsIfNeeded(repository // Audit the dependencies of the current commit. func (sr *ScanRepositoryCmd) scan() (*results.SecurityCommandResults, error) { - auditResults := sr.scanDetails.Audit(sr.baseWd) + scanWd := utils.ResolveScanWorkingDir(sr.baseWd, sr.workingDirectory) + log.Info(fmt.Sprintf("Working directory: %s", sr.workingDirectory)) + auditResults := sr.scanDetails.Audit(scanWd) if err := auditResults.GetErrors(); err != nil { return nil, err } diff --git a/utils/consts.go b/utils/consts.go index f124f9e1a..c9395a675 100644 --- a/utils/consts.go +++ b/utils/consts.go @@ -47,6 +47,8 @@ const ( GitPullRequestIDEnv = "JF_GIT_PULL_REQUEST_ID" GitApiEndpointEnv = "JF_GIT_API_ENDPOINT" + WorkingDirectoryEnv = "JF_WORKING_DIRECTORY" + // Placeholders for templates PackagePlaceHolder = "{IMPACTED_PACKAGE}" FixVersionPlaceHolder = "{FIX_VERSION}" diff --git a/utils/getconfiguration.go b/utils/getconfiguration.go index b4941e6c9..2c5c2d3d2 100644 --- a/utils/getconfiguration.go +++ b/utils/getconfiguration.go @@ -49,6 +49,7 @@ type Params struct { *services.ConfigProfile Git JFrogPlatform + WorkingDirectory string } type JFrogPlatform struct { @@ -190,6 +191,7 @@ func BuildRepositoryFromEnv(xrayVersion, xscVersion string, gitClient vcsclient. if err = repository.Params.Git.setDefaultsIfNeeded(gitParamsFromEnv, commandName); err != nil { return } + repository.Params.WorkingDirectory = getTrimmedEnv(WorkingDirectoryEnv) repository.setOutputWriterDetails() repository.OutputWriter.SetSizeLimit(gitClient) diff --git a/utils/utils.go b/utils/utils.go index 19f16d1a0..43bec1ae2 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -8,6 +8,7 @@ import ( "fmt" "net/http" "os" + "path/filepath" "regexp" "sort" "strings" @@ -266,6 +267,18 @@ func DownloadRepoToTempDir(client vcsclient.VcsClient, repoOwner, repoName, bran return } +func ResolveScanWorkingDir(repoRoot, workingDir string) string { + workingDir = strings.TrimSpace(workingDir) + if workingDir == "" || workingDir == "." { + return repoRoot + } + workingDir = strings.TrimPrefix(filepath.Clean(workingDir), "."+string(filepath.Separator)) + if workingDir == "" || workingDir == "." { + return repoRoot + } + return filepath.Join(repoRoot, workingDir) +} + // GetRelativeWd receive a base working directory along with a full path containing the base working directory, and the relative part is returned without the base prefix. func GetRelativeWd(fullPathWd, baseWd string) string { fullPathWd = strings.TrimSuffix(fullPathWd, string(os.PathSeparator))