Skip to content

Commit 07d21f5

Browse files
authored
refactor: stricter releaseType typing (googleapis#745)
1 parent f78fc93 commit 07d21f5

27 files changed

+196
-137
lines changed

.eslintrc.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
11
{
2-
"extends": "./node_modules/gts"
2+
"extends": "./node_modules/gts",
3+
"rules": {
4+
"@typescript-eslint/no-unused-vars": [
5+
"error",
6+
{"argsIgnorePattern": "^_", "varsIgnorePattern": "^_$"}
7+
]
8+
}
39
}

src/bin/release-please.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import {coerceOption} from '../util/coerce-option';
1919
import {GitHubReleaseOptions} from '../github-release';
2020
import {ReleasePROptions} from '../release-pr';
2121
import {factory} from '../factory';
22-
import {getReleaserNames} from '../releasers';
22+
import {getReleaserTypes, ReleaseType} from '../releasers';
2323
import * as yargs from 'yargs';
2424

2525
interface ErrorObject {
@@ -31,7 +31,7 @@ interface ErrorObject {
3131

3232
interface YargsOptions {
3333
describe: string;
34-
choices?: string[];
34+
choices?: readonly ReleaseType[];
3535
demand?: boolean;
3636
type?: string;
3737
default?: string | boolean;
@@ -113,7 +113,7 @@ export const parser = yargs
113113
})
114114
.option('release-type', {
115115
describe: 'what type of repo is a release being created for?',
116-
choices: getReleaserNames(),
116+
choices: getReleaserTypes(),
117117
default: 'node',
118118
})
119119
.option('bump-minor-pre-major', {

src/factory.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import {
2222
GitHubRelease,
2323
ReleaseResponse,
2424
} from './github-release';
25-
import {getReleasers} from './releasers';
25+
import {ReleaseType, getReleasers} from './releasers';
2626

2727
function runCommand(
2828
command: string,
@@ -55,7 +55,7 @@ function releasePR(options: ReleasePROptions): ReleasePR {
5555
return new (factory.releasePRClass(options.releaseType))(releaseOptions);
5656
}
5757

58-
export function releasePRClass(releaseType: string): typeof ReleasePR {
58+
export function releasePRClass(releaseType: ReleaseType): typeof ReleasePR {
5959
const releasers = getReleasers();
6060
const releaser = releasers[releaseType];
6161
if (!releaser) {

src/github-release.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {factory} from './factory';
1919
import {GitHub, OctokitAPIs} from './github';
2020
import {parse} from 'semver';
2121
import {ReleasePR} from './release-pr';
22+
import {ReleaseType} from './releasers';
2223

2324
// eslint-disable-next-line @typescript-eslint/no-var-requires
2425
const parseGithubRepoUrl = require('parse-github-repo-url');
@@ -38,7 +39,7 @@ export interface ReleaseResponse {
3839
}
3940

4041
export interface GitHubReleaseOptions extends SharedOptions {
41-
releaseType?: string;
42+
releaseType?: ReleaseType;
4243
changelogPath?: string;
4344
draft?: boolean;
4445
defaultBranch?: string;
@@ -54,7 +55,7 @@ export class GitHubRelease {
5455
packageName?: string;
5556
monorepoTags?: boolean;
5657
token?: string;
57-
releaseType?: string;
58+
releaseType?: ReleaseType;
5859
draft: boolean;
5960
defaultBranch?: string;
6061

@@ -108,7 +109,7 @@ export class GitHubRelease {
108109
})
109110
: new ReleasePR({
110111
...releaseOptions,
111-
...{releaseType: 'unknown'},
112+
...{releaseType: 'base'},
112113
});
113114

114115
const candidate = await releasePR.buildRelease(

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export interface SharedOptions {
3030
}
3131

3232
export {factory} from './factory';
33-
export {getReleaserNames, getReleasers} from './releasers';
33+
export {getReleaserTypes, getReleasers} from './releasers';
3434
export {GitHubRelease, GitHubReleaseOptions} from './github-release';
3535
export {JavaYoshi} from './releasers/java-yoshi';
3636
export {Ruby} from './releasers/ruby';

src/release-pr.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import {Commit} from './graphql-to-commits';
3131
import {Update} from './updaters/update';
3232
import {BranchName} from './util/branch-name';
3333
import {extractReleaseNotes} from './util/release-notes';
34+
import {ReleaseType} from './releasers';
3435

3536
// eslint-disable-next-line @typescript-eslint/no-var-requires
3637
const parseGithubRepoUrl = require('parse-github-repo-url');
@@ -51,7 +52,7 @@ export interface ReleasePROptions extends SharedOptions {
5152
octokitAPIs?: OctokitAPIs;
5253
// Path to version.rb file
5354
versionFile?: string;
54-
releaseType: string;
55+
releaseType: ReleaseType;
5556
changelogSections?: [];
5657
// Optionally provide GitHub instance
5758
github?: GitHub;
@@ -87,8 +88,6 @@ export interface OpenPROptions {
8788
}
8889

8990
export class ReleasePR {
90-
static releaserName = 'base';
91-
9291
apiUrl: string;
9392
defaultBranch?: string;
9493
labels: string[];
@@ -197,10 +196,8 @@ export class ReleasePR {
197196
// the release name when creating a GitHub release, for instance by returning
198197
// name in package.json, or setup.py.
199198
static async lookupPackageName(
200-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
201-
gh: GitHub,
202-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
203-
path?: string
199+
_gh: GitHub,
200+
_path?: string
204201
): Promise<string | undefined> {
205202
return Promise.resolve(undefined);
206203
}
@@ -292,7 +289,6 @@ export class ReleasePR {
292289

293290
// Override this method to detect the release version from code (if it cannot be
294291
// inferred from the release PR head branch)
295-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
296292
protected detectReleaseVersionFromTitle(title: string): string | undefined {
297293
const pattern = /^chore: release ?(?<component>.*) (?<version>\d+\.\d+\.\d+)$/;
298294
const match = title.match(pattern);
@@ -317,7 +313,7 @@ export class ReleasePR {
317313

318314
// Override this method to modify the pull request body
319315
protected async buildPullRequestBody(
320-
version: string,
316+
_version: string,
321317
changelogEntry: string
322318
): Promise<string> {
323319
return `:robot: I have created a release \\*beep\\* \\*boop\\* \n---\n${changelogEntry}\n\nThis PR was generated with [Release Please](https://github.com/googleapis/${RELEASE_PLEASE}). See [documentation](https://github.com/googleapis/${RELEASE_PLEASE}#${RELEASE_PLEASE}).`;

src/releasers/go-yoshi.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,12 @@ const SUB_MODULES = [
4747
const REGEN_PR_REGEX = /.*auto-regenerate.*/;
4848

4949
export class GoYoshi extends ReleasePR {
50-
static releaserName = 'go-yoshi';
5150
protected async _run(): Promise<number | undefined> {
5251
const latestTag = await this.gh.latestTag(
5352
this.monorepoTags ? `${this.packageName}-` : undefined,
5453
false
5554
);
56-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
57-
const [_owner, repo] = parseGithubRepoUrl(this.repoUrl);
55+
const [_, repo] = parseGithubRepoUrl(this.repoUrl);
5856
let regenPR: Commit | undefined;
5957
let sha: null | string = null;
6058
const commits = (

src/releasers/index.ts

Lines changed: 47 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -27,30 +27,55 @@ import {TerraformModule} from './terraform-module';
2727
import {Rust} from './rust';
2828
import {OCaml} from './ocaml';
2929

30-
// TODO: add any new releasers you create to this list:
31-
export function getReleasers(): {[key: string]: typeof ReleasePR} {
32-
const releasers = {
33-
go: GoYoshi, // TODO(codyoss): refactor this into a more generic go strategy.
34-
'go-yoshi': GoYoshi,
35-
'java-bom': JavaBom,
36-
'java-yoshi': JavaYoshi,
37-
node: Node,
38-
'php-yoshi': PHPYoshi,
39-
python: Python,
40-
'ruby-yoshi': RubyYoshi,
41-
ruby: Ruby,
42-
simple: Simple,
43-
'terraform-module': TerraformModule,
44-
rust: Rust,
45-
ocaml: OCaml,
46-
};
30+
// add any new releasers you create to this type as well as the `releasers`
31+
// object below.
32+
export type ReleaseType =
33+
| 'base' // not exposed to end users
34+
| 'go'
35+
| 'go-yoshi'
36+
| 'java-bom'
37+
| 'java-yoshi'
38+
| 'node'
39+
| 'ocaml'
40+
| 'php-yoshi'
41+
| 'python'
42+
| 'ruby'
43+
| 'ruby-yoshi'
44+
| 'rust'
45+
| 'simple'
46+
| 'terraform-module';
47+
48+
type Releasers = Partial<Record<ReleaseType, typeof ReleasePR>>;
49+
50+
const releasers: Releasers = {
51+
go: GoYoshi, // TODO(codyoss): refactor this into a more generic go strategy.
52+
'go-yoshi': GoYoshi,
53+
'java-bom': JavaBom,
54+
'java-yoshi': JavaYoshi,
55+
node: Node,
56+
ocaml: OCaml,
57+
'php-yoshi': PHPYoshi,
58+
python: Python,
59+
ruby: Ruby,
60+
'ruby-yoshi': RubyYoshi,
61+
rust: Rust,
62+
simple: Simple,
63+
'terraform-module': TerraformModule,
64+
};
65+
66+
export function getReleasers(): Releasers {
4767
return releasers;
4868
}
4969

70+
// deprecated, use getReleaserTypes
5071
export function getReleaserNames(): string[] {
51-
const releasers = getReleasers();
52-
return Object.keys(releasers).map(key => {
53-
const releaser = releasers[key];
54-
return releaser.releaserName;
55-
});
72+
return getReleaserTypes() as string[];
73+
}
74+
75+
export function getReleaserTypes(): readonly ReleaseType[] {
76+
const names: ReleaseType[] = [];
77+
for (const releaseType of Object.keys(releasers)) {
78+
names.push(releaseType as ReleaseType);
79+
}
80+
return names;
5681
}

src/releasers/java-bom.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ const DEPENDENCY_UPDATE_REGEX = /^deps: update dependency (.*) to (v.*)(\s\(#\d+
4747
const DEPENDENCY_PATCH_VERSION_REGEX = /^v\d+\.\d+\.[1-9]\d*(-.*)?/;
4848

4949
export class JavaBom extends ReleasePR {
50-
static releaserName = 'java-bom';
5150
protected async _run(): Promise<number | undefined> {
5251
const versionsManifestContent = await this.gh.getFileContents(
5352
'versions.txt'

src/releasers/java-yoshi.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ const CHANGELOG_SECTIONS = [
4949
];
5050

5151
export class JavaYoshi extends ReleasePR {
52-
static releaserName = 'java-yoshi';
5352
protected async _run(): Promise<number | undefined> {
5453
const versionsManifestContent = await this.gh.getFileContents(
5554
'versions.txt'
@@ -295,7 +294,7 @@ export class JavaYoshi extends ReleasePR {
295294
// If you modify this, you must ensure that the releaser can parse the tag version
296295
// from the pull request.
297296
protected async buildBranchName(
298-
version: string,
297+
_version: string,
299298
includePackageName: boolean
300299
): Promise<BranchName> {
301300
const defaultBranch = await this.getDefaultBranch();
@@ -321,7 +320,6 @@ export class JavaYoshi extends ReleasePR {
321320

322321
// Override this method to detect the release version from code (if it cannot be
323322
// inferred from the release PR head branch)
324-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
325323
protected detectReleaseVersionFromTitle(title: string): string | undefined {
326324
const pattern = /^chore\((?<branch>[^(]+)\): release ?(?<component>.*) (?<version>\d+\.\d+\.\d+)$/;
327325
const match = title.match(pattern);

0 commit comments

Comments
 (0)