Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions .github/scripts/release-section.mts
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,52 @@ export function getNewReleaseEntriesSection({
].join('\n');
}

/**
* Removes the entries whose owning PRs are all among the given PRs, leaving
* the other entries and the categories that still have one untouched. An entry
* that also owns another PR stays, so that PR is never dropped with it.
*
* @param options - Options.
* @param options.section - A release section.
* @param options.prNumbers - The PRs whose entries are removed.
* @returns The section without those entries; the same text when none match.
*/
export function removeReleaseEntries({
section,
prNumbers,
}: {
section: string;
prNumbers: ReadonlySet<string>;
}): string {
let removedAny = false;
const categories = parseReleaseSectionCategories(section)
.map(({ heading, entries }) => ({
heading,
entries: entries.filter(({ lines }) => {
const owners = getOwningPrNumbers(lines[0] ?? '');
const isRemoved =
owners.length > 0 && owners.every((owner) => prNumbers.has(owner));
removedAny ||= isRemoved;
return !isRemoved;
}),
}))
.filter(({ entries }) => entries.length > 0);

if (!removedAny) {
return section;
}

return [
splitLines(section)[0] ?? '',
...categories.flatMap(({ heading, entries }) => [
'',
heading,
'',
...entries.flatMap(({ lines }) => lines),
]),
].join('\n');
}

const CATEGORY_BY_LEADING_VERB = new Map([
['Added', '### Added'],
['Changed', '### Changed'],
Expand Down
55 changes: 55 additions & 0 deletions .github/scripts/tests/release-section.test.mts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
mergeCurrentReleaseSection,
mergeNewReleaseEntries,
normalizeRepositoryPrLinks,
removeReleaseEntries,
replaceReleaseSection,
restoreGeneratedCategories,
} from '../release-section.mts';
Expand Down Expand Up @@ -616,3 +617,57 @@ test('ends the oldest release section before the link references', () => {
.replace('Fixed an issue (#101)', 'Added a feature (#102)'),
);
});

test('removes the entries owning the given PRs and drops emptied categories', () => {
const section = [
'## [2.0.0]',
'',
'### Added',
'',
'- Added a thing (#1)',
'',
'### Fixed',
'',
'- Fixed one (#2)',
' more detail',
'- Fixed two (#3)',
].join('\n');

assert.equal(
removeReleaseEntries({ section, prNumbers: new Set(['1', '2']) }),
['## [2.0.0]', '', '### Fixed', '', '- Fixed two (#3)'].join('\n'),
);
assert.equal(
removeReleaseEntries({ section, prNumbers: new Set(['1', '2', '3']) }),
'## [2.0.0]',
);
});

test('keeps an entry that owns a PR outside the given PRs', () => {
const section = [
'## [2.0.0]',
'',
'### Fixed',
'',
'- Fixed from (#1) (#2)',
'- Fixed together (#3, #4)',
].join('\n');

assert.equal(
removeReleaseEntries({ section, prNumbers: new Set(['1', '3']) }),
section,
);
assert.equal(
removeReleaseEntries({ section, prNumbers: new Set(['3', '4']) }),
['## [2.0.0]', '', '### Fixed', '', '- Fixed from (#1) (#2)'].join('\n'),
);
});

