release-it 0.0.0-pl.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +421 -0
- package/bin/release-it.js +42 -0
- package/config/release-it.json +70 -0
- package/lib/cli.js +44 -0
- package/lib/config.js +139 -0
- package/lib/index.js +152 -0
- package/lib/log.js +69 -0
- package/lib/plugin/GitBase.js +125 -0
- package/lib/plugin/GitRelease.js +58 -0
- package/lib/plugin/Plugin.js +73 -0
- package/lib/plugin/factory.js +89 -0
- package/lib/plugin/git/Git.js +220 -0
- package/lib/plugin/git/prompts.js +19 -0
- package/lib/plugin/github/GitHub.js +403 -0
- package/lib/plugin/github/prompts.js +16 -0
- package/lib/plugin/github/util.js +39 -0
- package/lib/plugin/gitlab/GitLab.js +277 -0
- package/lib/plugin/gitlab/prompts.js +9 -0
- package/lib/plugin/npm/npm.js +281 -0
- package/lib/plugin/npm/prompts.js +12 -0
- package/lib/plugin/version/Version.js +129 -0
- package/lib/prompt.js +33 -0
- package/lib/shell.js +91 -0
- package/lib/spinner.js +29 -0
- package/lib/util.js +109 -0
- package/package.json +122 -0
- package/test/cli.js +20 -0
- package/test/config.js +144 -0
- package/test/git.init.js +250 -0
- package/test/git.js +358 -0
- package/test/github.js +487 -0
- package/test/gitlab.js +252 -0
- package/test/log.js +143 -0
- package/test/npm.js +417 -0
- package/test/plugin-name.js +9 -0
- package/test/plugins.js +238 -0
- package/test/prompt.js +97 -0
- package/test/resources/file-v2.0.1.txt +1 -0
- package/test/resources/file-v2.0.2.txt +1 -0
- package/test/resources/file1 +1 -0
- package/test/shell.js +74 -0
- package/test/spinner.js +58 -0
- package/test/stub/config/default/.release-it.json +5 -0
- package/test/stub/config/invalid-config-rc +1 -0
- package/test/stub/config/invalid-config-txt +2 -0
- package/test/stub/config/merge/.release-it.json +5 -0
- package/test/stub/config/merge/package.json +7 -0
- package/test/stub/config/toml/.release-it.toml +2 -0
- package/test/stub/config/yaml/.release-it.yaml +2 -0
- package/test/stub/config/yml/.release-it.yml +2 -0
- package/test/stub/github.js +130 -0
- package/test/stub/gitlab.js +44 -0
- package/test/stub/plugin-context.js +36 -0
- package/test/stub/plugin-replace.js +9 -0
- package/test/stub/plugin.js +39 -0
- package/test/stub/shell.js +24 -0
- package/test/tasks.interactive.js +208 -0
- package/test/tasks.js +585 -0
- package/test/util/helpers.js +32 -0
- package/test/util/index.js +78 -0
- package/test/util/setup.js +5 -0
- package/test/utils.js +97 -0
- package/test/version.js +173 -0
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
import { EOL } from 'node:os';
|
|
2
|
+
import _ from 'lodash';
|
|
3
|
+
import { execa } from 'execa';
|
|
4
|
+
import matcher from 'wildcard-match';
|
|
5
|
+
import { format, e } from '../../util.js';
|
|
6
|
+
import GitBase from '../GitBase.js';
|
|
7
|
+
import prompts from './prompts.js';
|
|
8
|
+
|
|
9
|
+
const noop = Promise.resolve();
|
|
10
|
+
const invalidPushRepoRe = /^\S+@/;
|
|
11
|
+
const options = { write: false };
|
|
12
|
+
const fixArgs = args => (args ? (typeof args === 'string' ? args.split(' ') : args) : []);
|
|
13
|
+
|
|
14
|
+
const docs = 'https://git.io/release-it-git';
|
|
15
|
+
|
|
16
|
+
const isGitRepo = () =>
|
|
17
|
+
execa('git', ['rev-parse', '--git-dir']).then(
|
|
18
|
+
() => true,
|
|
19
|
+
() => false
|
|
20
|
+
);
|
|
21
|
+
|
|
22
|
+
class Git extends GitBase {
|
|
23
|
+
constructor(...args) {
|
|
24
|
+
super(...args);
|
|
25
|
+
this.registerPrompts(prompts);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
static async isEnabled(options) {
|
|
29
|
+
return options !== false && (await isGitRepo());
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
async init() {
|
|
33
|
+
if (this.options.requireBranch && !(await this.isRequiredBranch(this.options.requireBranch))) {
|
|
34
|
+
throw e(`Must be on branch ${this.options.requireBranch}`, docs);
|
|
35
|
+
}
|
|
36
|
+
if (this.options.requireCleanWorkingDir && !(await this.isWorkingDirClean())) {
|
|
37
|
+
throw e(`Working dir must be clean.${EOL}Please stage and commit your changes.`, docs);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
await super.init();
|
|
41
|
+
|
|
42
|
+
const remoteUrl = this.getContext('remoteUrl');
|
|
43
|
+
if (this.options.push && !remoteUrl) {
|
|
44
|
+
throw e(`Could not get remote Git url.${EOL}Please add a remote repository.`, docs);
|
|
45
|
+
}
|
|
46
|
+
if (this.options.requireUpstream && !(await this.hasUpstreamBranch())) {
|
|
47
|
+
throw e(`No upstream configured for current branch.${EOL}Please set an upstream branch.`, docs);
|
|
48
|
+
}
|
|
49
|
+
if (this.options.requireCommits && (await this.getCommitsSinceLatestTag(this.options.commitsPath)) === 0) {
|
|
50
|
+
throw e(`There are no commits since the latest tag.`, docs, this.options.requireCommitsFail);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
rollback() {
|
|
55
|
+
this.log.info('Rolling back changes...');
|
|
56
|
+
const { tagName } = this.config.getContext();
|
|
57
|
+
const { isCommitted, isTagged } = this.getContext();
|
|
58
|
+
if (isTagged) {
|
|
59
|
+
this.exec(`git tag --delete ${tagName}`);
|
|
60
|
+
}
|
|
61
|
+
this.exec(`git reset --hard HEAD${isCommitted ? '~1' : ''}`);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
enableRollback() {
|
|
65
|
+
this.rollbackOnce = _.once(this.rollback.bind(this));
|
|
66
|
+
process.on('SIGINT', this.rollbackOnce);
|
|
67
|
+
process.on('exit', this.rollbackOnce);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
disableRollback() {
|
|
71
|
+
if (this.rollbackOnce) {
|
|
72
|
+
process.removeListener('SIGINT', this.rollbackOnce);
|
|
73
|
+
process.removeListener('exit', this.rollbackOnce);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
async beforeRelease() {
|
|
78
|
+
if (this.options.commit) {
|
|
79
|
+
if (this.options.requireCleanWorkingDir) {
|
|
80
|
+
this.enableRollback();
|
|
81
|
+
}
|
|
82
|
+
const changeSet = await this.status();
|
|
83
|
+
this.log.preview({ title: 'changeset', text: changeSet });
|
|
84
|
+
await this.stageDir();
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
async release() {
|
|
89
|
+
const { commit, tag, push } = this.options;
|
|
90
|
+
await this.step({ enabled: commit, task: () => this.commit(), label: 'Git commit', prompt: 'commit' });
|
|
91
|
+
await this.step({ enabled: tag, task: () => this.tag(), label: 'Git tag', prompt: 'tag' });
|
|
92
|
+
return !!(await this.step({ enabled: push, task: () => this.push(), label: 'Git push', prompt: 'push' }));
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
async isRequiredBranch() {
|
|
96
|
+
const branch = await this.getBranchName();
|
|
97
|
+
const requiredBranches = _.castArray(this.options.requireBranch);
|
|
98
|
+
const [branches, negated] = requiredBranches.reduce(
|
|
99
|
+
([p, n], b) => (b.startsWith('!') ? [p, [...n, b.slice(1)]] : [[...p, b], n]),
|
|
100
|
+
[[], []]
|
|
101
|
+
);
|
|
102
|
+
return (
|
|
103
|
+
(branches.length > 0 ? matcher(branches)(branch) : true) &&
|
|
104
|
+
(negated.length > 0 ? !matcher(negated)(branch) : true)
|
|
105
|
+
);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
async hasUpstreamBranch() {
|
|
109
|
+
const ref = await this.exec('git symbolic-ref HEAD', { options });
|
|
110
|
+
const branch = await this.exec(`git for-each-ref --format="%(upstream:short)" ${ref}`, { options }).catch(
|
|
111
|
+
() => null
|
|
112
|
+
);
|
|
113
|
+
return Boolean(branch);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
tagExists(tag) {
|
|
117
|
+
return this.exec(`git show-ref --tags --quiet --verify -- refs/tags/${tag}`, { options }).then(
|
|
118
|
+
() => true,
|
|
119
|
+
() => false
|
|
120
|
+
);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
isWorkingDirClean() {
|
|
124
|
+
return this.exec('git diff --quiet HEAD', { options }).then(
|
|
125
|
+
() => true,
|
|
126
|
+
() => false
|
|
127
|
+
);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
async getCommitsSinceLatestTag(commitsPath = '') {
|
|
131
|
+
const latestTagName = await this.getLatestTagName();
|
|
132
|
+
const ref = latestTagName ? `${latestTagName}..HEAD` : 'HEAD';
|
|
133
|
+
return this.exec(`git rev-list ${ref} --count ${commitsPath}`, { options }).then(Number);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
async getUpstreamArgs(pushRepo) {
|
|
137
|
+
if (pushRepo && !this.isRemoteName(pushRepo)) {
|
|
138
|
+
// Use (only) `pushRepo` if it's configured and looks like a url
|
|
139
|
+
return [pushRepo];
|
|
140
|
+
} else if (!(await this.hasUpstreamBranch())) {
|
|
141
|
+
// Start tracking upstream branch (`pushRepo` is a name if set)
|
|
142
|
+
return ['--set-upstream', pushRepo || 'origin', await this.getBranchName()];
|
|
143
|
+
} else if (pushRepo && !invalidPushRepoRe.test(pushRepo)) {
|
|
144
|
+
return [pushRepo];
|
|
145
|
+
} else {
|
|
146
|
+
return [];
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
stage(file) {
|
|
151
|
+
if (!file || !file.length) return noop;
|
|
152
|
+
const files = _.castArray(file);
|
|
153
|
+
return this.exec(['git', 'add', ...files]).catch(err => {
|
|
154
|
+
this.log.warn(`Could not stage ${files}`);
|
|
155
|
+
this.debug(err);
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
stageDir({ baseDir = '.' } = {}) {
|
|
160
|
+
const { addUntrackedFiles } = this.options;
|
|
161
|
+
return this.exec(['git', 'add', baseDir, addUntrackedFiles ? '--all' : '--update']);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
reset(file) {
|
|
165
|
+
const files = _.castArray(file);
|
|
166
|
+
return this.exec(['git', 'checkout', 'HEAD', '--', ...files]).catch(err => {
|
|
167
|
+
this.log.warn(`Could not reset ${files}`);
|
|
168
|
+
this.debug(err);
|
|
169
|
+
});
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
status() {
|
|
173
|
+
return this.exec('git status --short --untracked-files=no', { options }).catch(() => null);
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
commit({ message = this.options.commitMessage, args = this.options.commitArgs } = {}) {
|
|
177
|
+
const msg = format(message, this.config.getContext());
|
|
178
|
+
const commitMessageArgs = msg ? ['--message', msg] : [];
|
|
179
|
+
return this.exec(['git', 'commit', ...commitMessageArgs, ...fixArgs(args)]).then(
|
|
180
|
+
() => this.setContext({ isCommitted: true }),
|
|
181
|
+
err => {
|
|
182
|
+
this.debug(err);
|
|
183
|
+
if (/nothing (added )?to commit/.test(err) || /nichts zu committen/.test(err)) {
|
|
184
|
+
this.log.warn('No changes to commit. The latest commit will be tagged.');
|
|
185
|
+
} else {
|
|
186
|
+
throw new Error(err);
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
);
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
tag({ name, annotation = this.options.tagAnnotation, args = this.options.tagArgs } = {}) {
|
|
193
|
+
const message = format(annotation, this.config.getContext());
|
|
194
|
+
const tagName = name || this.config.getContext('tagName');
|
|
195
|
+
return this.exec(['git', 'tag', '--annotate', '--message', message, ...fixArgs(args), tagName])
|
|
196
|
+
.then(() => this.setContext({ isTagged: true }))
|
|
197
|
+
.catch(err => {
|
|
198
|
+
const { latestTag, tagName } = this.config.getContext();
|
|
199
|
+
if (/tag '.+' already exists/.test(err) && latestTag === tagName) {
|
|
200
|
+
this.log.warn(`Tag "${tagName}" already exists`);
|
|
201
|
+
} else {
|
|
202
|
+
throw err;
|
|
203
|
+
}
|
|
204
|
+
});
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
async push({ args = this.options.pushArgs } = {}) {
|
|
208
|
+
const { pushRepo } = this.options;
|
|
209
|
+
const upstreamArgs = await this.getUpstreamArgs(pushRepo);
|
|
210
|
+
const push = await this.exec(['git', 'push', ...fixArgs(args), ...upstreamArgs]);
|
|
211
|
+
this.disableRollback();
|
|
212
|
+
return push;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
afterRelease() {
|
|
216
|
+
this.disableRollback();
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
export default Git;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { format, truncateLines } from '../../util.js';
|
|
2
|
+
|
|
3
|
+
export default {
|
|
4
|
+
commit: {
|
|
5
|
+
type: 'confirm',
|
|
6
|
+
message: context => `Commit (${truncateLines(format(context.git.commitMessage, context), 1, ' [...]')})?`,
|
|
7
|
+
default: true
|
|
8
|
+
},
|
|
9
|
+
tag: {
|
|
10
|
+
type: 'confirm',
|
|
11
|
+
message: context => `Tag (${format(context.tagName, context)})?`,
|
|
12
|
+
default: true
|
|
13
|
+
},
|
|
14
|
+
push: {
|
|
15
|
+
type: 'confirm',
|
|
16
|
+
message: () => 'Push?',
|
|
17
|
+
default: true
|
|
18
|
+
}
|
|
19
|
+
};
|
|
@@ -0,0 +1,403 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import open from 'open';
|
|
4
|
+
import { Octokit } from '@octokit/rest';
|
|
5
|
+
import fetch from 'node-fetch';
|
|
6
|
+
import { globby } from 'globby';
|
|
7
|
+
import mime from 'mime-types';
|
|
8
|
+
import _ from 'lodash';
|
|
9
|
+
import retry from 'async-retry';
|
|
10
|
+
import newGithubReleaseUrl from 'new-github-release-url';
|
|
11
|
+
import { ProxyAgent } from 'proxy-agent';
|
|
12
|
+
import { format, parseVersion, readJSON, e } from '../../util.js';
|
|
13
|
+
import Release from '../GitRelease.js';
|
|
14
|
+
import prompts from './prompts.js';
|
|
15
|
+
import { getCommitsFromChangelog, getResolvedIssuesFromChangelog, searchQueries } from './util.js';
|
|
16
|
+
|
|
17
|
+
const pkg = readJSON(new URL('../../../package.json', import.meta.url));
|
|
18
|
+
|
|
19
|
+
const docs = 'https://git.io/release-it-github';
|
|
20
|
+
|
|
21
|
+
const RETRY_CODES = [408, 413, 429, 500, 502, 503, 504, 521, 522, 524];
|
|
22
|
+
|
|
23
|
+
const DEFAULT_RETRY_MIN_TIMEOUT = 1000;
|
|
24
|
+
|
|
25
|
+
const parseErrormsg = err => {
|
|
26
|
+
let msg = err;
|
|
27
|
+
if (err instanceof Error) {
|
|
28
|
+
const { status, message } = err;
|
|
29
|
+
const headers = err.response ? err.response.headers : {};
|
|
30
|
+
msg = `${_.get(headers, 'status', status)} (${message})`;
|
|
31
|
+
}
|
|
32
|
+
return msg;
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
const truncateBody = body => {
|
|
36
|
+
// https://github.com/release-it/release-it/issues/965
|
|
37
|
+
if (body && body.length >= 124000) return body.substring(0, 124000) + '...';
|
|
38
|
+
return body;
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
class GitHub extends Release {
|
|
42
|
+
constructor(...args) {
|
|
43
|
+
super(...args);
|
|
44
|
+
this.registerPrompts(prompts);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
async init() {
|
|
48
|
+
await super.init();
|
|
49
|
+
|
|
50
|
+
const { skipChecks, tokenRef, web, update, assets } = this.options;
|
|
51
|
+
|
|
52
|
+
if (!this.token || web) {
|
|
53
|
+
if (!web) {
|
|
54
|
+
this.log.warn(`Environment variable "${tokenRef}" is required for automated GitHub Releases.`);
|
|
55
|
+
this.log.warn('Falling back to web-based GitHub Release.');
|
|
56
|
+
}
|
|
57
|
+
this.setContext({ isWeb: true });
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
if (web && assets) {
|
|
62
|
+
this.log.warn('Assets are not included in web-based releases.');
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
if (!skipChecks) {
|
|
66
|
+
// If we're running on GitHub Actions, we can skip the authentication and
|
|
67
|
+
// collaborator checks. Ref: https://bit.ly/2vsyRzu
|
|
68
|
+
if (process.env.GITHUB_ACTIONS) {
|
|
69
|
+
this.setContext({ username: process.env.GITHUB_ACTOR });
|
|
70
|
+
} else {
|
|
71
|
+
if (!(await this.isAuthenticated())) {
|
|
72
|
+
throw e(`Could not authenticate with GitHub using environment variable "${tokenRef}".`, docs);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
if (!(await this.isCollaborator())) {
|
|
76
|
+
const { repository } = this.getContext('repo');
|
|
77
|
+
const { username } = this.getContext();
|
|
78
|
+
throw e(`User ${username} is not a collaborator for ${repository}.`, docs);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
if (update) {
|
|
84
|
+
const { latestTag } = this.config.getContext();
|
|
85
|
+
try {
|
|
86
|
+
const { id, upload_url, tag_name } = await this.getLatestRelease();
|
|
87
|
+
if (latestTag === tag_name) {
|
|
88
|
+
this.setContext({ isUpdate: true, isReleased: true, releaseId: id, upload_url });
|
|
89
|
+
} else {
|
|
90
|
+
this.setContext({ isUpdate: false });
|
|
91
|
+
}
|
|
92
|
+
} catch (error) {
|
|
93
|
+
this.setContext({ isUpdate: false });
|
|
94
|
+
}
|
|
95
|
+
if (!this.getContext('isUpdate')) {
|
|
96
|
+
this.log.warn(`GitHub release for tag ${latestTag} was not found. Creating new release.`);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
async isAuthenticated() {
|
|
102
|
+
if (this.config.isDryRun) return true;
|
|
103
|
+
try {
|
|
104
|
+
this.log.verbose('octokit users#getAuthenticated');
|
|
105
|
+
const { data } = await this.client.users.getAuthenticated();
|
|
106
|
+
this.setContext({ username: data.login });
|
|
107
|
+
return true;
|
|
108
|
+
} catch (error) {
|
|
109
|
+
this.debug(error);
|
|
110
|
+
return false;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
async isCollaborator() {
|
|
115
|
+
if (this.config.isDryRun) return true;
|
|
116
|
+
const { owner, project: repo } = this.getContext('repo');
|
|
117
|
+
const { username } = this.getContext();
|
|
118
|
+
try {
|
|
119
|
+
const options = { owner, repo, username };
|
|
120
|
+
this.log.verbose(`octokit repos#checkCollaborator (${username})`);
|
|
121
|
+
await this.client.repos.checkCollaborator(options);
|
|
122
|
+
return true;
|
|
123
|
+
} catch (error) {
|
|
124
|
+
this.debug(error);
|
|
125
|
+
return false;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
async release() {
|
|
130
|
+
const { assets } = this.options;
|
|
131
|
+
const { isWeb, isUpdate } = this.getContext();
|
|
132
|
+
const { isCI } = this.config;
|
|
133
|
+
|
|
134
|
+
const type = isUpdate ? 'update' : 'create';
|
|
135
|
+
const publishMethod = `${type}Release`;
|
|
136
|
+
|
|
137
|
+
if (isWeb) {
|
|
138
|
+
const task = () => this.createWebRelease();
|
|
139
|
+
return this.step({ task, label: 'Generating link to GitHub Release web interface', prompt: 'release' });
|
|
140
|
+
} else if (isCI) {
|
|
141
|
+
await this.step({ task: () => this[publishMethod](), label: `GitHub ${type} release` });
|
|
142
|
+
return this.step({ enabled: assets, task: () => this.uploadAssets(), label: 'GitHub upload assets' });
|
|
143
|
+
} else {
|
|
144
|
+
const release = async () => {
|
|
145
|
+
await this[publishMethod]();
|
|
146
|
+
await this.uploadAssets();
|
|
147
|
+
return isUpdate ? Promise.resolve() : this.commentOnResolvedItems();
|
|
148
|
+
};
|
|
149
|
+
return this.step({ task: release, label: `GitHub ${type} release`, prompt: 'release' });
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
handleError(err, bail) {
|
|
154
|
+
const message = parseErrormsg(err);
|
|
155
|
+
const githubError = new Error(message);
|
|
156
|
+
this.log.verbose(err.errors);
|
|
157
|
+
this.debug(err);
|
|
158
|
+
if (!_.includes(RETRY_CODES, err.status)) {
|
|
159
|
+
return bail(githubError);
|
|
160
|
+
}
|
|
161
|
+
throw githubError;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
get client() {
|
|
165
|
+
if (this._client) return this._client;
|
|
166
|
+
const { proxy, timeout } = this.options;
|
|
167
|
+
const host = this.options.host || this.getContext('repo.host');
|
|
168
|
+
const isGitHub = host === 'github.com';
|
|
169
|
+
const baseUrl = `https://${isGitHub ? 'api.github.com' : host}${isGitHub ? '' : '/api/v3'}`;
|
|
170
|
+
const options = {
|
|
171
|
+
baseUrl,
|
|
172
|
+
auth: `token ${this.token}`,
|
|
173
|
+
userAgent: `release-it/${pkg.version}`,
|
|
174
|
+
log: this.config.isDebug ? console : null,
|
|
175
|
+
request: {
|
|
176
|
+
timeout,
|
|
177
|
+
fetch
|
|
178
|
+
}
|
|
179
|
+
};
|
|
180
|
+
|
|
181
|
+
if (proxy) {
|
|
182
|
+
options.request.agent = new ProxyAgent(proxy);
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
const client = new Octokit(options);
|
|
186
|
+
|
|
187
|
+
this._client = client;
|
|
188
|
+
return client;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
async getLatestRelease() {
|
|
192
|
+
const { owner, project: repo } = this.getContext('repo');
|
|
193
|
+
try {
|
|
194
|
+
const options = { owner, repo };
|
|
195
|
+
this.debug(options);
|
|
196
|
+
const response = await this.client.repos.listReleases({ owner, repo, per_page: 1, page: 1 });
|
|
197
|
+
this.debug(response.data[0]);
|
|
198
|
+
return response.data[0];
|
|
199
|
+
} catch (err) {
|
|
200
|
+
return this.handleError(err, () => {});
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
getOctokitReleaseOptions(options = {}) {
|
|
205
|
+
const { owner, project: repo } = this.getContext('repo');
|
|
206
|
+
const { releaseName, draft = false, preRelease = false, autoGenerate = false } = this.options;
|
|
207
|
+
const { tagName } = this.config.getContext();
|
|
208
|
+
const { version, releaseNotes, isUpdate } = this.getContext();
|
|
209
|
+
const { isPreRelease } = parseVersion(version);
|
|
210
|
+
const name = format(releaseName, this.config.getContext());
|
|
211
|
+
const body = autoGenerate ? (isUpdate ? null : '') : truncateBody(releaseNotes);
|
|
212
|
+
|
|
213
|
+
return Object.assign(options, {
|
|
214
|
+
owner,
|
|
215
|
+
repo,
|
|
216
|
+
tag_name: tagName,
|
|
217
|
+
name,
|
|
218
|
+
body,
|
|
219
|
+
draft,
|
|
220
|
+
prerelease: isPreRelease || preRelease,
|
|
221
|
+
generate_release_notes: autoGenerate
|
|
222
|
+
});
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
retry(fn) {
|
|
226
|
+
const { retryMinTimeout } = this.options;
|
|
227
|
+
return retry(fn, {
|
|
228
|
+
retries: 2,
|
|
229
|
+
minTimeout: typeof retryMinTimeout === 'number' ? retryMinTimeout : DEFAULT_RETRY_MIN_TIMEOUT
|
|
230
|
+
});
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
async createRelease() {
|
|
234
|
+
const options = this.getOctokitReleaseOptions();
|
|
235
|
+
const { isDryRun } = this.config;
|
|
236
|
+
|
|
237
|
+
this.log.exec(`octokit repos.createRelease "${options.name}" (${options.tag_name})`, { isDryRun });
|
|
238
|
+
|
|
239
|
+
if (isDryRun) {
|
|
240
|
+
this.setContext({ isReleased: true, releaseUrl: this.getReleaseUrlFallback(options.tag_name) });
|
|
241
|
+
return true;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
return this.retry(async bail => {
|
|
245
|
+
try {
|
|
246
|
+
this.debug(options);
|
|
247
|
+
const response = await this.client.repos.createRelease(options);
|
|
248
|
+
this.debug(response.data);
|
|
249
|
+
const { html_url, upload_url, id } = response.data;
|
|
250
|
+
this.setContext({ isReleased: true, releaseId: id, releaseUrl: html_url, upload_url });
|
|
251
|
+
this.config.setContext({ isReleased: true, releaseId: id, releaseUrl: html_url, upload_url });
|
|
252
|
+
this.log.verbose(`octokit repos.createRelease: done (${response.headers.location})`);
|
|
253
|
+
return response.data;
|
|
254
|
+
} catch (err) {
|
|
255
|
+
return this.handleError(err, bail);
|
|
256
|
+
}
|
|
257
|
+
});
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
uploadAsset(filePath) {
|
|
261
|
+
const url = this.getContext('upload_url');
|
|
262
|
+
const name = path.basename(filePath);
|
|
263
|
+
const contentType = mime.contentType(name) || 'application/octet-stream';
|
|
264
|
+
const contentLength = fs.statSync(filePath).size;
|
|
265
|
+
|
|
266
|
+
return this.retry(async bail => {
|
|
267
|
+
try {
|
|
268
|
+
const options = {
|
|
269
|
+
url,
|
|
270
|
+
data: fs.createReadStream(filePath),
|
|
271
|
+
name,
|
|
272
|
+
headers: {
|
|
273
|
+
'content-type': contentType,
|
|
274
|
+
'content-length': contentLength
|
|
275
|
+
}
|
|
276
|
+
};
|
|
277
|
+
this.debug(options);
|
|
278
|
+
const response = await this.client.repos.uploadReleaseAsset(options);
|
|
279
|
+
this.debug(response.data);
|
|
280
|
+
this.log.verbose(`octokit repos.uploadReleaseAsset: done (${response.data.browser_download_url})`);
|
|
281
|
+
return response.data;
|
|
282
|
+
} catch (err) {
|
|
283
|
+
return this.handleError(err, bail);
|
|
284
|
+
}
|
|
285
|
+
});
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
uploadAssets() {
|
|
289
|
+
const { assets } = this.options;
|
|
290
|
+
const { isReleased } = this.getContext();
|
|
291
|
+
const context = this.config.getContext();
|
|
292
|
+
const { isDryRun } = this.config;
|
|
293
|
+
|
|
294
|
+
const patterns = _.castArray(assets).map(pattern => format(pattern, context));
|
|
295
|
+
|
|
296
|
+
this.log.exec('octokit repos.uploadReleaseAssets', patterns, { isDryRun });
|
|
297
|
+
|
|
298
|
+
if (!assets || !isReleased) {
|
|
299
|
+
return true;
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
return globby(patterns).then(files => {
|
|
303
|
+
if (!files.length) {
|
|
304
|
+
this.log.warn(`octokit repos.uploadReleaseAssets: did not find "${assets}" relative to ${process.cwd()}`);
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
if (isDryRun) return Promise.resolve();
|
|
308
|
+
|
|
309
|
+
return Promise.all(files.map(filePath => this.uploadAsset(filePath)));
|
|
310
|
+
});
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
getReleaseUrlFallback(tagName) {
|
|
314
|
+
const { host, repository } = this.getContext('repo');
|
|
315
|
+
return `https://${host}/${repository}/releases/tag/${tagName}`;
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
generateWebUrl() {
|
|
319
|
+
const host = this.options.host || this.getContext('repo.host');
|
|
320
|
+
const isGitHub = host === 'github.com';
|
|
321
|
+
|
|
322
|
+
const options = this.getOctokitReleaseOptions();
|
|
323
|
+
const url = newGithubReleaseUrl({
|
|
324
|
+
user: options.owner,
|
|
325
|
+
repo: options.repo,
|
|
326
|
+
tag: options.tag_name,
|
|
327
|
+
isPrerelease: options.prerelease,
|
|
328
|
+
title: options.name,
|
|
329
|
+
body: options.body
|
|
330
|
+
});
|
|
331
|
+
return isGitHub ? url : url.replace('github.com', host);
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
async createWebRelease() {
|
|
335
|
+
const { isCI } = this.config;
|
|
336
|
+
const { tagName } = this.config.getContext();
|
|
337
|
+
const url = this.generateWebUrl();
|
|
338
|
+
if (isCI) {
|
|
339
|
+
this.setContext({ isReleased: true, releaseUrl: url });
|
|
340
|
+
} else {
|
|
341
|
+
const isWindows = process.platform === 'win32';
|
|
342
|
+
if (isWindows) this.log.info(`Opening ${url}`);
|
|
343
|
+
await open(url, { wait: isWindows });
|
|
344
|
+
this.setContext({ isReleased: true, releaseUrl: this.getReleaseUrlFallback(tagName) });
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
updateRelease() {
|
|
349
|
+
const { isDryRun } = this.config;
|
|
350
|
+
const release_id = this.getContext('releaseId');
|
|
351
|
+
const options = this.getOctokitReleaseOptions({ release_id });
|
|
352
|
+
|
|
353
|
+
this.log.exec(`octokit repos.updateRelease (${options.tag_name})`, { isDryRun });
|
|
354
|
+
|
|
355
|
+
if (isDryRun) return true;
|
|
356
|
+
|
|
357
|
+
return this.retry(async bail => {
|
|
358
|
+
try {
|
|
359
|
+
this.debug(options);
|
|
360
|
+
const response = await this.client.repos.updateRelease(options);
|
|
361
|
+
this.setContext({ releaseUrl: response.data.html_url });
|
|
362
|
+
this.debug(response.data);
|
|
363
|
+
this.log.verbose(`octokit repos.updateRelease: done (${response.headers.location})`);
|
|
364
|
+
return true;
|
|
365
|
+
} catch (err) {
|
|
366
|
+
return this.handleError(err, bail);
|
|
367
|
+
}
|
|
368
|
+
});
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
async commentOnResolvedItems() {
|
|
372
|
+
const { isDryRun } = this.config;
|
|
373
|
+
const { owner, project: repo } = this.getContext('repo');
|
|
374
|
+
const { changelog } = this.config.getContext();
|
|
375
|
+
const { comments } = this.options;
|
|
376
|
+
const { submit, issue, pr } = comments;
|
|
377
|
+
const context = this.getContext();
|
|
378
|
+
|
|
379
|
+
if (!submit || !changelog || isDryRun) return;
|
|
380
|
+
|
|
381
|
+
const shas = getCommitsFromChangelog(changelog);
|
|
382
|
+
const searchResults = await Promise.all(searchQueries(this.client, owner, repo, shas));
|
|
383
|
+
const mergedPullRequests = searchResults.flatMap(items => items.map(item => ({ type: 'pr', number: item.number })));
|
|
384
|
+
|
|
385
|
+
const host = 'https://' + (this.options.host || this.getContext('repo.host'));
|
|
386
|
+
const resolvedIssues = getResolvedIssuesFromChangelog(host, owner, repo, changelog);
|
|
387
|
+
|
|
388
|
+
for (const item of [...resolvedIssues, ...mergedPullRequests]) {
|
|
389
|
+
const { type, number } = item;
|
|
390
|
+
const comment = format(format(type === 'pr' ? pr : issue, context), context);
|
|
391
|
+
const url = `${host}/${owner}/${repo}/${type === 'pr' ? 'pull' : 'issues'}/${number}`;
|
|
392
|
+
|
|
393
|
+
try {
|
|
394
|
+
await this.client.issues.createComment({ owner, repo, issue_number: number, body: comment });
|
|
395
|
+
this.log.log(`● Commented on ${url}`);
|
|
396
|
+
} catch (error) {
|
|
397
|
+
this.log.log(`✕ Failed to comment on ${url}`);
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
export default GitHub;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { format } from '../../util.js';
|
|
2
|
+
|
|
3
|
+
const message = context => {
|
|
4
|
+
const { isPreRelease, github } = context;
|
|
5
|
+
const { releaseName, update } = github;
|
|
6
|
+
const name = format(releaseName, context);
|
|
7
|
+
return `${update ? 'Update' : 'Create a'} ${isPreRelease ? 'pre-' : ''}release on GitHub (${name})?`;
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
export default {
|
|
11
|
+
release: {
|
|
12
|
+
type: 'confirm',
|
|
13
|
+
message,
|
|
14
|
+
default: true
|
|
15
|
+
}
|
|
16
|
+
};
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
// Totally much borrowed from https://github.com/semantic-release/github/blob/master/lib/success.js
|
|
2
|
+
import issueParser from 'issue-parser';
|
|
3
|
+
|
|
4
|
+
const getSearchQueries = (base, commits, separator = '+') => {
|
|
5
|
+
return commits.reduce((searches, commit) => {
|
|
6
|
+
const lastSearch = searches[searches.length - 1];
|
|
7
|
+
if (lastSearch && lastSearch.length + commit.length <= 256 - separator.length) {
|
|
8
|
+
searches[searches.length - 1] = `${lastSearch}${separator}${commit}`;
|
|
9
|
+
} else {
|
|
10
|
+
searches.push(`${base}${separator}${commit}`);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
return searches;
|
|
14
|
+
}, []);
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
export const searchQueries = (client, owner, repo, shas) =>
|
|
18
|
+
getSearchQueries(`repo:${owner}/${repo}+type:pr+is:merged`, shas).map(
|
|
19
|
+
async q => (await client.search.issuesAndPullRequests({ q })).data.items
|
|
20
|
+
);
|
|
21
|
+
|
|
22
|
+
export const getCommitsFromChangelog = changelog => {
|
|
23
|
+
const regex = /\(([a-f0-9]{7,})\)/i;
|
|
24
|
+
return changelog.split('\n').flatMap(message => {
|
|
25
|
+
const match = message.match(regex);
|
|
26
|
+
if (match) return match[1];
|
|
27
|
+
return [];
|
|
28
|
+
});
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
export const getResolvedIssuesFromChangelog = (host, owner, repo, changelog) => {
|
|
32
|
+
const parser = issueParser('github', { hosts: [host] });
|
|
33
|
+
return changelog
|
|
34
|
+
.split('\n')
|
|
35
|
+
.map(parser)
|
|
36
|
+
.flatMap(parsed => parsed.actions.close)
|
|
37
|
+
.filter(action => !action.slug || action.slug === `${owner}/${repo}`)
|
|
38
|
+
.map(action => ({ type: 'issue', number: parseInt(action.issue, 10) }));
|
|
39
|
+
};
|