Skip to content

Commit

Permalink
feat: add includeComponentInTag option for strategies and hook up t…
Browse files Browse the repository at this point in the history
…o `--monorepo-tags` (#1119)

* feat: add includeComponentInTag option for strategies

* hook up --monorepo-tags to includeComponentInTag
  • Loading branch information
chingor13 authored Nov 25, 2021
1 parent 4b6ae50 commit bf9aacd
Show file tree
Hide file tree
Showing 8 changed files with 177 additions and 16 deletions.
6 changes: 4 additions & 2 deletions __snapshots__/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ Options:
[string]
--repo-url GitHub URL to generate release for [required]
--dry-run Prepare but do not take action [boolean] [default: false]
--monorepo-tags include library name in tags and release branches
[boolean] [default: false]
--path release from path other than root directory [string]
--component name of component release is being minted for [string]
--package-name name of package release is being minted for [string]
Expand Down Expand Up @@ -151,8 +153,6 @@ Options:
the minor for non-breaking changes prior to
the first major release
[boolean] [default: false]
--monorepo-tags include library name in tags and release
branches [boolean] [default: false]
--extra-files extra files for the strategy to consider
[string]
--version-file path to version file to update, e.g.,
Expand Down Expand Up @@ -182,6 +182,8 @@ Options:
commit log message using the user and email
provided. (format "Name
<email@example.com>"). [string]
--monorepo-tags include library name in tags and release
branches [boolean] [default: false]
--path release from path other than root directory
[string]
--component name of component release is being minted
Expand Down
34 changes: 24 additions & 10 deletions src/bin/release-please.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ interface PullRequestArgs {

interface PullRequestStrategyArgs {
snapshot?: boolean;
monorepoTags?: boolean;
changelogSections?: ChangelogSection[];
changelogPath?: string;
versioningStrategy?: VersioningStrategyType;
Expand All @@ -103,18 +102,24 @@ interface PullRequestStrategyArgs {
extraFiles?: string[];
}

interface TaggingArgs {
monorepoTags?: boolean;
}

interface CreatePullRequestArgs
extends GitHubArgs,
ManifestArgs,
ManifestConfigArgs,
VersioningArgs,
PullRequestArgs,
PullRequestStrategyArgs {}
PullRequestStrategyArgs,
TaggingArgs {}
interface CreateReleaseArgs
extends GitHubArgs,
ManifestArgs,
ManifestConfigArgs,
ReleaseArgs {}
ReleaseArgs,
TaggingArgs {}
interface CreateManifestPullRequestArgs
extends GitHubArgs,
ManifestArgs,
Expand Down Expand Up @@ -239,11 +244,6 @@ function pullRequestStrategyOptions(yargs: yargs.Argv): yargs.Argv {
default: false,
type: 'boolean',
})
.option('monorepo-tags', {
describe: 'include library name in tags and release branches',
type: 'boolean',
default: false,
})
.option('extra-files', {
describe: 'extra files for the strategy to consider',
type: 'string',
Expand Down Expand Up @@ -350,6 +350,14 @@ function manifestOptions(yargs: yargs.Argv): yargs.Argv {
});
}
function taggingOptions(yargs: yargs.Argv): yargs.Argv {
return yargs.option('monorepo-tags', {
describe: 'include library name in tags and release branches',
type: 'boolean',
default: false,
});
}
const createReleasePullRequestCommand: yargs.CommandModule<
{},
CreatePullRequestArgs
Expand All @@ -359,7 +367,9 @@ const createReleasePullRequestCommand: yargs.CommandModule<
builder(yargs) {
return manifestOptions(
manifestConfigOptions(
pullRequestOptions(pullRequestStrategyOptions(gitHubOptions(yargs)))
taggingOptions(
pullRequestOptions(pullRequestStrategyOptions(gitHubOptions(yargs)))
)
)
);
},
Expand All @@ -384,6 +394,7 @@ const createReleasePullRequestCommand: yargs.CommandModule<
versioning: argv.versioningStrategy,
extraFiles: argv.extraFiles,
versionFile: argv.versionFile,
includeComponentInTag: argv.monorepoTags,
},
extractManifestOptions(argv),
argv.path
Expand Down Expand Up @@ -429,7 +440,9 @@ const createReleaseCommand: yargs.CommandModule<{}, CreateReleaseArgs> = {
describe: 'create a GitHub release from a release PR',
builder(yargs) {
return releaseOptions(
manifestOptions(manifestConfigOptions(gitHubOptions(yargs)))
manifestOptions(
manifestConfigOptions(taggingOptions(gitHubOptions(yargs)))
)
);
},
async handler(argv) {
Expand All @@ -448,6 +461,7 @@ const createReleaseCommand: yargs.CommandModule<{}, CreateReleaseArgs> = {
component: argv.component,
packageName: argv.packageName,
draft: argv.draft,
includeComponentInTag: argv.monorepoTags,
},
extractManifestOptions(argv),
argv.path
Expand Down
3 changes: 2 additions & 1 deletion src/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ export async function buildStrategy(
bumpMinorPreMajor: options.bumpMinorPreMajor,
bumpPatchForMinorPreMajor: options.bumpPatchForMinorPreMajor,
});
const strategyOptions = {
const strategyOptions: StrategyOptions = {
github: options.github,
targetBranch,
path: options.path,
Expand All @@ -124,6 +124,7 @@ export async function buildStrategy(
versioningStrategy,
skipGitHubRelease: options.skipGithubRelease,
releaseAs: options.releaseAs,
includeComponentInTag: options.includeComponentInTag,
};
switch (options.releaseType) {
case 'ruby': {
Expand Down
3 changes: 3 additions & 0 deletions src/manifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ export interface ReleaserConfig {
draftPullRequest?: boolean;
component?: string;
packageName?: string;
includeComponentInTag?: boolean;

// Ruby-only
versionFile?: string;
Expand Down Expand Up @@ -86,6 +87,7 @@ interface ReleaserConfigJson {
'draft-pull-request'?: boolean;
label?: string;
'release-label'?: string;
'include-component-in-tag'?: boolean;

// Ruby-only
'version-file'?: string;
Expand Down Expand Up @@ -823,6 +825,7 @@ function extractReleaserConfig(config: ReleaserPackageConfig): ReleaserConfig {
packageName: config['package-name'],
versionFile: config['version-file'],
extraFiles: config['extra-files'],
includeComponentInTag: config['include-component-in-tag'],
};
}

Expand Down
16 changes: 14 additions & 2 deletions src/strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export interface StrategyOptions {
skipGitHubRelease?: boolean;
releaseAs?: string;
changelogNotes?: ChangelogNotes;
includeComponentInTag?: boolean;
}

/**
Expand All @@ -78,6 +79,7 @@ export abstract class Strategy {
protected tagSeparator?: string;
private skipGitHubRelease: boolean;
private releaseAs?: string;
private includeComponentInTag: boolean;

protected changelogNotes: ChangelogNotes;

Expand All @@ -101,6 +103,7 @@ export abstract class Strategy {
this.releaseAs = options.releaseAs;
this.changelogNotes =
options.changelogNotes || new DefaultChangelogNotes(options);
this.includeComponentInTag = options.includeComponentInTag ?? true;
}

/**
Expand Down Expand Up @@ -197,7 +200,11 @@ export abstract class Strategy {
const component = await this.getComponent();
logger.debug('component:', component);

const newVersionTag = new TagName(newVersion, component);
const newVersionTag = new TagName(
newVersion,
this.includeComponentInTag ? component : undefined,
this.tagSeparator
);
const pullRequestTitle = PullRequestTitle.ofComponentTargetBranchVersion(
component || '',
this.targetBranch,
Expand Down Expand Up @@ -327,8 +334,13 @@ export abstract class Strategy {
throw new Error('Pull request should have included version');
}

const tag = new TagName(
version,
this.includeComponentInTag ? component : undefined,
this.tagSeparator
);
return {
tag: new TagName(version, component, this.tagSeparator),
tag,
notes: notes || '',
sha: mergedPullRequest.sha,
};
Expand Down
42 changes: 42 additions & 0 deletions test/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -913,6 +913,27 @@ describe('CLI', () => {
);
sinon.assert.calledOnce(createPullRequestsStub);
});

it('handles --monorepo-tags', async () => {
await parser.parseAsync(
'release-pr --repo-url=googleapis/release-please-cli --release-type=java-yoshi --monorepo-tags'
);

sinon.assert.calledOnceWithExactly(gitHubCreateStub, {
owner: 'googleapis',
repo: 'release-please-cli',
token: undefined,
});
sinon.assert.calledOnceWithExactly(
fromConfigStub,
fakeGitHub,
'main',
sinon.match({releaseType: 'java-yoshi', includeComponentInTag: true}),
sinon.match.any,
undefined
);
sinon.assert.calledOnce(createPullRequestsStub);
});
});
});
describe('github-release', () => {
Expand Down Expand Up @@ -1242,6 +1263,27 @@ describe('CLI', () => {
);
sinon.assert.calledOnce(createReleasesStub);
});

it('handles --monorepo-tags', async () => {
await parser.parseAsync(
'github-release --repo-url=googleapis/release-please-cli --release-type=java-yoshi --monorepo-tags'
);

sinon.assert.calledOnceWithExactly(gitHubCreateStub, {
owner: 'googleapis',
repo: 'release-please-cli',
token: undefined,
});
sinon.assert.calledOnceWithExactly(
fromConfigStub,
fakeGitHub,
'main',
sinon.match({releaseType: 'java-yoshi', includeComponentInTag: true}),
sinon.match.any,
undefined
);
sinon.assert.calledOnce(createReleasesStub);
});
});
});

Expand Down
46 changes: 46 additions & 0 deletions test/manifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1866,6 +1866,52 @@ describe('Manifest', () => {
expect(releases).lengthOf(1);
expect(releases[0].draft).to.be.true;
});

it('should skip component in tag', async () => {
mockPullRequests(
github,
[],
[
{
headBranchName: 'release-please/branches/main',
baseBranchName: 'main',
number: 1234,
title: 'chore: release main',
body: pullRequestBody('release-notes/single-manifest.txt'),
labels: ['autorelease: pending'],
files: [],
sha: 'abc123',
},
]
);
const getFileContentsStub = sandbox.stub(
github,
'getFileContentsOnBranch'
);
getFileContentsStub
.withArgs('package.json', 'main')
.resolves(
buildGitHubFileRaw(
JSON.stringify({name: '@google-cloud/release-brancher'})
)
);
const manifest = new Manifest(
github,
'main',
{
'.': {
releaseType: 'node',
includeComponentInTag: false,
},
},
{
'.': Version.parse('1.3.0'),
}
);
const releases = await manifest.buildReleases();
expect(releases).lengthOf(1);
expect(releases[0].tag.toString()).to.eql('v1.3.1');
});
});

describe('createReleases', () => {
Expand Down
43 changes: 42 additions & 1 deletion test/strategies/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,27 @@ describe('Strategy', () => {
const pullRequest = await strategy.buildReleasePullRequest([]);
expect(pullRequest).to.be.undefined;
});
});
describe('buildRelease', () => {
it('builds a release tag', async () => {
const strategy = new TestStrategy({
targetBranch: 'main',
github,
component: 'google-cloud-automl',
});
const release = await strategy.buildRelease({
title: 'chore(main): release v1.2.3',
headBranchName: 'release-please/branches/main',
baseBranchName: 'main',
number: 1234,
body: new PullRequestBody([]).toString(),
labels: [],
files: [],
sha: 'abc123',
});
expect(release, 'Release').to.not.be.undefined;
expect(release!.tag.toString()).to.eql('google-cloud-automl-v1.2.3');
});
it('overrides the tag separator', async () => {
const strategy = new TestStrategy({
targetBranch: 'main',
Expand All @@ -68,7 +89,27 @@ describe('Strategy', () => {
sha: 'abc123',
});
expect(release, 'Release').to.not.be.undefined;
expect(release!.tag.separator).to.eql('/');
expect(release!.tag.toString()).to.eql('google-cloud-automl/v1.2.3');
});
it('skips component in release tag', async () => {
const strategy = new TestStrategy({
targetBranch: 'main',
github,
component: 'google-cloud-automl',
includeComponentInTag: false,
});
const release = await strategy.buildRelease({
title: 'chore(main): release v1.2.3',
headBranchName: 'release-please/branches/main',
baseBranchName: 'main',
number: 1234,
body: new PullRequestBody([]).toString(),
labels: [],
files: [],
sha: 'abc123',
});
expect(release, 'Release').to.not.be.undefined;
expect(release!.tag.toString()).to.eql('v1.2.3');
});
});
});

0 comments on commit bf9aacd

Please sign in to comment.