-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
automerge.ts
159 lines (156 loc) · 4.34 KB
/
automerge.ts
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// TODO #22198
import { GlobalConfig } from '../../../../config/global';
import { logger } from '../../../../logger';
import { Pr, platform } from '../../../../modules/platform';
import {
ensureComment,
ensureCommentRemoval,
} from '../../../../modules/platform/comment';
import { scm } from '../../../../modules/platform/scm';
import type { BranchConfig } from '../../../types';
import { isScheduledNow } from '../branch/schedule';
import { resolveBranchStatus } from '../branch/status-checks';
export type PrAutomergeBlockReason =
| 'BranchModified'
| 'BranchNotGreen'
| 'Conflicted'
| 'DryRun'
| 'PlatformNotReady'
| 'PlatformRejection'
| 'off schedule';
export interface AutomergePrResult {
automerged: boolean;
branchRemoved?: boolean;
prAutomergeBlockReason?: PrAutomergeBlockReason;
}
export async function checkAutoMerge(
pr: Pr,
config: BranchConfig
): Promise<AutomergePrResult> {
logger.trace({ config }, 'checkAutoMerge');
const {
branchName,
automergeType,
automergeStrategy,
pruneBranchAfterAutomerge,
automergeComment,
ignoreTests,
rebaseRequested,
} = config;
// Return if PR not ready for automerge
if (!isScheduledNow(config, 'automergeSchedule')) {
logger.debug(`PR automerge is off schedule`);
return {
automerged: false,
prAutomergeBlockReason: 'off schedule',
};
}
const isConflicted =
config.isConflicted ??
(await scm.isBranchConflicted(config.baseBranch, config.branchName));
if (isConflicted) {
logger.debug('PR is conflicted');
return {
automerged: false,
prAutomergeBlockReason: 'Conflicted',
};
}
if (!ignoreTests && pr.cannotMergeReason) {
logger.debug(
`Platform reported that PR is not ready for merge. Reason: [${pr.cannotMergeReason}]`
);
return {
automerged: false,
prAutomergeBlockReason: 'PlatformNotReady',
};
}
const branchStatus = await resolveBranchStatus(
config.branchName,
!!config.internalChecksAsSuccess,
config.ignoreTests
);
if (branchStatus !== 'green') {
logger.debug(
`PR is not ready for merge (branch status is ${branchStatus})`
);
return {
automerged: false,
prAutomergeBlockReason: 'BranchNotGreen',
};
}
// Check if it's been touched
if (await scm.isBranchModified(branchName, config.baseBranch)) {
logger.debug('PR is ready for automerge but has been modified');
return {
automerged: false,
prAutomergeBlockReason: 'BranchModified',
};
}
if (automergeType === 'pr-comment') {
// TODO: types (#22198)
logger.debug(`Applying automerge comment: ${automergeComment!}`);
// istanbul ignore if
if (GlobalConfig.get('dryRun')) {
logger.info(
`DRY-RUN: Would add PR automerge comment to PR #${pr.number}`
);
return {
automerged: false,
prAutomergeBlockReason: 'DryRun',
};
}
if (rebaseRequested) {
await ensureCommentRemoval({
type: 'by-content',
number: pr.number,
content: automergeComment!,
});
}
await ensureComment({
number: pr.number,
topic: null,
content: automergeComment!,
});
return { automerged: true, branchRemoved: false };
}
// Let's merge this
// istanbul ignore if
if (GlobalConfig.get('dryRun')) {
// TODO: types (#22198)
logger.info(
`DRY-RUN: Would merge PR #${
pr.number
} with strategy "${automergeStrategy!}"`
);
return {
automerged: false,
prAutomergeBlockReason: 'DryRun',
};
}
// TODO: types (#22198)
logger.debug(`Automerging #${pr.number} with strategy ${automergeStrategy!}`);
const res = await platform.mergePr({
branchName,
id: pr.number,
strategy: automergeStrategy,
});
if (res) {
logger.info({ pr: pr.number, prTitle: pr.title }, 'PR automerged');
if (!pruneBranchAfterAutomerge) {
logger.info('Skipping pruning of merged branch');
return { automerged: true, branchRemoved: false };
}
let branchRemoved = false;
try {
await scm.deleteBranch(branchName);
branchRemoved = true;
} catch (err) /* istanbul ignore next */ {
logger.warn({ branchName, err }, 'Branch auto-remove failed');
}
return { automerged: true, branchRemoved };
}
return {
automerged: false,
prAutomergeBlockReason: 'PlatformRejection',
};
}