Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(release): allow version plans to have multi-line, arbitrarily formatted messages #27323

Merged
merged 3 commits into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
18 changes: 17 additions & 1 deletion packages/nx/release/changelog-renderer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -404,14 +404,30 @@ function formatChange(
changelogRenderOptions: DefaultChangelogRenderOptions,
repoSlug?: RepoSlug
): string {
let description = change.description;
let extraLines = [];
let extraLinesStr = '';
if (description.includes('\n')) {
[description, ...extraLines] = description.split('\n');
// Align the extra lines with the start of the description for better readability
const indentation = ' ';
extraLinesStr = extraLines
.filter((l) => l.trim().length > 0)
.map((l) => `${indentation}${l}`)
.join('\n');
}

let changeLine =
'- ' +
(change.isBreaking ? '⚠️ ' : '') +
(change.scope ? `**${change.scope.trim()}:** ` : '') +
change.description;
description;
if (repoSlug && changelogRenderOptions.commitReferences) {
changeLine += formatReferences(change.githubReferences, repoSlug);
}
if (extraLinesStr) {
changeLine += '\n\n' + extraLinesStr;
}
return changeLine;
}

Expand Down
163 changes: 93 additions & 70 deletions packages/nx/src/command-line/release/changelog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,22 @@ type PostGitTask = (latestCommit: string) => Promise<void>;
export const releaseChangelogCLIHandler = (args: ChangelogOptions) =>
handleErrors(args.verbose, () => releaseChangelog(args));

function semverBumpToReleaseType(bump: string): {
type: 'fix' | 'feat';
isBreaking: boolean;
} {
switch (bump) {
JamesHenry marked this conversation as resolved.
Show resolved Hide resolved
case 'major':
return { type: 'feat', isBreaking: true };
case 'minor':
return { type: 'feat', isBreaking: false };
case 'patch':
return { type: 'fix', isBreaking: false };
default:
throw new Error(`Invalid semver bump type: ${bump}`);
}
}

