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

feat(core): allow setting true for changelog config to enable with defaults #20376

Merged
merged 5 commits into from
Nov 24, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
172 changes: 172 additions & 0 deletions packages/nx/src/command-line/release/config/config.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,88 @@ describe('createNxReleaseConfig()', () => {
}
`);
});

it('should allow using true for group level changelog as an equivalent of an empty object (i.e. use the defaults)', async () => {
const res = await createNxReleaseConfig(projectGraph, {
groups: {
'group-1': {
projects: ['lib-a'],
changelog: true,
},
},
});
expect(res).toMatchInlineSnapshot(`
{
"error": null,
"nxReleaseConfig": {
"changelog": {
"git": {
"commit": false,
"commitArgs": "",
"commitMessage": "",
"tag": false,
"tagArgs": "",
"tagMessage": "",
},
"projectChangelogs": false,
"workspaceChangelog": {
"createRelease": false,
"entryWhenNoChanges": "This was a version bump only, there were no code changes.",
"file": "{workspaceRoot}/CHANGELOG.md",
"renderOptions": {
"includeAuthors": true,
},
"renderer": "nx/changelog-renderer",
},
},
"git": {
"commit": false,
"commitArgs": "",
"commitMessage": "",
"tag": false,
"tagArgs": "",
"tagMessage": "",
},
"groups": {
"group-1": {
"changelog": {
"createRelease": false,
"entryWhenNoChanges": "This was a version bump only for {projectName} to align it with other projects, there were no code changes.",
"file": "{projectRoot}/CHANGELOG.md",
"renderOptions": {
"includeAuthors": true,
},
"renderer": "nx/changelog-renderer",
},
"projects": [
"lib-a",
],
"projectsRelationship": "fixed",
"releaseTagPattern": "v{version}",
"version": {
"generator": "@nx/js:release-version",
"generatorOptions": {},
},
},
},
"projectsRelationship": "fixed",
"releaseTagPattern": "v{version}",
"version": {
"generator": "@nx/js:release-version",
"generatorOptions": {},
"git": {
"commit": false,
"commitArgs": "",
"commitMessage": "",
"tag": false,
"tagArgs": "",
"tagMessage": "",
},
},
},
}
`);
});
});

describe('user config -> top level version', () => {
Expand Down Expand Up @@ -1062,6 +1144,96 @@ describe('createNxReleaseConfig()', () => {
}
`);
});

it('should allow using true for workspaceChangelog and projectChangelogs as an equivalent of an empty object (i.e. use the defaults)', async () => {
const res = await createNxReleaseConfig(projectGraph, {
changelog: {
projectChangelogs: true,
workspaceChangelog: true,
},
});
expect(res).toMatchInlineSnapshot(`
{
"error": null,
"nxReleaseConfig": {
"changelog": {
"git": {
"commit": false,
"commitArgs": "",
"commitMessage": "",
"tag": false,
"tagArgs": "",
"tagMessage": "",
},
"projectChangelogs": {
"createRelease": false,
"entryWhenNoChanges": "This was a version bump only for {projectName} to align it with other projects, there were no code changes.",
"file": "{projectRoot}/CHANGELOG.md",
"renderOptions": {
"includeAuthors": true,
},
"renderer": "nx/changelog-renderer",
},
"workspaceChangelog": {
"createRelease": false,
"entryWhenNoChanges": "This was a version bump only, there were no code changes.",
"file": "{workspaceRoot}/CHANGELOG.md",
"renderOptions": {
"includeAuthors": true,
},
"renderer": "nx/changelog-renderer",
},
},
"git": {
"commit": false,
"commitArgs": "",
"commitMessage": "",
"tag": false,
"tagArgs": "",
"tagMessage": "",
},
"groups": {
"__default__": {
"changelog": {
"createRelease": false,
"entryWhenNoChanges": "This was a version bump only for {projectName} to align it with other projects, there were no code changes.",
"file": "{projectRoot}/CHANGELOG.md",
"renderOptions": {
"includeAuthors": true,
},
"renderer": "nx/changelog-renderer",
},
"projects": [
"lib-a",
"lib-b",
"nx",
],
"projectsRelationship": "fixed",
"releaseTagPattern": "v{version}",
"version": {
"generator": "@nx/js:release-version",
"generatorOptions": {},
},
},
},
"projectsRelationship": "fixed",
"releaseTagPattern": "v{version}",
"version": {
"generator": "@nx/js:release-version",
"generatorOptions": {},
"git": {
"commit": false,
"commitArgs": "",
"commitMessage": "",
"tag": false,
"tagArgs": "",
"tagMessage": "",
},
},
},
}
`);
});
});

