/
dependency-dashboard.ts
363 lines (351 loc) · 11.9 KB
/
dependency-dashboard.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
import is from '@sindresorhus/is';
import { nameFromLevel } from 'bunyan';
import { getGlobalConfig } from '../../config/global';
import type { RenovateConfig } from '../../config/types';
import { getProblems, logger } from '../../logger';
import { platform } from '../../platform';
import { BranchConfig, BranchResult } from '../types';
interface DependencyDashboard {
dependencyDashboardChecks: Record<string, string>;
dependencyDashboardRebaseAllOpen: boolean;
}
function parseDashboardIssue(issueBody: string): DependencyDashboard {
const checkMatch = ' - \\[x\\] <!-- ([a-zA-Z]+)-branch=([^\\s]+) -->';
const checked = issueBody.match(new RegExp(checkMatch, 'g'));
const dependencyDashboardChecks: Record<string, string> = {};
if (checked?.length) {
const re = new RegExp(checkMatch);
checked.forEach((check) => {
const [, type, branchName] = re.exec(check);
dependencyDashboardChecks[branchName] = type;
});
}
const checkedRebaseAll = issueBody.includes(
' - [x] <!-- rebase-all-open-prs -->'
);
let dependencyDashboardRebaseAllOpen = false;
if (checkedRebaseAll) {
dependencyDashboardRebaseAllOpen = true;
/* eslint-enable no-param-reassign */
}
return { dependencyDashboardChecks, dependencyDashboardRebaseAllOpen };
}
export async function readDashboardBody(config: RenovateConfig): Promise<void> {
/* eslint-disable no-param-reassign */
config.dependencyDashboardChecks = {};
const stringifiedConfig = JSON.stringify(config);
if (
config.dependencyDashboard ||
stringifiedConfig.includes('"dependencyDashboardApproval":true') ||
stringifiedConfig.includes('"prCreation":"approval"')
) {
config.dependencyDashboardTitle =
config.dependencyDashboardTitle || `Dependency Dashboard`;
const issue = await platform.findIssue(config.dependencyDashboardTitle);
if (issue) {
config.dependencyDashboardIssue = issue.number;
Object.assign(config, parseDashboardIssue(issue.body));
}
}
/* eslint-enable no-param-reassign */
}
function getListItem(branch: BranchConfig, type: string): string {
let item = ' - [ ] ';
item += `<!-- ${type}-branch=${branch.branchName} -->`;
if (branch.prNo) {
item += `[${branch.prTitle}](../pull/${branch.prNo})`;
} else {
item += branch.prTitle;
}
const uniquePackages = [
...new Set(branch.upgrades.map((upgrade) => '`' + upgrade.depName + '`')),
];
if (uniquePackages.length < 2) {
return item + '\n';
}
return item + ' (' + uniquePackages.join(', ') + ')\n';
}
function appendRepoProblems(config: RenovateConfig, issueBody: string): string {
let newIssueBody = issueBody;
const repoProblems = new Set(
getProblems()
.filter(
(problem) =>
problem.repository === config.repository && !problem.artifactErrors
)
.map(
(problem) =>
`${nameFromLevel[problem.level].toUpperCase()}: ${problem.msg}`
)
);
if (repoProblems.size) {
newIssueBody += '## Repository problems\n\n';
newIssueBody +=
'These problems occurred while renovating this repository.\n\n';
for (const repoProblem of repoProblems) {
newIssueBody += ` - ${repoProblem}\n`;
}
newIssueBody += '\n';
}
return newIssueBody;
}
export async function ensureDependencyDashboard(
config: RenovateConfig,
branches: BranchConfig[]
): Promise<void> {
// legacy/migrated issue
const reuseTitle = 'Update Dependencies (Renovate Bot)';
if (
!(
config.dependencyDashboard ||
config.dependencyDashboardApproval ||
config.packageRules?.some((rule) => rule.dependencyDashboardApproval) ||
branches.some(
(branch) =>
branch.dependencyDashboardApproval ||
branch.dependencyDashboardPrApproval
)
)
) {
if (getGlobalConfig().dryRun) {
logger.info(
{ title: config.dependencyDashboardTitle },
'DRY-RUN: Would close Dependency Dashboard'
);
} else {
logger.debug('Closing Dependency Dashboard');
await platform.ensureIssueClosing(config.dependencyDashboardTitle);
}
return;
}
// istanbul ignore if
if (config.repoIsOnboarded === false) {
logger.debug('Repo is onboarding - skipping dependency dashboard');
return;
}
logger.debug('Ensuring Dependency Dashboard');
const hasBranches =
is.nonEmptyArray(branches) &&
branches.some((branch) => branch.result !== BranchResult.Automerged);
if (config.dependencyDashboardAutoclose && !hasBranches) {
if (getGlobalConfig().dryRun) {
logger.info(
{ title: config.dependencyDashboardTitle },
'DRY-RUN: Would close Dependency Dashboard'
);
} else {
logger.debug('Closing Dependency Dashboard');
await platform.ensureIssueClosing(config.dependencyDashboardTitle);
}
return;
}
let issueBody = '';
if (config.dependencyDashboardHeader?.length) {
issueBody += `${config.dependencyDashboardHeader}\n\n`;
}
issueBody = appendRepoProblems(config, issueBody);
const pendingApprovals = branches.filter(
(branch) => branch.result === BranchResult.NeedsApproval
);
if (pendingApprovals.length) {
issueBody += '## Pending Approval\n\n';
issueBody += `These branches will be created by Renovate only once you click their checkbox below.\n\n`;
for (const branch of pendingApprovals) {
issueBody += getListItem(branch, 'approve');
}
issueBody += '\n';
}
const awaitingSchedule = branches.filter(
(branch) => branch.result === BranchResult.NotScheduled
);
if (awaitingSchedule.length) {
issueBody += '## Awaiting Schedule\n\n';
issueBody +=
'These updates are awaiting their schedule. Click on a checkbox to get an update now.\n';
for (const branch of awaitingSchedule) {
issueBody += getListItem(branch, 'unschedule');
}
issueBody += '\n';
}
const rateLimited = branches.filter(
(branch) =>
branch.result === BranchResult.BranchLimitReached ||
branch.result === BranchResult.PrLimitReached ||
branch.result === BranchResult.CommitLimitReached
);
if (rateLimited.length) {
issueBody += '## Rate Limited\n\n';
issueBody +=
'These updates are currently rate limited. Click on a checkbox below to force their creation now.\n\n';
for (const branch of rateLimited) {
issueBody += getListItem(branch, 'unlimit');
}
issueBody += '\n';
}
const errorList = branches.filter(
(branch) => branch.result === BranchResult.Error
);
if (errorList.length) {
issueBody += '## Errored\n\n';
issueBody +=
'These updates encountered an error and will be retried. Click on a checkbox below to force a retry now.\n\n';
for (const branch of errorList) {
issueBody += getListItem(branch, 'retry');
}
issueBody += '\n';
}
const awaitingPr = branches.filter(
(branch) => branch.result === BranchResult.NeedsPrApproval
);
if (awaitingPr.length) {
issueBody += '## PR Creation Approval Required\n\n';
issueBody +=
"These branches exist but PRs won't be created until you approve them by clicking on a checkbox.\n\n";
for (const branch of awaitingPr) {
issueBody += getListItem(branch, 'approvePr');
}
issueBody += '\n';
}
const prEdited = branches.filter(
(branch) => branch.result === BranchResult.PrEdited
);
if (prEdited.length) {
issueBody += '## Edited/Blocked\n\n';
issueBody += `These updates have been manually edited so Renovate will no longer make changes. To discard all commits and start over, click on a checkbox.\n\n`;
for (const branch of prEdited) {
issueBody += getListItem(branch, 'rebase');
}
issueBody += '\n';
}
const prPending = branches.filter(
(branch) => branch.result === BranchResult.Pending
);
if (prPending.length) {
issueBody += '## Pending Status Checks\n\n';
issueBody += `These updates await pending status checks. To force their creation now, click the checkbox below.\n\n`;
for (const branch of prPending) {
issueBody += getListItem(branch, 'approvePr');
}
issueBody += '\n';
}
const prPendingBranchAutomerge = branches.filter(
(branch) => branch.prBlockedBy === 'BranchAutomerge'
);
if (prPendingBranchAutomerge.length) {
issueBody += '## Pending Branch Automerge\n\n';
issueBody += `These updates await pending status checks before automerging. Click on a checkbox to abort the branch automerge, and create a PR instead.\n\n`;
for (const branch of prPendingBranchAutomerge) {
issueBody += getListItem(branch, 'approvePr');
}
issueBody += '\n';
}
const otherRes = [
BranchResult.Pending,
BranchResult.NeedsApproval,
BranchResult.NeedsPrApproval,
BranchResult.NotScheduled,
BranchResult.PrLimitReached,
BranchResult.CommitLimitReached,
BranchResult.BranchLimitReached,
BranchResult.AlreadyExisted,
BranchResult.Error,
BranchResult.Automerged,
BranchResult.PrEdited,
];
let inProgress = branches.filter(
(branch) =>
!otherRes.includes(branch.result) &&
branch.prBlockedBy !== 'BranchAutomerge'
);
const otherBranches = inProgress.filter(
(branch) => branch.prBlockedBy || !branch.prNo
);
// istanbul ignore if
if (otherBranches.length) {
issueBody += '## Other Branches\n\n';
issueBody += `These updates are pending. To force PRs open, click the checkbox below.\n\n`;
for (const branch of otherBranches) {
logger.info(
{
prBlockedBy: branch.prBlockedBy,
prNo: branch.prNo,
result: branch.result,
},
'Blocked PR'
);
issueBody += getListItem(branch, 'other');
}
issueBody += '\n';
}
inProgress = inProgress.filter(
(branch) => branch.prNo && !branch.prBlockedBy
);
if (inProgress.length) {
issueBody += '## Open\n\n';
issueBody +=
'These updates have all been created already. Click a checkbox below to force a retry/rebase of any.\n\n';
for (const branch of inProgress) {
issueBody += getListItem(branch, 'rebase');
}
if (inProgress.length > 2) {
issueBody += ' - [ ] ';
issueBody += '<!-- rebase-all-open-prs -->';
issueBody += '**Click on this checkbox to rebase all open PRs at once**';
issueBody += '\n';
}
issueBody += '\n';
}
const alreadyExisted = branches.filter(
(branch) => branch.result === BranchResult.AlreadyExisted
);
if (alreadyExisted.length) {
issueBody += '## Ignored or Blocked\n\n';
issueBody +=
'These are blocked by an existing closed PR and will not be recreated unless you click a checkbox below.\n\n';
for (const branch of alreadyExisted) {
issueBody += getListItem(branch, 'recreate');
}
issueBody += '\n';
}
if (!hasBranches) {
issueBody +=
'This repository currently has no open or pending branches.\n\n';
}
if (config.dependencyDashboardFooter?.length) {
issueBody += `---\n${config.dependencyDashboardFooter}\n`;
}
if (config.dependencyDashboardIssue) {
const updatedIssue = await platform.getIssue?.(
config.dependencyDashboardIssue,
false
);
if (updatedIssue) {
const { dependencyDashboardChecks } = parseDashboardIssue(
updatedIssue.body
);
for (const branchName of Object.keys(config.dependencyDashboardChecks)) {
delete dependencyDashboardChecks[branchName];
}
for (const branchName of Object.keys(dependencyDashboardChecks)) {
const checkText = `- [ ] <!-- ${dependencyDashboardChecks[branchName]}-branch=${branchName} -->`;
issueBody = issueBody.replace(
checkText,
checkText.replace('[ ]', '[x]')
);
}
}
}
if (getGlobalConfig().dryRun) {
logger.info(
{ title: config.dependencyDashboardTitle },
'DRY-RUN: Would ensure Dependency Dashboard'
);
} else {
await platform.ensureIssue({
title: config.dependencyDashboardTitle,
reuseTitle,
body: issueBody,
labels: config.dependencyDashboardLabels,
});
}
}