/**
* NOTE: This function is also exported for programmatic usage and forms part of the public API
* of Nx. We intentionally do not wrap the implementation with handleErrors because users need
Expand Down Expand Up @@ -265,28 +281,35 @@ export async function releaseChangelog(
const releaseGroup = releaseGroups[0];
if (releaseGroup.projectsRelationship === 'fixed') {
const versionPlans = releaseGroup.versionPlans as GroupVersionPlan[];
workspaceChangelogChanges = filterHiddenChanges(
versionPlans
.map((vp) => {
const parsedMessage = parseConventionalCommitsMessage(vp.message);

// only properly formatted conventional commits messages will be included in the changelog
if (!parsedMessage) {
return null;
}

return <ChangelogChange>{
type: parsedMessage.type,
scope: parsedMessage.scope,
description: parsedMessage.description,
body: '',
isBreaking: parsedMessage.breaking,
githubReferences: [],
};
})
.filter(Boolean),
nxReleaseConfig.conventionalCommits
);
workspaceChangelogChanges = versionPlans
.flatMap((vp) => {
const releaseType = semverBumpToReleaseType(vp.groupVersionBump);
const changes: ChangelogChange | ChangelogChange[] =
!vp.triggeredByProjects
? {
type: releaseType.type,
scope: '',
description: vp.message,
body: '',
isBreaking: releaseType.isBreaking,
githubReferences: [],
affectedProjects: '*',
}
: vp.triggeredByProjects.map((project) => {
return {
type: releaseType.type,
scope: project,
description: vp.message,
body: '',
// TODO: what about github references?
isBreaking: releaseType.isBreaking,
githubReferences: [],
affectedProjects: [project],
};
});
return changes;
})
.filter(Boolean);
}
}
} else {
Expand Down Expand Up @@ -466,31 +489,25 @@ export async function releaseChangelog(
let commits: GitCommit[];

if (releaseGroup.versionPlans) {
changes = filterHiddenChanges(
(releaseGroup.versionPlans as ProjectsVersionPlan[])
.map((vp) => {
const parsedMessage = parseConventionalCommitsMessage(
vp.message
);

// only properly formatted conventional commits messages will be included in the changelog
if (!parsedMessage) {
return null;
}

return {
type: parsedMessage.type,
scope: parsedMessage.scope,
description: parsedMessage.description,
body: '',
isBreaking: parsedMessage.breaking,
affectedProjects: Object.keys(vp.projectVersionBumps),
githubReferences: [],
};
})
.filter(Boolean),
nxReleaseConfig.conventionalCommits
);
changes = (releaseGroup.versionPlans as ProjectsVersionPlan[])
.map((vp) => {
const bumpForProject = vp.projectVersionBumps[project.name];
if (!bumpForProject) {
return null;
}
const releaseType = semverBumpToReleaseType(bumpForProject);
return {
type: releaseType.type,
scope: project.name,
description: vp.message,
body: '',
isBreaking: releaseType.isBreaking,
affectedProjects: Object.keys(vp.projectVersionBumps),
// TODO: can we include github references when using version plans?
githubReferences: [],
};
})
.filter(Boolean);
} else {
let fromRef =
args.from ||
Expand Down Expand Up @@ -615,29 +632,35 @@ export async function releaseChangelog(
// TODO: remove this after the changelog renderer is refactored to remove coupling with git commits
let commits: GitCommit[] = [];
if (releaseGroup.versionPlans) {
changes = filterHiddenChanges(
(releaseGroup.versionPlans as GroupVersionPlan[])
.map((vp) => {
const parsedMessage = parseConventionalCommitsMessage(vp.message);

// only properly formatted conventional commits messages will be included in the changelog
if (!parsedMessage) {
return null;
}

return <ChangelogChange>{
type: parsedMessage.type,
scope: parsedMessage.scope,
description: parsedMessage.description,
body: '',
isBreaking: parsedMessage.breaking,
githubReferences: [],
affectedProjects: '*',
};
})
.filter(Boolean),
nxReleaseConfig.conventionalCommits
);
changes = (releaseGroup.versionPlans as GroupVersionPlan[])
.flatMap((vp) => {
const releaseType = semverBumpToReleaseType(vp.groupVersionBump);
const changes: ChangelogChange | ChangelogChange[] =
!vp.triggeredByProjects
? {
type: releaseType.type,
scope: '',
description: vp.message,
body: '',
isBreaking: releaseType.isBreaking,
githubReferences: [],
affectedProjects: '*',
}
: vp.triggeredByProjects.map((project) => {
return {
type: releaseType.type,
scope: project,
description: vp.message,
body: '',
// TODO: what about github references?
isBreaking: releaseType.isBreaking,
githubReferences: [],
affectedProjects: [project],
};
});
return changes;
})
.filter(Boolean);
} else {
let fromRef =
args.from ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ pkg4: minor
---

This is a change to packages 3 and 4

...and it includes multiple lines of text
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ describe('version-plans', () => {
pkg1: patch,
},
fileName: plan1.md,
message: This is a change to just package 1,
message: This is a change to just package 1
,
relativePath: .nx/version-plans/plan1.md,
},
{
Expand All @@ -85,7 +86,8 @@ describe('version-plans', () => {
pkg2: patch,
},
fileName: plan2.md,
message: This is a change to package 1 and package 2,
message: This is a change to package 1 and package 2
,
relativePath: .nx/version-plans/plan2.md,
},
{
Expand All @@ -95,7 +97,10 @@ describe('version-plans', () => {
pkg4: minor,
},
fileName: plan3.md,
message: This is a change to packages 3 and 4,
message: This is a change to packages 3 and 4

...and it includes multiple lines of text
,
relativePath: .nx/version-plans/plan3.md,
},
{
Expand All @@ -107,7 +112,8 @@ describe('version-plans', () => {
pkg6: preminor,
},
fileName: plan4.md,
message: This is a change to packages 3, 4, 5, and 6,
message: This is a change to packages 3, 4, 5, and 6
,
relativePath: .nx/version-plans/plan4.md,
},
{
Expand All @@ -116,7 +122,8 @@ describe('version-plans', () => {
fixed-group-1: minor,
},
fileName: plan5.md,
message: This is a change to fixed-group-1,
message: This is a change to fixed-group-1
,
relativePath: .nx/version-plans/plan5.md,
},
{
Expand All @@ -127,7 +134,8 @@ describe('version-plans', () => {
pkg3: major,
},
fileName: plan6.md,
message: This is a major change to fixed-group-1 and pkg3 and a minor change to fixed-group-2,
message: This is a major change to fixed-group-1 and pkg3 and a minor change to fixed-group-2
,
relativePath: .nx/version-plans/plan6.md,
},
]
Expand Down Expand Up @@ -786,6 +794,11 @@ describe('version-plans', () => {
groupVersionBump: patch,
message: plan1 message,
relativePath: .nx/version-plans/plan1.md,
triggeredByProjects: [
pkg1,
pkg2,
pkg3,
],
},
{
absolutePath: <workspace-root>/version-plans/plan2.md,
Expand All @@ -794,6 +807,11 @@ describe('version-plans', () => {
groupVersionBump: minor,
message: plan2 message,
relativePath: .nx/version-plans/plan2.md,
triggeredByProjects: [
pkg1,
pkg2,
pkg3,
],
},
],
},
Expand Down Expand Up @@ -1005,6 +1023,9 @@ describe('version-plans', () => {
groupVersionBump: minor,
message: plan2 message,
relativePath: .nx/version-plans/plan2.md,
triggeredByProjects: [
pkg1,
],
},
],
},
Expand All @@ -1026,6 +1047,9 @@ describe('version-plans', () => {
groupVersionBump: minor,
message: plan2 message,
relativePath: .nx/version-plans/plan2.md,
triggeredByProjects: [
pkg2,
],
},
],
},
Expand All @@ -1047,6 +1071,9 @@ describe('version-plans', () => {
groupVersionBump: minor,
message: plan2 message,
relativePath: .nx/version-plans/plan2.md,
triggeredByProjects: [
pkg3,
],
},
],
},
Expand Down
Loading