Skip to content

Commit

Permalink
Fix aggregate PRs test (jfrog#420)
Browse files Browse the repository at this point in the history
  • Loading branch information
sverdlov93 committed Aug 13, 2023
1 parent 42f9f61 commit cd94264
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 24 deletions.
10 changes: 6 additions & 4 deletions commands/createfixpullrequests_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,12 @@ func TestCreateFixPullRequestsCmd_Run(t *testing.T) {
t.Run(test.repoName, func(t *testing.T) {
// Prepare
serverParams, restoreEnv := verifyEnv(t)
assert.NoError(t, os.Setenv(utils.GitAggregateFixesEnv, "true"))
defer func() {
assert.NoError(t, os.Setenv(utils.GitAggregateFixesEnv, "false"))
}()
if test.aggregateFixes {
assert.NoError(t, os.Setenv(utils.GitAggregateFixesEnv, "true"))
defer func() {
assert.NoError(t, os.Setenv(utils.GitAggregateFixesEnv, "false"))
}()
}
var port string
server := httptest.NewServer(createHttpHandler(t, &port, test.repoName))
port = server.URL[strings.LastIndex(server.URL, ":")+1:]
Expand Down
26 changes: 7 additions & 19 deletions commands/scanpullrequest.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,7 @@ func auditPullRequest(repoConfig *utils.Repository, client vcsclient.VcsClient,
// Download target branch (if needed)
targetBranchWd := ""
cleanupTarget := func() error { return nil }
if repoConfig.IncludeAllVulnerabilities {
log.Info("Frogbot is configured to show all vulnerabilities")
if !repoConfig.IncludeAllVulnerabilities {
targetBranchInfo := pullRequestDetails.Target
targetBranchWd, cleanupTarget, err = utils.DownloadRepoToTempDir(client, targetBranchInfo.Owner, targetBranchInfo.Repository, targetBranchInfo.Name)
if err != nil {
Expand All @@ -127,7 +126,8 @@ func auditPullRequest(repoConfig *utils.Repository, client vcsclient.VcsClient,

// Audit source branch
var sourceResults *audit.Results
sourceResults, err = runInstallAndAudit(scanDetails, sourceBranchWd)
workingDirs := getFullPathWorkingDirs(scanDetails.Project.WorkingDirs, sourceBranchWd)
sourceResults, err = runInstallAndAudit(scanDetails, workingDirs...)
if err != nil {
return
}
Expand All @@ -138,6 +138,7 @@ func auditPullRequest(repoConfig *utils.Repository, client vcsclient.VcsClient,

// Get all issues that were found in the source branch
if repoConfig.IncludeAllVulnerabilities {
log.Info("Frogbot is configured to show all vulnerabilities")
var allIssuesRows []formats.VulnerabilityOrViolationRow
allIssuesRows, err = getScanVulnerabilitiesRows(sourceResults)
if err != nil {
Expand All @@ -150,7 +151,8 @@ func auditPullRequest(repoConfig *utils.Repository, client vcsclient.VcsClient,

// Set target branch scan details
var targetResults *audit.Results
targetResults, err = runInstallAndAudit(scanDetails, targetBranchWd)
workingDirs = getFullPathWorkingDirs(scanDetails.Project.WorkingDirs, targetBranchWd)
targetResults, err = runInstallAndAudit(scanDetails, workingDirs...)
if err != nil {
return
}
Expand Down Expand Up @@ -277,21 +279,7 @@ func getFullPathWorkingDirs(workingDirs []string, baseWd string) []string {
return fullPathWds
}

func runInstallAndAudit(scanSetup *utils.ScanDetails, branchWd string) (auditResults *audit.Results, err error) {
currWd, err := os.Getwd()
if err != nil {
err = errors.New("unable to retrieve to current working directory while auditing the project. error received:\n" + err.Error())
return
}
if err = os.Chdir(branchWd); err != nil {
err = errors.New("unable to change directory to run an audit on it due to an error:\n" + err.Error())
return
}
defer func() {
err = errors.Join(err, os.Chdir(currWd))
}()

workDirs := getFullPathWorkingDirs(scanSetup.Project.WorkingDirs, branchWd)
func runInstallAndAudit(scanSetup *utils.ScanDetails, workDirs ...string) (auditResults *audit.Results, err error) {
for _, wd := range workDirs {
if err = runInstallIfNeeded(scanSetup, wd); err != nil {
return nil, err
Expand Down
3 changes: 2 additions & 1 deletion commands/utils/params_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -481,8 +481,9 @@ func TestBuildMergedRepoAggregator(t *testing.T) {
}
repoAggregator, err := BuildRepoAggregator(fileContent, gitClientInfo, &server)
assert.NoError(t, err)

repo := repoAggregator[0]
assert.Equal(t, repo.AggregateFixes, false)
assert.Equal(t, repo.AggregateFixes, true)
assert.True(t, repo.IncludeAllVulnerabilities)
assert.True(t, repo.FixableOnly)
assert.True(t, *repo.FailOnSecurityIssues)
Expand Down

0 comments on commit cd94264

Please sign in to comment.