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: node-workspace plugin should update package.json versions #1319

Merged
merged 4 commits into from
Mar 1, 2022
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
27 changes: 24 additions & 3 deletions __snapshots__/node-workspace.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Release notes for path: node1, releaseType: node
* @here/pkgA bumped from 3.3.3 to ^3.3.4
</details>

<details><summary>@here/pkgC: 1.1.1</summary>
<details><summary>@here/pkgC: 1.1.2</summary>

### Dependencies

Expand All @@ -31,6 +31,27 @@ Release notes for path: node1, releaseType: node
This PR was generated with [Release Please](https://github.com/googleapis/release-please). See [documentation](https://github.com/googleapis/release-please#release-please).
`

exports['NodeWorkspace plugin run appends dependency notes to an updated module 2'] = `
other notes
`

exports['NodeWorkspace plugin run appends dependency notes to an updated module 3'] = `
### Dependencies

* update dependency foo/bar to 1.2.3
* The following workspace dependencies were updated
* dependencies
* @here/pkgA bumped from 3.3.3 to ^3.3.4
`

exports['NodeWorkspace plugin run appends dependency notes to an updated module 4'] = `
### Dependencies

* The following workspace dependencies were updated
* dependencies
* @here/pkgB bumped from 2.2.2 to ^2.2.3
`

exports['NodeWorkspace plugin run combines node packages 1'] = `
:robot: I have created a release *beep* *boop*
---
Expand Down Expand Up @@ -74,7 +95,7 @@ exports['NodeWorkspace plugin run walks dependency tree and updates previously u
Release notes for path: node1, releaseType: node
</details>

<details><summary>@here/pkgB: 2.2.2</summary>
<details><summary>@here/pkgB: 2.2.3</summary>

### Dependencies

Expand All @@ -83,7 +104,7 @@ Release notes for path: node1, releaseType: node
* @here/pkgA bumped from 3.3.3 to ^3.3.4
</details>

<details><summary>@here/pkgC: 1.1.1</summary>
<details><summary>@here/pkgC: 1.1.2</summary>

### Dependencies

Expand Down
29 changes: 24 additions & 5 deletions src/plugins/node-workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,13 @@ export class NodeWorkspace extends WorkspacePlugin<Package> {
throw new Error(`Could not find graph package for ${pkg.name}`);
}
const updatedPackage = pkg.clone();
// Update version of the package
const newVersion = updatedVersions.get(updatedPackage.name);
if (newVersion) {
logger.info(`Updating ${updatedPackage.name} to ${newVersion}`);
updatedPackage.version = newVersion.toString();
}
// Update dependency versions
for (const [depName, resolved] of graphPackage.localDependencies) {
const depVersion = updatedVersions.get(depName);
if (depVersion && resolved.type !== 'directory') {
Expand All @@ -178,10 +185,13 @@ export class NodeWorkspace extends WorkspacePlugin<Package> {
jsonStringify(updatedPackage.toJSON(), updatedPackage.rawContent)
);
} else if (update.updater instanceof Changelog) {
update.updater.changelogEntry = appendDependenciesSectionToChangelog(
update.updater.changelogEntry,
dependencyNotes
);
if (dependencyNotes) {
update.updater.changelogEntry =
appendDependenciesSectionToChangelog(
update.updater.changelogEntry,
dependencyNotes
);
}
}
return update;
});
Expand Down Expand Up @@ -213,6 +223,12 @@ export class NodeWorkspace extends WorkspacePlugin<Package> {
throw new Error(`Could not find graph package for ${pkg.name}`);
}
const updatedPackage = pkg.clone();
// Update version of the package
const newVersion = updatedVersions.get(updatedPackage.name);
if (newVersion) {
logger.info(`Updating ${updatedPackage.name} to ${newVersion}`);
updatedPackage.version = newVersion.toString();
}
for (const [depName, resolved] of graphPackage.localDependencies) {
const depVersion = updatedVersions.get(depName);
if (depVersion && resolved.type !== 'directory') {
Expand Down Expand Up @@ -251,7 +267,10 @@ export class NodeWorkspace extends WorkspacePlugin<Package> {
createIfMissing: false,
updater: new Changelog({
version,
changelogEntry: dependencyNotes,
changelogEntry: appendDependenciesSectionToChangelog(
'',
dependencyNotes
),
}),
},
],
Expand Down
81 changes: 73 additions & 8 deletions test/plugins/node-workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@ import {
dateSafe,
assertNoHasUpdate,
buildMockCandidatePullRequest,
buildGitHubFileRaw,
} from '../helpers';
import {RawContent} from '../../src/updaters/raw-content';
import snapshot = require('snap-shot-it');
import {ManifestPlugin} from '../../src/plugin';
import {Changelog} from '../../src/updaters/changelog';

const sandbox = sinon.createSandbox();
const fixturesPath = './test/fixtures/plugins/node-workspace';
Expand All @@ -53,6 +55,30 @@ export function buildMockPackageUpdate(
};
}

function buildMockChangelogUpdate(
path: string,
versionString: string,
changelogEntry: string
): Update {
const cachedFileContents = buildGitHubFileRaw(changelogEntry);
return {
path,
createIfMissing: false,
cachedFileContents,
updater: new Changelog({
changelogEntry,
version: Version.parse(versionString),
}),
};
}

function assertHasVersionUpdate(update: Update, expectedVersion: string) {
expect(update.updater).instanceof(RawContent);
const updater = update.updater as RawContent;
const data = JSON.parse(updater.rawContent);
expect(data.version).to.eql(expectedVersion);
}

describe('NodeWorkspace plugin', () => {
let github: GitHub;
let plugin: ManifestPlugin;
Expand Down Expand Up @@ -170,10 +196,22 @@ describe('NodeWorkspace plugin', () => {
);
expect(nodeCandidate).to.not.be.undefined;
const updates = nodeCandidate!.pullRequest.updates;
assertHasUpdate(updates, 'node1/package.json', RawContent);
assertHasUpdate(updates, 'node2/package.json', RawContent);
assertHasUpdate(updates, 'node3/package.json', RawContent);
assertHasUpdate(updates, 'node4/package.json', RawContent);
assertHasVersionUpdate(
assertHasUpdate(updates, 'node1/package.json', RawContent),
'3.3.4'
);
assertHasVersionUpdate(
assertHasUpdate(updates, 'node2/package.json', RawContent),
'2.2.3'
);
assertHasVersionUpdate(
assertHasUpdate(updates, 'node3/package.json', RawContent),
'1.1.2'
);
assertHasVersionUpdate(
assertHasUpdate(updates, 'node4/package.json', RawContent),
'4.4.5'
);
snapshot(dateSafe(nodeCandidate!.pullRequest.body.toString()));
});
it('appends dependency notes to an updated module', async () => {
Expand All @@ -182,13 +220,25 @@ describe('NodeWorkspace plugin', () => {
const candidates: CandidateReleasePullRequest[] = [
buildMockCandidatePullRequest('node1', 'node', '3.3.4', '@here/pkgA', [
buildMockPackageUpdate('node1/package.json', 'node1/package.json'),
buildMockChangelogUpdate(
'node1/CHANGELOG.md',
'3.3.4',
'other notes'
),
]),
buildMockCandidatePullRequest(
'node2',
'node',
'2.2.3',
'@here/pkgB',
[buildMockPackageUpdate('node2/package.json', 'node2/package.json')],
[
buildMockPackageUpdate('node2/package.json', 'node2/package.json'),
buildMockChangelogUpdate(
'node2/CHANGELOG.md',
'3.3.4',
existingNotes
),
],
existingNotes
),
];
Expand All @@ -212,11 +262,26 @@ describe('NodeWorkspace plugin', () => {
);
expect(nodeCandidate).to.not.be.undefined;
const updates = nodeCandidate!.pullRequest.updates;
assertHasUpdate(updates, 'node1/package.json', RawContent);
assertHasUpdate(updates, 'node2/package.json', RawContent);
assertHasUpdate(updates, 'node3/package.json', RawContent);
assertHasVersionUpdate(
assertHasUpdate(updates, 'node1/package.json', RawContent),
'3.3.4'
);
assertHasVersionUpdate(
assertHasUpdate(updates, 'node2/package.json', RawContent),
'2.2.3'
);
assertHasVersionUpdate(
assertHasUpdate(updates, 'node3/package.json', RawContent),
'1.1.2'
);
assertNoHasUpdate(updates, 'node4/package.json');
snapshot(dateSafe(nodeCandidate!.pullRequest.body.toString()));
const update = assertHasUpdate(updates, 'node1/CHANGELOG.md', Changelog);
snapshot((update.updater as Changelog).changelogEntry);
const update2 = assertHasUpdate(updates, 'node2/CHANGELOG.md', Changelog);
snapshot((update2.updater as Changelog).changelogEntry);
const update3 = assertHasUpdate(updates, 'node3/CHANGELOG.md', Changelog);
snapshot((update3.updater as Changelog).changelogEntry);
});
});
});