/
index.ts
502 lines (480 loc) · 16.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
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
import { getGlobalConfig } from '../../config/global';
import type { RenovateConfig } from '../../config/types';
import {
PLATFORM_INTEGRATION_UNAUTHORIZED,
PLATFORM_RATE_LIMIT_EXCEEDED,
REPOSITORY_CHANGED,
} from '../../constants/error-messages';
import { logger } from '../../logger';
import { PlatformPrOptions, Pr, platform } from '../../platform';
import { BranchStatus } from '../../types';
import { ExternalHostError } from '../../types/errors/external-host-error';
import { sampleSize } from '../../util';
import { stripEmojis } from '../../util/emoji';
import { deleteBranch, getBranchLastCommitTime } from '../../util/git';
import * as template from '../../util/template';
import { resolveBranchStatus } from '../branch/status-checks';
import { Limit, incLimitedValue, isLimitReached } from '../global/limits';
import type { BranchConfig, PrBlockedBy } from '../types';
import { getPrBody } from './body';
import { ChangeLogError } from './changelog/types';
import { codeOwnersForPr } from './code-owners';
function noWhitespaceOrHeadings(input: string): string {
return input.replace(/\r?\n|\r|\s|#/g, '');
}
function noLeadingAtSymbol(input: string): string {
return input.length && input.startsWith('@') ? input.slice(1) : input;
}
async function addCodeOwners(
assigneesOrReviewers: string[],
pr: Pr
): Promise<string[]> {
return [...new Set(assigneesOrReviewers.concat(await codeOwnersForPr(pr)))];
}
function filterUnavailableUsers(
config: RenovateConfig,
users: string[]
): Promise<string[]> {
return config.filterUnavailableUsers && platform.filterUnavailableUsers
? platform.filterUnavailableUsers(users)
: Promise.resolve(users);
}
function prepareAssigneesReviewers(
config: RenovateConfig,
usernames: string[]
): Promise<string[]> {
const normalizedUsernames = [...new Set(usernames.map(noLeadingAtSymbol))];
return filterUnavailableUsers(config, normalizedUsernames);
}
export async function addAssigneesReviewers(
config: RenovateConfig,
pr: Pr
): Promise<void> {
let assignees = config.assignees;
if (config.assigneesFromCodeOwners) {
assignees = await addCodeOwners(assignees, pr);
}
if (assignees.length > 0) {
try {
assignees = await prepareAssigneesReviewers(config, assignees);
if (config.assigneesSampleSize !== null) {
assignees = sampleSize(assignees, config.assigneesSampleSize);
}
if (assignees.length > 0) {
// istanbul ignore if
if (getGlobalConfig().dryRun) {
logger.info(`DRY-RUN: Would add assignees to PR #${pr.number}`);
} else {
await platform.addAssignees(pr.number, assignees);
logger.debug({ assignees }, 'Added assignees');
}
}
} catch (err) {
logger.debug(
{ assignees: config.assignees, err },
'Failed to add assignees'
);
}
}
let reviewers = config.reviewers;
if (config.reviewersFromCodeOwners) {
reviewers = await addCodeOwners(reviewers, pr);
}
if (config.additionalReviewers.length > 0) {
reviewers = reviewers.concat(config.additionalReviewers);
}
if (reviewers.length > 0) {
try {
reviewers = await prepareAssigneesReviewers(config, reviewers);
if (config.reviewersSampleSize !== null) {
reviewers = sampleSize(reviewers, config.reviewersSampleSize);
}
if (reviewers.length > 0) {
// istanbul ignore if
if (getGlobalConfig().dryRun) {
logger.info(`DRY-RUN: Would add reviewers to PR #${pr.number}`);
} else {
await platform.addReviewers(pr.number, reviewers);
logger.debug({ reviewers }, 'Added reviewers');
}
}
} catch (err) {
logger.debug(
{ reviewers: config.reviewers, err },
'Failed to add reviewers'
);
}
}
}
export function getPlatformPrOptions(
config: RenovateConfig & PlatformPrOptions
): PlatformPrOptions {
return {
azureAutoApprove: config.azureAutoApprove,
azureAutoComplete: config.azureAutoComplete,
azureWorkItemId: config.azureWorkItemId,
bbUseDefaultReviewers: config.bbUseDefaultReviewers,
gitLabAutomerge:
config.automerge &&
config.automergeType === 'pr' &&
config.gitLabAutomerge,
gitLabIgnoreApprovals: config.gitLabIgnoreApprovals,
};
}
export type ResultWithPr = {
pr: Pr;
prBlockedBy?: never;
};
export type ResultWithoutPr = {
pr?: never;
prBlockedBy: PrBlockedBy;
};
export type EnsurePrResult = ResultWithPr | ResultWithoutPr;
// Ensures that PR exists with matching title/body
export async function ensurePr(
prConfig: BranchConfig
): Promise<EnsurePrResult> {
const config: BranchConfig = { ...prConfig };
logger.trace({ config }, 'ensurePr');
// If there is a group, it will use the config of the first upgrade in the array
const { branchName, ignoreTests, prTitle, upgrades } = config;
const dependencyDashboardCheck = (config.dependencyDashboardChecks || {})[
config.branchName
];
// Check if existing PR exists
const existingPr = await platform.getBranchPr(branchName);
if (existingPr) {
logger.debug('Found existing PR');
}
config.upgrades = [];
if (config.artifactErrors?.length) {
logger.debug('Forcing PR because of artifact errors');
config.forcePr = true;
}
let branchStatus: BranchStatus;
// Only create a PR if a branch automerge has failed
if (
config.automerge === true &&
config.automergeType.startsWith('branch') &&
!config.forcePr
) {
branchStatus ||= await resolveBranchStatus(branchName, ignoreTests);
logger.debug(
`Branch is configured for branch automerge, branch status) is: ${branchStatus}`
);
if (
config.stabilityStatus !== BranchStatus.yellow &&
branchStatus === BranchStatus.yellow
) {
logger.debug('Checking how long this branch has been pending');
const lastCommitTime = await getBranchLastCommitTime(branchName);
const currentTime = new Date();
const millisecondsPerHour = 1000 * 60 * 60;
const elapsedHours = Math.round(
(currentTime.getTime() - lastCommitTime.getTime()) / millisecondsPerHour
);
if (elapsedHours >= config.prNotPendingHours) {
logger.debug('Branch exceeds prNotPending hours - forcing PR creation');
config.forcePr = true;
}
}
if (config.forcePr || branchStatus === BranchStatus.red) {
logger.debug(`Branch tests failed, so will create PR`);
} else {
// Branch should be automerged, so we don't want to create a PR
return { prBlockedBy: 'BranchAutomerge' };
}
}
if (config.prCreation === 'status-success') {
logger.debug('Checking branch combined status');
branchStatus ||= await resolveBranchStatus(branchName, ignoreTests);
if (branchStatus !== BranchStatus.green) {
logger.debug(`Branch status is "${branchStatus}" - not creating PR`);
return { prBlockedBy: 'AwaitingTests' };
}
logger.debug('Branch status success');
} else if (
config.prCreation === 'approval' &&
!existingPr &&
dependencyDashboardCheck !== 'approvePr'
) {
return { prBlockedBy: 'NeedsApproval' };
} else if (
config.prCreation === 'not-pending' &&
!existingPr &&
!config.forcePr
) {
logger.debug('Checking branch combined status');
branchStatus ||= await resolveBranchStatus(branchName, ignoreTests);
if (branchStatus === BranchStatus.yellow) {
logger.debug(`Branch status is "${branchStatus}" - checking timeout`);
const lastCommitTime = await getBranchLastCommitTime(branchName);
const currentTime = new Date();
const millisecondsPerHour = 1000 * 60 * 60;
const elapsedHours = Math.round(
(currentTime.getTime() - lastCommitTime.getTime()) / millisecondsPerHour
);
if (
!dependencyDashboardCheck &&
((config.stabilityStatus &&
config.stabilityStatus !== BranchStatus.yellow) ||
elapsedHours < config.prNotPendingHours)
) {
logger.debug(
`Branch is ${elapsedHours} hours old - skipping PR creation`
);
return {
prBlockedBy: 'AwaitingTests',
};
}
const prNotPendingHours = String(config.prNotPendingHours);
logger.debug(
`prNotPendingHours=${prNotPendingHours} threshold hit - creating PR`
);
}
logger.debug('Branch status success');
}
const processedUpgrades: string[] = [];
const commitRepos: string[] = [];
// Get changelog and then generate template strings
for (const upgrade of upgrades) {
const upgradeKey = `${upgrade.depType}-${upgrade.depName}-${
upgrade.manager
}-${upgrade.currentVersion || upgrade.currentValue}-${upgrade.newVersion}`;
if (processedUpgrades.includes(upgradeKey)) {
continue; // eslint-disable-line no-continue
}
processedUpgrades.push(upgradeKey);
const logJSON = upgrade.logJSON;
if (logJSON) {
if (typeof logJSON.error === 'undefined') {
if (logJSON.project) {
upgrade.repoName = logJSON.project.repository;
}
upgrade.hasReleaseNotes = logJSON.hasReleaseNotes;
upgrade.releases = [];
if (
upgrade.hasReleaseNotes &&
upgrade.repoName &&
!commitRepos.includes(upgrade.repoName)
) {
commitRepos.push(upgrade.repoName);
if (logJSON.versions) {
logJSON.versions.forEach((version) => {
const release = { ...version };
upgrade.releases.push(release);
});
}
}
} else if (logJSON.error === ChangeLogError.MissingGithubToken) {
upgrade.prBodyNotes = [
...upgrade.prBodyNotes,
[
'\n',
':warning: Release Notes retrieval for this PR were skipped because no github.com credentials were available.',
'If you are self-hosted, please see [this instruction](https://github.com/renovatebot/renovate/blob/master/docs/usage/examples/self-hosting.md#githubcom-token-for-release-notes).',
'\n',
].join('\n'),
];
}
}
config.upgrades.push(upgrade);
}
config.hasReleaseNotes = config.upgrades.some((upg) => upg.hasReleaseNotes);
const releaseNoteRepos: string[] = [];
for (const upgrade of config.upgrades) {
if (upgrade.hasReleaseNotes) {
if (releaseNoteRepos.includes(upgrade.sourceUrl)) {
logger.debug(
{ depName: upgrade.depName },
'Removing duplicate release notes'
);
upgrade.hasReleaseNotes = false;
} else {
releaseNoteRepos.push(upgrade.sourceUrl);
}
}
}
const prBody = await getPrBody(config);
try {
if (existingPr) {
logger.debug('Processing existing PR');
// istanbul ignore if
if (
!existingPr.hasAssignees &&
!existingPr.hasReviewers &&
config.automerge &&
branchStatus === BranchStatus.red
) {
logger.debug(`Setting assignees and reviewers as status checks failed`);
await addAssigneesReviewers(config, existingPr);
}
// Check if existing PR needs updating
const reviewableIndex = existingPr.body.indexOf(
'<!-- Reviewable:start -->'
);
let existingPrBody = existingPr.body;
if (reviewableIndex > -1) {
logger.debug('Stripping Reviewable content');
existingPrBody = existingPrBody.slice(0, reviewableIndex);
}
const existingPrTitle = stripEmojis(existingPr.title);
const newPrTitle = stripEmojis(prTitle);
existingPrBody = existingPrBody.trim();
if (
existingPrTitle === newPrTitle &&
noWhitespaceOrHeadings(stripEmojis(existingPrBody)) ===
noWhitespaceOrHeadings(stripEmojis(prBody))
) {
logger.debug(`${existingPr.displayNumber} does not need updating`);
return { pr: existingPr };
}
// PR must need updating
if (existingPrTitle !== newPrTitle) {
logger.debug(
{
branchName,
oldPrTitle: existingPr.title,
newPrTitle: prTitle,
},
'PR title changed'
);
} else if (!config.committedFiles && !config.rebaseRequested) {
logger.debug(
{
prTitle,
},
'PR body changed'
);
}
// istanbul ignore if
if (getGlobalConfig().dryRun) {
logger.info(`DRY-RUN: Would update PR #${existingPr.number}`);
} else {
await platform.updatePr({
number: existingPr.number,
prTitle,
prBody,
platformOptions: getPlatformPrOptions(config),
});
logger.info({ pr: existingPr.number, prTitle }, `PR updated`);
}
return {
pr: existingPr,
};
}
logger.debug({ branch: branchName, prTitle }, `Creating PR`);
// istanbul ignore if
if (config.updateType === 'rollback') {
logger.info('Creating Rollback PR');
}
let pr: Pr;
try {
// istanbul ignore if
if (getGlobalConfig().dryRun) {
logger.info('DRY-RUN: Would create PR: ' + prTitle);
pr = { number: 0, displayNumber: 'Dry run PR' } as never;
} else {
if (
!dependencyDashboardCheck &&
isLimitReached(Limit.PullRequests) &&
!config.isVulnerabilityAlert
) {
logger.debug('Skipping PR - limit reached');
return { prBlockedBy: 'RateLimited' };
}
pr = await platform.createPr({
sourceBranch: branchName,
targetBranch: config.baseBranch,
prTitle,
prBody,
labels: [...new Set([...config.labels, ...config.addLabels])].map(
(label) => template.compile(label, config)
),
platformOptions: getPlatformPrOptions(config),
draftPR: config.draftPR,
});
incLimitedValue(Limit.PullRequests);
logger.info({ pr: pr.number, prTitle }, 'PR created');
}
} catch (err) /* istanbul ignore next */ {
logger.debug({ err }, 'Pull request creation error');
if (
err.body?.message === 'Validation failed' &&
err.body.errors?.length &&
err.body.errors.some((error: { message?: string }) =>
error.message?.startsWith('A pull request already exists')
)
) {
logger.warn('A pull requests already exists');
return { prBlockedBy: 'Error' };
}
if (err.statusCode === 502) {
logger.warn(
{ branch: branchName },
'Deleting branch due to server error'
);
if (getGlobalConfig().dryRun) {
logger.info('DRY-RUN: Would delete branch: ' + config.branchName);
} else {
await deleteBranch(branchName);
}
}
return { prBlockedBy: 'Error' };
}
if (
config.branchAutomergeFailureMessage &&
!config.suppressNotifications.includes('branchAutomergeFailure')
) {
const topic = 'Branch automerge failure';
let content =
'This PR was configured for branch automerge, however this is not possible so it has been raised as a PR instead.';
if (config.branchAutomergeFailureMessage === 'branch status error') {
content += '\n___\n * Branch has one or more failed status checks';
}
content = platform.massageMarkdown(content);
logger.debug('Adding branch automerge failure message to PR');
// istanbul ignore if
if (getGlobalConfig().dryRun) {
logger.info(`DRY-RUN: Would add comment to PR #${pr.number}`);
} else {
await platform.ensureComment({
number: pr.number,
topic,
content,
});
}
}
// Skip assign and review if automerging PR
if (
config.automerge &&
!config.assignAutomerge &&
branchStatus !== BranchStatus.red
) {
logger.debug(
`Skipping assignees and reviewers as automerge=${config.automerge}`
);
} else {
await addAssigneesReviewers(config, pr);
}
logger.debug(`Created ${pr.displayNumber}`);
return { pr };
} catch (err) {
// istanbul ignore if
if (
err instanceof ExternalHostError ||
err.message === REPOSITORY_CHANGED ||
err.message === PLATFORM_RATE_LIMIT_EXCEEDED ||
err.message === PLATFORM_INTEGRATION_UNAUTHORIZED
) {
logger.debug('Passing error up');
throw err;
}
logger.error({ err }, 'Failed to ensure PR: ' + prTitle);
}
if (existingPr) {
return { pr: existingPr };
}
// istanbul ignore next
return { prBlockedBy: 'Error' };
}