/
index.spec.ts
403 lines (365 loc) · 14.6 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
import { mock } from 'jest-mock-extended';
import {
RenovateConfig,
fs,
getConfig,
git,
mocked,
platform,
scm,
} from '../../../../../test/util';
import { configFileNames } from '../../../../config/app-strings';
import { GlobalConfig } from '../../../../config/global';
import {
REPOSITORY_FORKED,
REPOSITORY_NO_PACKAGE_FILES,
} from '../../../../constants/error-messages';
import { logger } from '../../../../logger';
import type { Pr } from '../../../../modules/platform';
import * as memCache from '../../../../util/cache/memory';
import * as _cache from '../../../../util/cache/repository';
import type { RepoCacheData } from '../../../../util/cache/repository/types';
import type { FileAddition } from '../../../../util/git/types';
import { OnboardingState } from '../common';
import * as _config from './config';
import * as _onboardingCache from './onboarding-branch-cache';
import { checkOnboardingBranch } from '.';
const configModule: any = _config;
jest.mock('../../../../util/cache/repository');
jest.mock('../../../../util/fs');
jest.mock('../../../../util/git');
jest.mock('./config');
jest.mock('./onboarding-branch-cache');
const cache = mocked(_cache);
const onboardingCache = mocked(_onboardingCache);
describe('workers/repository/onboarding/branch/index', () => {
describe('checkOnboardingBranch', () => {
let config: RenovateConfig;
beforeEach(() => {
memCache.init();
jest.resetAllMocks();
config = getConfig();
config.repository = 'some/repo';
OnboardingState.prUpdateRequested = false;
scm.getFileList.mockResolvedValue([]);
cache.getCache.mockReturnValue({});
});
it('throws if no package files', async () => {
await expect(checkOnboardingBranch(config)).rejects.toThrow(
REPOSITORY_NO_PACKAGE_FILES
);
});
it("doesn't throw if there are no package files and onboardingNoDeps config option is set", async () => {
config.onboardingNoDeps = true;
await expect(checkOnboardingBranch(config)).resolves.not.toThrow(
REPOSITORY_NO_PACKAGE_FILES
);
});
it('throws if fork', async () => {
config.isFork = true;
await expect(checkOnboardingBranch(config)).rejects.toThrow(
REPOSITORY_FORKED
);
});
it.each`
checkboxEnabled | expected
${true} | ${true}
${false} | ${false}
`(
'has default onboarding config' +
'(config.onboardingRebaseCheckbox="$checkboxEnabled")',
async ({ checkboxEnabled, expected }) => {
config.onboardingRebaseCheckbox = checkboxEnabled;
configModule.getOnboardingConfig.mockResolvedValue(
config.onboardingConfig
);
configModule.getOnboardingConfigContents.mockResolvedValue(
'{\n' +
' "$schema": "https://docs.renovatebot.com/renovate-schema.json"\n' +
'}\n'
);
scm.getFileList.mockResolvedValue(['package.json']);
fs.readLocalFile.mockResolvedValue('{}');
await checkOnboardingBranch(config);
const file = scm.commitAndPush.mock.calls[0][0]
.files[0] as FileAddition;
const contents = file.contents?.toString();
expect(contents).toBeJsonString();
// TODO #7154
expect(JSON.parse(contents!)).toEqual({
$schema: 'https://docs.renovatebot.com/renovate-schema.json',
});
expect(OnboardingState.prUpdateRequested).toBe(expected);
}
);
it('uses discovered onboarding config', async () => {
configModule.getOnboardingConfig.mockResolvedValue({
onboardingBranch: 'test',
});
configModule.getOnboardingConfigContents.mockResolvedValue(
'{\n' +
' "$schema": "https://docs.renovatebot.com/renovate-schema.json",\n' +
' "extends": ["some/renovate-config"]\n' +
'}\n'
);
scm.getFileList.mockResolvedValue(['package.json']);
fs.readLocalFile.mockResolvedValue('{}');
await checkOnboardingBranch(config);
const expectConfig = {
...config,
onboardingBranch: 'test',
renovateJsonPresent: true,
warnings: [],
};
delete expectConfig.extends;
delete expectConfig.ignorePresets;
expect(configModule.getOnboardingConfigContents).toHaveBeenCalledWith(
expectConfig,
configFileNames[0]
);
const file = scm.commitAndPush.mock.calls[0][0].files[0] as FileAddition;
const contents = file.contents?.toString();
expect(contents).toBeJsonString();
// TODO #7154
expect(JSON.parse(contents!)).toEqual({
$schema: 'https://docs.renovatebot.com/renovate-schema.json',
extends: ['some/renovate-config'],
});
});
it('handles skipped onboarding combined with requireConfig = optional', async () => {
config.requireConfig = 'optional';
config.onboarding = false;
const res = await checkOnboardingBranch(config);
expect(res.repoIsOnboarded).toBeTrue();
});
it('handles skipped onboarding, requireConfig=required, and a config file', async () => {
config.requireConfig = 'required';
config.onboarding = false;
scm.getFileList.mockResolvedValueOnce(['renovate.json']);
const res = await checkOnboardingBranch(config);
expect(res.repoIsOnboarded).toBeTrue();
});
it('handles skipped onboarding, requireConfig=ignored', async () => {
config.requireConfig = 'ignored';
config.onboarding = false;
const res = await checkOnboardingBranch(config);
expect(res.repoIsOnboarded).toBeTrue();
});
it('handles skipped onboarding, requireConfig=required, and no config file', async () => {
config.requireConfig = 'required';
config.onboarding = false;
scm.getFileList.mockResolvedValueOnce(['package.json']);
fs.readLocalFile.mockResolvedValueOnce('{}');
const onboardingResult = checkOnboardingBranch(config);
await expect(onboardingResult).rejects.toThrow('disabled');
});
it('detects repo is onboarded via file', async () => {
scm.getFileList.mockResolvedValueOnce(['renovate.json']);
const res = await checkOnboardingBranch(config);
expect(res.repoIsOnboarded).toBeTrue();
expect(onboardingCache.deleteOnboardingCache).toHaveBeenCalledTimes(1); // removes onboarding cache when repo is onboarded
});
it('handles removed cached file name', async () => {
cache.getCache.mockReturnValue({ configFileName: '.renovaterc' });
scm.getFileList.mockResolvedValueOnce(['renovate.json']);
const res = await checkOnboardingBranch(config);
expect(res.repoIsOnboarded).toBeTrue();
});
it('handles cached file name', async () => {
cache.getCache.mockReturnValue({ configFileName: '.renovaterc' });
platform.getJsonFile.mockResolvedValueOnce({});
const res = await checkOnboardingBranch(config);
expect(logger.debug).toHaveBeenCalledWith(
'Checking cached config file name'
);
expect(logger.debug).toHaveBeenCalledWith(
'Existing config file confirmed'
);
expect(logger.debug).toHaveBeenCalledWith(
{
fileName: '.renovaterc',
config: {},
},
'Repository config'
);
expect(res.repoIsOnboarded).toBeTrue();
});
it('handles cached package.json', async () => {
cache.getCache.mockReturnValue({ configFileName: 'package.json' });
platform.getJsonFile.mockResolvedValueOnce({ renovate: {} });
fs.readLocalFile.mockResolvedValueOnce('{}');
const res = await checkOnboardingBranch(config);
expect(logger.debug).toHaveBeenCalledWith(
'Checking cached config file name'
);
expect(logger.debug).toHaveBeenCalledWith(
'Existing config file confirmed'
);
expect(logger.debug).toHaveBeenCalledWith(
{
fileName: 'package.json',
config: {
renovate: {},
},
},
'Repository config'
);
expect(res.repoIsOnboarded).toBeTrue();
});
it('detects repo is onboarded via package.json config', async () => {
scm.getFileList.mockResolvedValueOnce(['package.json']);
fs.readLocalFile.mockResolvedValueOnce('{"renovate":{}}');
const res = await checkOnboardingBranch(config);
expect(res.repoIsOnboarded).toBeTrue();
});
it('detects repo is onboarded via PR', async () => {
config.requireConfig = 'optional';
platform.findPr.mockResolvedValueOnce(mock<Pr>());
const res = await checkOnboardingBranch(config);
expect(res.repoIsOnboarded).toBeTrue();
});
it('throws if no required config', async () => {
config.requireConfig = 'required';
platform.findPr.mockResolvedValue(mock<Pr>());
platform.getPrList.mockResolvedValueOnce([
{
...mock<Pr>(),
sourceBranch: 'renovate/something',
state: 'open',
},
]);
await expect(checkOnboardingBranch(config)).rejects.toThrow();
});
it('processes onboarding branch', async () => {
scm.getFileList.mockResolvedValue(['package.json']);
platform.findPr.mockResolvedValue(null);
platform.getBranchPr.mockResolvedValueOnce(mock<Pr>());
const res = await checkOnboardingBranch(config);
expect(res.repoIsOnboarded).toBeFalse();
expect(res.branchList).toEqual(['renovate/configure']);
expect(git.mergeBranch).toHaveBeenCalledOnce();
expect(scm.commitAndPush).toHaveBeenCalledTimes(0);
});
it('processes modified onboarding branch and invalidates extract cache', async () => {
const dummyCache = {
scan: {
master: {
sha: 'base_sha',
configHash: 'hash',
packageFiles: {},
extractionFingerprints: {},
},
},
} satisfies RepoCacheData;
cache.getCache.mockReturnValue(dummyCache);
platform.findPr.mockResolvedValue(null);
platform.getBranchPr.mockResolvedValueOnce(mock<Pr>());
git.getBranchCommit
.mockReturnValueOnce('default-sha')
.mockReturnValueOnce('new-onboarding-sha');
onboardingCache.isOnboardingBranchModified.mockResolvedValueOnce(true);
onboardingCache.hasOnboardingBranchChanged.mockReturnValueOnce(true);
onboardingCache.isOnboardingBranchConflicted.mockResolvedValueOnce(false);
config.baseBranch = 'master';
await checkOnboardingBranch(config);
expect(git.mergeBranch).toHaveBeenCalledOnce();
expect(onboardingCache.setOnboardingCache).toHaveBeenCalledWith(
'default-sha',
'new-onboarding-sha',
false,
true
);
expect(dummyCache).toMatchObject({
scan: {},
});
});
it('skips processing conflicted onboarding branch', async () => {
scm.getFileList.mockResolvedValue(['package.json']);
platform.findPr.mockResolvedValue(null);
platform.getBranchPr.mockResolvedValueOnce(mock<Pr>());
platform.getBranchPr.mockResolvedValueOnce(mock<Pr>());
git.getBranchCommit
.mockReturnValueOnce('default-sha')
.mockReturnValueOnce('onboarding-sha');
onboardingCache.isOnboardingBranchModified.mockResolvedValueOnce(true);
onboardingCache.hasOnboardingBranchChanged.mockReturnValueOnce(true);
onboardingCache.isOnboardingBranchConflicted.mockResolvedValueOnce(true);
await checkOnboardingBranch(config);
expect(git.mergeBranch).not.toHaveBeenCalled();
expect(onboardingCache.setOnboardingCache).toHaveBeenCalledWith(
'default-sha',
'onboarding-sha',
true,
true
);
});
it('sets onboarding cache for existing onboarding branch', async () => {
scm.getFileList.mockResolvedValue(['package.json']);
platform.findPr.mockResolvedValue(null);
platform.getBranchPr.mockResolvedValueOnce(mock<Pr>());
git.getBranchCommit
.mockReturnValueOnce('default-sha')
.mockReturnValueOnce('onboarding-sha');
onboardingCache.isOnboardingBranchModified.mockResolvedValueOnce(false);
await checkOnboardingBranch(config);
expect(git.mergeBranch).toHaveBeenCalled();
expect(onboardingCache.setOnboardingCache).toHaveBeenCalledWith(
'default-sha',
'onboarding-sha',
false,
false
);
});
describe('tests onboarding rebase/retry checkbox handling', () => {
beforeEach(() => {
GlobalConfig.set({ platform: 'github' });
config.onboardingRebaseCheckbox = true;
OnboardingState.prUpdateRequested = false;
scm.getFileList.mockResolvedValueOnce(['package.json']);
platform.findPr.mockResolvedValueOnce(null);
});
it('detects unsupported platfom', async () => {
const pl = 'bitbucket';
GlobalConfig.set({ platform: pl });
platform.getBranchPr.mockResolvedValueOnce(mock<Pr>({}));
await checkOnboardingBranch(config);
expect(logger.trace).toHaveBeenCalledWith(
`Platform '${pl}' does not support extended markdown`
);
expect(OnboardingState.prUpdateRequested).toBeTrue();
expect(git.mergeBranch).toHaveBeenCalledOnce();
expect(scm.commitAndPush).toHaveBeenCalledTimes(0);
});
it('detects missing rebase checkbox', async () => {
const pr = { bodyStruct: { rebaseRequested: undefined } };
platform.getBranchPr.mockResolvedValueOnce(mock<Pr>(pr));
await checkOnboardingBranch(config);
expect(logger.debug).toHaveBeenCalledWith(
`No rebase checkbox was found in the onboarding PR`
);
expect(OnboardingState.prUpdateRequested).toBeTrue();
expect(git.mergeBranch).toHaveBeenCalledOnce();
expect(scm.commitAndPush).toHaveBeenCalledTimes(0);
});
it('detects manual pr update requested', async () => {
const pr = { bodyStruct: { rebaseRequested: true } };
platform.getBranchPr.mockResolvedValueOnce(mock<Pr>(pr));
await checkOnboardingBranch(config);
expect(logger.debug).toHaveBeenCalledWith(
`Manual onboarding PR update requested`
);
expect(OnboardingState.prUpdateRequested).toBeTrue();
expect(git.mergeBranch).toHaveBeenCalledOnce();
expect(scm.commitAndPush).toHaveBeenCalledTimes(0);
});
it('handles unchecked rebase checkbox', async () => {
const pr = { bodyStruct: { rebaseRequested: false } };
platform.getBranchPr.mockResolvedValueOnce(mock<Pr>(pr));
await checkOnboardingBranch(config);
expect(OnboardingState.prUpdateRequested).toBeFalse();
expect(git.mergeBranch).toHaveBeenCalledOnce();
expect(scm.commitAndPush).toHaveBeenCalledTimes(0);
});
});
});
});