describe('user config -> top level and group level changelog combined', () => {
Expand Down
54 changes: 51 additions & 3 deletions packages/nx/src/command-line/release/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@
* defaults and user overrides, as well as handling common errors, up front to produce a single, consistent,
* and easy to consume config object for all the `nx release` command implementations.
*/
import { NxJsonConfiguration } from '../../../config/nx-json';
import {
NxJsonConfiguration,
NxReleaseChangelogConfiguration,
} from '../../../config/nx-json';
import { output, type ProjectGraph } from '../../../devkit-exports';
import { findMatchingProjects } from '../../../utils/find-matching-projects';
import { projectHasTarget } from '../../../utils/project-graph-utils';
Expand All @@ -27,6 +30,14 @@ type EnsureProjectsArray<T> = {
: T[K];
};

type RemoveTrueFromType<T> = T extends true ? never : T;
type RemoveTrueFromProperties<T, K extends keyof T> = {
[P in keyof T]: P extends K ? RemoveTrueFromType<T[P]> : T[P];
};
type RemoveTrueFromPropertiesOnEach<T, K extends keyof T[keyof T]> = {
[U in keyof T]: RemoveTrueFromProperties<T[U], K>;
};

export const CATCH_ALL_RELEASE_GROUP = '__default__';

/**
Expand All @@ -41,7 +52,15 @@ export const CATCH_ALL_RELEASE_GROUP = '__default__';
export type NxReleaseConfig = DeepRequired<
NxJsonConfiguration['release'] & {
groups: DeepRequired<
EnsureProjectsArray<NxJsonConfiguration['release']['groups']>
RemoveTrueFromPropertiesOnEach<
EnsureProjectsArray<NxJsonConfiguration['release']['groups']>,
'changelog'
>
>;
// Remove the true shorthand from the changelog config types, it will be normalized to a default object
changelog: RemoveTrueFromProperties<
DeepRequired<NxJsonConfiguration['release']['changelog']>,
'workspaceChangelog' | 'projectChangelogs'
>;
}
>;
Expand Down Expand Up @@ -166,13 +185,27 @@ export async function createNxReleaseConfig(
],
userConfig.version as Partial<NxReleaseConfig['version']>
);

if (userConfig.changelog?.workspaceChangelog) {
userConfig.changelog.workspaceChangelog = normalizeTrueToEmptyObject(
userConfig.changelog.workspaceChangelog
);
}
if (userConfig.changelog?.projectChangelogs) {
userConfig.changelog.projectChangelogs = normalizeTrueToEmptyObject(
userConfig.changelog.projectChangelogs
);
}

const rootChangelogConfig: NxReleaseConfig['changelog'] = deepMergeDefaults(
[
WORKSPACE_DEFAULTS.changelog,
// Merge in the git defaults from the top level
{ git: rootGitConfig } as NxReleaseConfig['changelog'],
],
userConfig.changelog as Partial<NxReleaseConfig['changelog']>
normalizeTrueToEmptyObject(userConfig.changelog) as Partial<
NxReleaseConfig['changelog']
>
);

// git configuration is not supported at the group level, only the root/command level
Expand Down Expand Up @@ -293,6 +326,12 @@ export async function createNxReleaseConfig(
const projectsRelationship =
releaseGroup.projectsRelationship || GROUP_DEFAULTS.projectsRelationship;

if (releaseGroup.changelog) {
releaseGroup.changelog = normalizeTrueToEmptyObject(
releaseGroup.changelog
) as NxReleaseConfig['groups']['string']['changelog'];
}

const groupDefaults: NxReleaseConfig['groups']['string'] = {
projectsRelationship,
projects: matchingProjects,
Expand Down Expand Up @@ -338,6 +377,15 @@ export async function createNxReleaseConfig(
};
}

/**
* In some cases it is much cleaner and more intuitive for the user to be able to
* specify `true` in their config when they want to use the default config for a
* particular property, rather than having to specify an empty object.
*/
function normalizeTrueToEmptyObject<T>(value: T | boolean): T | {} {
return value === true ? {} : value;
}

export async function handleNxReleaseConfigError(
error: CreateNxReleaseConfigError
): Promise<never> {
Expand Down
26 changes: 22 additions & 4 deletions packages/nx/src/config/nx-json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,11 +162,17 @@ interface NxReleaseConfiguration {
*/
version?: NxReleaseVersionConfiguration;
/**
* Optionally override project changelog configuration for this group.
* Project changelogs are disabled by default.
*
* Here you can optionally override project changelog configuration for this group.
* Notes about boolean values:
*
* - true = enable project level changelogs using default configuration
* - false = explicitly disable project level changelogs
*
* NOTE: git configuration is not supported at the group level, only the root/command level
*/
changelog?: NxReleaseChangelogConfiguration | false;
changelog?: NxReleaseChangelogConfiguration | boolean;
/**
* Optionally override the git/release tag pattern to use for this group.
*/
Expand All @@ -185,8 +191,20 @@ interface NxReleaseConfiguration {
* Enable or override configuration for git operations as part of the changelog subcommand
*/
git?: NxReleaseGitConfiguration;
workspaceChangelog?: NxReleaseChangelogConfiguration | false;
projectChangelogs?: NxReleaseChangelogConfiguration | false;
/**
* Workspace changelog is enabled by default. Notes about boolean values:
*
* - true = explicitly enable workspace changelog using default configuration
* - false = disable workspace changelog
*/
workspaceChangelog?: NxReleaseChangelogConfiguration | boolean;
/**
* Project changelogs are disabled by default. Notes about boolean values:
*
* - true = enable project level changelogs using default configuration
* - false = explicitly disable project level changelogs
*/
projectChangelogs?: NxReleaseChangelogConfiguration | boolean;
};
/**
* If no version config is provided, we will assume that @nx/js:release-version
Expand Down