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

Fix resolve common ancestor hash when revision not found #802

Merged
merged 4 commits into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
4 changes: 0 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,7 @@ require (
github.com/CycloneDX/cyclonedx-go v0.9.0 // indirect
github.com/Microsoft/go-winio v0.6.1 // indirect
github.com/ProtonMail/go-crypto v1.1.2 // indirect
github.com/VividCortex/ewma v1.2.0 // indirect
github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d // indirect
github.com/andybalholm/brotli v1.1.0 // indirect
github.com/beevik/etree v1.4.0 // indirect
github.com/buger/jsonparser v1.1.1 // indirect
github.com/c-bata/go-prompt v0.2.5 // indirect
github.com/chzyer/readline v1.5.1 // indirect
Expand Down Expand Up @@ -96,7 +93,6 @@ require (
github.com/subosito/gotenv v1.6.0 // indirect
github.com/ulikunitz/xz v0.5.12 // indirect
github.com/urfave/cli v1.22.16 // indirect
github.com/vbauerster/mpb/v8 v8.8.3 // indirect
github.com/virtuald/go-ordered-json v0.0.0-20170621173500-b18e6e673d74 // indirect
github.com/xanzy/go-gitlab v0.110.0 // indirect
github.com/xanzy/ssh-agent v0.3.3 // indirect
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be/go.mod h1:ySMOLuW
github.com/apparentlymart/go-textseg/v13 v13.0.0/go.mod h1:ZK2fH7c4NqDTLtiYLvIkEghdlcqw7yxLeM89kiTRPUo=
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5 h1:0CwZNZbxp69SHPdPJAN/hZIm0C4OItdklCFmMRWYpio=
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5/go.mod h1:wHh0iHkYZB8zMSxRWpUBQtwG5a7fFgvEO+odwuTv2gs=
github.com/beevik/etree v1.4.0 h1:oz1UedHRepuY3p4N5OjE0nK1WLCqtzHf25bxplKOHLs=
github.com/beevik/etree v1.4.0/go.mod h1:cyWiXwGoasx60gHvtnEh5x8+uIjUVnjWqBvEnhnqKDA=
github.com/bradleyjkemp/cupaloy/v2 v2.8.0 h1:any4BmKE+jGIaMpnU8YgH/I2LPiLBufr6oMMlVBbn9M=
github.com/bradleyjkemp/cupaloy/v2 v2.8.0/go.mod h1:bm7JXdkRd4BHJk9HpwqAI8BoAY1lps46Enkdqw6aRX0=
github.com/buger/jsonparser v1.1.1 h1:2PnMjfWD7wBILjqQbt530v576A/cAbQvEW9gGIpYMUs=
Expand Down
30 changes: 11 additions & 19 deletions scanpullrequest/scanpullrequest.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,39 +255,31 @@ func tryCheckoutToMostCommonAncestor(scanDetails *utils.ScanDetails, baseBranch,
return
}
scanDetails.Git.RepositoryCloneUrl = repositoryInfo.CloneInfo.HTTP
bestAncestorHash, err := getMostCommonAncestorCommitHash(scanDetails, baseBranch, headBranch)
if err != nil {
return
}
return checkoutToCommitAtTempWorkingDir(scanDetails, bestAncestorHash, targetBranchWd)
}

func getMostCommonAncestorCommitHash(scanDetails *utils.ScanDetails, baseBranch, headBranch string) (hash string, err error) {
gitManager, err := utils.NewGitManager().SetAuth(scanDetails.Username, scanDetails.Token).SetRemoteGitUrl(scanDetails.Git.RepositoryCloneUrl)
if err != nil {
return
}
return gitManager.GetMostCommonAncestorHash(baseBranch, headBranch)
}

func checkoutToCommitAtTempWorkingDir(scanDetails *utils.ScanDetails, commitHash, wd string) (err error) {
// Change working directory to the temp target branch directory
cwd, err := os.Getwd()
if err != nil {
return
}
if err = os.Chdir(wd); err != nil {
if err = os.Chdir(targetBranchWd); err != nil {
return
}
defer func() {
err = errors.Join(err, os.Chdir(cwd))
}()
// Load .git info in directory and Checkout to the commit hash
// Create a new git manager and fetch
gitManager, err := utils.NewGitManager().SetAuth(scanDetails.Username, scanDetails.Token).SetRemoteGitUrl(scanDetails.Git.RepositoryCloneUrl)
if err != nil {
return
}
return gitManager.CheckoutToHash(commitHash, wd)
if err = gitManager.Fetch(); err != nil {
return
}
// Get the most common ancestor commit hash
bestAncestorHash, err := gitManager.GetMostCommonAncestorHash(baseBranch, headBranch)
if err != nil {
return
}
return gitManager.CheckoutToHash(bestAncestorHash)
}

func getAllIssues(cmdResults *results.SecurityCommandResults, allowedLicenses []string, hasViolationContext bool) (*utils.IssuesCollection, error) {
Expand Down
9 changes: 3 additions & 6 deletions utils/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,7 @@ func (gm *GitManager) Checkout(branchName string) error {
return nil
}

func (gm *GitManager) CheckoutToHash(hash, targetBranchWd string) error {
if err := gm.Fetch(); err != nil {
return err
}
func (gm *GitManager) CheckoutToHash(hash string) error {
log.Debug("Running git checkout to hash:", hash)
if err := gm.createBranchAndCheckoutToHash(hash, false); err != nil {
return fmt.Errorf("'git checkout %s' failed with error: %s", hash, err.Error())
Expand All @@ -187,7 +184,7 @@ func (gm *GitManager) Fetch() error {

func (gm *GitManager) GetMostCommonAncestorHash(baseBranch, targetBranch string) (string, error) {
// Get the commit of the base branch
baseCommitHash, err := gm.localGitRepository.ResolveRevision(plumbing.Revision(baseBranch))
baseCommitHash, err := gm.localGitRepository.ResolveRevision(plumbing.Revision(fmt.Sprintf("%s/%s", gm.remoteName, baseBranch)))
if err != nil {
return "", err
}
Expand All @@ -196,7 +193,7 @@ func (gm *GitManager) GetMostCommonAncestorHash(baseBranch, targetBranch string)
return "", err
}
// Get the HEAD commit of the target branch
headCommitHash, err := gm.localGitRepository.ResolveRevision(plumbing.Revision(targetBranch))
headCommitHash, err := gm.localGitRepository.ResolveRevision(plumbing.Revision(fmt.Sprintf("%s/%s", gm.remoteName, targetBranch)))
if err != nil {
return "", err
}
Expand Down
Loading