Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clear new locally created files before pushing a fix #772

Merged
merged 10 commits into from
Oct 20, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
41 changes: 39 additions & 2 deletions scanrepository/scanrepository.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"github.com/go-git/go-git/v5"
biutils "github.com/jfrog/build-info-go/utils"
"os"
"path/filepath"
Expand Down Expand Up @@ -313,7 +314,7 @@ func (cfp *ScanRepositoryCmd) fixMultiplePackages(fullProjectPath string, vulner
return
}

// fixIssuesSinglePR fixes all the vulnerabilities in a single aggregated pull request.
// Fixes all the vulnerabilities in a single aggregated pull request.
// If an existing aggregated fix is present, it checks for different scan results.
// If the scan results are the same, no action is taken.
// Otherwise, it performs a force push to the same branch and reopens the pull request if it was closed.
Expand Down Expand Up @@ -398,6 +399,9 @@ func (cfp *ScanRepositoryCmd) openFixingPullRequest(repository *utils.Repository
return &utils.ErrNothingToCommit{PackageName: vulnDetails.ImpactedDependencyName}
}
commitMessage := cfp.gitManager.GenerateCommitMessage(vulnDetails.ImpactedDependencyName, vulnDetails.SuggestedFixedVersion)
if err = cfp.cleanNewFilesMissingInRemote(); err != nil {
return
}
if err = cfp.gitManager.AddAllAndCommit(commitMessage); err != nil {
return
}
Expand Down Expand Up @@ -443,10 +447,13 @@ func (cfp *ScanRepositoryCmd) createOrUpdatePullRequest(repository *utils.Reposi
return pullRequestInfo, utils.DeletePullRequestComments(repository, cfp.scanDetails.Client(), int(pullRequestInfo.ID))
}

