Skip to content

Commit

Permalink
feat(node-workspace): use configured strategies to build new dependen…
Browse files Browse the repository at this point in the history
…t version bumps (#2112)

Co-authored-by: Jeff Ching <chingor@google.com>
  • Loading branch information
kinyoklion and chingor13 committed Dec 28, 2023
1 parent 611db3d commit de4088b
Show file tree
Hide file tree
Showing 10 changed files with 318 additions and 20 deletions.
76 changes: 76 additions & 0 deletions __snapshots__/node-workspace.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,82 @@ 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 includes headers for packages with configured strategies 1'] = `
:robot: I have created a release *beep* *boop*
---
<details><summary>@here/pkgA: 3.3.4</summary>
Release notes for path: node1, releaseType: node
</details>
<details><summary>pkgB: 2.2.3</summary>
## [2.2.3](https://github.com/googleapis/node-test-repo/compare/pkgB-v2.2.2...pkgB-v2.2.3) (1983-10-10)
### Dependencies
* The following workspace dependencies were updated
* dependencies
* @here/pkgA bumped from 3.3.3 to 3.3.4
</details>
<details><summary>pkgC: 1.1.2</summary>
## 1.1.2 (1983-10-10)
### Dependencies
* The following workspace dependencies were updated
* dependencies
* @here/pkgB bumped from 2.2.2 to 2.2.3
</details>
<details><summary>pkgE: 1.0.1</summary>
## 1.0.1 (1983-10-10)
### Dependencies
* The following workspace dependencies were updated
* dependencies
* @here/pkgA bumped to 3.3.4
</details>
---
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 includes headers for packages with configured strategies 2'] = `
other notes
`

exports['NodeWorkspace plugin run includes headers for packages with configured strategies 3'] = `
## [2.2.3](https://github.com/googleapis/node-test-repo/compare/pkgB-v2.2.2...pkgB-v2.2.3) (1983-10-10)
### Dependencies
* The following workspace dependencies were updated
* dependencies
* @here/pkgA bumped from 3.3.3 to 3.3.4
`

exports['NodeWorkspace plugin run includes headers for packages with configured strategies 4'] = `
## 1.1.2 (1983-10-10)
### Dependencies
* The following workspace dependencies were updated
* dependencies
* @here/pkgB bumped from 2.2.2 to 2.2.3
`

exports['NodeWorkspace plugin run respects version prefix 1'] = `
{
"name": "@here/plugin1",
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/cargo-workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,10 +233,10 @@ export class CargoWorkspace extends WorkspacePlugin<CrateInfo> {
return existingCandidate;
}

protected newCandidate(
protected async newCandidate(
pkg: CrateInfo,
updatedVersions: VersionsMap
): CandidateReleasePullRequest {
): Promise<CandidateReleasePullRequest> {
const version = updatedVersions.get(pkg.name);
if (!version) {
throw new Error(`Didn't find updated version for ${pkg.name}`);
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/maven-workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -394,10 +394,10 @@ export class MavenWorkspace extends WorkspacePlugin<MavenArtifact> {
}
return existingCandidate;
}
protected newCandidate(
protected async newCandidate(
artifact: MavenArtifact,
updatedVersions: VersionsMap
): CandidateReleasePullRequest {
): Promise<CandidateReleasePullRequest> {
const version = updatedVersions.get(artifact.name);
if (!version) {
throw new Error(`Didn't find updated version for ${artifact.name}`);
Expand Down
47 changes: 45 additions & 2 deletions src/plugins/node-workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ import {
addPath,
} from './workspace';
import {PatchVersionUpdate} from '../versioning-strategy';
import {Strategy} from '../strategy';
import {Commit} from '../commit';
import {Release} from '../release';
import {CompositeUpdater} from '../updaters/composite';
import {PackageJson, newVersionWithRange} from '../updaters/node/package-json';
import {Logger} from '../util/logger';
Expand Down Expand Up @@ -67,6 +70,10 @@ interface NodeWorkspaceOptions extends WorkspacePluginOptions {
*/
export class NodeWorkspace extends WorkspacePlugin<Package> {
private alwaysLinkLocal: boolean;

private strategiesByPath: Record<string, Strategy> = {};
private releasesByPath: Record<string, Release> = {};

readonly updatePeerDependencies: boolean;
constructor(
github: GitHub,
Expand Down Expand Up @@ -230,10 +237,10 @@ export class NodeWorkspace extends WorkspacePlugin<Package> {
}
return existingCandidate;
}
protected newCandidate(
protected async newCandidate(
pkg: Package,
updatedVersions: VersionsMap
): CandidateReleasePullRequest {
): Promise<CandidateReleasePullRequest> {
// Update version of the package
const newVersion = updatedVersions.get(pkg.name);
if (!newVersion) {
Expand All @@ -250,6 +257,30 @@ export class NodeWorkspace extends WorkspacePlugin<Package> {
updatedVersions,
this.logger
);

const strategy = this.strategiesByPath[updatedPackage.path];
const latestRelease = this.releasesByPath[updatedPackage.path];

const basePullRequest = strategy
? await strategy.buildReleasePullRequest([], latestRelease, false, [], {
newVersion,
})
: undefined;

if (basePullRequest) {
return this.updateCandidate(
{
path: pkg.path,
pullRequest: basePullRequest,
config: {
releaseType: 'node',
},
},
pkg,
updatedVersions
);
}

const pullRequest: ReleasePullRequest = {
title: PullRequestTitle.ofTargetBranch(this.targetBranch),
body: new PullRequestBody([
Expand Down Expand Up @@ -350,6 +381,18 @@ export class NodeWorkspace extends WorkspacePlugin<Package> {
: {}),
};
}

async preconfigure(
strategiesByPath: Record<string, Strategy>,
_commitsByPath: Record<string, Commit[]>,
_releasesByPath: Record<string, Release>
): Promise<Record<string, Strategy>> {
// Using preconfigure to siphon releases and strategies.
this.strategiesByPath = strategiesByPath;
this.releasesByPath = _releasesByPath;

return strategiesByPath;
}
}

function getChangelogDepsNotes(
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ export abstract class WorkspacePlugin<T> extends ManifestPlugin {
pkg
)}`
);
const newCandidate = this.newCandidate(pkg, updatedVersions);
const newCandidate = await this.newCandidate(pkg, updatedVersions);
if (newCandidatePaths.has(newCandidate.path)) {
this.logger.info(
`Already created new candidate for path: ${newCandidate.path}`
Expand Down Expand Up @@ -320,7 +320,7 @@ export abstract class WorkspacePlugin<T> extends ManifestPlugin {
protected abstract newCandidate(
pkg: T,
updatedVersions: VersionsMap
): CandidateReleasePullRequest;
): Promise<CandidateReleasePullRequest>;

/**
* Collect all packages being managed in this workspace.
Expand Down
16 changes: 8 additions & 8 deletions src/strategies/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

import {Strategy, BuildReleaseOptions} from '../strategy';
import {Strategy, BuildReleaseOptions, BumpReleaseOptions} from '../strategy';
import {GitHub} from '../github';
import {VersioningStrategy} from '../versioning-strategy';
import {Repository} from '../repository';
Expand Down Expand Up @@ -261,19 +261,19 @@ export abstract class BaseStrategy implements Strategy {
commits: ConventionalCommit[],
latestRelease?: Release,
draft?: boolean,
labels: string[] = []
labels: string[] = [],
bumpOnlyOptions?: BumpReleaseOptions
): Promise<ReleasePullRequest | undefined> {
const conventionalCommits = await this.postProcessCommits(commits);
this.logger.info(`Considering: ${conventionalCommits.length} commits`);
if (conventionalCommits.length === 0) {
if (!bumpOnlyOptions && conventionalCommits.length === 0) {
this.logger.info(`No commits for path: ${this.path}, skipping`);
return undefined;
}

const newVersion = await this.buildNewVersion(
conventionalCommits,
latestRelease
);
const newVersion =
bumpOnlyOptions?.newVersion ??
(await this.buildNewVersion(conventionalCommits, latestRelease));
const versionsMap = await this.updateVersionsMap(
await this.buildVersionsMap(conventionalCommits),
conventionalCommits,
Expand Down Expand Up @@ -309,7 +309,7 @@ export abstract class BaseStrategy implements Strategy {
latestRelease,
commits
);
if (this.changelogEmpty(releaseNotesBody)) {
if (!bumpOnlyOptions && this.changelogEmpty(releaseNotesBody)) {
this.logger.info(
`No user facing commits found since ${
latestRelease ? latestRelease.sha : 'beginning of time'
Expand Down
4 changes: 3 additions & 1 deletion src/strategies/java.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {DEFAULT_SNAPSHOT_LABELS} from '../manifest';
import {JavaReleased} from '../updaters/java/java-released';
import {mergeUpdates} from '../updaters/composite';
import {logger as defaultLogger} from '../util/logger';
import {BumpReleaseOptions} from '../strategy';

const CHANGELOG_SECTIONS = [
{type: 'feat', section: 'Features'},
Expand Down Expand Up @@ -77,7 +78,8 @@ export class Java extends BaseStrategy {
commits: ConventionalCommit[],
latestRelease?: Release,
draft?: boolean,
labels: string[] = []
labels: string[] = [],
_bumpOnlyOptions?: BumpReleaseOptions
): Promise<ReleasePullRequest | undefined> {
if (await this.needsSnapshot(commits, latestRelease)) {
this.logger.info('Repository needs a snapshot bump.');
Expand Down
6 changes: 4 additions & 2 deletions src/strategies/php-yoshi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {BranchName} from '../util/branch-name';
import {PullRequestBody} from '../util/pull-request-body';
import {GitHubFileContents} from '@google-automations/git-file-utils';
import {FileNotFoundError} from '../errors';
import {BumpReleaseOptions} from '../strategy';

const CHANGELOG_SECTIONS = [
{type: 'feat', section: 'Features'},
Expand Down Expand Up @@ -68,12 +69,13 @@ export class PHPYoshi extends BaseStrategy {
commits: Commit[],
latestRelease?: Release,
draft?: boolean,
labels: string[] = []
labels: string[] = [],
bumpOnlyOptions?: BumpReleaseOptions
): Promise<ReleasePullRequest | undefined> {
const conventionalCommits = await this.postProcessCommits(
parseConventionalCommits(commits, this.logger)
);
if (conventionalCommits.length === 0) {
if (!bumpOnlyOptions && conventionalCommits.length === 0) {
this.logger.info(`No commits for path: ${this.path}, skipping`);
return undefined;
}
Expand Down
11 changes: 10 additions & 1 deletion src/strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ export interface BuildReleaseOptions {
groupPullRequestTitlePattern?: string;
}

export interface BumpReleaseOptions {
newVersion: Version;
}

/**
* A strategy is responsible for determining which files are
* necessary to update in a release pull request.
Expand All @@ -39,6 +43,10 @@ export interface Strategy {
* component if available.
* @param {boolean} draft Optional. Whether or not to create the pull
* request as a draft. Defaults to `false`.
* @param {BumpReleaseOptions} bumpOnlyOptions Optional. Options, that when
* present, indicate a release should be created even if there are no
* conventional commits. This is used when a release is required for
* a dependency update with a workspace plugin.
* @returns {ReleasePullRequest | undefined} The release pull request to
* open for this path/component. Returns undefined if we should not
* open a pull request.
Expand All @@ -47,7 +55,8 @@ export interface Strategy {
commits: Commit[],
latestRelease?: Release,
draft?: boolean,
labels?: string[]
labels?: string[],
bumpOnlyOptions?: BumpReleaseOptions
): Promise<ReleasePullRequest | undefined>;

/**
Expand Down

0 comments on commit de4088b

Please sign in to comment.