/
index.spec.ts
152 lines (140 loc) · 5.82 KB
/
index.spec.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
import { mock } from 'jest-mock-extended';
import { Fixtures } from '../../../../../test/fixtures';
import {
RenovateConfig,
getConfig,
git,
mockedFunction,
partial,
platform,
scm,
} from '../../../../../test/util';
import { GlobalConfig } from '../../../../config/global';
import { logger } from '../../../../logger';
import type { Pr } from '../../../../modules/platform';
import { createConfigMigrationBranch } from './create';
import type { MigratedData } from './migrated-data';
import { rebaseMigrationBranch } from './rebase';
import { checkConfigMigrationBranch } from '.';
jest.mock('./migrated-data');
jest.mock('./rebase');
jest.mock('./create');
jest.mock('../../../../util/git');
jest.mock('../../update/branch/handle-existing');
const migratedData = Fixtures.getJson<MigratedData>('./migrated-data.json');
describe('workers/repository/config-migration/branch/index', () => {
describe('checkConfigMigrationBranch', () => {
let config: RenovateConfig;
beforeEach(() => {
GlobalConfig.set({
dryRun: null,
});
jest.resetAllMocks();
config = getConfig();
config.branchPrefix = 'some/';
});
it('Exits when Migration is not needed', async () => {
await expect(
checkConfigMigrationBranch(config, null)
).resolves.toBeNull();
expect(logger.debug).toHaveBeenCalledWith(
'checkConfigMigrationBranch() Config does not need migration'
);
});
it('Updates migration branch & refresh PR', async () => {
platform.getBranchPr.mockResolvedValue(mock<Pr>());
// platform.refreshPr is undefined as it is an optional function
// declared as: refreshPr?(number: number): Promise<void>;
platform.refreshPr = jest.fn().mockResolvedValueOnce(null);
mockedFunction(rebaseMigrationBranch).mockResolvedValueOnce('committed');
const res = await checkConfigMigrationBranch(config, migratedData);
// TODO: types (#7154)
expect(res).toBe(`${config.branchPrefix!}migrate-config`);
expect(scm.checkoutBranch).toHaveBeenCalledTimes(1);
expect(git.commitFiles).toHaveBeenCalledTimes(0);
expect(logger.debug).toHaveBeenCalledWith(
'Config Migration PR already exists'
);
});
it('Dry runs update migration branch', async () => {
GlobalConfig.set({
dryRun: 'full',
});
platform.getBranchPr.mockResolvedValueOnce(mock<Pr>());
mockedFunction(rebaseMigrationBranch).mockResolvedValueOnce('committed');
const res = await checkConfigMigrationBranch(config, migratedData);
// TODO: types (#7154)
expect(res).toBe(`${config.branchPrefix!}migrate-config`);
expect(scm.checkoutBranch).toHaveBeenCalledTimes(0);
expect(git.commitFiles).toHaveBeenCalledTimes(0);
});
it('Creates migration PR', async () => {
mockedFunction(createConfigMigrationBranch).mockResolvedValueOnce(
'committed'
);
const res = await checkConfigMigrationBranch(config, migratedData);
// TODO: types (#7154)
expect(res).toBe(`${config.branchPrefix!}migrate-config`);
expect(scm.checkoutBranch).toHaveBeenCalledTimes(1);
expect(git.commitFiles).toHaveBeenCalledTimes(0);
expect(logger.debug).toHaveBeenCalledWith('Need to create migration PR');
});
it('Dry runs create migration PR', async () => {
GlobalConfig.set({
dryRun: 'full',
});
mockedFunction(createConfigMigrationBranch).mockResolvedValueOnce(
'committed'
);
const res = await checkConfigMigrationBranch(config, migratedData);
// TODO: types (#7154)
expect(res).toBe(`${config.branchPrefix!}migrate-config`);
expect(scm.checkoutBranch).toHaveBeenCalledTimes(0);
expect(git.commitFiles).toHaveBeenCalledTimes(0);
});
describe('handle closed PR', () => {
const title = 'PR title';
const pr = partial<Pr>({ title, state: 'closed', number: 1 });
it('skips branch when there is a closed one delete it and add an ignore PR message', async () => {
platform.findPr.mockResolvedValueOnce(pr);
platform.getBranchPr.mockResolvedValue(null);
scm.branchExists.mockResolvedValueOnce(true);
const res = await checkConfigMigrationBranch(config, migratedData);
expect(res).toBeNull();
expect(scm.checkoutBranch).toHaveBeenCalledTimes(0);
expect(scm.commitAndPush).toHaveBeenCalledTimes(0);
expect(scm.deleteBranch).toHaveBeenCalledTimes(1);
expect(logger.debug).toHaveBeenCalledWith(
{ prTitle: title },
'Closed PR already exists. Skipping branch.'
);
expect(platform.ensureComment).toHaveBeenCalledTimes(1);
expect(platform.ensureComment).toHaveBeenCalledWith({
content:
'\n\nIf you accidentally closed this PR, or if you changed your mind: rename this PR to get a fresh replacement PR.',
topic: 'Renovate Ignore Notification',
number: 1,
});
});
it('dryrun: skips branch when there is a closed one and add an ignore PR message', async () => {
GlobalConfig.set({ dryRun: 'full' });
platform.findPr.mockResolvedValueOnce(pr);
platform.getBranchPr.mockResolvedValue(null);
scm.branchExists.mockResolvedValueOnce(true);
const res = await checkConfigMigrationBranch(config, migratedData);
expect(res).toBeNull();
expect(logger.info).toHaveBeenCalledWith(
`DRY-RUN: Would ensure closed PR comment in PR #${pr.number}`
);
expect(logger.info).toHaveBeenCalledWith(
'DRY-RUN: Would delete branch ' + pr.sourceBranch
);
expect(logger.debug).toHaveBeenCalledWith(
{ prTitle: title },
'Closed PR already exists. Skipping branch.'
);
expect(platform.ensureComment).toHaveBeenCalledTimes(0);
});
});
});
});