Skip to content

Commit 1f906b4

Browse files
committed
feat: use cwd and env options passed by core
BREAKING CHANGE: require `semantic-release` >= `15.8.0`
1 parent 683cf3d commit 1f906b4

15 files changed

+387
-440
lines changed

lib/fail.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,15 @@ const getClient = require('./get-client');
77
const findSRIssues = require('./find-sr-issues');
88
const getFailComment = require('./get-fail-comment');
99

10-
module.exports = async (pluginConfig, {options: {branch, repositoryUrl}, errors, logger}) => {
10+
module.exports = async (pluginConfig, context) => {
11+
const {
12+
options: {branch, repositoryUrl},
13+
errors,
14+
logger,
15+
} = context;
1116
const {githubToken, githubUrl, githubApiPathPrefix, proxy, failComment, failTitle, labels, assignees} = resolveConfig(
12-
pluginConfig
17+
pluginConfig,
18+
context
1319
);
1420
const {name: repo, owner} = parseGithubUrl(repositoryUrl);
1521
const github = getClient({githubToken, githubUrl, githubApiPathPrefix, proxy});

lib/glob-assets.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const {isPlainObject, castArray, uniqWith} = require('lodash');
33
const globby = require('globby');
44
const debug = require('debug')('semantic-release:github');
55

6-
module.exports = async assets =>
6+
module.exports = async ({cwd}, assets) =>
77
uniqWith(
88
[]
99
.concat(
@@ -19,7 +19,7 @@ module.exports = async assets =>
1919
);
2020
return [];
2121
}
22-
const globbed = await globby(glob, {expandDirectories: true, gitignore: false, dot: true});
22+
const globbed = await globby(glob, {cwd, expandDirectories: true, gitignore: false, dot: true});
2323
if (isPlainObject(asset)) {
2424
if (globbed.length > 1) {
2525
// If asset is an Object with a glob the `path` property that resolve to multiple files,

lib/publish.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const {basename, extname} = require('path');
1+
const {basename, extname, resolve} = require('path');
22
const {stat, readFile} = require('fs-extra');
33
const {isPlainObject} = require('lodash');
44
const parseGithubUrl = require('parse-github-url');
@@ -8,8 +8,14 @@ const globAssets = require('./glob-assets.js');
88
const resolveConfig = require('./resolve-config');
99
const getClient = require('./get-client');
1010

11-
module.exports = async (pluginConfig, {options: {branch, repositoryUrl}, nextRelease: {gitTag, notes}, logger}) => {
12-
const {githubToken, githubUrl, githubApiPathPrefix, proxy, assets} = resolveConfig(pluginConfig);
11+
module.exports = async (pluginConfig, context) => {
12+
const {
13+
cwd,
14+
options: {branch, repositoryUrl},
15+
nextRelease: {gitTag, notes},
16+
logger,
17+
} = context;
18+
const {githubToken, githubUrl, githubApiPathPrefix, proxy, assets} = resolveConfig(pluginConfig, context);
1319
const {name: repo, owner} = parseGithubUrl(repositoryUrl);
1420
const github = getClient({githubToken, githubUrl, githubApiPathPrefix, proxy});
1521
const release = {owner, repo, tag_name: gitTag, name: gitTag, target_commitish: branch, body: notes}; // eslint-disable-line camelcase
@@ -25,7 +31,7 @@ module.exports = async (pluginConfig, {options: {branch, repositoryUrl}, nextRel
2531
logger.log('Published GitHub release: %s', url);
2632

2733
if (assets && assets.length > 0) {
28-
const globbedAssets = await globAssets(assets);
34+
const globbedAssets = await globAssets(context, assets);
2935
debug('globed assets: %o', globbedAssets);
3036

3137
await Promise.all(
@@ -34,7 +40,7 @@ module.exports = async (pluginConfig, {options: {branch, repositoryUrl}, nextRel
3440
let file;
3541

3642
try {
37-
file = await stat(filePath);
43+
file = await stat(resolve(cwd, filePath));
3844
} catch (err) {
3945
logger.error('The asset %s cannot be read, and will be ignored.', filePath);
4046
return;
@@ -47,7 +53,7 @@ module.exports = async (pluginConfig, {options: {branch, repositoryUrl}, nextRel
4753
const fileName = asset.name || basename(filePath);
4854
const upload = {
4955
url: uploadUrl,
50-
file: await readFile(filePath),
56+
file: await readFile(resolve(cwd, filePath)),
5157
contentType: mime.getType(extname(fileName)) || 'text/plain',
5258
contentLength: file.size,
5359
name: fileName,

lib/resolve-config.js

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,13 @@
11
const {isUndefined, castArray} = require('lodash');
22

3-
module.exports = ({
4-
githubUrl,
5-
githubApiPathPrefix,
6-
proxy,
7-
assets,
8-
successComment,
9-
failTitle,
10-
failComment,
11-
labels,
12-
assignees,
13-
}) => ({
14-
githubToken: process.env.GH_TOKEN || process.env.GITHUB_TOKEN,
15-
githubUrl: githubUrl || process.env.GH_URL || process.env.GITHUB_URL,
16-
githubApiPathPrefix: githubApiPathPrefix || process.env.GH_PREFIX || process.env.GITHUB_PREFIX || '',
17-
proxy: proxy || process.env.HTTP_PROXY,
3+
module.exports = (
4+
{githubUrl, githubApiPathPrefix, proxy, assets, successComment, failTitle, failComment, labels, assignees},
5+
{env}
6+
) => ({
7+
githubToken: env.GH_TOKEN || env.GITHUB_TOKEN,
8+
githubUrl: githubUrl || env.GH_URL || env.GITHUB_URL,
9+
githubApiPathPrefix: githubApiPathPrefix || env.GH_PREFIX || env.GITHUB_PREFIX || '',
10+
proxy: proxy || env.HTTP_PROXY,
1811
assets: assets ? castArray(assets) : assets,
1912
successComment,
2013
failTitle: isUndefined(failTitle) || failTitle === false ? 'The automated release is failing 🚨' : failTitle,

lib/success.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,19 @@ const getSearchQueries = require('./get-search-queries');
1010
const getSuccessComment = require('./get-success-comment');
1111
const findSRIssues = require('./find-sr-issues');
1212

13-
module.exports = async (
14-
pluginConfig,
15-
{options: {branch, repositoryUrl}, lastRelease, commits, nextRelease, releases, logger}
16-
) => {
17-
const {githubToken, githubUrl, githubApiPathPrefix, proxy, successComment, failTitle} = resolveConfig(pluginConfig);
13+
module.exports = async (pluginConfig, context) => {
14+
const {
15+
options: {branch, repositoryUrl},
16+
lastRelease,
17+
commits,
18+
nextRelease,
19+
releases,
20+
logger,
21+
} = context;
22+
const {githubToken, githubUrl, githubApiPathPrefix, proxy, successComment, failTitle} = resolveConfig(
23+
pluginConfig,
24+
context
25+
);
1826
const {name: repo, owner} = parseGithubUrl(repositoryUrl);
1927
const github = getClient({githubToken, githubUrl, githubApiPathPrefix, proxy});
2028
const parser = issueParser('github', githubUrl ? {hosts: [githubUrl]} : {});

lib/verify.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,12 @@ const VALIDATORS = {
2323
assignees: isArrayOf(isStringOrStringArray),
2424
};
2525

26-
module.exports = async (pluginConfig, {options: {repositoryUrl}, logger}) => {
27-
const {githubToken, githubUrl, githubApiPathPrefix, proxy, ...options} = resolveConfig(pluginConfig);
26+
module.exports = async (pluginConfig, context) => {
27+
const {
28+
options: {repositoryUrl},
29+
logger,
30+
} = context;
31+
const {githubToken, githubUrl, githubApiPathPrefix, proxy, ...options} = resolveConfig(pluginConfig, context);
2832

2933
const errors = Object.entries({...options, proxy}).reduce(
3034
(errors, [option, value]) =>

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@
8282
],
8383
"all": true
8484
},
85+
"peerDependencies": {
86+
"semantic-release": ">=15.8.0 <16.0.0"
87+
},
8588
"prettier": {
8689
"printWidth": 120,
8790
"trailingComma": "es5"

test/fail.test.js

Lines changed: 18 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -14,34 +14,22 @@ const fail = proxyquire('../lib/fail', {
1414
'./get-client': proxyquire('../lib/get-client', {'./definitions/rate-limit': rateLimit}),
1515
});
1616

17-
// Save the current process.env
18-
const envBackup = Object.assign({}, process.env);
19-
2017
test.beforeEach(t => {
21-
// Delete env variables in case they are on the machine running the tests
22-
delete process.env.GH_TOKEN;
23-
delete process.env.GITHUB_TOKEN;
24-
delete process.env.GH_URL;
25-
delete process.env.GITHUB_URL;
26-
delete process.env.GH_PREFIX;
27-
delete process.env.GITHUB_PREFIX;
2818
// Mock logger
2919
t.context.log = stub();
3020
t.context.error = stub();
3121
t.context.logger = {log: t.context.log, error: t.context.error};
3222
});
3323

3424
test.afterEach.always(() => {
35-
// Restore process.env
36-
process.env = envBackup;
3725
// Clear nock
3826
nock.cleanAll();
3927
});
4028

4129
test.serial('Open a new issue with the list of errors', async t => {
4230
const owner = 'test_user';
4331
const repo = 'test_repo';
44-
process.env.GITHUB_TOKEN = 'github_token';
32+
const env = {GITHUB_TOKEN: 'github_token'};
4533
const failTitle = 'The automated release is failing 🚨';
4634
const pluginConfig = {failTitle};
4735
const options = {branch: 'master', repositoryUrl: `https://github.com/${owner}/${repo}.git`};
@@ -50,7 +38,7 @@ test.serial('Open a new issue with the list of errors', async t => {
5038
new SemanticReleaseError('Error message 2', 'ERR2', 'Error 2 details'),
5139
new SemanticReleaseError('Error message 3', 'ERR3', 'Error 3 details'),
5240
];
53-
const github = authenticate()
41+
const github = authenticate(env)
5442
.get(
5543
`/search/issues?q=${escape('in:title')}+${escape(`repo:${owner}/${repo}`)}+${escape('type:issue')}+${escape(
5644
'state:open'
@@ -64,7 +52,7 @@ test.serial('Open a new issue with the list of errors', async t => {
6452
})
6553
.reply(200, {html_url: 'https://github.com/issues/1', number: 1});
6654

67-
await fail(pluginConfig, {options, errors, logger: t.context.logger});
55+
await fail(pluginConfig, {env, options, errors, logger: t.context.logger});
6856

6957
t.true(t.context.log.calledWith('Created issue #%d: %s.', 1, 'https://github.com/issues/1'));
7058
t.true(github.isDone());
@@ -73,7 +61,7 @@ test.serial('Open a new issue with the list of errors', async t => {
7361
test.serial('Open a new issue with the list of errors, retrying 4 times', async t => {
7462
const owner = 'test_user';
7563
const repo = 'test_repo';
76-
process.env.GITHUB_TOKEN = 'github_token';
64+
const env = {GITHUB_TOKEN: 'github_token'};
7765
const failTitle = 'The automated release is failing 🚨';
7866
const pluginConfig = {failTitle};
7967
const options = {branch: 'master', repositoryUrl: `https://github.com/${owner}/${repo}.git`};
@@ -82,7 +70,7 @@ test.serial('Open a new issue with the list of errors, retrying 4 times', async
8270
new SemanticReleaseError('Error message 2', 'ERR2', 'Error 2 details'),
8371
new SemanticReleaseError('Error message 3', 'ERR3', 'Error 3 details'),
8472
];
85-
const github = authenticate()
73+
const github = authenticate(env)
8674
.get(
8775
`/search/issues?q=${escape('in:title')}+${escape(`repo:${owner}/${repo}`)}+${escape('type:issue')}+${escape(
8876
'state:open'
@@ -110,7 +98,7 @@ test.serial('Open a new issue with the list of errors, retrying 4 times', async
11098
})
11199
.reply(200, {html_url: 'https://github.com/issues/1', number: 1});
112100

113-
await fail(pluginConfig, {options, errors, logger: t.context.logger});
101+
await fail(pluginConfig, {env, options, errors, logger: t.context.logger});
114102

115103
t.true(t.context.log.calledWith('Created issue #%d: %s.', 1, 'https://github.com/issues/1'));
116104
t.true(github.isDone());
@@ -119,7 +107,7 @@ test.serial('Open a new issue with the list of errors, retrying 4 times', async
119107
test.serial('Open a new issue with the list of errors and custom title and comment', async t => {
120108
const owner = 'test_user';
121109
const repo = 'test_repo';
122-
process.env.GITHUB_TOKEN = 'github_token';
110+
const env = {GITHUB_TOKEN: 'github_token'};
123111
const failTitle = 'Custom title';
124112
const failComment = `branch \${branch} \${errors[0].message} \${errors[1].message} \${errors[2].message}`;
125113
const pluginConfig = {failTitle, failComment};
@@ -129,7 +117,7 @@ test.serial('Open a new issue with the list of errors and custom title and comme
129117
new SemanticReleaseError('Error message 2', 'ERR2', 'Error 2 details'),
130118
new SemanticReleaseError('Error message 3', 'ERR3', 'Error 3 details'),
131119
];
132-
const github = authenticate()
120+
const github = authenticate(env)
133121
.get(
134122
`/search/issues?q=${escape('in:title')}+${escape(`repo:${owner}/${repo}`)}+${escape('type:issue')}+${escape(
135123
'state:open'
@@ -143,7 +131,7 @@ test.serial('Open a new issue with the list of errors and custom title and comme
143131
})
144132
.reply(200, {html_url: 'https://github.com/issues/1', number: 1});
145133

146-
await fail(pluginConfig, {options, errors, logger: t.context.logger});
134+
await fail(pluginConfig, {env, options, errors, logger: t.context.logger});
147135

148136
t.true(t.context.log.calledWith('Created issue #%d: %s.', 1, 'https://github.com/issues/1'));
149137
t.true(github.isDone());
@@ -152,7 +140,7 @@ test.serial('Open a new issue with the list of errors and custom title and comme
152140
test.serial('Open a new issue with assignees and the list of errors', async t => {
153141
const owner = 'test_user';
154142
const repo = 'test_repo';
155-
process.env.GITHUB_TOKEN = 'github_token';
143+
const env = {GITHUB_TOKEN: 'github_token'};
156144
const failTitle = 'The automated release is failing 🚨';
157145
const assignees = ['user1', 'user2'];
158146
const pluginConfig = {failTitle, assignees};
@@ -161,7 +149,7 @@ test.serial('Open a new issue with assignees and the list of errors', async t =>
161149
new SemanticReleaseError('Error message 1', 'ERR1', 'Error 1 details'),
162150
new SemanticReleaseError('Error message 2', 'ERR2', 'Error 2 details'),
163151
];
164-
const github = authenticate()
152+
const github = authenticate(env)
165153
.get(
166154
`/search/issues?q=${escape('in:title')}+${escape(`repo:${owner}/${repo}`)}+${escape('type:issue')}+${escape(
167155
'state:open'
@@ -176,7 +164,7 @@ test.serial('Open a new issue with assignees and the list of errors', async t =>
176164
})
177165
.reply(200, {html_url: 'https://github.com/issues/1', number: 1});
178166

179-
await fail(pluginConfig, {options, errors, logger: t.context.logger});
167+
await fail(pluginConfig, {env, options, errors, logger: t.context.logger});
180168

181169
t.true(t.context.log.calledWith('Created issue #%d: %s.', 1, 'https://github.com/issues/1'));
182170
t.true(github.isDone());
@@ -185,7 +173,7 @@ test.serial('Open a new issue with assignees and the list of errors', async t =>
185173
test.serial('Open a new issue without labels and the list of errors', async t => {
186174
const owner = 'test_user';
187175
const repo = 'test_repo';
188-
process.env.GITHUB_TOKEN = 'github_token';
176+
const env = {GITHUB_TOKEN: 'github_token'};
189177
const failTitle = 'The automated release is failing 🚨';
190178
const labels = false;
191179
const pluginConfig = {failTitle, labels};
@@ -194,7 +182,7 @@ test.serial('Open a new issue without labels and the list of errors', async t =>
194182
new SemanticReleaseError('Error message 1', 'ERR1', 'Error 1 details'),
195183
new SemanticReleaseError('Error message 2', 'ERR2', 'Error 2 details'),
196184
];
197-
const github = authenticate()
185+
const github = authenticate(env)
198186
.get(
199187
`/search/issues?q=${escape('in:title')}+${escape(`repo:${owner}/${repo}`)}+${escape('type:issue')}+${escape(
200188
'state:open'
@@ -208,7 +196,7 @@ test.serial('Open a new issue without labels and the list of errors', async t =>
208196
})
209197
.reply(200, {html_url: 'https://github.com/issues/1', number: 1});
210198

211-
await fail(pluginConfig, {options, errors, logger: t.context.logger});
199+
await fail(pluginConfig, {env, options, errors, logger: t.context.logger});
212200

213201
t.true(t.context.log.calledWith('Created issue #%d: %s.', 1, 'https://github.com/issues/1'));
214202
t.true(github.isDone());
@@ -217,7 +205,7 @@ test.serial('Open a new issue without labels and the list of errors', async t =>
217205
test.serial('Update the first existing issue with the list of errors', async t => {
218206
const owner = 'test_user';
219207
const repo = 'test_repo';
220-
process.env.GITHUB_TOKEN = 'github_token';
208+
const env = {GITHUB_TOKEN: 'github_token'};
221209
const failTitle = 'The automated release is failing 🚨';
222210
const pluginConfig = {failTitle};
223211
const options = {branch: 'master', repositoryUrl: `https://github.com/${owner}/${repo}.git`};
@@ -231,7 +219,7 @@ test.serial('Update the first existing issue with the list of errors', async t =
231219
{number: 2, body: `Issue 2 body\n\n${ISSUE_ID}`, title: failTitle},
232220
{number: 3, body: `Issue 3 body\n\n${ISSUE_ID}`, title: failTitle},
233221
];
234-
const github = authenticate()
222+
const github = authenticate(env)
235223
.get(
236224
`/search/issues?q=${escape('in:title')}+${escape(`repo:${owner}/${repo}`)}+${escape('type:issue')}+${escape(
237225
'state:open'
@@ -243,7 +231,7 @@ test.serial('Update the first existing issue with the list of errors', async t =
243231
})
244232
.reply(200, {html_url: 'https://github.com/issues/2', number: 2});
245233

246-
await fail(pluginConfig, {options, errors, logger: t.context.logger});
234+
await fail(pluginConfig, {env, options, errors, logger: t.context.logger});
247235

248236
t.true(t.context.log.calledWith('Found existing semantic-release issue #%d.', 2));
249237
t.true(t.context.log.calledWith('Added comment to issue #%d: %s.', 2, 'https://github.com/issues/2'));

0 commit comments

Comments
 (0)