/
index.ts
665 lines (646 loc) · 24.2 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
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
import is from '@sindresorhus/is';
import { DateTime } from 'luxon';
import minimatch from 'minimatch';
import { RenovateConfig } from '../../config';
import {
MANAGER_LOCKFILE_ERROR,
PLATFORM_AUTHENTICATION_ERROR,
PLATFORM_BAD_CREDENTIALS,
PLATFORM_INTEGRATION_UNAUTHORIZED,
PLATFORM_RATE_LIMIT_EXCEEDED,
REPOSITORY_CHANGED,
SYSTEM_INSUFFICIENT_DISK_SPACE,
WORKER_FILE_UPDATE_FAILED,
} from '../../constants/error-messages';
import {
PR_STATE_CLOSED,
PR_STATE_MERGED,
PR_STATE_OPEN,
} from '../../constants/pull-requests';
import { logger } from '../../logger';
import { getAdditionalFiles } from '../../manager/npm/post-update';
import { platform } from '../../platform';
import { BranchStatus } from '../../types';
import { ExternalHostError } from '../../types/errors/external-host-error';
import { emojify } from '../../util/emoji';
import { exec } from '../../util/exec';
import { readLocalFile, writeLocalFile } from '../../util/fs';
import { regEx } from '../../util/regex';
import { BranchConfig, PrResult, ProcessBranchResult } from '../common';
import { checkAutoMerge, ensurePr } from '../pr';
import { tryBranchAutomerge } from './automerge';
import { prAlreadyExisted } from './check-existing';
import { commitFilesToBranch } from './commit';
import { getUpdatedPackageFiles } from './get-updated';
import { shouldReuseExistingBranch } from './reuse';
import { isScheduledNow } from './schedule';
import { setStability, setUnpublishable } from './status-checks';
// TODO: proper typings
function rebaseCheck(config: RenovateConfig, branchPr: any): boolean {
const titleRebase = branchPr.title && branchPr.title.startsWith('rebase!');
const labelRebase =
branchPr.labels && branchPr.labels.includes(config.rebaseLabel);
const prRebaseChecked =
branchPr.body && branchPr.body.includes(`- [x] <!-- rebase-check -->`);
return titleRebase || labelRebase || prRebaseChecked;
}
export async function processBranch(
branchConfig: BranchConfig,
prHourlyLimitReached?: boolean
): Promise<ProcessBranchResult> {
const config: BranchConfig = { ...branchConfig };
const dependencies = config.upgrades
.map((upgrade) => upgrade.depName)
.filter((v) => v) // remove nulls (happens for lock file maintenance)
.filter((value, i, list) => list.indexOf(value) === i); // remove duplicates
logger.debug(
{ dependencies },
`processBranch with ${branchConfig.upgrades.length} upgrades`
);
logger.trace({ config }, 'branch config');
await platform.setBaseBranch(config.baseBranch);
const branchExists = await platform.branchExists(config.branchName);
const branchPr = await platform.getBranchPr(config.branchName);
logger.debug(`branchExists=${branchExists}`);
const masterIssueCheck = (config.masterIssueChecks || {})[config.branchName];
// istanbul ignore if
if (masterIssueCheck) {
logger.debug(
'Branch has been checked in master issue: ' + masterIssueCheck
);
}
if (branchPr) {
config.rebaseRequested = rebaseCheck(config, branchPr);
logger.debug(`Branch pr rebase requested: ${config.rebaseRequested}`);
}
try {
logger.debug(`Branch has ${dependencies.length} upgrade(s)`);
// Check if branch already existed
const existingPr = branchPr ? undefined : await prAlreadyExisted(config);
if (existingPr && !masterIssueCheck) {
logger.debug(
{ prTitle: config.prTitle },
'Closed PR already exists. Skipping branch.'
);
if (existingPr.state === PR_STATE_CLOSED) {
const topic = `Renovate Ignore Notification`;
let content;
if (config.updateType === 'major') {
content = `As this PR has been closed unmerged, Renovate will ignore this upgrade and you will not receive PRs for *any* future ${config.newMajor}.x releases. However, if you upgrade to ${config.newMajor}.x manually then Renovate will then reenable updates for minor and patch updates automatically.`;
} else if (config.updateType === 'digest') {
content = `As this PR has been closed unmerged, Renovate will ignore this upgrade updateType and you will not receive PRs for *any* future ${config.depName}:${config.currentValue} digest updates. Digest updates will resume if you update the specified tag at any time.`;
} else {
content = `As this PR has been closed unmerged, Renovate will now ignore this update (${config.newValue}). You will still receive a PR once a newer version is released, so if you wish to permanently ignore this dependency, please add it to the \`ignoreDeps\` array of your renovate config.`;
}
content +=
'\n\nIf this PR was closed by mistake or you changed your mind, you can simply rename this PR and you will soon get a fresh replacement PR opened.';
if (!config.suppressNotifications.includes('prIgnoreNotification')) {
if (config.dryRun) {
logger.info(
'DRY-RUN: Would ensure closed PR comment in PR #' +
existingPr.number
);
} else {
await platform.ensureComment({
number: existingPr.number,
topic,
content,
});
}
}
if (branchExists) {
if (config.dryRun) {
logger.info('DRY-RUN: Would delete branch ' + config.branchName);
} else {
await platform.deleteBranch(config.branchName);
}
}
} else if (existingPr.state === PR_STATE_MERGED) {
logger.debug(
{ pr: existingPr.number },
'Merged PR is blocking this branch'
);
}
return 'already-existed';
}
// istanbul ignore if
if (!branchExists && config.masterIssueApproval) {
if (masterIssueCheck) {
logger.debug(`Branch ${config.branchName} is approved for creation`);
} else {
logger.debug(`Branch ${config.branchName} needs approval`);
return 'needs-approval';
}
}
if (
!branchExists &&
prHourlyLimitReached &&
!masterIssueCheck &&
!config.vulnerabilityAlert
) {
logger.debug(
'Reached PR creation limit or per run commits limit - skipping branch creation'
);
return 'pr-hourly-limit-reached';
}
if (branchExists) {
logger.debug('Checking if PR has been edited');
if (branchPr) {
logger.debug('Found existing branch PR');
if (branchPr.state !== PR_STATE_OPEN) {
logger.debug(
'PR has been closed or merged since this run started - aborting'
);
throw new Error(REPOSITORY_CHANGED);
}
const topic = 'PR has been edited';
if (
branchPr.isModified ||
(branchPr.targetBranch &&
branchPr.targetBranch !== branchConfig.baseBranch)
) {
logger.debug({ prNo: branchPr.number }, 'PR has been edited');
if (masterIssueCheck || config.rebaseRequested) {
if (config.dryRun) {
logger.info(
'DRY-RUN: Would ensure PR edited comment removal in PR #' +
branchPr.number
);
} else {
// Remove any "PR has been edited" comment only when rebasing
await platform.ensureCommentRemoval({
number: branchPr.number,
topic,
});
}
} else {
let content = emojify(
`:construction_worker: This PR has received other commits, so Renovate will stop updating it to avoid conflicts or other problems.`
);
content += ` If you wish to abandon your changes and have Renovate start over you may click the "rebase" checkbox in the PR body/description.`;
content += `\n\nIf you think this comment is in error and the branch is *not* modified, try deleting this comment. If it comes back again the next time Renovate runs, please submit an issue or seek config help.`;
if (!config.suppressNotifications.includes('prEditNotification')) {
if (config.dryRun) {
logger.info(
'DRY-RUN: ensure comment in PR #' + branchPr.number
);
} else {
await platform.ensureComment({
number: branchPr.number,
topic,
content,
});
}
}
return 'pr-edited';
}
}
}
}
// Check schedule
config.isScheduledNow = isScheduledNow(config);
if (!config.isScheduledNow && !masterIssueCheck) {
if (!branchExists) {
logger.debug('Skipping branch creation as not within schedule');
return 'not-scheduled';
}
if (config.updateNotScheduled === false && !config.rebaseRequested) {
logger.debug('Skipping branch update as not within schedule');
return 'not-scheduled';
}
// istanbul ignore if
if (!branchPr) {
logger.debug('Skipping PR creation out of schedule');
return 'not-scheduled';
}
logger.debug(
'Branch + PR exists but is not scheduled -- will update if necessary'
);
}
if (
config.updateType !== 'lockFileMaintenance' &&
config.unpublishSafe &&
config.canBeUnpublished &&
(config.prCreation === 'not-pending' ||
/* istanbul ignore next */ config.prCreation === 'status-success')
) {
logger.debug(
'Skipping branch creation due to unpublishSafe + status checks'
);
return 'pending';
}
if (
config.upgrades.some(
(upgrade) => upgrade.stabilityDays && upgrade.releaseTimestamp
)
) {
// Only set a stability status check if one or more of the updates contain
// both a stabilityDays setting and a releaseTimestamp
config.stabilityStatus = BranchStatus.green;
// Default to 'success' but set 'pending' if any update is pending
const oneDay = 24 * 60 * 60 * 1000;
for (const upgrade of config.upgrades) {
if (upgrade.stabilityDays && upgrade.releaseTimestamp) {
const daysElapsed = Math.floor(
(new Date().getTime() -
new Date(upgrade.releaseTimestamp).getTime()) /
oneDay
);
if (!masterIssueCheck && daysElapsed < upgrade.stabilityDays) {
logger.debug(
{
depName: upgrade.depName,
daysElapsed,
stabilityDays: upgrade.stabilityDays,
},
'Update has not passed stability days'
);
config.stabilityStatus = BranchStatus.yellow;
}
}
}
// Don't create a branch if we know it will be status 'pending'
if (
!masterIssueCheck &&
!branchExists &&
config.stabilityStatus === BranchStatus.yellow &&
['not-pending', 'status-success'].includes(config.prCreation)
) {
logger.debug('Skipping branch creation due to stability days not met');
return 'pending';
}
}
// istanbul ignore if
if (masterIssueCheck === 'rebase' || config.masterIssueRebaseAllOpen) {
logger.debug('Manual rebase requested via master issue');
config.reuseExistingBranch = false;
} else {
Object.assign(config, await shouldReuseExistingBranch(config));
}
logger.debug(`Using reuseExistingBranch: ${config.reuseExistingBranch}`);
const res = await getUpdatedPackageFiles(config);
// istanbul ignore if
if (res.artifactErrors && config.artifactErrors) {
res.artifactErrors = config.artifactErrors.concat(res.artifactErrors);
}
Object.assign(config, res);
if (config.updatedPackageFiles && config.updatedPackageFiles.length) {
logger.debug(
`Updated ${config.updatedPackageFiles.length} package files`
);
} else {
logger.debug('No package files need updating');
}
const additionalFiles = await getAdditionalFiles(
config,
branchConfig.packageFiles
);
config.artifactErrors = (config.artifactErrors || []).concat(
additionalFiles.artifactErrors
);
config.updatedArtifacts = (config.updatedArtifacts || []).concat(
additionalFiles.updatedArtifacts
);
if (config.updatedArtifacts && config.updatedArtifacts.length) {
logger.debug(
{
updatedArtifacts: config.updatedArtifacts.map((f) =>
f.name === '|delete|' ? `${f.contents} (delete)` : f.name
),
},
`Updated ${config.updatedArtifacts.length} lock files`
);
} else {
logger.debug('No updated lock files in branch');
}
if (
/* Only run post-upgrade tasks if there are changes to package files... */
(config.updatedPackageFiles?.length > 0 ||
/* ... or changes to artifacts */
config.updatedArtifacts?.length > 0) &&
global.trustLevel === 'high' &&
is.nonEmptyArray(config.allowedPostUpgradeCommands)
) {
logger.debug(
{
tasks: config.postUpgradeTasks,
allowedCommands: config.allowedPostUpgradeCommands,
},
'Checking for post-upgrade tasks'
);
const commands = config.postUpgradeTasks.commands || [];
const fileFilters = config.postUpgradeTasks.fileFilters || [];
if (is.nonEmptyArray(commands)) {
// Persist updated files in file system so any executed commands can see them
for (const file of config.updatedPackageFiles.concat(
config.updatedArtifacts
)) {
if (file.name !== '|delete|') {
let contents;
if (typeof file.contents === 'string') {
contents = Buffer.from(file.contents);
} else {
contents = file.contents;
}
await writeLocalFile(file.name, contents);
}
}
for (const cmd of commands) {
if (
!config.allowedPostUpgradeCommands.some((pattern) =>
regEx(pattern).test(cmd)
)
) {
logger.warn(
{
cmd,
allowedPostUpgradeCommands: config.allowedPostUpgradeCommands,
},
'Post-upgrade task did not match any on allowed list'
);
} else {
logger.debug({ cmd }, 'Executing post-upgrade task');
const execResult = await exec(cmd, {
cwd: config.localDir,
});
logger.debug({ cmd, ...execResult }, 'Executed post-upgrade task');
}
}
const status = await platform.getRepoStatus();
for (const relativePath of status.modified.concat(status.not_added)) {
for (const pattern of fileFilters) {
if (minimatch(relativePath, pattern)) {
logger.debug(
{ file: relativePath, pattern },
'Post-upgrade file saved'
);
const existingContent = await readLocalFile(relativePath);
config.updatedArtifacts.push({
name: relativePath,
contents: existingContent,
});
}
}
}
for (const relativePath of status.deleted || []) {
for (const pattern of fileFilters) {
if (minimatch(relativePath, pattern)) {
logger.debug(
{ file: relativePath, pattern },
'Post-upgrade file removed'
);
config.updatedArtifacts.push({
name: '|delete|',
contents: relativePath,
});
}
}
}
}
}
if (config.artifactErrors && config.artifactErrors.length) {
if (config.releaseTimestamp) {
logger.debug(`Branch timestamp: ` + config.releaseTimestamp);
const releaseTimestamp = DateTime.fromISO(config.releaseTimestamp);
if (releaseTimestamp.plus({ days: 1 }) < DateTime.local()) {
logger.debug(
'PR is older than a day, raise PR with lock file errors'
);
} else if (branchExists) {
logger.debug(
'PR is less than a day old but branchExists so updating anyway'
);
} else {
logger.debug('PR is less than a day old - raise error instead of PR');
throw new Error(MANAGER_LOCKFILE_ERROR);
}
} else {
logger.debug('PR has no releaseTimestamp');
}
}
config.forceCommit =
!!masterIssueCheck || config.rebaseRequested || branchPr?.isConflicted;
const commitHash = await commitFilesToBranch(config);
if (!commitHash && !branchExists) {
return 'no-work';
}
if (commitHash) {
const action = branchExists ? 'updated' : 'created';
logger.info({ commitHash }, `Branch ${action}`);
}
// Set branch statuses
await setStability(config);
await setUnpublishable(config);
// break if we pushed a new commit because status check are pretty sure pending but maybe not reported yet
if (
!masterIssueCheck &&
commitHash &&
(config.requiredStatusChecks?.length || config.prCreation !== 'immediate')
) {
logger.debug({ commitHash }, `Branch status pending`);
return 'pending';
}
// Try to automerge branch and finish if successful, but only if branch already existed before this run
if (branchExists || !config.requiredStatusChecks) {
const mergeStatus = await tryBranchAutomerge(config);
logger.debug(`mergeStatus=${mergeStatus}`);
if (mergeStatus === 'automerged') {
logger.debug('Branch is automerged - returning');
return 'automerged';
}
if (
mergeStatus === 'automerge aborted - PR exists' ||
mergeStatus === 'branch status error' ||
mergeStatus === 'failed'
) {
logger.debug({ mergeStatus }, 'Branch automerge not possible');
config.forcePr = true;
config.branchAutomergeFailureMessage = mergeStatus;
}
}
} catch (err) /* istanbul ignore next */ {
if (err.statusCode === 404) {
throw new Error(REPOSITORY_CHANGED);
}
if (err.message === PLATFORM_RATE_LIMIT_EXCEEDED) {
logger.debug('Passing rate-limit-exceeded error up');
throw err;
}
if (err.message === REPOSITORY_CHANGED) {
logger.debug('Passing repository-changed error up');
throw err;
}
if (
err.message &&
err.message.startsWith('remote: Invalid username or password')
) {
logger.debug('Throwing bad credentials');
throw new Error(PLATFORM_BAD_CREDENTIALS);
}
if (
err.message &&
err.message.startsWith(
'ssh_exchange_identification: Connection closed by remote host'
)
) {
logger.debug('Throwing bad credentials');
throw new Error(PLATFORM_BAD_CREDENTIALS);
}
if (err.message === PLATFORM_BAD_CREDENTIALS) {
logger.debug('Passing bad-credentials error up');
throw err;
}
if (err.message === PLATFORM_INTEGRATION_UNAUTHORIZED) {
logger.debug('Passing integration-unauthorized error up');
throw err;
}
if (err.message === MANAGER_LOCKFILE_ERROR) {
logger.debug('Passing lockfile-error up');
throw err;
}
if (err.message && err.message.includes('space left on device')) {
throw new Error(SYSTEM_INSUFFICIENT_DISK_SPACE);
}
if (err.message === SYSTEM_INSUFFICIENT_DISK_SPACE) {
logger.debug('Passing disk-space error up');
throw err;
}
if (err.message.startsWith('Resource not accessible by integration')) {
logger.debug('Passing 403 error up');
throw err;
}
if (err.message === WORKER_FILE_UPDATE_FAILED) {
logger.warn('Error updating branch: update failure');
} else if (err.message.startsWith('bundler-')) {
// we have already warned inside the bundler artifacts error handling, so just return
return 'error';
} else if (
err.messagee &&
err.message.includes('fatal: Authentication failed')
) {
throw new Error(PLATFORM_AUTHENTICATION_ERROR);
} else if (!(err instanceof ExternalHostError)) {
logger.error({ err }, `Error updating branch: ${err.message}`);
}
// Don't throw here - we don't want to stop the other renovations
return 'error';
}
try {
logger.debug('Ensuring PR');
logger.debug(
`There are ${config.errors.length} errors and ${config.warnings.length} warnings`
);
const { prResult: result, pr } = await ensurePr(config);
// TODO: ensurePr should check for automerge itself
if (result === PrResult.AwaitingApproval) {
return 'needs-pr-approval';
}
if (
result === PrResult.AwaitingGreenBranch ||
result === PrResult.AwaitingNotPending
) {
return 'pending';
}
if (pr) {
const topic = emojify(':warning: Artifact update problem');
if (config.artifactErrors && config.artifactErrors.length) {
logger.warn(
{ artifactErrors: config.artifactErrors },
'artifactErrors'
);
let content = `Renovate failed to update `;
content +=
config.artifactErrors.length > 1 ? 'artifacts' : 'an artifact';
content +=
' related to this branch. You probably do not want to merge this PR as-is.';
content += emojify(
`\n\n:recycle: Renovate will retry this branch, including artifacts, only when one of the following happens:\n\n`
);
content +=
' - any of the package files in this branch needs updating, or \n';
content += ' - the branch becomes conflicted, or\n';
content +=
' - you check the rebase/retry checkbox if found above, or\n';
content +=
' - you rename this PR\'s title to start with "rebase!" to trigger it manually';
content += '\n\nThe artifact failure details are included below:\n\n';
config.artifactErrors.forEach((error) => {
content += `##### File name: ${error.lockFile}\n\n`;
content += `\`\`\`\n${error.stderr}\n\`\`\`\n\n`;
});
if (
!(
config.suppressNotifications.includes('artifactErrors') ||
config.suppressNotifications.includes('lockFileErrors')
)
) {
if (config.dryRun) {
logger.info(
'DRY-RUN: Would ensure lock file error comment in PR #' +
pr.number
);
} else {
await platform.ensureComment({
number: pr.number,
topic,
content,
});
}
}
const context = `renovate/artifacts`;
const description = 'Artifact file update failure';
const state = BranchStatus.red;
const existingState = await platform.getBranchStatusCheck(
config.branchName,
context
);
// Check if state needs setting
if (existingState !== state) {
logger.debug(`Updating status check state to failed`);
if (config.dryRun) {
logger.info(
'DRY-RUN: Would set branch status in ' + config.branchName
);
} else {
await platform.setBranchStatus({
branchName: config.branchName,
context,
description,
state,
});
}
}
} else {
if (config.updatedArtifacts && config.updatedArtifacts.length) {
// istanbul ignore if
if (config.dryRun) {
logger.info(
'DRY-RUN: Would ensure comment removal in PR #' + pr.number
);
} else {
// Remove artifacts error comment only if this run has successfully updated artifacts
await platform.ensureCommentRemoval({ number: pr.number, topic });
}
}
const prAutomerged = await checkAutoMerge(pr, config);
if (prAutomerged) {
return 'automerged';
}
}
}
} catch (err) /* istanbul ignore next */ {
if (
err instanceof ExternalHostError ||
[PLATFORM_RATE_LIMIT_EXCEEDED, REPOSITORY_CHANGED].includes(err.message)
) {
logger.debug('Passing PR error up');
throw err;
}
// Otherwise don't throw here - we don't want to stop the other renovations
logger.error({ err }, `Error ensuring PR: ${err.message}`);
}
if (!branchExists) {
return 'pr-created';
}
return 'done';
}