diff --git a/components/git/release.js b/components/git/release.js index 41d633b0..7ed893d8 100644 --- a/components/git/release.js +++ b/components/git/release.js @@ -78,7 +78,7 @@ export function builder(yargs) { yargsInstance = yargs; return yargs .options(releaseOptions).positional('prid', { - describe: 'PR number or URL of the release proposal to be promoted', + describe: 'PR number or URL of the release proposal to be prepared or promoted', type: 'string' }) .example('git node release --prepare --security', @@ -89,6 +89,8 @@ export function builder(yargs) { 'Prepare the first Alpha release of Node.js 27') .example('git node release --promote https://github.com/nodejs/node/pull/12345 https://github.com/nodejs/node/pull/54321', 'Promote two prepared releases of Node.js with PR #12345 and #54321') + .example('git node release --prepare --newVersion=1.2.3 https://github.com/nodejs/node/pull/12345', + 'Prepare a new release of Node.js tagged v1.2.3 for the proposal opened as PR #12345') .example('git node --prepare --startLTS', 'Prepare the first LTS release'); } @@ -138,8 +140,19 @@ async function wrapStash(fn) { } } +function parsePRID(pr) { + const match = /^(?:https:\/\/github\.com\/([^/]+)\/([^/]+)\/pull\/)?(\d+)(?:#.*)?$/.exec(pr); + if (!match) throw new Error('Invalid PR ID or URL', { cause: pr }); + const [, owner, repo, prid] = match; + return { owner, repo, prid: Number(prid) }; +} + async function main(state, argv, cli, dir) { if (state === PREPARE) { + if (argv.prid?.length > 1) { + throw new Error('Cannot prepare more than one release proposal at a time'); + } + const proposal = argv.prid?.length ? parsePRID(argv.prid[0]) : null; const release = new ReleasePreparation(argv, cli, dir); await release.prepareLocalBranch(); @@ -158,6 +171,11 @@ async function main(state, argv, cli, dir) { } } + if (proposal) { + const credentials = await auth({ github: true }); + await release.loadReleaseProposal(new Request(credentials), proposal); + } + return release.prepare(); } else if (state === PROMOTE) { const credentials = await auth({ github: true }); @@ -184,9 +202,7 @@ async function main(state, argv, cli, dir) { const releases = []; for (const pr of argv.prid) { - const match = /^(?:https:\/\/github\.com\/([^/]+)\/([^/]+)\/pull\/)?(\d+)(?:#.*)?$/.exec(pr); - if (!match) throw new Error('Invalid PR ID or URL', { cause: pr }); - const [,owner, repo, prid] = match; + const { owner, repo, prid } = parsePRID(pr); if ( owner && @@ -201,7 +217,7 @@ async function main(state, argv, cli, dir) { releases.push(await release.preparePromotion({ owner: owner ?? release.owner, repo: repo ?? release.repo, - prid: Number(prid) + prid })); } return release.promote(releases); diff --git a/docs/git-node.md b/docs/git-node.md index d403ee34..46b4cb40 100644 --- a/docs/git-node.md +++ b/docs/git-node.md @@ -265,6 +265,12 @@ git node release --prepare --startLTS git node release --prepare --security --filterLabel 18.x 18.20.1 ``` +```sh +# Prepare a new release of Node.js for an already opened release proposal, +# using the date from its last commit and filling in the PR-URL of the release commit +git node release --prepare --newVersion=1.2.3 https://github.com/nodejs/node/pull/12345 +``` + ``` # Skip the branch-diff initial check (useful when updating ongoing proposals) git node release --prepare 1.2.3 --skipBranchDiff diff --git a/lib/prepare_release.js b/lib/prepare_release.js index b0b816a9..d1d0d16a 100644 --- a/lib/prepare_release.js +++ b/lib/prepare_release.js @@ -31,6 +31,12 @@ export function parsePullRequestURL(url) { return { owner: match[1], repo: match[2], number: Number(match[3]) }; } +// Extract the release date from a release commit title, e.g. +// `2026-11-25, Version 25.3.0 (Current)`. Returns null if there is none. +export function getReleaseDateFromTitle(title) { + return /^(\d{4}-\d{2}-\d{2}), Version /.exec(title || '')?.[1] ?? null; +} + // Resolve the PR URL that fixes a given release line from a report or // dependency `affectedVersions` entry. // Supports the current schema, where `affectedVersions` maps a release line to @@ -132,6 +138,81 @@ export default class ReleasePreparation extends Session { this.ltsCodename = ''; this.date = ''; this.newVersion = argv.newVersion; + this.hasExplicitReleaseDate = argv.releaseDate != null; + this.prUrl = null; + this.releaseProposalCommit = null; + this.proposalUpstreamRemote = argv.fetchFrom ?? this.upstream; + } + + // Assert that the given PR is the release proposal for newVersion, and use + // the date of its last commit (the release commit) as the default release + // date, unless --releaseDate was also passed. + async loadReleaseProposal(request, { prid, owner, repo }) { + const { cli, newVersion, isSecurityRelease } = this; + if (!owner) { + owner = isSecurityRelease ? 'nodejs-private' : this.owner; + repo = isSecurityRelease ? 'node-private' : this.repo; + } + const prUrl = `https://github.com/${owner}/${repo}/pull/${prid}`; + + cli.startSpinner(`Fetching release proposal ${prUrl}`); + const { repository: { pullRequest } } = await request.gql('ReleaseProposalPR', { + prid, owner, repo + }); + cli.stopSpinner(`Fetched release proposal ${prUrl}`); + + const expectedBranch = `v${newVersion}-proposal`; + if (pullRequest.headRefName !== expectedBranch) { + throw new Error(`${prUrl} is not the release proposal for ${newVersion}: ` + + `it is opened from ${pullRequest.headRefName}, expected ${expectedBranch}`); + } + + const lastCommit = pullRequest.commits.nodes[0]?.commit; + const lastCommitTitle = lastCommit?.messageHeadline; + const date = getReleaseDateFromTitle(lastCommitTitle); + if (!date) { + throw new Error(`The last commit of ${prUrl} is not a release commit`, + { cause: lastCommitTitle }); + } + if (!this.hasExplicitReleaseDate) { + this.defaultReleaseDate = date; + } + this.prUrl = prUrl; + this.releaseProposalCommit = lastCommit.oid; + } + + get majorChangelogPath() { + return path.join('doc', 'changelogs', `CHANGELOG_V${this.versionComponents.major}.md`); + } + + // Stage the major CHANGELOG from the release proposal, and reset its working + // tree copy so it can be re-generated. The releaser can then compare the two + // versions, and restore the one from the release proposal if needed. + // Returns false if the release proposal could not be fetched or checked out. + async stageReleaseProposalChangelog() { + const { cli, majorChangelogPath, proposalUpstreamRemote, releaseProposalCommit } = this; + + cli.startSpinner(`Fetching release proposal from ${proposalUpstreamRemote}`); + try { + // We need a sync spawn here to avoid the spinner getting in the way of SSH key passphrase + runSync('git', ['fetch', proposalUpstreamRemote, releaseProposalCommit]); + runSync('git', ['checkout', releaseProposalCommit, '--', majorChangelogPath]); + } catch (err) { + cli.stopSpinner(`Could not check out ${majorChangelogPath} from ${this.prUrl}`, + cli.SPINNER_STATUS.FAILED); + cli.warn(err.message); + return false; + } + cli.stopSpinner(`Staged ${majorChangelogPath} from ${this.prUrl}`); + + if (this.isFirstRelease) { + await fs.rm(majorChangelogPath); + } else { + await forceRunAsync('git', + ['restore', '--worktree', '--source=HEAD', '--', majorChangelogPath], + { ignoreFailure: false }); + } + return true; } get branch() { @@ -396,10 +477,19 @@ export default class ReleasePreparation extends Session { await this.updateMainChangelog(); cli.stopSpinner('Updated CHANGELOG.md'); + const hasStagedChangelog = this.releaseProposalCommit != null && + await this.stageReleaseProposalChangelog(); + cli.startSpinner(`Updating CHANGELOG_V${versionComponents.major}.md`); await this.updateMajorChangelog(); cli.stopSpinner(`Updated CHANGELOG_V${versionComponents.major}.md`); + if (hasStagedChangelog) { + const { majorChangelogPath } = this; + cli.info(`${majorChangelogPath} from ${this.prUrl} is staged, ` + + 'the re-generated one is in the working tree.'); + } + // Create release commit. const shouldCreateReleaseCommit = await cli.prompt( 'Create release commit?'); @@ -948,7 +1038,7 @@ export default class ReleasePreparation extends Session { messageBody.push(`${getStartLTSBlurb(this)}\n\n`); } messageBody.push(notableChanges); - messageBody.push('\nPR-URL: TODO'); + messageBody.push(`\nPR-URL: ${this.prUrl ?? 'TODO'}`); // Create commit and then allow releaser to amend. runSync('git', ['add', '.']); diff --git a/lib/queries/ReleaseProposalPR.gql b/lib/queries/ReleaseProposalPR.gql new file mode 100644 index 00000000..1a4ba12c --- /dev/null +++ b/lib/queries/ReleaseProposalPR.gql @@ -0,0 +1,15 @@ +query ReleaseProposalPR($prid: Int!, $owner: String!, $repo: String!) { + repository(owner: $owner, name: $repo) { + pullRequest(number: $prid) { + headRefName, + commits(last: 1) { + nodes { + commit { + oid, + messageHeadline + } + } + } + } + } +} diff --git a/test/unit/prepare_release.test.js b/test/unit/prepare_release.test.js index 9d8219cb..44b1f78d 100644 --- a/test/unit/prepare_release.test.js +++ b/test/unit/prepare_release.test.js @@ -13,7 +13,8 @@ import ReleasePreparation, { filterAlphaReleaseCommits, formatMajorReleaseCommits, formatReleaseInfo, - formatSecurityNotableChanges + formatSecurityNotableChanges, + getReleaseDateFromTitle } from '../../lib/prepare_release.js'; function nodeVersionH({ major, minor, patch, isRelease = 0, alpha }) { @@ -184,6 +185,112 @@ describe('prepare_release: parsePullRequestURL', () => { }); }); +describe('prepare_release: getReleaseDateFromTitle', () => { + it('extracts the date from a release commit title', () => { + assert.strictEqual( + getReleaseDateFromTitle('2026-11-25, Version 25.3.0 (Current)'), + '2026-11-25' + ); + assert.strictEqual( + getReleaseDateFromTitle("2026-11-25, Version 24.12.0 'Krypton' (LTS)"), + '2026-11-25' + ); + }); + + it('returns null for other titles', () => { + assert.strictEqual(getReleaseDateFromTitle('doc: fix typo'), null); + assert.strictEqual(getReleaseDateFromTitle(undefined), null); + }); +}); + +describe('prepare_release: loadReleaseProposal', () => { + const prUrl = 'https://github.com/nodejs/node/pull/12345'; + function fakeRequest(headRefName, messageHeadline) { + return { + async gql(name, variables) { + assert.strictEqual(name, 'ReleaseProposalPR'); + assert.deepStrictEqual(variables, { prid: 12345, owner: 'nodejs', repo: 'node' }); + return { + repository: { + pullRequest: { + headRefName, + commits: { nodes: [{ commit: { oid: 'abc123', messageHeadline } }] } + } + } + }; + } + }; + } + const proposal = { owner: 'nodejs', repo: 'node', prid: 12345 }; + function prepare({ releaseDate }) { + return releasePreparation({ + newVersion: '25.3.0', + prUrl: null, + hasExplicitReleaseDate: releaseDate != null, + defaultReleaseDate: releaseDate ?? '2026-11-21' + }); + } + + it('uses the date of the release commit as default release date', async() => { + const release = prepare({}); + await release.loadReleaseProposal( + fakeRequest('v25.3.0-proposal', '2026-11-25, Version 25.3.0 (Current)'), proposal); + assert.strictEqual(release.defaultReleaseDate, '2026-11-25'); + assert.strictEqual(release.prUrl, prUrl); + assert.strictEqual(release.releaseProposalCommit, 'abc123'); + }); + + it('resolves a PR number against the configured repository', async() => { + const release = prepare({}); + await release.loadReleaseProposal( + fakeRequest('v25.3.0-proposal', '2026-11-25, Version 25.3.0 (Current)'), + { prid: 12345 }); + assert.strictEqual(release.prUrl, prUrl); + }); + + it('gives precedence to --releaseDate', async() => { + const release = prepare({ releaseDate: '2026-11-26' }); + await release.loadReleaseProposal( + fakeRequest('v25.3.0-proposal', '2026-11-25, Version 25.3.0 (Current)'), proposal); + assert.strictEqual(release.defaultReleaseDate, '2026-11-26'); + }); + + it('rejects PRs that are not the release proposal', async() => { + await assert.rejects( + prepare({}).loadReleaseProposal( + fakeRequest('some-branch', '2026-11-25, Version 25.3.0 (Current)'), proposal), + /is not the release proposal for 25\.3\.0/); + await assert.rejects( + prepare({}).loadReleaseProposal( + fakeRequest('v25.3.0-proposal', 'doc: fix typo'), proposal), + /is not a release commit/); + }); +}); + +describe('prepare_release: stageReleaseProposalChangelog', () => { + it('does not fail when the release proposal cannot be fetched', async(t) => { + const changelog = path.join('doc', 'changelogs', 'CHANGELOG_V25.md'); + nodeCheckout(t, { [changelog]: 'previous' }); + const warnings = []; + const noop = () => {}; + const release = releasePreparation({ + cli: { + startSpinner: noop, + stopSpinner: noop, + warn: (msg) => warnings.push(msg), + SPINNER_STATUS: {} + }, + versionComponents: { major: 25 }, + proposalUpstreamRemote: 'does-not-exist', + releaseProposalCommit: 'abc123', + prUrl: 'https://github.com/nodejs/node/pull/12345' + }); + assert.strictEqual(await release.stageReleaseProposalChangelog(), false); + assert.strictEqual(warnings.length, 1); + assert.strictEqual(fs.readFileSync(changelog, 'utf8'), 'previous'); + }); +}); + describe('prepare_release: getPullRequestURLForLine', () => { const affectedVersions = { main: 'https://github.com/nodejs-private/node-private/pull/924',