// openAggregatedPullRequest handles the opening or updating of a pull request when the aggregate mode is active.
// Handles the opening or updating of a pull request when the aggregate mode is active.
// If a pull request is already open, Frogbot will update the branch and the pull request body.
func (cfp *ScanRepositoryCmd) openAggregatedPullRequest(repository *utils.Repository, fixBranchName string, pullRequestInfo *vcsclient.PullRequestInfo, vulnerabilities []*utils.VulnerabilityDetails) (err error) {
commitMessage := cfp.gitManager.GenerateAggregatedCommitMessage(cfp.projectTech)
if err = cfp.cleanNewFilesMissingInRemote(); err != nil {
return
}
if err = cfp.gitManager.AddAllAndCommit(commitMessage); err != nil {
return
}
Expand All @@ -456,6 +463,36 @@ func (cfp *ScanRepositoryCmd) openAggregatedPullRequest(repository *utils.Reposi
return cfp.handleFixPullRequestContent(repository, fixBranchName, pullRequestInfo, vulnerabilities...)
}

func (cfp *ScanRepositoryCmd) cleanNewFilesMissingInRemote() error {
eranturgeman marked this conversation as resolved.
Show resolved Hide resolved
// Open the local repository and get its Git status
localRepo, err := git.PlainOpen(cfp.baseWd)
if err != nil {
return err
}

worktree, err := localRepo.Worktree()
if err != nil {
return err
}

gitStatus, err := worktree.Status()
if err != nil {
return err
}

for relativeFilePath, status := range gitStatus {
if status.Worktree == git.Untracked {
log.Debug(fmt.Sprintf("Deleting file '%s' that was created locally during the scan/fix process", relativeFilePath))
fileDeletionErr := os.Remove(filepath.Join(cfp.baseWd, relativeFilePath))
if fileDeletionErr != nil {
log.Debug(fmt.Sprintf("Failed to delete file '%s' with error: %s:", relativeFilePath, fileDeletionErr.Error()))
continue
}
}
}
return nil
}

func (cfp *ScanRepositoryCmd) preparePullRequestDetails(vulnerabilitiesDetails ...*utils.VulnerabilityDetails) (prTitle, prBody string, otherComments []string, err error) {
if cfp.dryRun && cfp.aggregateFixes {
// For testings, don't compare pull request body as scan results order may change.
Expand Down
32 changes: 32 additions & 0 deletions scanrepository/scanrepository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ func TestScanRepositoryCmd_Run(t *testing.T) {
configPath string
expectedPackagesInBranch map[string][]string
expectedVersionUpdatesInBranch map[string][]string
expectedMissingFilesInBranch map[string][]string
packageDescriptorPaths []string
aggregateFixes bool
allowPartialResults bool
Expand All @@ -100,6 +101,7 @@ func TestScanRepositoryCmd_Run(t *testing.T) {
testName: "aggregate-multi-dir",
expectedPackagesInBranch: map[string][]string{"frogbot-update-npm-dependencies-master": {"uuid", "minimatch", "mpath", "minimist"}},
expectedVersionUpdatesInBranch: map[string][]string{"frogbot-update-npm-dependencies-master": {"^1.2.6", "^9.0.0", "^0.8.4", "^3.0.5"}},
expectedMissingFilesInBranch: map[string][]string{"frogbot-update-npm-dependencies-master": {"npm1/package-lock.json", "npm2/package-lock.json"}},
packageDescriptorPaths: []string{"npm1/package.json", "npm2/package.json"},
aggregateFixes: true,
configPath: "../testdata/scanrepository/cmd/aggregate-multi-dir/.frogbot/frogbot-config.yml",
Expand All @@ -108,6 +110,7 @@ func TestScanRepositoryCmd_Run(t *testing.T) {
testName: "aggregate-multi-project",
expectedPackagesInBranch: map[string][]string{"frogbot-update-npm-dependencies-master": {"uuid", "minimatch", "mpath"}, "frogbot-update-Pip-dependencies-master": {"pyjwt", "pexpect"}},
expectedVersionUpdatesInBranch: map[string][]string{"frogbot-update-npm-dependencies-master": {"^9.0.0", "^0.8.4", "^3.0.5"}, "frogbot-update-Pip-dependencies-master": {"2.4.0"}},
expectedMissingFilesInBranch: map[string][]string{"frogbot-update-npm-dependencies-master": {"npm/package-lock.json"}},
packageDescriptorPaths: []string{"npm/package.json", "pip/requirements.txt"},
aggregateFixes: true,
configPath: "../testdata/scanrepository/cmd/aggregate-multi-project/.frogbot/frogbot-config.yml",
Expand Down Expand Up @@ -221,6 +224,14 @@ func TestScanRepositoryCmd_Run(t *testing.T) {
assert.Contains(t, string(resultDiff), updatedVersion)
}
}

if len(test.expectedMissingFilesInBranch) > 0 {
for branch, expectedMissingFiles := range test.expectedMissingFilesInBranch {
resultDiff, err := verifyLockFileDiff(branch, expectedMissingFiles...)
assert.NoError(t, err)
assert.Empty(t, resultDiff)
}
}
})
}
}
Expand Down Expand Up @@ -698,3 +709,24 @@ func verifyDependencyFileDiff(baseBranch string, fixBranch string, packageDescri
}
return
}

func verifyLockFileDiff(branchToInspect string, lockFiles ...string) (output []byte, err error) {
log.Debug(fmt.Sprintf("Checking lock files differences in %s between branches 'master' and '%s'", lockFiles, branchToInspect))
// Suppress condition always false warning
//goland:noinspection ALL
var args []string
if coreutils.IsWindows() {
args = []string{"/c", "git", "ls-tree", branchToInspect, "--"}
args = append(args, lockFiles...)
output, err = exec.Command("cmd", args...).Output()
} else {
args = []string{"ls-tree", branchToInspect, "--"}
args = append(args, lockFiles...)
output, err = exec.Command("git", args...).Output()
}
var exitError *exec.ExitError
if errors.As(err, &exitError) {
err = errors.New("git error: " + string(exitError.Stderr))
}
return
}
12 changes: 12 additions & 0 deletions utils/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,18 @@ func (gm *GitManager) dryRunClone(destination string) error {
return nil
}

func (gm *GitManager) GetRemoteGitUrl() string {
return gm.remoteGitUrl
}

func (gm *GitManager) GetAuth() *githttp.BasicAuth {
return gm.auth
}

func (gm *GitManager) GetRemoteName() string {
return gm.remoteName
}

func toBasicAuth(username, token string) *githttp.BasicAuth {
// The username can be anything except for an empty string
if username == "" {
Expand Down
Loading