Skip to content

Commit 326ca99

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

File tree

8 files changed

+107
-126
lines changed

8 files changed

+107
-126
lines changed

lib/publish.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,13 @@ const debug = require('debug')('semantic-release:gitlab');
44
const resolveConfig = require('./resolve-config');
55
const getRepoId = require('./get-repo-id');
66

7-
module.exports = async (pluginConfig, {options: {repositoryUrl}, nextRelease: {gitTag, gitHead, notes}, logger}) => {
8-
const {gitlabToken, gitlabUrl, gitlabApiPathPrefix} = resolveConfig(pluginConfig);
7+
module.exports = async (pluginConfig, context) => {
8+
const {
9+
options: {repositoryUrl},
10+
nextRelease: {gitTag, gitHead, notes},
11+
logger,
12+
} = context;
13+
const {gitlabToken, gitlabUrl, gitlabApiPathPrefix} = resolveConfig(pluginConfig, context);
914
const repoId = getRepoId(gitlabUrl, repositoryUrl);
1015
const encodedRepoId = encodeURIComponent(repoId);
1116
const apiUrl = urlJoin(gitlabUrl, gitlabApiPathPrefix);

lib/resolve-config.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
module.exports = ({gitlabUrl, gitlabApiPathPrefix}) => ({
2-
gitlabToken: process.env.GL_TOKEN || process.env.GITLAB_TOKEN,
3-
gitlabUrl: gitlabUrl || process.env.GL_URL || process.env.GITLAB_URL || 'https://gitlab.com',
1+
module.exports = ({gitlabUrl, gitlabApiPathPrefix}, {env}) => ({
2+
gitlabToken: env.GL_TOKEN || env.GITLAB_TOKEN,
3+
gitlabUrl: gitlabUrl || env.GL_URL || env.GITLAB_URL || 'https://gitlab.com',
44
gitlabApiPathPrefix:
55
typeof gitlabApiPathPrefix === 'string'
66
? gitlabApiPathPrefix
7-
: null || typeof process.env.GL_PREFIX === 'string'
8-
? process.env.GL_PREFIX
9-
: null || typeof process.env.GITLAB_PREFIX === 'string'
10-
? process.env.GITLAB_PREFIX
7+
: null || typeof env.GL_PREFIX === 'string'
8+
? env.GL_PREFIX
9+
: null || typeof env.GITLAB_PREFIX === 'string'
10+
? env.GITLAB_PREFIX
1111
: null || '/api/v4',
1212
});

lib/verify.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,13 @@ const resolveConfig = require('./resolve-config');
66
const getRepoId = require('./get-repo-id');
77
const getError = require('./get-error');
88

9-
module.exports = async (pluginConfig, {options: {repositoryUrl}, logger}) => {
9+
module.exports = async (pluginConfig, context) => {
10+
const {
11+
options: {repositoryUrl},
12+
logger,
13+
} = context;
1014
const errors = [];
11-
const {gitlabToken, gitlabUrl, gitlabApiPathPrefix} = resolveConfig(pluginConfig);
15+
const {gitlabToken, gitlabUrl, gitlabApiPathPrefix} = resolveConfig(pluginConfig, context);
1216
const repoId = getRepoId(gitlabUrl, repositoryUrl);
1317
debug('repoId: %o', repoId);
1418

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@
6868
"all": true
6969
},
7070
"peerDependencies": {
71-
"semantic-release": ">=13.3.0 <16.0.0"
71+
"semantic-release": ">=15.8.0 <16.0.0"
7272
},
7373
"prettier": {
7474
"printWidth": 120,

test/helpers/mock-gitlab.js

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,23 @@ import urlJoin from 'url-join';
44
/**
55
* Retun a `nock` object setup to respond to a GitLab authentication request. Other expectation and responses can be chained.
66
*
7-
* @param {String} [gitlabToken=process.env.GL_TOKEN || process.env.GITLAB_TOKEN || 'GL_TOKEN'] The github token to return in the authentication response.
8-
* @param {String} [gitlabUrl=process.env.GL_URL || process.env.GITLAB_URL || 'https://api.github.com'] The url on which to intercept http requests.
9-
* @param {String} [gitlabApiPathPrefix=process.env.GL_PREFIX || process.env.GITLAB_PREFIX || ''] The GitHub Enterprise API prefix.
7+
* @param {Object} [env={}] Environment variables.
8+
* @param {String} [gitlabToken=env.GL_TOKEN || env.GITLAB_TOKEN || 'GL_TOKEN'] The github token to return in the authentication response.
9+
* @param {String} [gitlabUrl=env.GL_URL || env.GITLAB_URL || 'https://api.github.com'] The url on which to intercept http requests.
10+
* @param {String} [gitlabApiPathPrefix=env.GL_PREFIX || env.GITLAB_PREFIX || ''] The GitHub Enterprise API prefix.
1011
* @return {Object} A `nock` object ready to respond to a github authentication request.
1112
*/
12-
export default function authenticate({
13-
gitlabToken = process.env.GL_TOKEN || process.env.GITLAB_TOKEN || 'GL_TOKEN',
14-
gitlabUrl = process.env.GL_URL || process.env.GITLAB_URL || 'https://gitlab.com',
15-
gitlabApiPathPrefix = typeof process.env.GL_PREFIX === 'string'
16-
? process.env.GL_PREFIX
17-
: null || typeof process.env.GITLAB_PREFIX === 'string'
18-
? process.env.GITLAB_PREFIX
19-
: null || '/api/v4',
20-
} = {}) {
13+
export default function authenticate(
14+
env = {},
15+
{
16+
gitlabToken = env.GL_TOKEN || env.GITLAB_TOKEN || 'GL_TOKEN',
17+
gitlabUrl = env.GL_URL || env.GITLAB_URL || 'https://gitlab.com',
18+
gitlabApiPathPrefix = typeof env.GL_PREFIX === 'string'
19+
? env.GL_PREFIX
20+
: null || typeof env.GITLAB_PREFIX === 'string'
21+
? env.GITLAB_PREFIX
22+
: null || '/api/v4',
23+
} = {}
24+
) {
2125
return nock(urlJoin(gitlabUrl, gitlabApiPathPrefix), {reqheaders: {'Private-Token': gitlabToken}});
2226
}

test/integration.test.js

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,7 @@ import authenticate from './helpers/mock-gitlab';
66

77
/* eslint camelcase: ["error", {properties: "never"}] */
88

9-
// Save the current process.env
10-
const envBackup = Object.assign({}, process.env);
11-
129
test.beforeEach(t => {
13-
// Delete env variables in case they are on the machine running the tests
14-
delete process.env.GL_TOKEN;
15-
delete process.env.GITLAB_TOKEN;
16-
delete process.env.GL_URL;
17-
delete process.env.GITLAB_URL;
18-
delete process.env.GL_PREFIX;
19-
delete process.env.GITLAB_PREFIX;
2010
// Clear npm cache to refresh the module state
2111
clearModule('..');
2212
t.context.m = require('..');
@@ -27,35 +17,34 @@ test.beforeEach(t => {
2717
});
2818

2919
test.afterEach.always(() => {
30-
// Restore process.env
31-
process.env = envBackup;
3220
// Clear nock
3321
nock.cleanAll();
3422
});
3523

3624
test.serial('Verify GitLab auth', async t => {
37-
process.env.GITLAB_TOKEN = 'gitlab_token';
25+
const env = {GITLAB_TOKEN: 'gitlab_token'};
3826
const owner = 'test_user';
3927
const repo = 'test_repo';
4028
const options = {repositoryUrl: `git+https://othertesturl.com/${owner}/${repo}.git`};
41-
const github = authenticate()
29+
const github = authenticate(env)
4230
.get(`/projects/${owner}%2F${repo}`)
4331
.reply(200, {permissions: {project_access: {access_level: 30}}});
4432

45-
await t.notThrows(t.context.m.verifyConditions({}, {options, logger: t.context.logger}));
33+
await t.notThrows(t.context.m.verifyConditions({}, {env, options, logger: t.context.logger}));
4634

4735
t.true(github.isDone());
4836
});
4937

5038
test.serial('Throw SemanticReleaseError if invalid config', async t => {
39+
const env = {};
5140
const options = {
5241
publish: [{path: '@semantic-release/npm'}, {path: '@semantic-release/gitlab'}],
5342
repositoryUrl: 'git+ssh://[email protected]/context.git',
5443
};
5544

5645
const errors = [
5746
...(await t.throws(
58-
t.context.m.verifyConditions({gitlabUrl: 'https://gitlab.com/context'}, {options, logger: t.context.logger})
47+
t.context.m.verifyConditions({gitlabUrl: 'https://gitlab.com/context'}, {env, options, logger: t.context.logger})
5948
)),
6049
];
6150

@@ -68,12 +57,12 @@ test.serial('Throw SemanticReleaseError if invalid config', async t => {
6857
test.serial('Publish a release', async t => {
6958
const owner = 'test_user';
7059
const repo = 'test_repo';
71-
process.env.GL_TOKEN = 'gitlab_token';
60+
const env = {GL_TOKEN: 'gitlab_token'};
7261
const nextRelease = {gitHead: '123', gitTag: 'v1.0.0', notes: 'Test release note body'};
7362
const options = {branch: 'master', repositoryUrl: `https://gitlab.com/${owner}/${repo}.git`};
7463
const encodedRepoId = encodeURIComponent(`${owner}/${repo}`);
7564

76-
const gitlab = authenticate()
65+
const gitlab = authenticate(env)
7766
.get(`/projects/${encodedRepoId}`)
7867
.reply(200, {permissions: {project_access: {access_level: 30}}})
7968
.post(`/projects/${encodedRepoId}/repository/tags/${nextRelease.gitTag}/release`, {
@@ -82,7 +71,7 @@ test.serial('Publish a release', async t => {
8271
})
8372
.reply(200);
8473

85-
const result = await t.context.m.publish({}, {nextRelease, options, logger: t.context.logger});
74+
const result = await t.context.m.publish({}, {env, nextRelease, options, logger: t.context.logger});
8675

8776
t.is(result.url, `https://gitlab.com/${encodedRepoId}/tags/${nextRelease.gitTag}`);
8877
t.deepEqual(t.context.log.args[0], ['Verify GitLab authentication (%s)', 'https://gitlab.com/api/v4']);
@@ -91,14 +80,14 @@ test.serial('Publish a release', async t => {
9180
});
9281

9382
test.serial('Verify Github auth and release', async t => {
94-
process.env.GL_TOKEN = 'gitlab_token';
83+
const env = {GL_TOKEN: 'gitlab_token'};
9584
const owner = 'test_user';
9685
const repo = 'test_repo';
9786
const options = {repositoryUrl: `https://github.com/${owner}/${repo}.git`};
9887
const encodedRepoId = encodeURIComponent(`${owner}/${repo}`);
9988
const nextRelease = {gitHead: '123', gitTag: 'v1.0.0', notes: 'Test release note body'};
10089

101-
const gitlab = authenticate()
90+
const gitlab = authenticate(env)
10291
.get(`/projects/${encodedRepoId}`)
10392
.reply(200, {permissions: {project_access: {access_level: 30}}})
10493
.post(`/projects/${encodedRepoId}/repository/tags/${nextRelease.gitTag}/release`, {
@@ -107,8 +96,8 @@ test.serial('Verify Github auth and release', async t => {
10796
})
10897
.reply(200);
10998

110-
await t.notThrows(t.context.m.verifyConditions({}, {options, logger: t.context.logger}));
111-
const result = await t.context.m.publish({}, {nextRelease, options, logger: t.context.logger});
99+
await t.notThrows(t.context.m.verifyConditions({}, {env, options, logger: t.context.logger}));
100+
const result = await t.context.m.publish({}, {env, options, nextRelease, logger: t.context.logger});
112101

113102
t.is(result.url, `https://gitlab.com/${encodedRepoId}/tags/${nextRelease.gitTag}`);
114103
t.deepEqual(t.context.log.args[0], ['Verify GitLab authentication (%s)', 'https://gitlab.com/api/v4']);

test/publish.test.js

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,48 +6,36 @@ import authenticate from './helpers/mock-gitlab';
66

77
/* eslint camelcase: ["error", {properties: "never"}] */
88

9-
// Save the current process.env
10-
const envBackup = Object.assign({}, process.env);
11-
129
test.beforeEach(t => {
13-
// Delete env variables in case they are on the machine running the tests
14-
delete process.env.GL_TOKEN;
15-
delete process.env.GITLAB_TOKEN;
16-
delete process.env.GL_URL;
17-
delete process.env.GITLAB_URL;
18-
delete process.env.GL_PREFIX;
19-
delete process.env.GITLAB_PREFIX;
2010
// Mock logger
2111
t.context.log = stub();
2212
t.context.error = stub();
2313
t.context.logger = {log: t.context.log, error: t.context.error};
2414
});
2515

2616
test.afterEach.always(() => {
27-
// Restore process.env
28-
process.env = envBackup;
2917
// Clear nock
3018
nock.cleanAll();
3119
});
3220

3321
test.serial('Publish a release', async t => {
3422
const owner = 'test_user';
3523
const repo = 'test_repo';
36-
process.env.GITLAB_TOKEN = 'gitlab_token';
24+
const env = {GITLAB_TOKEN: 'gitlab_token'};
3725
const pluginConfig = {};
3826
const nextRelease = {gitHead: '123', gitTag: '@scope/v1.0.0', notes: 'Test release note body'};
3927
const options = {repositoryUrl: `https://gitlab.com/${owner}/${repo}.git`};
4028
const encodedRepoId = encodeURIComponent(`${owner}/${repo}`);
4129
const encodedGitTag = encodeURIComponent(nextRelease.gitTag);
4230

43-
const gitlab = authenticate()
31+
const gitlab = authenticate(env)
4432
.post(`/projects/${encodedRepoId}/repository/tags/${encodedGitTag}/release`, {
4533
tag_name: nextRelease.gitTag,
4634
description: nextRelease.notes,
4735
})
4836
.reply(200);
4937

50-
const result = await publish(pluginConfig, {options, nextRelease, logger: t.context.logger});
38+
const result = await publish(pluginConfig, {env, options, nextRelease, logger: t.context.logger});
5139

5240
t.is(result.url, `https://gitlab.com/${encodedRepoId}/tags/${encodedGitTag}`);
5341
t.deepEqual(t.context.log.args[0], ['Published GitLab release: %s', nextRelease.gitTag]);

0 commit comments

Comments
 (0)