Skip to content

Commit f6cdf8f

Browse files
authored
Pr head checkout fallback (#231)
* Add commit checkout fallback to PR head branch checkout method * Validate commit checkout fallback required params * Add some logs to commit checkout fallback * Mock pr head branch checkout error * Fix commit checkout error mocking * Remove commit checkout error mocking * Add fork repo PR handling to the commit checkout fallback * Remove error mock
1 parent 93b66d4 commit f6cdf8f

File tree

3 files changed

+43
-4
lines changed

3 files changed

+43
-4
lines changed

gitclone/checkout.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,33 @@ func createCheckoutStrategy(checkoutMethod CheckoutMethod, cfg Config, patchFile
314314

315315
return checkoutCommit{
316316
params: *params,
317+
fallbackCheckout: func(gitCmd git.Git) error {
318+
log.Warnf("Using commit checkout strategy with PR source branch")
319+
320+
if cfg.Branch == "" || cfg.Commit == "" {
321+
return fmt.Errorf("inconsistent checkout strategy and checkout params: branch=%s, commit=%s", cfg.Branch, cfg.Commit)
322+
}
323+
324+
branchRef := refsHeadsPrefix + cfg.Branch
325+
commitCheckoutFallbackFetchOpts := selectFetchOptions(CheckoutCommitMethod, cfg.CloneDepth, cfg.FetchTags, cfg.UpdateSubmodules, len(cfg.SparseDirectories) != 0)
326+
commitCheckoutFallbackFallback := selectFallbacks(CheckoutCommitMethod, commitCheckoutFallbackFetchOpts)
327+
328+
prRepositoryURL := ""
329+
if isFork(cfg.RepositoryURL, cfg.PRSourceRepositoryURL) {
330+
prRepositoryURL = cfg.PRSourceRepositoryURL
331+
}
332+
333+
params, err := NewCommitParams(cfg.Commit, branchRef, prRepositoryURL)
334+
if err != nil {
335+
return err
336+
}
337+
338+
commitCheckoutFallbackCheckoutMethod := checkoutCommit{
339+
params: *params,
340+
}
341+
342+
return commitCheckoutFallbackCheckoutMethod.do(gitCmd, commitCheckoutFallbackFetchOpts, commitCheckoutFallbackFallback)
343+
},
317344
}, nil
318345
}
319346
case CheckoutForkCommitMethod:

gitclone/checkout_method_pr_auto_merge_branch.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package gitclone
33
import (
44
"fmt"
55
"strings"
6-
6+
77
"github.com/bitrise-io/go-utils/command/git"
88
"github.com/bitrise-io/go-utils/log"
99
)
@@ -42,9 +42,8 @@ type fallbackCheckoutFunc func(gitCmd git.Git) error
4242

4343
func (c checkoutPRMergeRef) do(gitCmd git.Git, fetchOpts fetchOptions, fallback fallbackRetry) error {
4444
if err := c.performCheckout(gitCmd, fetchOpts, fallback); err != nil {
45-
log.Warnf("Failed to checkout PR merge branch: %s", err)
46-
4745
if c.fallbackCheckout != nil {
46+
log.Warnf("Failed to checkout PR merge branch: %s", err)
4847
return c.fallbackCheckout(gitCmd)
4948
}
5049
return err

gitclone/checkout_method_simple.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"strings"
66

77
"github.com/bitrise-io/go-utils/command/git"
8+
"github.com/bitrise-io/go-utils/log"
89
)
910

1011
// checkoutNone
@@ -40,10 +41,22 @@ func NewCommitParams(commit, branchRef, sourceRepoURL string) (*CommitParams, er
4041

4142
// checkoutCommit
4243
type checkoutCommit struct {
43-
params CommitParams
44+
params CommitParams
45+
fallbackCheckout fallbackCheckoutFunc
4446
}
4547

4648
func (c checkoutCommit) do(gitCmd git.Git, fetchOptions fetchOptions, fallback fallbackRetry) error {
49+
if err := c.performCheckout(gitCmd, fetchOptions, fallback); err != nil {
50+
if c.fallbackCheckout != nil {
51+
log.Warnf("Failed to checkout commit: %s", err)
52+
return c.fallbackCheckout(gitCmd)
53+
}
54+
return err
55+
}
56+
return nil
57+
}
58+
59+
func (c checkoutCommit) performCheckout(gitCmd git.Git, fetchOptions fetchOptions, fallback fallbackRetry) error {
4760
remote := originRemoteName
4861
if c.params.SourceRepoURL != "" {
4962
remote = forkRemoteName

0 commit comments

Comments
 (0)