-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
automerge.ts
129 lines (126 loc) · 3.66 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
import { getGlobalConfig } from '../../config/global';
import { logger } from '../../logger';
import { Pr, platform } from '../../platform';
import { BranchStatus } from '../../types';
import { deleteBranch, isBranchModified } from '../../util/git';
import { BranchConfig } from '../types';
export enum PrAutomergeBlockReason {
BranchModified = 'BranchModified',
BranchNotGreen = 'BranchNotGreen',
Conflicted = 'Conflicted',
DryRun = 'DryRun',
PlatformNotReady = 'PlatformNotReady',
PlatformRejection = 'PlatformRejection',
}
export type 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,
automergeComment,
ignoreTests,
rebaseRequested,
} = config;
// Return if PR not ready for automerge
if (pr.isConflicted) {
logger.debug('PR is conflicted');
return {
automerged: false,
prAutomergeBlockReason: PrAutomergeBlockReason.Conflicted,
};
}
if (!ignoreTests && pr.canMerge !== true) {
logger.debug(
{ canMergeReason: pr.canMergeReason },
'PR is not ready for merge'
);
return {
automerged: false,
prAutomergeBlockReason: PrAutomergeBlockReason.PlatformNotReady,
};
}
const branchStatus = await platform.getBranchStatus(branchName, ignoreTests);
if (branchStatus !== BranchStatus.green) {
logger.debug(
`PR is not ready for merge (branch status is ${branchStatus})`
);
return {
automerged: false,
prAutomergeBlockReason: PrAutomergeBlockReason.BranchNotGreen,
};
}
// Check if it's been touched
if (await isBranchModified(branchName)) {
logger.debug('PR is ready for automerge but has been modified');
return {
automerged: false,
prAutomergeBlockReason: PrAutomergeBlockReason.BranchModified,
};
}
if (automergeType === 'pr-comment') {
logger.debug(`Applying automerge comment: ${automergeComment}`);
// istanbul ignore if
if (getGlobalConfig().dryRun) {
logger.info(
`DRY-RUN: Would add PR automerge comment to PR #${pr.number}`
);
return {
automerged: false,
prAutomergeBlockReason: PrAutomergeBlockReason.DryRun,
};
}
if (rebaseRequested) {
await platform.ensureCommentRemoval({
number: pr.number,
content: automergeComment,
});
}
await platform.ensureComment({
number: pr.number,
topic: null,
content: automergeComment,
});
return { automerged: true, branchRemoved: false };
}
// Let's merge this
// istanbul ignore if
if (getGlobalConfig().dryRun) {
logger.info(
`DRY-RUN: Would merge PR #${pr.number} with strategy "${automergeStrategy}"`
);
return {
automerged: false,
prAutomergeBlockReason: PrAutomergeBlockReason.DryRun,
};
}
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');
let branchRemoved = false;
try {
await 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: PrAutomergeBlockReason.PlatformRejection,
};
}