Skip to content
Open
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
29 changes: 17 additions & 12 deletions scanrepository/scanrepository.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
Expand Down
2 changes: 2 additions & 0 deletions utils/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand Down
2 changes: 2 additions & 0 deletions utils/getconfiguration.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ type Params struct {
*services.ConfigProfile
Git
JFrogPlatform
WorkingDirectory string
}

type JFrogPlatform struct {
Expand Down Expand Up @@ -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)
Expand Down
13 changes: 13 additions & 0 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"fmt"
"net/http"
"os"
"path/filepath"
"regexp"
"sort"
"strings"
Expand Down Expand Up @@ -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))
Expand Down
Loading