Skip to content
Closed
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
6 changes: 4 additions & 2 deletions scanpullrequest/scanpullrequest.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,9 @@ func downloadSourceAndTarget(repoConfig *utils.Repository, scanDetails *utils.Sc
cleanup = func() error { return errors.Join(cleanupSource(), cleanupTarget()) }

log.Info("Downloading source branch code...")
if sourceBranchWd, cleanupSource, err = utils.DownloadRepoToTempDir(scanDetails.Client(),
if sourceBranchWd, cleanupSource, err = utils.CloneRepoToTempDir(scanDetails.Client(),
scanDetails.Username,
scanDetails.Token,
scanDetails.PullRequestDetails.Source.Owner,
scanDetails.PullRequestDetails.Source.Repository,
scanDetails.PullRequestDetails.Source.Name,
Expand All @@ -125,7 +127,7 @@ func downloadSourceAndTarget(repoConfig *utils.Repository, scanDetails *utils.Sc
return
}
target := repoConfig.Params.Git.PullRequestDetails.Target
if targetBranchWd, cleanupTarget, err = utils.DownloadRepoToTempDir(scanDetails.Client(), target.Owner, target.Repository, target.Name); err != nil {
if targetBranchWd, cleanupTarget, err = utils.CloneRepoToTempDir(scanDetails.Client(), scanDetails.Username, scanDetails.Token, target.Owner, target.Repository, target.Name); err != nil {
err = fmt.Errorf("failed to download target branch code. Error: %s", err.Error())
return
}
Expand Down
5 changes: 5 additions & 0 deletions utils/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@ func (gm *GitManager) SetAuth(username, token string) *GitManager {
return gm
}

func (gm *GitManager) SetUrl(remoteHttpsGitUrl string) *GitManager {
gm.remoteGitUrl = remoteHttpsGitUrl
return gm
}

func (gm *GitManager) SetRemoteGitUrl(remoteHttpsGitUrl string) (*GitManager, error) {
// Check if the .git directory exists
dotGitExists, err := fileutils.IsDirExists(git.GitDirName, false)
Expand Down
15 changes: 10 additions & 5 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,20 +257,25 @@ func GenerateFrogbotSarifReport(extendedResults *results.SecurityCommandResults)
return stringReport, hasRuns, err
}

func DownloadRepoToTempDir(client vcsclient.VcsClient, repoOwner, repoName, branch string) (wd string, cleanup func() error, err error) {
func CloneRepoToTempDir(client vcsclient.VcsClient, username, token, repoOwner, repoName, branch string) (wd string, cleanup func() error, err error) {
wd, err = fileutils.CreateTempDir()
if err != nil {
return
}
cleanup = func() error {
return fileutils.RemoveTempDir(wd)
}
log.Debug(fmt.Sprintf("Downloading <%s/%s/%s> to: '%s'", repoOwner, repoName, branch, wd))
if err = client.DownloadRepository(context.Background(), repoOwner, repoName, branch, wd); err != nil {
err = fmt.Errorf("failed to download branch: <%s/%s/%s> with error: %s", repoOwner, repoName, branch, err.Error())
repoInfo, err := client.GetRepositoryInfo(context.Background(), repoOwner, repoName)
if err != nil {
err = fmt.Errorf("failed to get repository info for <%s/%s>: %s", repoOwner, repoName, err.Error())
return
}
log.Debug(fmt.Sprintf("Cloning <%s/%s/%s> to: '%s'", repoOwner, repoName, branch, wd))
if err = NewGitManager().SetAuth(username, token).SetUrl(repoInfo.CloneInfo.HTTP).Clone(wd, branch); err != nil {
err = fmt.Errorf("failed to clone branch: <%s/%s/%s> with error: %s", repoOwner, repoName, branch, err.Error())
return
}
log.Debug("Repository download completed")
log.Debug("Repository clone completed")
return
}

Expand Down
Loading