test('leaves the section untouched when no entry matches', () => {
const section = '## [2.0.0]\n\n### Added\n\n- Added a thing (#1)';

assert.equal(
removeReleaseEntries({ section, prNumbers: new Set(['9']) }),
section,
);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import assert from 'node:assert/strict';
import { mkdtempSync, rmSync, writeFileSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { afterEach, beforeEach, describe, it, mock } from 'node:test';

import { git } from './git-test-environment.mts';
import { isCommitReachableFrom } from '../update-release-changelog.mts';

describe('isCommitReachableFrom', () => {
let directory: string;
let shared: string;
let onlyOnMain: string;

beforeEach(() => {
directory = mkdtempSync(join(tmpdir(), 'reachability-'));
git(directory, 'init', '-q', '-b', 'main');
const commit = (file: string): string => {
writeFileSync(join(directory, file), file);
git(directory, 'add', file);
git(directory, 'commit', '-q', '-m', file);
return git(directory, 'rev-parse', 'HEAD');
};
shared = commit('shared.txt');
git(directory, 'branch', 'release');
onlyOnMain = commit('main-only.txt');
});

afterEach(() => {
mock.restoreAll();
rmSync(directory, { recursive: true, force: true });
});

it('is true for a commit on the ref and false for one that is not', () => {
assert.equal(isCommitReachableFrom(shared, 'release', directory), true);
assert.equal(
isCommitReachableFrom(onlyOnMain, 'release', directory),
false,
);
});

it('warns and returns false when Git cannot resolve the commit', () => {
const warn = mock.method(console, 'warn', () => undefined);

assert.equal(
isCommitReachableFrom('f'.repeat(40), 'release', directory),
false,
);
assert.equal(warn.mock.callCount(), 1);
});

it('does not warn for an ordinary non-ancestor', () => {
const warn = mock.method(console, 'warn', () => undefined);

isCommitReachableFrom(onlyOnMain, 'release', directory);

assert.equal(warn.mock.callCount(), 0);
});
});
179 changes: 179 additions & 0 deletions .github/scripts/tests/update-release-changelog.test.mts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,20 @@ import test from 'node:test';

import {
assertReleaseSectionHasEntries,
excludePrsCherryPickedOntoBaseline,
findPrsMissingFromChangelog,
findReleaseVersionsContainingPrs,
getAutoChangelogCli,
getAcceptedPrCommits,
getChangelogValidationArguments,
getChangelogPushArguments,
getDeletedPrNumbers,
getChangelogPrPresentation,
getCherryPickAliasesByPr,
getCherryPickedPrsByCommit,
getHistoricalSkippedPrNumbers,
getPrsCherryPickedOntoBaseline,
getUnlistedCherryPicksWarning,
isReleaseSectionEmpty,
hasOnlyUpdaterCommits,
isUntouchedUnproofreadChangelogPr,
Expand Down Expand Up @@ -585,3 +591,176 @@ test('rejects generated sections with missing or duplicate PR references', () =>
),
);
});

test('collects the canonical PR of each accepted cherry-pick commit', () => {
assert.deepEqual(
[
...getCherryPickedPrsByCommit(`[auto-changelog] commit=${COMMIT_A} accepted source-pr=1 canonical-pr=1 cherry-pick=false
[auto-changelog] commit=${COMMIT_B} accepted source-pr=7 canonical-pr=1 cherry-pick=true
[auto-changelog] commit=${COMMIT_C} accepted source-pr=none canonical-pr=none cherry-pick=true`),
],
[[COMMIT_B, '1']],
);
});

test('finds the PRs cherry-picked onto the baseline branch', () => {
const cherryPickedPrsByCommit = new Map([
[COMMIT_A, '1'],
[COMMIT_B, '2'],
]);

assert.deepEqual(
getPrsCherryPickedOntoBaseline({
cherryPickedPrsByCommit,
baselineRef: 'origin/release/1.0.0',
isAncestor: (commit, ref) =>
commit === COMMIT_A && ref === 'origin/release/1.0.0',
}),
new Set(['1']),
);
assert.deepEqual(
getPrsCherryPickedOntoBaseline({
cherryPickedPrsByCommit,
baselineRef: 'origin/stable',
isAncestor: () => false,
}),
new Set(),
);
});

test('maps each canonical PR to its cherry-pick PR numbers', () => {
assert.deepEqual(
[
...getCherryPickAliasesByPr(`[auto-changelog] commit=${COMMIT_A} accepted source-pr=1 canonical-pr=1 cherry-pick=false
[auto-changelog] commit=${COMMIT_B} accepted source-pr=7 canonical-pr=1 cherry-pick=true
[auto-changelog] commit=${COMMIT_C} accepted source-pr=8 canonical-pr=1 cherry-pick=true`),
],
[['1', ['7', '8']]],
);
});

test('reports PRs that no other section lists under any of their numbers', () => {
const changelog = [
'## [2.0.0]',
'',
'### Fixed',
'',
'- Fixed one (#1)',
'',
'## [1.0.0]',
'',
'### Fixed',
'',
'- Fixed two (#2)',
'- Fixed three by its cherry-pick (#31)',
'',
].join('\n');

assert.deepEqual(
findPrsMissingFromChangelog({
changelog,
prNumbers: ['1', '2', '3', '4'],
aliasesByPr: new Map([['3', ['31']]]),
version: '2.0.0',
repositoryUrl: 'https://github.com/MetaMask/example',
}),
['1', '4'],
);
});

test('words the warning for PRs the earlier changelog lacks', () => {
assert.equal(
getUnlistedCherryPicksWarning('13.56.0', {
baselineVersion: '13.55.0',
prNumbers: ['47395', '47400'],
}),
'> #47395, #47400 were left out of 13.56.0 because their cherry-picks are already on the 13.55.0 branch, but the 13.55.0 changelog does not list them. Add an entry to the right release if one is needed.',
);
assert.equal(
getUnlistedCherryPicksWarning('13.56.0', {
baselineVersion: '13.55.0',
prNumbers: ['47395'],
}),
'> #47395 was left out of 13.56.0 because its cherry-pick is already on the 13.55.0 branch, but the 13.55.0 changelog does not list it. Add an entry to the right release if one is needed.',
);
});

const EXCLUSION_SECTION = [
'## [2.0.0]',
'',
'### Fixed',
'',
'- Fixed one (#1)',
'- Fixed two (#2)',
'- Fixed three (#3, #7)',
'- Fixed the regression from (#4) (#5)',
'',
].join('\n');

test('excludes the entries of PRs already on the baseline branch', () => {
const result = excludePrsCherryPickedOntoBaseline({
section: EXCLUSION_SECTION,
shippedEarlier: new Set(['1', '3']),
aliasesByPr: new Map([['3', ['7']]]),
reviewedPrNumbers: new Set(),
});

assert.deepEqual(result.excludedPrNumbers, ['1', '3']);
assert.equal(
result.section,
[
'## [2.0.0]',
'',
'### Fixed',
'',
'- Fixed two (#2)',
'- Fixed the regression from (#4) (#5)',
].join('\n'),
);
});

test('keeps an entry that also owns a PR that is not excluded', () => {
const result = excludePrsCherryPickedOntoBaseline({
section: EXCLUSION_SECTION,
shippedEarlier: new Set(['4']),
aliasesByPr: new Map(),
reviewedPrNumbers: new Set(),
});

assert.equal(result.section, EXCLUSION_SECTION);
assert.deepEqual(result.excludedPrNumbers, []);
});

test('does not report PRs a reviewer already approved in the section', () => {
const result = excludePrsCherryPickedOntoBaseline({
section: EXCLUSION_SECTION,
shippedEarlier: new Set(['1', '2']),
aliasesByPr: new Map(),
reviewedPrNumbers: new Set(['1']),
});

assert.deepEqual(result.excludedPrNumbers, ['2']);
});

test('does not report excluded PRs that the section never listed', () => {
const result = excludePrsCherryPickedOntoBaseline({
section: EXCLUSION_SECTION,
shippedEarlier: new Set(['99']),
aliasesByPr: new Map(),
reviewedPrNumbers: new Set(),
});

assert.equal(result.section, EXCLUSION_SECTION);
assert.deepEqual(result.excludedPrNumbers, []);
});

test('adds the unlisted cherry-pick warning to the PR body', () => {
const { body } = getChangelogPrPresentation({
version: '2.0.0',
changelogBranch: 'release-changelog/2.0.0',
proofreadingStatus: 'succeeded',
unlistedCherryPicks: { baselineVersion: '1.0.0', prNumbers: ['5'] },
});

assert.match(body, /#5 was left out of 2\.0\.0/u);
});
Loading
Loading