feat(release): enable git operations by default (#21082)

This commit is contained in:
Austin Fahsl 2024-01-11 10:37:03 -07:00 committed by GitHub
parent a564ca0211
commit b9192cdcf2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
16 changed files with 477 additions and 131 deletions

View File

@ -161,13 +161,11 @@ Type: `string`
Exact version or semver keyword to apply to the selected release group.
##### stageChanges
##### stage-changes
Type: `boolean`
Default: `false`
Whether or not to stage the changes made by this command, irrespective of the git config in nx.json related to automated commits. Useful when combining this command with changelog generation.
Whether or not to stage the changes made by this command. Useful when combining this command with changelog generation.
##### version

View File

@ -161,13 +161,11 @@ Type: `string`
Exact version or semver keyword to apply to the selected release group.
##### stageChanges
##### stage-changes
Type: `boolean`
Default: `false`
Whether or not to stage the changes made by this command, irrespective of the git config in nx.json related to automated commits. Useful when combining this command with changelog generation.
Whether or not to stage the changes made by this command. Useful when combining this command with changelog generation.
##### version

View File

@ -105,8 +105,6 @@ import * as yargs from 'yargs';
const { workspaceVersion, projectsVersionData } = await releaseVersion({
specifier: options.version,
// stage package.json updates to be committed later by the changelog command
stageChanges: true,
dryRun: options.dryRun,
verbose: options.verbose,
});

View File

@ -125,6 +125,9 @@ describe('nx release - independent projects', () => {
"scripts": {
> NX Staging changed files with git
`);
const versionPkg2Output = runCLI(
@ -154,6 +157,9 @@ describe('nx release - independent projects', () => {
+
> NX Staging changed files with git
`);
const versionPkg3Output = runCLI(
@ -190,6 +196,9 @@ describe('nx release - independent projects', () => {
}
> NX Staging changed files with git
`);
}, 500000);
@ -653,7 +662,7 @@ describe('nx release - independent projects', () => {
});
describe('release command', () => {
beforeEach(() => {
it('should allow versioning projects independently', async () => {
updateJson('nx.json', () => {
return {
release: {
@ -670,9 +679,10 @@ describe('nx release - independent projects', () => {
},
};
});
});
it('should allow versioning projects independently', async () => {
runCommand(`git add .`);
runCommand(`git commit -m "chore: initial commit"`);
runCommand(`git tag ${pkg1}@v1.2.0`);
runCommand(`git tag ${pkg2}@v1.4.0`);
runCommand(`git tag ${pkg3}@v1.6.0`);
@ -728,6 +738,9 @@ describe('nx release - independent projects', () => {
};
});
runCommand(`git add .`);
runCommand(`git commit -m "chore: initial commit"`);
runCommand(`git tag ${pkg1}@v1.3.0`);
runCommand(`git tag ${pkg2}@v1.5.0`);
runCommand(`git tag ${pkg3}@v1.7.0`);

View File

@ -76,6 +76,12 @@
"version": "17.0.0-rc.1",
"description": "Migration for v17.0.0-rc.1",
"implementation": "./src/migrations/update-17-0-0/rm-default-collection-npm-scope"
},
"17.3.0-nx-release-git-operations-explicit-opt-out": {
"cli": "nx",
"version": "17.3.0-beta.3",
"description": "Explicitly opt-out of git operations in nx release",
"implementation": "./src/migrations/update-17-3-0/nx-release-git-operations-explicit-opt-out"
}
}
}

View File

@ -140,14 +140,14 @@ export async function releaseChangelog(
const tree = new FsTree(workspaceRoot, args.verbose);
const userCommitMessage: string | undefined =
const commitMessage: string | undefined =
args.gitCommitMessage || nxReleaseConfig.changelog.git.commitMessage;
const commitMessageValues: string[] = createCommitMessageValues(
releaseGroups,
releaseGroupToFilteredProjects,
projectsVersionData,
userCommitMessage
commitMessage
);
// Resolve any git tags as early as possible so that we can hard error in case of any duplicates before reaching the actual git command

View File

@ -183,11 +183,10 @@ const versionCommand: CommandModule<NxReleaseArgs, VersionOptions> = {
'The optional prerelease identifier to apply to the version, in the case that specifier has been set to prerelease.',
default: '',
})
.option('stageChanges', {
.option('stage-changes', {
type: 'boolean',
describe:
'Whether or not to stage the changes made by this command, irrespective of the git config in nx.json related to automated commits. Useful when combining this command with changelog generation.',
default: false,
'Whether or not to stage the changes made by this command. Useful when combining this command with changelog generation.',
})
),
handler: (args) =>

View File

@ -54,7 +54,7 @@ describe('createNxReleaseConfig()', () => {
"git": {
"commit": false,
"commitArgs": "",
"commitMessage": "",
"commitMessage": "chore(release): publish {version}",
"tag": false,
"tagArgs": "",
"tagMessage": "",
@ -75,7 +75,7 @@ describe('createNxReleaseConfig()', () => {
"git": {
"commit": false,
"commitArgs": "",
"commitMessage": "",
"commitMessage": "chore(release): publish {version}",
"tag": false,
"tagArgs": "",
"tagMessage": "",
@ -104,7 +104,8 @@ describe('createNxReleaseConfig()', () => {
"git": {
"commit": false,
"commitArgs": "",
"commitMessage": "",
"commitMessage": "chore(release): publish {version}",
"stageChanges": true,
"tag": false,
"tagArgs": "",
"tagMessage": "",
@ -124,7 +125,7 @@ describe('createNxReleaseConfig()', () => {
"git": {
"commit": false,
"commitArgs": "",
"commitMessage": "",
"commitMessage": "chore(release): publish {version}",
"tag": false,
"tagArgs": "",
"tagMessage": "",
@ -145,7 +146,7 @@ describe('createNxReleaseConfig()', () => {
"git": {
"commit": false,
"commitArgs": "",
"commitMessage": "",
"commitMessage": "chore(release): publish {version}",
"tag": false,
"tagArgs": "",
"tagMessage": "",
@ -174,7 +175,8 @@ describe('createNxReleaseConfig()', () => {
"git": {
"commit": false,
"commitArgs": "",
"commitMessage": "",
"commitMessage": "chore(release): publish {version}",
"stageChanges": true,
"tag": false,
"tagArgs": "",
"tagMessage": "",
@ -197,7 +199,7 @@ describe('createNxReleaseConfig()', () => {
"git": {
"commit": false,
"commitArgs": "",
"commitMessage": "",
"commitMessage": "chore(release): publish {version}",
"tag": false,
"tagArgs": "",
"tagMessage": "",
@ -218,7 +220,7 @@ describe('createNxReleaseConfig()', () => {
"git": {
"commit": false,
"commitArgs": "",
"commitMessage": "",
"commitMessage": "chore(release): publish {version}",
"tag": false,
"tagArgs": "",
"tagMessage": "",
@ -247,7 +249,8 @@ describe('createNxReleaseConfig()', () => {
"git": {
"commit": false,
"commitArgs": "",
"commitMessage": "",
"commitMessage": "chore(release): publish {version}",
"stageChanges": true,
"tag": false,
"tagArgs": "",
"tagMessage": "",
@ -286,7 +289,7 @@ describe('createNxReleaseConfig()', () => {
"git": {
"commit": false,
"commitArgs": "",
"commitMessage": "",
"commitMessage": "chore(release): publish {version}",
"tag": false,
"tagArgs": "",
"tagMessage": "",
@ -307,7 +310,7 @@ describe('createNxReleaseConfig()', () => {
"git": {
"commit": false,
"commitArgs": "",
"commitMessage": "",
"commitMessage": "chore(release): publish {version}",
"tag": false,
"tagArgs": "",
"tagMessage": "",
@ -336,7 +339,8 @@ describe('createNxReleaseConfig()', () => {
"git": {
"commit": false,
"commitArgs": "",
"commitMessage": "",
"commitMessage": "chore(release): publish {version}",
"stageChanges": true,
"tag": false,
"tagArgs": "",
"tagMessage": "",
@ -365,7 +369,7 @@ describe('createNxReleaseConfig()', () => {
"git": {
"commit": false,
"commitArgs": "",
"commitMessage": "",
"commitMessage": "chore(release): publish {version}",
"tag": false,
"tagArgs": "",
"tagMessage": "",
@ -386,7 +390,7 @@ describe('createNxReleaseConfig()', () => {
"git": {
"commit": false,
"commitArgs": "",
"commitMessage": "",
"commitMessage": "chore(release): publish {version}",
"tag": false,
"tagArgs": "",
"tagMessage": "",
@ -413,7 +417,8 @@ describe('createNxReleaseConfig()', () => {
"git": {
"commit": false,
"commitArgs": "",
"commitMessage": "",
"commitMessage": "chore(release): publish {version}",
"stageChanges": true,
"tag": false,
"tagArgs": "",
"tagMessage": "",
@ -440,7 +445,7 @@ describe('createNxReleaseConfig()', () => {
"git": {
"commit": false,
"commitArgs": "",
"commitMessage": "",
"commitMessage": "chore(release): publish {version}",
"tag": false,
"tagArgs": "",
"tagMessage": "",
@ -461,7 +466,7 @@ describe('createNxReleaseConfig()', () => {
"git": {
"commit": false,
"commitArgs": "",
"commitMessage": "",
"commitMessage": "chore(release): publish {version}",
"tag": false,
"tagArgs": "",
"tagMessage": "",
@ -489,7 +494,8 @@ describe('createNxReleaseConfig()', () => {
"git": {
"commit": false,
"commitArgs": "",
"commitMessage": "",
"commitMessage": "chore(release): publish {version}",
"stageChanges": true,
"tag": false,
"tagArgs": "",
"tagMessage": "",
@ -528,7 +534,7 @@ describe('createNxReleaseConfig()', () => {
"git": {
"commit": false,
"commitArgs": "",
"commitMessage": "",
"commitMessage": "chore(release): publish {version}",
"tag": false,
"tagArgs": "",
"tagMessage": "",
@ -549,7 +555,7 @@ describe('createNxReleaseConfig()', () => {
"git": {
"commit": false,
"commitArgs": "",
"commitMessage": "",
"commitMessage": "chore(release): publish {version}",
"tag": false,
"tagArgs": "",
"tagMessage": "",
@ -590,7 +596,8 @@ describe('createNxReleaseConfig()', () => {
"git": {
"commit": false,
"commitArgs": "",
"commitMessage": "",
"commitMessage": "chore(release): publish {version}",
"stageChanges": true,
"tag": false,
"tagArgs": "",
"tagMessage": "",
@ -618,7 +625,7 @@ describe('createNxReleaseConfig()', () => {
"git": {
"commit": false,
"commitArgs": "",
"commitMessage": "",
"commitMessage": "chore(release): publish {version}",
"tag": false,
"tagArgs": "",
"tagMessage": "",
@ -639,7 +646,7 @@ describe('createNxReleaseConfig()', () => {
"git": {
"commit": false,
"commitArgs": "",
"commitMessage": "",
"commitMessage": "chore(release): publish {version}",
"tag": false,
"tagArgs": "",
"tagMessage": "",
@ -676,7 +683,8 @@ describe('createNxReleaseConfig()', () => {
"git": {
"commit": false,
"commitArgs": "",
"commitMessage": "",
"commitMessage": "chore(release): publish {version}",
"stageChanges": true,
"tag": false,
"tagArgs": "",
"tagMessage": "",
@ -706,7 +714,7 @@ describe('createNxReleaseConfig()', () => {
"git": {
"commit": false,
"commitArgs": "",
"commitMessage": "",
"commitMessage": "chore(release): publish {version}",
"tag": false,
"tagArgs": "",
"tagMessage": "",
@ -727,7 +735,7 @@ describe('createNxReleaseConfig()', () => {
"git": {
"commit": false,
"commitArgs": "",
"commitMessage": "",
"commitMessage": "chore(release): publish {version}",
"tag": false,
"tagArgs": "",
"tagMessage": "",
@ -760,7 +768,8 @@ describe('createNxReleaseConfig()', () => {
"git": {
"commit": false,
"commitArgs": "",
"commitMessage": "",
"commitMessage": "chore(release): publish {version}",
"stageChanges": true,
"tag": false,
"tagArgs": "",
"tagMessage": "",
@ -786,7 +795,7 @@ describe('createNxReleaseConfig()', () => {
"git": {
"commit": true,
"commitArgs": "--no-verify",
"commitMessage": "",
"commitMessage": "chore(release): publish {version}",
"tag": false,
"tagArgs": "",
"tagMessage": "",
@ -807,7 +816,7 @@ describe('createNxReleaseConfig()', () => {
"git": {
"commit": true,
"commitArgs": "--no-verify",
"commitMessage": "",
"commitMessage": "chore(release): publish {version}",
"tag": false,
"tagArgs": "",
"tagMessage": "",
@ -836,7 +845,8 @@ describe('createNxReleaseConfig()', () => {
"git": {
"commit": true,
"commitArgs": "--no-verify",
"commitMessage": "",
"commitMessage": "chore(release): publish {version}",
"stageChanges": true,
"tag": false,
"tagArgs": "",
"tagMessage": "",
@ -868,7 +878,7 @@ describe('createNxReleaseConfig()', () => {
"git": {
"commit": false,
"commitArgs": "",
"commitMessage": "",
"commitMessage": "chore(release): publish {version}",
"tag": false,
"tagArgs": "",
"tagMessage": "",
@ -889,7 +899,7 @@ describe('createNxReleaseConfig()', () => {
"git": {
"commit": false,
"commitArgs": "",
"commitMessage": "",
"commitMessage": "chore(release): publish {version}",
"tag": false,
"tagArgs": "",
"tagMessage": "",
@ -918,7 +928,8 @@ describe('createNxReleaseConfig()', () => {
"git": {
"commit": true,
"commitArgs": "--no-verify",
"commitMessage": "",
"commitMessage": "chore(release): publish {version}",
"stageChanges": true,
"tag": true,
"tagArgs": "",
"tagMessage": "",
@ -963,7 +974,7 @@ describe('createNxReleaseConfig()', () => {
"git": {
"commit": false,
"commitArgs": "",
"commitMessage": "",
"commitMessage": "chore(release): publish {version}",
"tag": false,
"tagArgs": "",
"tagMessage": "",
@ -984,7 +995,7 @@ describe('createNxReleaseConfig()', () => {
"git": {
"commit": false,
"commitArgs": "",
"commitMessage": "",
"commitMessage": "chore(release): publish {version}",
"tag": false,
"tagArgs": "",
"tagMessage": "",
@ -1011,7 +1022,8 @@ describe('createNxReleaseConfig()', () => {
"git": {
"commit": false,
"commitArgs": "",
"commitMessage": "",
"commitMessage": "chore(release): publish {version}",
"stageChanges": true,
"tag": false,
"tagArgs": "",
"tagMessage": "",
@ -1036,7 +1048,7 @@ describe('createNxReleaseConfig()', () => {
"git": {
"commit": false,
"commitArgs": "",
"commitMessage": "",
"commitMessage": "chore(release): publish {version}",
"tag": false,
"tagArgs": "",
"tagMessage": "",
@ -1057,7 +1069,7 @@ describe('createNxReleaseConfig()', () => {
"git": {
"commit": false,
"commitArgs": "",
"commitMessage": "",
"commitMessage": "chore(release): publish {version}",
"tag": false,
"tagArgs": "",
"tagMessage": "",
@ -1086,7 +1098,8 @@ describe('createNxReleaseConfig()', () => {
"git": {
"commit": false,
"commitArgs": "",
"commitMessage": "",
"commitMessage": "chore(release): publish {version}",
"stageChanges": true,
"tag": false,
"tagArgs": "",
"tagMessage": "",
@ -1121,7 +1134,7 @@ describe('createNxReleaseConfig()', () => {
"git": {
"commit": false,
"commitArgs": "",
"commitMessage": "",
"commitMessage": "chore(release): publish {version}",
"tag": false,
"tagArgs": "",
"tagMessage": "",
@ -1142,7 +1155,7 @@ describe('createNxReleaseConfig()', () => {
"git": {
"commit": false,
"commitArgs": "",
"commitMessage": "",
"commitMessage": "chore(release): publish {version}",
"tag": false,
"tagArgs": "",
"tagMessage": "",
@ -1182,7 +1195,8 @@ describe('createNxReleaseConfig()', () => {
"git": {
"commit": false,
"commitArgs": "",
"commitMessage": "",
"commitMessage": "chore(release): publish {version}",
"stageChanges": true,
"tag": false,
"tagArgs": "",
"tagMessage": "",
@ -1210,7 +1224,7 @@ describe('createNxReleaseConfig()', () => {
"git": {
"commit": false,
"commitArgs": "",
"commitMessage": "",
"commitMessage": "chore(release): publish {version}",
"tag": false,
"tagArgs": "",
"tagMessage": "",
@ -1221,7 +1235,7 @@ describe('createNxReleaseConfig()', () => {
"git": {
"commit": false,
"commitArgs": "",
"commitMessage": "",
"commitMessage": "chore(release): publish {version}",
"tag": false,
"tagArgs": "",
"tagMessage": "",
@ -1250,7 +1264,8 @@ describe('createNxReleaseConfig()', () => {
"git": {
"commit": false,
"commitArgs": "",
"commitMessage": "",
"commitMessage": "chore(release): publish {version}",
"stageChanges": true,
"tag": false,
"tagArgs": "",
"tagMessage": "",
@ -1286,7 +1301,7 @@ describe('createNxReleaseConfig()', () => {
"git": {
"commit": false,
"commitArgs": "",
"commitMessage": "",
"commitMessage": "chore(release): publish {version}",
"tag": false,
"tagArgs": "",
"tagMessage": "",
@ -1317,7 +1332,7 @@ describe('createNxReleaseConfig()', () => {
"git": {
"commit": false,
"commitArgs": "",
"commitMessage": "",
"commitMessage": "chore(release): publish {version}",
"tag": false,
"tagArgs": "",
"tagMessage": "",
@ -1356,7 +1371,8 @@ describe('createNxReleaseConfig()', () => {
"git": {
"commit": false,
"commitArgs": "",
"commitMessage": "",
"commitMessage": "chore(release): publish {version}",
"stageChanges": true,
"tag": false,
"tagArgs": "",
"tagMessage": "",
@ -1382,7 +1398,7 @@ describe('createNxReleaseConfig()', () => {
"git": {
"commit": false,
"commitArgs": "",
"commitMessage": "",
"commitMessage": "chore(release): publish {version}",
"tag": false,
"tagArgs": "",
"tagMessage": "",
@ -1413,7 +1429,7 @@ describe('createNxReleaseConfig()', () => {
"git": {
"commit": false,
"commitArgs": "",
"commitMessage": "",
"commitMessage": "chore(release): publish {version}",
"tag": false,
"tagArgs": "",
"tagMessage": "",
@ -1452,7 +1468,8 @@ describe('createNxReleaseConfig()', () => {
"git": {
"commit": false,
"commitArgs": "",
"commitMessage": "",
"commitMessage": "chore(release): publish {version}",
"stageChanges": true,
"tag": false,
"tagArgs": "",
"tagMessage": "",
@ -1506,7 +1523,7 @@ describe('createNxReleaseConfig()', () => {
"git": {
"commit": false,
"commitArgs": "",
"commitMessage": "",
"commitMessage": "chore(release): publish {version}",
"tag": false,
"tagArgs": "",
"tagMessage": "",
@ -1537,7 +1554,7 @@ describe('createNxReleaseConfig()', () => {
"git": {
"commit": false,
"commitArgs": "",
"commitMessage": "",
"commitMessage": "chore(release): publish {version}",
"tag": false,
"tagArgs": "",
"tagMessage": "",
@ -1608,7 +1625,8 @@ describe('createNxReleaseConfig()', () => {
"git": {
"commit": false,
"commitArgs": "",
"commitMessage": "",
"commitMessage": "chore(release): publish {version}",
"stageChanges": true,
"tag": false,
"tagArgs": "",
"tagMessage": "",
@ -1649,7 +1667,7 @@ describe('createNxReleaseConfig()', () => {
"git": {
"commit": false,
"commitArgs": "",
"commitMessage": "",
"commitMessage": "chore(release): publish {version}",
"tag": false,
"tagArgs": "",
"tagMessage": "",
@ -1680,7 +1698,7 @@ describe('createNxReleaseConfig()', () => {
"git": {
"commit": false,
"commitArgs": "",
"commitMessage": "",
"commitMessage": "chore(release): publish {version}",
"tag": false,
"tagArgs": "",
"tagMessage": "",
@ -1739,7 +1757,8 @@ describe('createNxReleaseConfig()', () => {
"git": {
"commit": false,
"commitArgs": "",
"commitMessage": "",
"commitMessage": "chore(release): publish {version}",
"stageChanges": true,
"tag": false,
"tagArgs": "",
"tagMessage": "",
@ -2034,7 +2053,7 @@ describe('createNxReleaseConfig()', () => {
"git": {
"commit": false,
"commitArgs": "",
"commitMessage": "",
"commitMessage": "chore(release): publish {version}",
"tag": false,
"tagArgs": "",
"tagMessage": "",
@ -2055,7 +2074,7 @@ describe('createNxReleaseConfig()', () => {
"git": {
"commit": false,
"commitArgs": "",
"commitMessage": "",
"commitMessage": "chore(release): publish {version}",
"tag": false,
"tagArgs": "",
"tagMessage": "",
@ -2095,7 +2114,8 @@ describe('createNxReleaseConfig()', () => {
"git": {
"commit": false,
"commitArgs": "",
"commitMessage": "",
"commitMessage": "chore(release): publish {version}",
"stageChanges": true,
"tag": false,
"tagArgs": "",
"tagMessage": "",

View File

@ -99,12 +99,21 @@ export async function createNxReleaseConfig(
const gitDefaults = {
commit: false,
commitMessage: '',
commitMessage: 'chore(release): publish {version}',
commitArgs: '',
tag: false,
tagMessage: '',
tagArgs: '',
};
const versionGitDefaults: NxReleaseConfig['version']['git'] = {
...gitDefaults,
stageChanges: true,
};
const changelogGitDefaults = {
...gitDefaults,
commit: true,
tag: true,
};
const defaultFixedReleaseTagPattern = 'v{version}';
const defaultIndependentReleaseTagPattern = '{projectName}@{version}';
@ -117,12 +126,12 @@ export async function createNxReleaseConfig(
projectsRelationship: workspaceProjectsRelationship,
git: gitDefaults,
version: {
git: gitDefaults,
git: versionGitDefaults,
generator: '@nx/js:release-version',
generatorOptions: {},
},
changelog: {
git: gitDefaults,
git: changelogGitDefaults,
workspaceChangelog: {
createRelease: false,
entryWhenNoChanges:

View File

@ -12,10 +12,10 @@ describe('filterReleaseGroups()', () => {
groups: {},
changelog: {
git: {
commit: false,
commit: true,
commitMessage: '',
commitArgs: '',
tag: false,
tag: true,
tagMessage: '',
tagArgs: '',
},
@ -26,6 +26,7 @@ describe('filterReleaseGroups()', () => {
generator: '',
generatorOptions: {},
git: {
stageChanges: true,
commit: false,
commitMessage: '',
commitArgs: '',

View File

@ -56,9 +56,11 @@ export async function release(
const versionResult: NxReleaseVersionResult = await releaseVersion({
...args,
// if enabled, committing and tagging will be handled by the changelog
// command, so we should only stage the changes in the version command
stageChanges: nxReleaseConfig.git?.commit,
// We should stage the changes in the version command only if
// the changelog command will actually be committing the files.
// Since git.commit defaults to true for the changelog command, we
// only need to disable staging if git.commit is explicitly set to false.
stageChanges: nxReleaseConfig.git?.commit ?? true,
gitCommit: false,
gitTag: false,
});

View File

@ -76,12 +76,9 @@ export function createCommitMessageValues(
releaseGroups: ReleaseGroupWithName[],
releaseGroupToFilteredProjects: Map<ReleaseGroupWithName, Set<string>>,
versionData: VersionData,
userCommitMessage?: string
commitMessage: string
): string[] {
const defaultCommitMessage = `chore(release): publish {version}`;
const commitMessageValues = userCommitMessage
? [userCommitMessage]
: [defaultCommitMessage];
const commitMessageValues = [commitMessage];
if (releaseGroups.length === 0) {
return commitMessageValues;
@ -115,7 +112,7 @@ export function createCommitMessageValues(
if (
releaseGroups.length === 1 &&
releaseGroups[0].projectsRelationship === 'independent' &&
userCommitMessage?.includes('{projectName}')
commitMessage.includes('{projectName}')
) {
const releaseGroup = releaseGroups[0];
const releaseGroupProjectNames = Array.from(

View File

@ -120,7 +120,7 @@ export async function releaseVersion(
const tree = new FsTree(workspaceRoot, args.verbose);
const versionData: VersionData = {};
const userCommitMessage: string | undefined =
const commitMessage: string | undefined =
args.gitCommitMessage || nxReleaseConfig.version.git.commitMessage;
if (args.projects?.length) {
@ -180,17 +180,6 @@ export async function releaseVersion(
};
}
if (args.stageChanges) {
output.logSingleLine(
`Staging changed files with git because --stage-changes was set`
);
await gitAdd({
changedFiles,
dryRun: args.dryRun,
verbose: args.verbose,
});
}
if (args.gitCommit ?? nxReleaseConfig.version.git.commit) {
await commitChanges(
tree.listChanges().map((f) => f.path),
@ -200,10 +189,17 @@ export async function releaseVersion(
releaseGroups,
releaseGroupToFilteredProjects,
versionData,
userCommitMessage
commitMessage
),
args.gitCommitArgs || nxReleaseConfig.version.git.commitArgs
);
} else if (args.stageChanges ?? nxReleaseConfig.version.git.stageChanges) {
output.logSingleLine(`Staging changed files with git`);
await gitAdd({
changedFiles,
dryRun: args.dryRun,
verbose: args.verbose,
});
}
if (args.gitTag ?? nxReleaseConfig.version.git.tag) {
@ -294,17 +290,6 @@ export async function releaseVersion(
};
}
if (args.stageChanges) {
output.logSingleLine(
`Staging changed files with git because --stage-changes was set`
);
await gitAdd({
changedFiles,
dryRun: args.dryRun,
verbose: args.verbose,
});
}
if (args.gitCommit ?? nxReleaseConfig.version.git.commit) {
await commitChanges(
changedFiles,
@ -314,10 +299,17 @@ export async function releaseVersion(
releaseGroups,
releaseGroupToFilteredProjects,
versionData,
userCommitMessage
commitMessage
),
args.gitCommitArgs || nxReleaseConfig.version.git.commitArgs
);
} else if (args.stageChanges ?? nxReleaseConfig.version.git.stageChanges) {
output.logSingleLine(`Staging changed files with git`);
await gitAdd({
changedFiles,
dryRun: args.dryRun,
verbose: args.verbose,
});
}
if (args.gitTag ?? nxReleaseConfig.version.git.tag) {

View File

@ -219,7 +219,12 @@ interface NxReleaseConfiguration {
/**
* Enable or override configuration for git operations as part of the version subcommand
*/
git?: NxReleaseGitConfiguration;
git?: NxReleaseGitConfiguration & {
/**
* Whether or not to stage the changes made by this command. Useful when combining the version command with changelog generation.
*/
stageChanges?: boolean;
};
};
/**
* Optionally override the git/release tag pattern to use. This field is the source of truth

View File

@ -0,0 +1,250 @@
import { NxJsonConfiguration } from '../../config/nx-json';
import { createTree } from '../../generators/testing-utils/create-tree';
import { createTreeWithEmptyWorkspace } from '../../generators/testing-utils/create-tree-with-empty-workspace';
import { Tree } from '../../generators/tree';
import { readJson, writeJson } from '../../generators/utils/json';
import nxReleaseGitOperationsExplicitOptOut from './nx-release-git-operations-explicit-opt-out';
describe('nxReleaseGitOperationsExplicitOptOut', () => {
let tree: Tree;
beforeEach(() => {
tree = createTreeWithEmptyWorkspace();
});
it('should not error if nx.json is not present', () => {
nxReleaseGitOperationsExplicitOptOut(createTree());
});
it('should do nothing if release is not configured', () => {
writeJson(tree, 'nx.json', {});
nxReleaseGitOperationsExplicitOptOut(tree);
const nxJson = readJson<NxJsonConfiguration>(tree, 'nx.json');
expect(nxJson.release).toBeUndefined();
});
it('should set commit and tag to false if changelog.git.commit is not defined', () => {
writeJson(tree, 'nx.json', {
release: {
version: {
git: {
// override properties in version.git should be ignored
commit: true,
tag: true,
},
},
},
});
nxReleaseGitOperationsExplicitOptOut(tree);
const nxJson = readJson<NxJsonConfiguration>(tree, 'nx.json');
expect(nxJson.release).toEqual({
git: {
commit: false,
tag: false,
},
version: {
git: {
commit: true,
tag: true,
},
},
});
});
it('should not set commit and tag to false if changelog.git.commit is not defined but global git config is defined', () => {
writeJson(tree, 'nx.json', {
release: {
git: {
commit: false,
tag: true,
},
version: {
git: {
// override properties in version.git should be ignored
commit: true,
tag: true,
},
},
},
});
nxReleaseGitOperationsExplicitOptOut(tree);
const nxJson = readJson<NxJsonConfiguration>(tree, 'nx.json');
expect(nxJson.release).toEqual({
git: {
commit: false,
tag: true,
},
version: {
git: {
commit: true,
tag: true,
},
},
});
});
describe('stageChanges', () => {
it('should set version.stageChanges to false if committing is not explicitly enabled and granular version.git config exists', () => {
writeJson(tree, 'nx.json', {
release: {
version: {
git: {},
},
},
});
nxReleaseGitOperationsExplicitOptOut(tree);
const nxJson = readJson<NxJsonConfiguration>(tree, 'nx.json');
expect(nxJson.release).toEqual({
git: {
commit: false,
tag: false,
},
version: {
git: {
stageChanges: false,
},
},
});
});
it('should set version.stageChanges to false if committing is not explicitly enabled and granular changelog.git config exists', () => {
writeJson(tree, 'nx.json', {
release: {
changelog: {
git: {},
},
},
});
nxReleaseGitOperationsExplicitOptOut(tree);
const nxJson = readJson<NxJsonConfiguration>(tree, 'nx.json');
expect(nxJson.release).toEqual({
git: {
commit: false,
tag: false,
},
changelog: {
git: {},
},
version: {
git: {
stageChanges: false,
},
},
});
});
it('should not set version.stageChanges if granular config is not found', () => {
writeJson(tree, 'nx.json', {
release: {
git: {
commit: false,
tag: false,
},
},
});
nxReleaseGitOperationsExplicitOptOut(tree);
const nxJson = readJson<NxJsonConfiguration>(tree, 'nx.json');
expect(nxJson.release).toEqual({
git: {
commit: false,
tag: false,
},
});
});
it('should not set version.stageChanges if committing is enabled globally', () => {
writeJson(tree, 'nx.json', {
release: {
git: {
commit: true,
tag: false,
},
},
});
nxReleaseGitOperationsExplicitOptOut(tree);
const nxJson = readJson<NxJsonConfiguration>(tree, 'nx.json');
expect(nxJson.release).toEqual({
git: {
commit: true,
tag: false,
},
});
});
it('should not set version.stageChanges if committing is enabled in version', () => {
writeJson(tree, 'nx.json', {
release: {
git: {
commit: false,
tag: false,
},
version: {
git: {
commit: true,
},
},
},
});
nxReleaseGitOperationsExplicitOptOut(tree);
const nxJson = readJson<NxJsonConfiguration>(tree, 'nx.json');
expect(nxJson.release).toEqual({
git: {
commit: false,
tag: false,
},
version: {
git: {
commit: true,
},
},
});
});
it('should not set version.stageChanges if committing is enabled in changelog', () => {
writeJson(tree, 'nx.json', {
release: {
git: {
commit: false,
tag: false,
},
changelog: {
git: {
commit: true,
},
},
},
});
nxReleaseGitOperationsExplicitOptOut(tree);
const nxJson = readJson<NxJsonConfiguration>(tree, 'nx.json');
expect(nxJson.release).toEqual({
git: {
commit: false,
tag: false,
},
changelog: {
git: {
commit: true,
},
},
});
});
});
});

View File

@ -0,0 +1,58 @@
import { NxJsonConfiguration } from '../../config/nx-json';
import { Tree } from '../../generators/tree';
import { updateJson } from '../../generators/utils/json';
export default function nxReleaseGitOperationsExplicitOptOut(tree: Tree) {
if (!tree.exists('nx.json')) {
return;
}
updateJson<NxJsonConfiguration>(tree, 'nx.json', (nxJson) => {
if (!nxJson.release) {
return nxJson;
}
nxJson.release.git = nxJson.release.git ?? {};
// Explicitly opt out of git operations if not defined
// to preserve the old behavior
if (
nxJson.release.git.commit === undefined &&
nxJson.release.changelog?.git?.commit === undefined
) {
nxJson.release.git.commit = false;
}
if (
nxJson.release.git.tag === undefined &&
nxJson.release.changelog?.git?.tag === undefined
) {
nxJson.release.git.tag = false;
}
// Opt out of staging changes in version only if using
// granular git config AND if committing is not enabled.
// We don't want to add granular git config if it doesn't already exist,
// because the nx release meta command is incompatible with it.
if (
(nxJson.release.version?.git || nxJson.release.changelog?.git) &&
!nxJson.release.git.commit &&
!nxJson.release.version?.git?.commit &&
!nxJson.release.changelog?.git?.commit
) {
nxJson.release.version = {
...nxJson.release.version,
git: {
...nxJson.release.version?.git,
stageChanges: false,
},
};
}
// Ensure that we don't leave any empty object properties in their config
if (Object.keys(nxJson.release.git).length === 0) {
delete nxJson.release.git;
}
return nxJson;
});
}