-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
rebase.ts
69 lines (64 loc) · 2.15 KB
/
rebase.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
import { GlobalConfig } from '../../../../config/global';
import type { RenovateConfig } from '../../../../config/types';
import { logger } from '../../../../logger';
import { commitAndPush } from '../../../../modules/platform/commit';
import {
checkoutBranch,
getFile,
isBranchModified,
} from '../../../../util/git';
import { quickStringify } from '../../../../util/stringify';
import { getMigrationBranchName } from '../common';
import { ConfigMigrationCommitMessageFactory } from './commit-message';
import { MigratedDataFactory } from './migrated-data';
import type { MigratedData } from './migrated-data';
export async function rebaseMigrationBranch(
config: RenovateConfig,
migratedConfigData: MigratedData
): Promise<string | null> {
logger.debug('Checking if migration branch needs rebasing');
const branchName = getMigrationBranchName(config);
if (await isBranchModified(branchName)) {
logger.debug('Migration branch has been edited and cannot be rebased');
return null;
}
const configFileName = migratedConfigData.filename;
let contents = migratedConfigData.content;
const existingContents = await getFile(configFileName, branchName);
if (stripWhitespaces(contents) === stripWhitespaces(existingContents)) {
logger.debug('Migration branch is up to date');
return null;
}
logger.debug('Rebasing migration branch');
if (GlobalConfig.get('dryRun')) {
logger.info('DRY-RUN: Would rebase files in migration branch');
return null;
}
const commitMessageFactory = new ConfigMigrationCommitMessageFactory(
config,
configFileName
);
const commitMessage = commitMessageFactory.getCommitMessage();
await checkoutBranch(config.defaultBranch!);
contents = await MigratedDataFactory.applyPrettierFormatting(
migratedConfigData
);
return commitAndPush({
branchName,
files: [
{
type: 'addition',
path: configFileName,
contents,
},
],
message: commitMessage.toString(),
platformCommit: !!config.platformCommit,
});
}
function stripWhitespaces(str: string | null): string | null {
if (!str) {
return null;
}
return quickStringify(JSON.parse(str));
}