@@ -6,18 +6,48 @@ import { replaceInFile } from 'replace-in-file';
66
77import { forceRunAsync , runAsync , runSync } from './run.js' ;
88import { writeJson , readJson } from './file.js' ;
9- import Request from './request.js' ;
10- import auth from './auth.js' ;
119import {
1210 getEOLDate ,
1311 getStartLTSBlurb ,
1412 updateTestProcessRelease
1513} from './release/utils.js' ;
1614import CherryPick from './cherry_pick.js' ;
1715import Session from './session.js' ;
16+ import { getAffectedVersionLines } from './security-release/security-release.js' ;
1817
1918const isWindows = process . platform === 'win32' ;
2019
20+ // Parse a GitHub Pull Request URL into its owner, repo and number.
21+ // Returns null when the URL does not look like a PR URL.
22+ export function parsePullRequestURL ( url ) {
23+ const match = / g i t h u b \. c o m \/ ( [ ^ / ] + ) \/ ( [ ^ / ] + ) \/ p u l l \/ ( \d + ) / . exec ( url || '' ) ;
24+ if ( ! match ) return null ;
25+ return { owner : match [ 1 ] , repo : match [ 2 ] , number : Number ( match [ 3 ] ) } ;
26+ }
27+
28+ // Resolve the PR URL that fixes a given release line from a report or
29+ // dependency `affectedVersions` entry.
30+ // Supports the current schema, where `affectedVersions` maps a release line to
31+ // its PR URL (e.g. `{ '22.x': 'https://.../pull/1' }`), and the legacy schema,
32+ // where `affectedVersions` is an array of lines paired with a single `prURL`.
33+ export function getPullRequestURLForLine ( affectedVersions , line , legacyPrURL ) {
34+ if (
35+ affectedVersions &&
36+ typeof affectedVersions === 'object' &&
37+ ! Array . isArray ( affectedVersions )
38+ ) {
39+ return affectedVersions [ line ] || null ;
40+ }
41+
42+ if ( Array . isArray ( affectedVersions ) && legacyPrURL ) {
43+ return getAffectedVersionLines ( affectedVersions ) . includes ( line )
44+ ? legacyPrURL
45+ : null ;
46+ }
47+
48+ return null ;
49+ }
50+
2151export default class ReleasePreparation extends Session {
2252 constructor ( argv , cli , dir ) {
2353 super ( cli , dir ) ;
@@ -32,101 +62,137 @@ export default class ReleasePreparation extends Session {
3262 this . defaultReleaseDate = argv . releaseDate ?? new Date ( ) . toISOString ( ) . slice ( 0 , 10 ) ;
3363 this . ltsCodename = '' ;
3464 this . date = '' ;
35- this . filterLabels = argv . filterLabel && argv . filterLabel . split ( ',' ) ;
3665 this . newVersion = argv . newVersion ;
3766 }
3867
3968 get branch ( ) {
4069 return this . isSecurityRelease ? this . releaseBranch : this . stagingBranch ;
4170 }
4271
43- warnForNonMergeablePR ( pr ) {
72+ // Resolve the path to the security release vulnerabilities.json.
73+ // `--security` may point either directly at a vulnerabilities.json file or at
74+ // the root of the security-release repository, in which case the well-known
75+ // sub-path is appended.
76+ getVulnerabilitiesJSONPath ( ) {
77+ if ( ! this . securityReleaseRepo ) return null ;
78+ if ( this . securityReleaseRepo . endsWith ( '.json' ) ) {
79+ return this . securityReleaseRepo ;
80+ }
81+ return path . join (
82+ this . securityReleaseRepo ,
83+ 'security-release' ,
84+ 'next-security-release' ,
85+ 'vulnerabilities.json'
86+ ) ;
87+ }
88+
89+ loadVulnerabilitiesJSON ( ) {
4490 const { cli } = this ;
91+ const vulnPath = this . getVulnerabilitiesJSONPath ( ) ;
4592
46- cli . warn ( `PR#${ pr . number } - ${ pr . title } is not 'MERGEABLE'.
47- Status: ${ pr . mergeable } ` ) ;
93+ if ( ! vulnPath ) {
94+ cli . error ( 'No vulnerabilities.json path provided. Pass the path to ' +
95+ 'vulnerabilities.json (or the security-release repository) via ' +
96+ '`--security=<path>`.' ) ;
97+ return null ;
98+ }
99+
100+ if ( ! existsSync ( vulnPath ) ) {
101+ cli . error ( `vulnerabilities.json not found at ${ vulnPath } .` ) ;
102+ return null ;
103+ }
104+
105+ try {
106+ cli . startSpinner ( `Reading vulnerabilities.json from ${ vulnPath } ..` ) ;
107+ const vulnData = JSON . parse ( readFileSync ( vulnPath , 'utf-8' ) ) ;
108+ cli . stopSpinner ( `Done reading vulnerabilities.json from ${ vulnPath } ` ) ;
109+ return vulnData ;
110+ } catch ( err ) {
111+ cli . error ( `Failed to read vulnerabilities.json: ${ err . message } ` ) ;
112+ return null ;
113+ }
48114 }
49115
50- async getOpenPRs ( filterLabels ) {
51- const credentials = await auth ( { github : true } ) ;
52- const request = new Request ( credentials ) ;
53- const data = await request . gql ( 'PRs' , {
54- owner : this . owner ,
55- repo : this . repo ,
56- labels : filterLabels
57- } ) ;
58- return data . repository . pullRequests . nodes ;
116+ // Build the list of PRs to cherry-pick for a given release line from both the
117+ // reports and the dependency updates, using their `affectedVersions` mapping.
118+ getSecurityCherryPickTargets ( vulnData , line ) {
119+ const targets = [ ] ;
120+
121+ for ( const report of vulnData . reports ?? [ ] ) {
122+ const url = getPullRequestURLForLine (
123+ report . affectedVersions , line , report . prURL ) ;
124+ if ( url ) {
125+ targets . push ( {
126+ url,
127+ cveIds : report . cveIds ?. length ? report . cveIds : null ,
128+ label : report . title || report . id
129+ } ) ;
130+ }
131+ }
132+
133+ for ( const [ name , dep ] of Object . entries ( vulnData . dependencies ?? { } ) ) {
134+ const url = getPullRequestURLForLine (
135+ dep . affectedVersions , line , dep . prURL ) ;
136+ if ( url ) {
137+ targets . push ( { url, cveIds : null , label : `dependency: ${ name } ` } ) ;
138+ }
139+ }
140+
141+ return targets ;
142+ }
143+
144+ upstreamForPR ( { owner, repo } ) {
145+ if ( owner === 'nodejs-private' ) {
146+ return `https://${ this . username } :${ this . config . token } ` +
147+ `@github.com/${ owner } /${ repo } .git` ;
148+ }
149+ return `https://github.com/${ owner } /${ repo } .git` ;
59150 }
60151
61- async cherryPickSecurityPRs ( filterLabels ) {
152+ async cherryPickSecurityPRs ( ) {
62153 const { cli } = this ;
154+ const line = `${ this . versionComponents . major } .x` ;
63155
64- const prs = await this . getOpenPRs ( filterLabels ) ;
65- if ( prs . length === 0 ) {
66- cli . warn ( `There are no PRs available in ${ this . owner } /${ this . repo } ` ) ;
156+ const vulnData = this . loadVulnerabilitiesJSON ( ) ;
157+ if ( ! vulnData ) {
158+ cli . error ( 'Cannot proceed with a security release without a valid ' +
159+ 'vulnerabilities.json.' ) ;
67160 return false ;
68161 }
69162
70- const vulnCveMap = new Map ( ) ;
71- if ( this . isSecurityRelease && this . securityReleaseRepo ) {
72- const vulnPath = path . join (
73- this . securityReleaseRepo ,
74- 'security-release' ,
75- 'next-security-release' ,
76- 'vulnerabilities.json'
77- ) ;
78-
79- if ( ! existsSync ( vulnPath ) ) {
80- cli . error ( `vulnerabilities.json not found at ${ vulnPath } . ` +
81- 'Skipping CVE auto-population.' ) ;
82- cli . warn ( 'PRs will require manual CVE-ID entry.' ) ;
83- } else {
84- try {
85- cli . startSpinner ( `Reading vulnerabilities.json from ${ vulnPath } ..` ) ;
86- const vulnData = JSON . parse ( readFileSync ( vulnPath , 'utf-8' ) ) ;
87- cli . stopSpinner ( `Done reading vulnerabilities.json from ${ vulnPath } ` ) ;
88-
89- if ( vulnData . reports && Array . isArray ( vulnData . reports ) ) {
90- vulnData . reports . forEach ( report => {
91- if ( report . prURL && report . cveIds && report . cveIds . length > 0 ) {
92- vulnCveMap . set ( report . prURL , report . cveIds ) ;
93- }
94- } ) ;
95- }
96- cli . ok ( `Loaded ${ vulnCveMap . size } CVE mappings from vulnerabilities.json` ) ;
97- } catch ( err ) {
98- cli . error ( `Failed to read vulnerabilities.json: ${ err . message } ` ) ;
99- cli . warn ( 'Continuing without CVE auto-population.' ) ;
100- }
101- }
163+ const targets = this . getSecurityCherryPickTargets ( vulnData , line ) ;
164+ if ( targets . length === 0 ) {
165+ cli . warn ( `No PRs found in vulnerabilities.json for release line ${ line } .` ) ;
166+ return false ;
102167 }
103168
104- for ( const pr of prs ) {
105- if ( pr . mergeable !== 'MERGEABLE' ) {
106- this . warnForNonMergeablePR ( pr ) ;
107- }
169+ cli . info ( `Cherry-picking ${ targets . length } PR(s) for ${ line } from ` +
170+ 'vulnerabilities.json' ) ;
108171
109- // Look up CVE-IDs from vulnerabilities.json
110- const prUrl = `https://github.com/${ this . owner } /${ this . repo } /pull/${ pr . number } ` ;
111- const cveIds = vulnCveMap . get ( prUrl ) ;
172+ for ( const target of targets ) {
173+ const pr = parsePullRequestURL ( target . url ) ;
174+ if ( ! pr ) {
175+ cli . warn ( `Skipping invalid PR URL for ${ target . label } : ${ target . url } ` ) ;
176+ continue ;
177+ }
112178
113- if ( ! cveIds || cveIds . length === 0 ) {
114- cli . warn ( `No CVE-IDs found in vulnerabilities.json for ${ prUrl } ` ) ;
179+ if ( ! target . cveIds ) {
180+ cli . warn ( `No CVE-IDs found in vulnerabilities.json for ${ target . url } ` ) ;
115181 }
116182
183+ cli . info ( `Cherry-picking ${ target . label } (${ target . url } )` ) ;
117184 const cp = new CherryPick ( pr . number , this . dir , cli , {
118- owner : this . owner ,
119- repo : this . repo ,
185+ owner : pr . owner ,
186+ repo : pr . repo ,
120187 gpgSign : this . gpgSign ,
121- upstream : this . isSecurityRelease ? `https:// ${ this . username } : ${ this . config . token } @github.com/ ${ this . owner } / ${ this . repo } .git` : this . upstream ,
188+ upstream : this . upstreamForPR ( pr ) ,
122189 lint : false ,
123190 includeCVE : true ,
124- cveIds : cveIds || null ,
125- vulnCveMap
191+ cveIds : target . cveIds
126192 } ) ;
127193 const success = await cp . start ( ) ;
128194 if ( ! success ) {
129- cli . warn ( ' The cherry-pick has failed. PR-ID: ' + pr . number ) ;
195+ cli . warn ( ` The cherry-pick has failed. PR: ${ target . url } ` ) ;
130196 return false ;
131197 }
132198 }
@@ -147,7 +213,7 @@ export default class ReleasePreparation extends Session {
147213 { defaultAnswer : this . runBranchDiff } ) ;
148214 if ( runBranchDiff ) {
149215 if ( isSecurityRelease ) {
150- const success = await this . cherryPickSecurityPRs ( this . filterLabels ) ;
216+ const success = await this . cherryPickSecurityPRs ( ) ;
151217 if ( ! success ) {
152218 cli . error ( 'Aborting security release preparation.' ) ;
153219 return ;
0 commit comments