Skip to content

Commit

Permalink
fix(release): move github release creation to git tasks (#21510)
Browse files Browse the repository at this point in the history
  • Loading branch information
fahslaj authored Feb 14, 2024
1 parent f4dd440 commit b625a79
Show file tree
Hide file tree
Showing 10 changed files with 595 additions and 302 deletions.
4 changes: 2 additions & 2 deletions docs/generated/devkit/FileChange.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Description of a file change in the Nx virtual file system/
- [content](../../devkit/documents/FileChange#content): Buffer
- [options](../../devkit/documents/FileChange#options): TreeWriteOptions
- [path](../../devkit/documents/FileChange#path): string
- [type](../../devkit/documents/FileChange#type): "DELETE" | "CREATE" | "UPDATE"
- [type](../../devkit/documents/FileChange#type): "CREATE" | "DELETE" | "UPDATE"

## Properties

Expand Down Expand Up @@ -39,6 +39,6 @@ Path relative to the workspace root

### type

**type**: `"DELETE"` \| `"CREATE"` \| `"UPDATE"`
**type**: `"CREATE"` \| `"DELETE"` \| `"UPDATE"`

Type of change: 'CREATE' | 'DELETE' | 'UPDATE'
6 changes: 1 addition & 5 deletions docs/shared/features/manage-releases.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,16 +105,12 @@ import * as yargs from 'yargs';
verbose: options.verbose,
});

// The returned number value from releaseChangelog will be non-zero if something went wrong
const changelogStatus = await releaseChangelog({
await releaseChangelog({
versionData: projectsVersionData,
version: workspaceVersion,
dryRun: options.dryRun,
verbose: options.verbose,
});
if (changelogStatus !== 0) {
process.exit(changelogStatus);
}

// The returned number value from releasePublish will be zero if all projects are published successfully, non-zero if not
const publishStatus = await releasePublish({
Expand Down
165 changes: 165 additions & 0 deletions e2e/release/src/create-github-release.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
import { NxJsonConfiguration } from '@nx/devkit';
import {
cleanupProject,
newProject,
runCLI,
runCommandAsync,
uniq,
updateJson,
} from '@nx/e2e/utils';

expect.addSnapshotSerializer({
serialize(str: string) {
return (
str
// Remove all output unique to specific projects to ensure deterministic snapshots
.replaceAll(/my-pkg-\d+/g, '{project-name}')
.replaceAll(
/integrity:\s*.*/g,
'integrity: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
)
.replaceAll(/\b[0-9a-f]{40}\b/g, '{SHASUM}')
.replaceAll(/\d*B index\.js/g, 'XXB index.js')
.replaceAll(/\d*B project\.json/g, 'XXB project.json')
.replaceAll(/\d*B package\.json/g, 'XXXB package.json')
.replaceAll(/size:\s*\d*\s?B/g, 'size: XXXB')
.replaceAll(/\d*\.\d*\s?kB/g, 'XXX.XXX kb')
.replaceAll(/[a-fA-F0-9]{7}/g, '{COMMIT_SHA}')
.replaceAll(/Test @[\w\d]+/g, 'Test @{COMMIT_AUTHOR}')
// Normalize the version title date.
.replaceAll(/\(\d{4}-\d{2}-\d{2}\)/g, '(YYYY-MM-DD)')
// We trim each line to reduce the chances of snapshot flakiness
.split('\n')
.map((r) => r.trim())
.join('\n')
);
},
test(val: string) {
return val != null && typeof val === 'string';
},
});

describe('nx release create github release', () => {
let pkg1: string;
let pkg2: string;
let pkg3: string;

beforeAll(async () => {
newProject({
unsetProjectNameAndRootFormat: false,
packages: ['@nx/js'],
});

pkg1 = uniq('my-pkg-1');
runCLI(`generate @nx/workspace:npm-package ${pkg1}`);

pkg2 = uniq('my-pkg-2');
runCLI(`generate @nx/workspace:npm-package ${pkg2}`);

pkg3 = uniq('my-pkg-3');
runCLI(`generate @nx/workspace:npm-package ${pkg3}`);

// Update pkg2 to depend on pkg1
updateJson(`${pkg2}/package.json`, (json) => {
json.dependencies ??= {};
json.dependencies[`@proj/${pkg1}`] = '0.0.0';
return json;
});

// Normalize git committer information so it is deterministic in snapshots
await runCommandAsync(`git config user.email "[email protected]"`);
await runCommandAsync(`git config user.name "Test"`);

// update my-pkg-1 with a fix commit
updateJson(`${pkg1}/package.json`, (json) => ({
...json,
license: 'MIT',
}));
await runCommandAsync(`git add ${pkg1}/package.json`);
await runCommandAsync(`git commit -m "fix(${pkg1}): fix 1"`);

// update my-pkg-2 with a breaking change
updateJson(`${pkg2}/package.json`, (json) => ({
...json,
license: 'GNU GPLv3',
}));
await runCommandAsync(`git add ${pkg2}/package.json`);
await runCommandAsync(`git commit -m "feat(${pkg2})!: breaking change 2"`);

// update my-pkg-3 with a feature commit
updateJson(`${pkg3}/package.json`, (json) => ({
...json,
license: 'GNU GPLv3',
}));
await runCommandAsync(`git add ${pkg3}/package.json`);
await runCommandAsync(`git commit -m "feat(${pkg3}): feat 3"`);

// We need a valid git origin to exist for the commit references to work (and later the test for createRelease)
await runCommandAsync(
`git remote add origin https://github.com/nrwl/fake-repo.git`
);
});
afterAll(() => cleanupProject());

it('should create github release for the first release', async () => {
updateJson<NxJsonConfiguration>('nx.json', (nxJson) => {
nxJson.release = {
changelog: {
workspaceChangelog: {
createRelease: 'github',
},
},
};
return nxJson;
});
const result = runCLI('release patch -d --first-release --verbose');

expect(
result.match(new RegExp(`> NX Pushing to git remote`, 'g')).length
).toEqual(1);
expect(
result.match(new RegExp(`> NX Creating GitHub Release`, 'g')).length
).toEqual(1);

// should have two occurrences of each - one for the changelog file, one for the github release
expect(result.match(new RegExp(`### 🚀 Features`, 'g')).length).toEqual(2);
expect(result.match(new RegExp(`### 🩹 Fixes`, 'g')).length).toEqual(2);
expect(
result.match(new RegExp(`#### ⚠️ Breaking Changes`, 'g')).length
).toEqual(2);
});

it('should create github releases for all independent packages', async () => {
updateJson<NxJsonConfiguration>('nx.json', (nxJson) => {
nxJson.release = {
projectsRelationship: 'independent',
version: {
conventionalCommits: true,
},
changelog: {
projectChangelogs: {
file: false,
createRelease: 'github',
},
},
};
return nxJson;
});

const result = runCLI('release -d --first-release --verbose');

expect(
result.match(new RegExp(`> NX Pushing to git remote`, 'g')).length
).toEqual(1);
expect(
result.match(new RegExp(`> NX Creating GitHub Release`, 'g')).length
).toEqual(3);

// should have one occurrence of each because files are disabled
expect(result.match(new RegExp(`### 🚀 Features`, 'g')).length).toEqual(2);
expect(result.match(new RegExp(`### 🩹 Fixes`, 'g')).length).toEqual(1);
expect(
result.match(new RegExp(`#### ⚠️ Breaking Changes`, 'g')).length
).toEqual(1);
});
});
33 changes: 30 additions & 3 deletions e2e/release/src/release.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -726,23 +726,23 @@ ${JSON.stringify(
> NX Previewing a GitHub release and an entry in {project-name}/CHANGELOG.md for v1000.0.0-next.0
> NX Previewing an entry in {project-name}/CHANGELOG.md for v1000.0.0-next.0
+ ## 1000.0.0-next.0
+
+ This was a version bump only for {project-name} to align it with other projects, there were no code changes.
> NX Previewing a GitHub release and an entry in {project-name}/CHANGELOG.md for v1000.0.0-next.0
> NX Previewing an entry in {project-name}/CHANGELOG.md for v1000.0.0-next.0
+ ## 1000.0.0-next.0
+
+ This was a version bump only for {project-name} to align it with other projects, there were no code changes.
> NX Previewing a GitHub release and an entry in {project-name}/CHANGELOG.md for v1000.0.0-next.0
> NX Previewing an entry in {project-name}/CHANGELOG.md for v1000.0.0-next.0
+ ## 1000.0.0-next.0
Expand All @@ -756,6 +756,33 @@ ${JSON.stringify(
> NX Tagging commit with git
> NX Pushing to git remote
> NX Creating GitHub Release
+ ## 1000.0.0-next.0
+
+ This was a version bump only for {project-name} to align it with other projects, there were no code changes.
> NX Creating GitHub Release
+ ## 1000.0.0-next.0
+
+ This was a version bump only for {project-name} to align it with other projects, there were no code changes.
> NX Creating GitHub Release
+ ## 1000.0.0-next.0
+
+ This was a version bump only for {project-name} to align it with other projects, there were no code changes.
`);

// port and process cleanup
Expand Down
Loading

0 comments on commit b625a79

Please sign in to comment.