-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathcheckout_method_pr_auto_diff.go
More file actions
64 lines (51 loc) · 1.81 KB
/
checkout_method_pr_auto_diff.go
File metadata and controls
64 lines (51 loc) · 1.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package gitclone
import (
"fmt"
"strings"
"github.com/bitrise-io/go-utils/log"
"github.com/bitrise-io/go-utils/v2/git"
)
// PRDiffFileParams are parameters to check out a Merge/Pull Request (when a diff file is available)
type PRDiffFileParams struct {
DestinationBranch string
PRManualMergeStrategy checkoutStrategy
}
// NewPRDiffFileParams validates and returns a new PRDiffFileParams
func NewPRDiffFileParams(
destBranch string,
prManualMergeStrategy checkoutStrategy,
) (*PRDiffFileParams, error) {
if strings.TrimSpace(destBranch) == "" {
return nil, NewParameterValidationError("PR diff file based checkout strategy can not be used: no base branch specified")
}
return &PRDiffFileParams{
DestinationBranch: destBranch,
PRManualMergeStrategy: prManualMergeStrategy,
}, nil
}
// checkoutPRDiffFile
type checkoutPRDiffFile struct {
params PRDiffFileParams
patchFile string
}
func (c checkoutPRDiffFile) do(gitFactory git.Factory, fetchOptions fetchOptions, fallback fallbackRetry) error {
destBranchRef := refsHeadsPrefix + c.params.DestinationBranch
if err := fetch(gitFactory, originRemoteName, destBranchRef, fetchOptions); err != nil {
return fmt.Errorf("failed to fetch base branch: %w", err)
}
if err := checkoutWithCustomRetry(gitFactory, c.params.DestinationBranch, fallback); err != nil {
return err
}
if err := runner.Run(gitFactory.Apply(c.patchFile)); err != nil {
log.Warnf("Could not apply patch (%s): %v", c.patchFile, err)
log.Warnf("Falling back to manual merge...")
if err := c.params.PRManualMergeStrategy.do(gitFactory, fetchOptions, fallback); err != nil {
return fmt.Errorf("fallback failed for applying patch (%s): %v", c.patchFile, err)
}
return nil
}
return detachHead(gitFactory)
}
func (c checkoutPRDiffFile) getBuildTriggerRef() string {
return ""
}