Skip to content

Commit f521793

Browse files
feat: prompt dependency updates url in vulnerabilities.json creation (#788)
* feat: prompt dependency updates url in vulnerabilities.json creation * feat: get pr from github * fix: improve usability
1 parent 8a04848 commit f521793

File tree

3 files changed

+79
-4
lines changed

3 files changed

+79
-4
lines changed

lib/prepare_security.js

Lines changed: 60 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,11 @@ import {
1111
checkoutOnSecurityReleaseBranch,
1212
commitAndPushVulnerabilitiesJSON,
1313
getSummary,
14-
validateDate
14+
validateDate,
15+
promptDependencies,
16+
getSupportedVersions
1517
} from './security-release/security-release.js';
18+
import _ from 'lodash';
1619

1720
export default class SecurityReleaseSteward {
1821
repository = NEXT_SECURITY_RELEASE_REPOSITORY;
@@ -54,9 +57,11 @@ export default class SecurityReleaseSteward {
5457

5558
// choose the reports to include in the security release
5659
const reports = await release.chooseReports(cli);
60+
const depUpdates = await release.getDependencyUpdates({ cli });
61+
const deps = _.groupBy(depUpdates, 'name');
5762

5863
// create the vulnerabilities.json file in the security-release repo
59-
const filePath = await release.createVulnerabilitiesJSON(reports, { cli });
64+
const filePath = await release.createVulnerabilitiesJSON(reports, deps, { cli });
6065

6166
// review the vulnerabilities.json file
6267
const review = await release.promptReviewVulnerabilitiesJSON(cli);
@@ -212,10 +217,11 @@ class PrepareSecurityRelease {
212217
return selectedReports;
213218
}
214219

215-
async createVulnerabilitiesJSON(reports, { cli }) {
220+
async createVulnerabilitiesJSON(reports, dependencies, { cli }) {
216221
cli.separator('Creating vulnerabilities.json...');
217222
const file = JSON.stringify({
218-
reports
223+
reports,
224+
dependencies
219225
}, null, 2);
220226

221227
const folderPath = path.join(process.cwd(), NEXT_SECURITY_RELEASE_FOLDER);
@@ -259,4 +265,54 @@ class PrepareSecurityRelease {
259265
}
260266
process.exit(1);
261267
}
268+
269+
async getDependencyUpdates({ cli }) {
270+
const deps = [];
271+
cli.log('\n');
272+
cli.separator('Dependency Updates');
273+
const updates = await cli.prompt('Are there dependency updates in this security release?', {
274+
defaultAnswer: true,
275+
questionType: 'confirm'
276+
});
277+
278+
if (!updates) return deps;
279+
280+
const supportedVersions = await getSupportedVersions();
281+
282+
let asking = true;
283+
while (asking) {
284+
const dep = await promptDependencies(cli);
285+
if (!dep) {
286+
asking = false;
287+
break;
288+
}
289+
290+
const name = await cli.prompt('What is the name of the dependency that has been updated?', {
291+
defaultAnswer: '',
292+
questionType: 'input'
293+
});
294+
295+
const versions = await cli.prompt('Which release line does this dependency update affect?', {
296+
defaultAnswer: supportedVersions,
297+
questionType: 'input'
298+
});
299+
300+
try {
301+
const prUrl = dep.replace('https://github.com/', 'https://api.github.com/repos/').replace('pull', 'pulls');
302+
const res = await this.req.getPullRequest(prUrl);
303+
const { html_url, title } = res;
304+
deps.push({
305+
name,
306+
url: html_url,
307+
title,
308+
affectedVersions: versions.split(',').map((v) => v.replace('v', '').trim())
309+
});
310+
cli.separator();
311+
} catch (error) {
312+
this.cli.error('Invalid PR url. Please provide a valid PR url.');
313+
this.cli.error(error);
314+
}
315+
}
316+
return deps;
317+
}
262318
}

lib/request.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,18 @@ export default class Request {
7777
return this.json(url, options);
7878
}
7979

80+
async getPullRequest(url) {
81+
const options = {
82+
method: 'GET',
83+
headers: {
84+
Authorization: `Basic ${this.credentials.github}`,
85+
'User-Agent': 'node-core-utils',
86+
Accept: 'application/vnd.github+json'
87+
}
88+
};
89+
return this.json(url, options);
90+
}
91+
8092
async createPullRequest(title, body, { owner, repo, head, base }) {
8193
const url = `https://api.github.com/repos/${owner}/${repo}/pulls`;
8294
const options = {

lib/security-release/security-release.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,3 +107,10 @@ export function formatDateToYYYYMMDD(date) {
107107
// Concatenate year, month, and day with slashes
108108
return `${year}/${month}/${day}`;
109109
}
110+
111+
export function promptDependencies(cli) {
112+
return cli.prompt('Enter the link to the dependency update PR (leave empty to exit): ', {
113+
defaultAnswer: '',
114+
questionType: 'input'
115+
});
116+
}

0 commit comments

Comments
 (0)