/
release-please.ts
311 lines (297 loc) · 8.97 KB
/
release-please.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
#!/usr/bin/env node
// Copyright 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import chalk = require('chalk');
import {coerceOption} from '../util/coerce-option';
import {factory} from '../factory';
import {getReleaserTypes, ReleaseType} from '../releasers';
import * as yargs from 'yargs';
import {
GitHubReleaseFactoryOptions,
ReleasePRFactoryOptions,
ManifestFactoryOptions,
} from '..';
import {GH_API_URL} from '../constants';
interface ErrorObject {
body?: object;
status?: number;
message: string;
stack: string;
}
interface YargsOptions {
describe: string;
choices?: readonly ReleaseType[];
demand?: boolean;
type?: string;
default?: string | boolean;
}
interface YargsOptionsBuilder {
option(opt: string, options: YargsOptions): YargsOptionsBuilder;
}
function releaserCommon(ya: YargsOptionsBuilder) {
// common to ReleasePR and GitHubRelease
ya.option('label', {
default: 'autorelease: pending',
describe: 'label to remove from release PR',
});
ya.option('release-as', {
describe: 'override the semantically determined release version',
type: 'string',
});
ya.option('bump-minor-pre-major', {
describe:
'should we bump the semver minor prior to the first major release',
default: false,
type: 'boolean',
});
ya.option('bump-patch-for-minor-pre-major', {
describe:
'should we bump the semver patch instead of the minor for non-breaking' +
' changes prior to the first major release',
default: false,
type: 'boolean',
});
ya.option('path', {
describe: 'release from path other than root directory',
type: 'string',
});
ya.option('package-name', {
describe: 'name of package release is being minted for',
});
ya.option('monorepo-tags', {
describe: 'include library name in tags and release branches',
type: 'boolean',
default: false,
});
ya.option('version-file', {
describe: 'path to version file to update, e.g., version.rb',
});
ya.option('last-package-version', {
describe: 'last version # that package was released as',
});
ya.option('snapshot', {
describe: 'is it a snapshot (or pre-release) being generated?',
type: 'boolean',
default: false,
});
ya.option('pull-request-title-pattern', {
describe: 'Title pattern to make release PR',
type: 'string',
});
ya.option('signoff', {
describe:
'Add Signed-off-by line at the end of the commit log message using the user and email provided. (format "Name <email@example.com>").',
type: 'string',
});
ya.option('changelog-path', {
default: 'CHANGELOG.md',
describe: 'where can the CHANGELOG be found in the project?',
});
ya.option('latest-tag-version', {
describe: 'Override the detected latest tag version',
type: 'string',
});
ya.option('latest-tag-sha', {
describe: 'Override the detected latest tag SHA',
type: 'string',
});
ya.option('latest-tag-name', {
describe: 'Override the detected latest tag name',
type: 'string',
});
}
function releaseType(ya: YargsOptionsBuilder, defaultType?: string) {
const relTypeOptions: YargsOptions = {
describe: 'what type of repo is a release being created for?',
choices: getReleaserTypes(),
};
if (defaultType) {
relTypeOptions.default = defaultType;
}
ya.option('release-type', relTypeOptions);
}
function manifestOptions(ya: YargsOptionsBuilder) {
ya.option('config-file', {
default: 'release-please-config.json',
describe: 'where can the config file be found in the project?',
});
ya.option('manifest-file', {
default: '.release-please-manifest.json',
describe: 'where can the manifest file be found in the project?',
});
}
export const parser = yargs
.command(
'manifest-pr',
'create a release-PR using a manifest file',
(yargs: YargsOptionsBuilder) => {
manifestOptions(yargs);
},
(argv: ManifestFactoryOptions) => {
factory.runCommand('manifest-pr', argv).catch(handleError);
}
)
.command(
'manifest-release',
'create releases/tags from last release-PR using a manifest file',
(yargs: YargsOptionsBuilder) => {
manifestOptions(yargs);
},
(argv: ManifestFactoryOptions) => {
factory.runCommand('manifest-release', argv).catch(handleError);
}
)
.command(
'release-pr',
'create or update a PR representing the next release',
// options unique to ReleasePR
(yargs: YargsOptionsBuilder) => {
releaseType(yargs, 'node');
releaserCommon(yargs);
},
(argv: ReleasePRFactoryOptions) => {
factory.runCommand('release-pr', argv).catch(handleError);
}
)
.command(
'latest-tag',
'find the sha of the latest release',
// options unique to ReleasePR
(yargs: YargsOptionsBuilder) => {
releaseType(yargs, 'node');
releaserCommon(yargs);
},
(argv: ReleasePRFactoryOptions) => {
factory
.runCommand('latest-tag', argv)
.catch(handleError)
.then(latestTag => {
if (latestTag) {
console.log(latestTag);
} else {
console.log('No latest tag found.');
process.exitCode = 1;
}
});
}
)
.command(
'github-release',
'create a GitHub release from a release PR',
// options unique to GitHubRelease
(yargs: YargsOptionsBuilder) => {
releaseType(yargs);
releaserCommon(yargs);
yargs.option('draft', {
describe:
'mark release as a draft. no tag is created but tag_name and ' +
'target_commitish are associated with the release for future ' +
'tag creation upon "un-drafting" the release.',
type: 'boolean',
default: false,
});
yargs.option('release-label', {
describe: 'set a pull request label other than "autorelease: tagged"',
type: 'string',
});
},
(argv: GitHubReleaseFactoryOptions) => {
factory.runCommand('github-release', argv).catch(handleError);
}
)
.middleware(_argv => {
const argv = _argv as GitHubReleaseFactoryOptions;
// allow secrets to be loaded from file path
// rather than being passed directly to the bin.
if (argv.token) argv.token = coerceOption(argv.token);
if (argv.apiUrl) argv.apiUrl = coerceOption(argv.apiUrl);
})
.option('debug', {
describe: 'print verbose errors (use only for local debugging).',
default: false,
type: 'boolean',
})
// See base GitHub options (e.g. GitHubFactoryOptions)
.option('token', {describe: 'GitHub token with repo write permissions'})
.option('api-url', {
describe: 'URL to use when making API requests',
default: GH_API_URL,
type: 'string',
})
.option('default-branch', {
describe: 'The branch to open release PRs against and tag releases on',
type: 'string',
})
.option('fork', {
describe: 'should the PR be created from a fork',
type: 'boolean',
default: false,
})
.option('repo-url', {
describe: 'GitHub URL to generate release for',
demand: true,
})
.demandCommand(1)
.strict(true)
.scriptName('release-please');
interface HandleError {
(err: ErrorObject): void;
logger?: Console;
yargsArgs?: yargs.Arguments;
}
// The errors returned by octokit currently contain the
// request object, this contains information we don't want to
// leak. For this reason, we capture exceptions and print
// a less verbose error message (run with --debug to output
// the request object, don't do this in CI/CD).
export const handleError: HandleError = (err: ErrorObject) => {
let status = '';
if (handleError.yargsArgs === undefined) {
throw new Error(
'Set handleError.yargsArgs with a yargs.Arguments instance.'
);
}
if (!handleError.logger) {
handleError.logger = console;
}
const ya = handleError.yargsArgs;
const logger = handleError.logger;
const command = ya?._?.length ? ya._[0] : '';
if (err.status) {
status = '' + err.status;
}
logger.error(
chalk.red(
`command ${command} failed${status ? ` with status ${status}` : ''}`
)
);
if (ya?.debug) {
logger.error('---------');
logger.error(err.stack);
}
process.exitCode = 1;
};
// Only run parser if executed with node bin, this allows
// for the parser to be easily tested:
let argv: yargs.Arguments;
if (require.main === module) {
argv = parser.parse();
handleError.yargsArgs = argv;
process.on('unhandledRejection', err => {
handleError(err as ErrorObject);
});
process.on('uncaughtException', err => {
handleError(err as ErrorObject);
});
}