@@ -37,34 +37,10 @@ export async function shouldReuseExistingBranch(
37
37
return result ;
38
38
}
39
39
logger . debug ( `Branch already exists` ) ;
40
- if ( result . rebaseWhen === 'auto' ) {
41
- if ( result . automerge === true ) {
42
- logger . debug (
43
- 'Converting rebaseWhen=auto to rebaseWhen=behind-base-branch because automerge=true' ,
44
- ) ;
45
- result . rebaseWhen = 'behind-base-branch' ;
46
- } else if ( await platform . getBranchForceRebase ?.( result . baseBranch ) ) {
47
- logger . debug (
48
- 'Converting rebaseWhen=auto to rebaseWhen=behind-base-branch because platform is configured to require up-to-date branches' ,
49
- ) ;
50
- result . rebaseWhen = 'behind-base-branch' ;
51
- } else if ( await shouldKeepUpdated ( result , baseBranch , branchName ) ) {
52
- logger . debug (
53
- 'Converting rebaseWhen=auto to rebaseWhen=behind-base-branch because keep-updated label is set' ,
54
- ) ;
55
- result . rebaseWhen = 'behind-base-branch' ;
56
- }
57
- }
58
- if ( result . rebaseWhen === 'auto' ) {
59
- logger . debug (
60
- 'Converting rebaseWhen=auto to rebaseWhen=conflicted because no rule for converting to rebaseWhen=behind-base-branch applies' ,
61
- ) ;
62
- result . rebaseWhen = 'conflicted' ;
63
- }
64
- if (
65
- result . rebaseWhen === 'behind-base-branch' ||
66
- ( await shouldKeepUpdated ( result , baseBranch , branchName ) )
67
- ) {
40
+ const keepUpdated = await shouldKeepUpdated ( result , baseBranch , branchName ) ;
41
+ await determineRebaseWhenValue ( result , keepUpdated ) ;
42
+
43
+ if ( result . rebaseWhen === 'behind-base-branch' || keepUpdated ) {
68
44
if ( await scm . isBranchBehindBase ( branchName , baseBranch ) ) {
69
45
logger . debug ( `Branch is behind base branch and needs rebasing` ) ;
70
46
// We can rebase the branch only if no PR or PR can be rebased
@@ -91,10 +67,7 @@ export async function shouldReuseExistingBranch(
91
67
92
68
if ( ( await scm . isBranchModified ( branchName , baseBranch ) ) === false ) {
93
69
logger . debug ( `Branch is not mergeable and needs rebasing` ) ;
94
- if (
95
- result . rebaseWhen === 'never' &&
96
- ! ( await shouldKeepUpdated ( result , baseBranch , branchName ) )
97
- ) {
70
+ if ( result . rebaseWhen === 'never' && ! keepUpdated ) {
98
71
logger . debug ( 'Rebasing disabled by config' ) ;
99
72
result . reuseExistingBranch = true ;
100
73
result . isModified = false ;
@@ -136,3 +109,35 @@ export async function shouldReuseExistingBranch(
136
109
result . isModified = false ;
137
110
return result ;
138
111
}
112
+
113
+ /**
114
+ * This method updates rebaseWhen value when it's set to auto(default)
115
+ *
116
+ * @param result BranchConfig
117
+ * @param keepUpdated boolean
118
+ */
119
+ async function determineRebaseWhenValue (
120
+ result : BranchConfig ,
121
+ keepUpdated : boolean ,
122
+ ) : Promise < void > {
123
+ if ( result . rebaseWhen === 'auto' ) {
124
+ let reason ;
125
+
126
+ let newValue = 'behind-base-branch' ;
127
+ if ( result . automerge === true ) {
128
+ reason = 'automerge=true' ;
129
+ } else if ( await platform . getBranchForceRebase ?.( result . baseBranch ) ) {
130
+ reason = 'platform is configured to require up-to-date branches' ;
131
+ } else if ( keepUpdated ) {
132
+ reason = 'keep-updated label is set' ;
133
+ } else {
134
+ newValue = 'conflicted' ;
135
+ reason = 'no rule for behind-base-branch applies' ;
136
+ }
137
+
138
+ logger . debug (
139
+ `Converting rebaseWhen=${ result . rebaseWhen } to rebaseWhen=${ newValue } because ${ reason } ` ,
140
+ ) ;
141
+ result . rebaseWhen = newValue ;
142
+ }
143
+ }
0 commit comments