forked from renovatebot/renovate
/
index.ts
104 lines (96 loc) · 3.4 KB
/
index.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
import { GlobalConfig } from '../../../../config/global';
import type { RenovateConfig } from '../../../../config/types';
import { logger } from '../../../../logger';
import { FindPRConfig, Pr, platform } from '../../../../modules/platform';
import { ensureComment } from '../../../../modules/platform/comment';
import { scm } from '../../../../modules/platform/scm';
import { getMigrationBranchName } from '../common';
import { ConfigMigrationCommitMessageFactory } from './commit-message';
import { createConfigMigrationBranch } from './create';
import type { MigratedData } from './migrated-data';
import { rebaseMigrationBranch } from './rebase';
export async function checkConfigMigrationBranch(
config: RenovateConfig,
migratedConfigData: MigratedData | null
): Promise<string | null> {
logger.debug('checkConfigMigrationBranch()');
if (!migratedConfigData) {
logger.debug('checkConfigMigrationBranch() Config does not need migration');
return null;
}
const configMigrationBranch = getMigrationBranchName(config);
const branchPr = await migrationPrExists(configMigrationBranch); // handles open/autoClosed PRs
if (!branchPr) {
const commitMessageFactory = new ConfigMigrationCommitMessageFactory(
config,
migratedConfigData.filename
);
const prTitle = commitMessageFactory.getPrTitle();
const closedPrConfig: FindPRConfig = {
branchName: configMigrationBranch,
prTitle,
state: 'closed',
};
// handles closed PR
const closedPr = await platform.findPr(closedPrConfig);
// found closed migration PR
if (closedPr) {
logger.debug(
{ prTitle: closedPr.title },
'Closed PR already exists. Skipping branch.'
);
await handlepr(config, closedPr);
return null;
}
}
if (branchPr) {
logger.debug('Config Migration PR already exists');
await rebaseMigrationBranch(config, migratedConfigData);
if (platform.refreshPr) {
const configMigrationPr = await platform.getBranchPr(
configMigrationBranch
);
if (configMigrationPr) {
await platform.refreshPr(configMigrationPr.number);
}
}
} else {
logger.debug('Config Migration PR does not exist');
logger.debug('Need to create migration PR');
await createConfigMigrationBranch(config, migratedConfigData);
}
if (!GlobalConfig.get('dryRun')) {
await scm.checkoutBranch(configMigrationBranch);
}
return configMigrationBranch;
}
export async function migrationPrExists(branchName: string): Promise<boolean> {
return !!(await platform.getBranchPr(branchName));
}
async function handlepr(config: RenovateConfig, pr: Pr): Promise<void> {
if (
pr.state === 'closed' &&
!config.suppressNotifications!.includes('prIgnoreNotification')
) {
if (GlobalConfig.get('dryRun')) {
logger.info(
`DRY-RUN: Would ensure closed PR comment in PR #${pr.number}`
);
} else {
const content =
'\n\nIf you accidentally closed this PR, or if you changed your mind: rename this PR to get a fresh replacement PR.';
await ensureComment({
number: pr.number,
topic: 'Renovate Ignore Notification',
content,
});
}
if (await scm.branchExists(pr.sourceBranch)) {
if (GlobalConfig.get('dryRun')) {
logger.info('DRY-RUN: Would delete branch ' + pr.sourceBranch);
} else {
await scm.deleteBranch(pr.sourceBranch);
}
}
}
}