1+ import fs from 'node:fs' ;
2+ import os from 'node:os' ;
13import path from 'node:path' ;
24import auth from './auth.js' ;
35import Request from './request.js' ;
6+ import { forceRunAsync } from './run.js' ;
7+ import { getEditor } from './utils.js' ;
48import { parsePRFromURL } from './links.js' ;
5- import {
6- assertNewSecurityRelease ,
7- getSecurityReleaseDraftPath ,
8- writeSecurityReleaseDraft
9- } from './security-release/draft.js' ;
109import {
1110 buildIncludedTriagedReport ,
1211 getReportPRURL ,
@@ -18,7 +17,7 @@ import {
1817
1918import {
2019 NEXT_SECURITY_RELEASE_BRANCH ,
21- checkoutOnSecurityReleaseBranch ,
20+ NEXT_SECURITY_RELEASE_FOLDER ,
2221 commitAndPushVulnerabilitiesJSON ,
2322 validateDate ,
2423 promptDependencies ,
@@ -102,7 +101,6 @@ export default class PrepareSecurityRelease extends SecurityRelease {
102101 title = 'Next Security Release' ;
103102
104103 async start ( ) {
105- assertNewSecurityRelease ( process . cwd ( ) ) ;
106104 const credentials = await auth ( {
107105 github : true ,
108106 h1 : true
@@ -115,25 +113,43 @@ export default class PrepareSecurityRelease extends SecurityRelease {
115113 if ( releaseDate !== 'TBD' ) {
116114 validateDate ( releaseDate ) ;
117115 }
118- const createVulnerabilitiesJSON = await this . promptVulnerabilitiesJSON ( ) ;
119116
120117 const content = await this . buildDescription ( releaseDate ) ;
121- if ( createVulnerabilitiesJSON ) {
122- const reportSelectionMode = await this . promptReportSelectionMode ( ) ;
123- const candidates = await listSecurityReleaseCandidates ( this . req ) ;
124- if ( reportSelectionMode === 'review' ) {
125- const showTriaged = await this . promptShowTriagedWithoutPR ( ) ;
126- if ( showTriaged ) {
127- excludedReports = await this . showTriagedReportsWithoutPR ( candidates ) ;
128- }
118+ await this . checkBranchDoesNotExist ( ) ;
119+ const reportSelectionMode = await this . promptReportSelectionMode ( ) ;
120+ const candidates = await listSecurityReleaseCandidates ( this . req ) ;
121+ if ( reportSelectionMode === 'review' ) {
122+ const showTriaged = await this . promptShowTriagedWithoutPR ( ) ;
123+ if ( showTriaged ) {
124+ excludedReports = await this . showTriagedReportsWithoutPR ( candidates ) ;
129125 }
130- await this . startVulnerabilitiesJSONCreation (
131- releaseDate , content , excludedReports , reportSelectionMode , candidates ) ;
132126 }
127+ await this . startVulnerabilitiesJSONCreation (
128+ releaseDate , content , excludedReports , reportSelectionMode , candidates ) ;
133129
134130 this . cli . ok ( 'Done!' ) ;
135131 }
136132
133+ async checkBranchDoesNotExist ( ) {
134+ const { owner, repo } = this . repository ;
135+ const branch = NEXT_SECURITY_RELEASE_BRANCH ;
136+ this . cli . startSpinner ( `Checking that ${ branch } does not exist on ${ owner } /${ repo } ...` ) ;
137+ if ( await this . req . getBranchSHA ( { owner, repo, branch } ) ) {
138+ this . cli . stopSpinner ( `${ branch } already exists on ${ owner } /${ repo } ` ,
139+ this . cli . SPINNER_STATUS . FAILED ) ;
140+ throw new Error ( 'A security release seems to be in progress. Delete the ' +
141+ `https://github.com/${ owner } /${ repo } /tree/${ branch } branch if it is stale.` ) ;
142+ }
143+ const draft = `${ NEXT_SECURITY_RELEASE_FOLDER } /vulnerabilities.json` ;
144+ if ( await this . req . getFileSHA ( { owner, repo, path : draft , ref : 'main' } ) ) {
145+ this . cli . stopSpinner ( `${ draft } already exists on ${ owner } /${ repo } @main` ,
146+ this . cli . SPINNER_STATUS . FAILED ) ;
147+ throw new Error ( `Security release draft already exists: ${ draft } on main. ` +
148+ 'Use --sync, --add-report, or --remove-report to update the existing release.' ) ;
149+ }
150+ this . cli . stopSpinner ( `${ branch } does not exist on ${ owner } /${ repo } ` ) ;
151+ }
152+
137153 async cleanup ( ) {
138154 const credentials = await auth ( {
139155 github : true ,
@@ -203,55 +219,30 @@ export default class PrepareSecurityRelease extends SecurityRelease {
203219 reportSelectionMode = 'review' ,
204220 candidates
205221 ) {
206- assertNewSecurityRelease ( process . cwd ( ) ) ;
207-
208222 // choose the reports to include in the security release
209223 const reports = reportSelectionMode === 'include-all'
210224 ? await this . includeAllTriagedReports ( excludedReports , candidates )
211225 : await this . chooseReports ( excludedReports , candidates ) ;
212226 const deps = await this . getDependencyUpdates ( ) ;
213227 const { release } = prepareSecurityRelease ( { releaseDate, reports, dependencies : deps } ) ;
214228
215- // Prepare all data before changing Git state. An existing branch may contain
216- // a draft that was not present on the branch where this command started.
217- await checkoutOnSecurityReleaseBranch ( this . cli , this . repository ) ;
218- assertNewSecurityRelease ( process . cwd ( ) ) ;
229+ let fileContent = this . createVulnerabilitiesJSON ( release ) ;
219230
220- const filePath = await this . createVulnerabilitiesJSON (
221- release . reports , release . dependencies , release . releaseDate ) ;
222-
223- // review the vulnerabilities.json file
224231 const review = await this . promptReviewVulnerabilitiesJSON ( ) ;
225-
226- if ( ! review ) {
227- this . cli . info ( `To push the vulnerabilities.json file run:
228- - git add ${ filePath }
229- - git commit -m "chore: create vulnerabilities.json for next security release"
230- - git push -u origin ${ NEXT_SECURITY_RELEASE_BRANCH }
231- - open a PR on ${ this . repository . owner } /${ this . repository . repo } ` ) ;
232- return ;
232+ if ( review ) {
233+ fileContent = await this . editVulnerabilitiesJSON ( fileContent ) ;
233234 }
234235
235- // commit and push the vulnerabilities.json file
236- const commitMessage = 'chore: create vulnerabilities.json for next security release' ;
237- await commitAndPushVulnerabilitiesJSON ( filePath ,
238- commitMessage ,
239- { cli : this . cli , repository : this . repository } ) ;
240-
241- const createPr = await this . promptCreatePR ( ) ;
242-
243- if ( ! createPr ) return ;
236+ await this . pushVulnerabilitiesJSON (
237+ fileContent ,
238+ 'chore: create vulnerabilities.json for next security release' ,
239+ { branchExists : false }
240+ ) ;
244241
245242 // create pr on the security-release repo
246243 return this . createPullRequest ( content ) ;
247244 }
248245
249- promptCreatePR ( ) {
250- return this . cli . prompt (
251- 'Create the Next Security Release PR?' ,
252- { defaultAnswer : true } ) ;
253- }
254-
255246 async getSecurityIssueTemplate ( ) {
256247 const url = 'https://raw.githubusercontent.com/nodejs/node/main/doc/contributing/security-release-process.md' ;
257248 try {
@@ -311,10 +302,37 @@ export default class PrepareSecurityRelease extends SecurityRelease {
311302
312303 async promptReviewVulnerabilitiesJSON ( ) {
313304 return this . cli . prompt (
314- 'Please review vulnerabilities.json and press enter to proceed. ' ,
305+ 'Do you want to review/edit vulnerabilities.json before pushing it? ' ,
315306 { defaultAnswer : true } ) ;
316307 }
317308
309+ async editVulnerabilitiesJSON ( content ) {
310+ const editor = await getEditor ( { git : true } ) ;
311+ const dir = await fs . promises . mkdtemp ( path . join ( os . tmpdir ( ) , 'ncu-security-' ) ) ;
312+ const filePath = path . join ( dir , 'vulnerabilities.json' ) ;
313+ try {
314+ await fs . promises . writeFile ( filePath , content ) ;
315+ for ( ; ; ) {
316+ await forceRunAsync ( editor , [ `"${ filePath } "` ] ,
317+ { ignoreFailure : false , spawnArgs : { shell : true } } ) ;
318+ const edited = await fs . promises . readFile ( filePath , 'utf8' ) ;
319+ try {
320+ const { release } = prepareSecurityRelease ( JSON . parse ( edited ) ) ;
321+ return this . createVulnerabilitiesJSON ( release ) ;
322+ } catch ( error ) {
323+ this . cli . error ( `vulnerabilities.json is not valid: ${ error . message } ` ) ;
324+ const retry = await this . cli . prompt ( 'Edit vulnerabilities.json again?' ,
325+ { defaultAnswer : true } ) ;
326+ if ( ! retry ) {
327+ throw new Error ( 'Aborted: vulnerabilities.json is not valid.' , { cause : error } ) ;
328+ }
329+ }
330+ }
331+ } finally {
332+ await fs . promises . rm ( dir , { recursive : true , force : true } ) ;
333+ }
334+ }
335+
318336 async promptShowTriagedWithoutPR ( ) {
319337 return this . cli . prompt (
320338 'Do you want to see which reports are triaged but have no PR URL?' ,
@@ -451,25 +469,47 @@ export default class PrepareSecurityRelease extends SecurityRelease {
451469 }
452470 }
453471
454- async createVulnerabilitiesJSON ( reports , dependencies , releaseDate ) {
455- const { release } = prepareSecurityRelease ( { releaseDate, reports, dependencies } ) ;
456- const directory = process . cwd ( ) ;
457- const fullPath = getSecurityReleaseDraftPath ( directory ) ;
458- assertNewSecurityRelease ( directory ) ;
459- await confirmSecurityStep (
460- this . cli ,
461- `create directory \`${ path . dirname ( fullPath ) } \`` ,
462- 'This creates the security release folder if it does not already exist.'
463- ) ;
472+ createVulnerabilitiesJSON ( release ) {
473+ return JSON . stringify ( release , null , 2 ) + '\n' ;
474+ }
475+
476+ async pushVulnerabilitiesJSON ( content , message , { branchExists } = { } ) {
477+ const { owner, repo } = this . repository ;
478+ const branch = NEXT_SECURITY_RELEASE_BRANCH ;
479+ const filePath = `${ NEXT_SECURITY_RELEASE_FOLDER } /vulnerabilities.json` ;
480+
481+ this . cli . startSpinner ( `Looking for ${ branch } on ${ owner } /${ repo } ...` ) ;
482+ branchExists ??= ! ! ( await this . req . getBranchSHA ( { owner, repo, branch } ) ) ;
483+ let fileSHA ;
484+ if ( branchExists ) {
485+ fileSHA = await this . req . getFileSHA ( { owner, repo, path : filePath , ref : branch } ) ;
486+ this . cli . stopSpinner ( `Found ${ branch } on ${ owner } /${ repo } ` ) ;
487+ } else {
488+ const baseSHA = await this . req . getBranchSHA ( { owner, repo, branch : 'main' } ) ;
489+ if ( ! baseSHA ) {
490+ this . cli . stopSpinner ( `Could not find main on ${ owner } /${ repo } ` ,
491+ this . cli . SPINNER_STATUS . FAILED ) ;
492+ throw new Error ( `Could not find main on ${ owner } /${ repo } ` ) ;
493+ }
494+ this . cli . stopSpinner ( `Found main on ${ owner } /${ repo } ` ) ;
495+ await confirmSecurityStep (
496+ this . cli ,
497+ `create branch \`${ branch } \` on \`${ owner } /${ repo } \`` ,
498+ `This creates the ${ branch } branch from main (${ baseSHA } ).`
499+ ) ;
500+ await this . req . createBranch ( { owner, repo, branch, sha : baseSHA } ) ;
501+ }
502+
464503 await confirmSecurityStep (
465504 this . cli ,
466- `write \`${ fullPath } \`` ,
467- ' This creates vulnerabilities.json for the next security release.'
505+ `push \`${ filePath } \` to \` ${ owner } / ${ repo } @ ${ branch } \`` ,
506+ ` This ${ fileSHA ? 'replaces' : ' creates' } ${ filePath } in a new commit: ${ message } `
468507 ) ;
469- this . cli . startSpinner ( 'Creating vulnerabilities.json...' ) ;
470- writeSecurityReleaseDraft ( directory , release ) ;
471- this . cli . stopSpinner ( `Created ${ fullPath } ` ) ;
472- return fullPath ;
508+ this . cli . startSpinner ( `Pushing ${ filePath } to ${ owner } /${ repo } @${ branch } ...` ) ;
509+ const { commit } = await this . req . createOrUpdateFile ( {
510+ owner, repo, path : filePath , branch, content, message, sha : fileSHA
511+ } ) ;
512+ this . cli . stopSpinner ( `Pushed ${ commit ?. html_url ?? message } ` ) ;
473513 }
474514
475515 async createPullRequest ( content ) {
@@ -486,7 +526,7 @@ export default class PrepareSecurityRelease extends SecurityRelease {
486526 owner,
487527 repo,
488528 base : 'main' ,
489- head : 'next-security-release'
529+ head : NEXT_SECURITY_RELEASE_BRANCH
490530 }
491531
492532 ) ;
0 commit comments