Skip to content

Commit 92fd43f

Browse files
committed
feat: use GitLab CI/CD environment variables as default
This require [email protected] or higher
1 parent 611f08c commit 92fd43f

File tree

7 files changed

+288
-49
lines changed

7 files changed

+288
-49
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,11 @@ Create a [personal access token](https://docs.gitlab.com/ce/user/profile/persona
6262

6363
### Options
6464

65-
| Option | Description | Default |
66-
|-----------------------|--------------------------------------------------------------------|------------------------------------------------------------------------|
67-
| `gitlabUrl` | The GitLab endpoint. | `GL_URL` or `GITLAB_URL` environment variable or `https://gitlab.com`. |
68-
| `gitlabApiPathPrefix` | The GitLab API prefix. | `GL_PREFIX` or `GITLAB_PREFIX` environment variable or `/api/v4`. |
69-
| `assets` | An array of files to upload to the release. See [assets](#assets). | - |
65+
| Option | Description | Default |
66+
|-----------------------|--------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
67+
| `gitlabUrl` | The GitLab endpoint. | `GL_URL` or `GITLAB_URL` environment variable or CI provided environment variables if running on [GitLab CI/CD](https://docs.gitlab.com/ee/ci) or `https://gitlab.com`. |
68+
| `gitlabApiPathPrefix` | The GitLab API prefix. | `GL_PREFIX` or `GITLAB_PREFIX` environment variable or CI provided environment variables if running on [GitLab CI/CD](https://docs.gitlab.com/ee/ci) or `/api/v4`. |
69+
| `assets` | An array of files to upload to the release. See [assets](#assets). | - |
7070

7171
#### assets
7272

lib/get-repo-id.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
const parsePath = require('parse-path');
22
const escapeStringRegexp = require('escape-string-regexp');
33

4-
module.exports = (gitlabUrl, repositoryUrl) => {
5-
return parsePath(repositoryUrl)
6-
.pathname.replace(new RegExp(`^${escapeStringRegexp(parsePath(gitlabUrl).pathname)}`), '')
7-
.replace(/^\//, '')
8-
.replace(/\/$/, '')
9-
.replace(/\.git$/, '');
10-
};
4+
module.exports = ({envCi: {service} = {}, env: {CI_PROJECT_PATH}}, gitlabUrl, repositoryUrl) =>
5+
service === 'gitlab' && CI_PROJECT_PATH
6+
? CI_PROJECT_PATH
7+
: parsePath(repositoryUrl)
8+
.pathname.replace(new RegExp(`^${escapeStringRegexp(parsePath(gitlabUrl).pathname)}`), '')
9+
.replace(/^\//, '')
10+
.replace(/\/$/, '')
11+
.replace(/\.git$/, '');

lib/publish.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,11 @@ module.exports = async (pluginConfig, context) => {
1717
nextRelease: {gitTag, gitHead, notes},
1818
logger,
1919
} = context;
20-
const {gitlabToken, gitlabUrl, gitlabApiPathPrefix, assets} = resolveConfig(pluginConfig, context);
20+
const {gitlabToken, gitlabUrl, gitlabApiUrl, assets} = resolveConfig(pluginConfig, context);
2121
const assetsList = [];
22-
const repoId = getRepoId(gitlabUrl, repositoryUrl);
22+
const repoId = getRepoId(context, gitlabUrl, repositoryUrl);
2323
const encodedRepoId = encodeURIComponent(repoId);
2424
const encodedGitTag = encodeURIComponent(gitTag);
25-
const apiUrl = urlJoin(gitlabUrl, gitlabApiPathPrefix, `projects/${encodedRepoId}`);
2625
const apiOptions = {json: true, headers: {'PRIVATE-TOKEN': gitlabToken}};
2726

2827
debug('repoId: %o', repoId);
@@ -57,7 +56,7 @@ module.exports = async (pluginConfig, context) => {
5756
// Uploaded assets to the project
5857
const form = new FormData();
5958
form.append('file', createReadStream(file));
60-
const {body} = await got.post(urlJoin(apiUrl, '/uploads'), {
59+
const {body} = await got.post(urlJoin(gitlabApiUrl, `/projects/${encodedRepoId}/uploads`), {
6160
...apiOptions,
6261
json: false,
6362
body: form,
@@ -72,7 +71,7 @@ module.exports = async (pluginConfig, context) => {
7271
}
7372

7473
debug('Update git tag %o with commit %o and release description', gitTag, gitHead);
75-
await got.post(urlJoin(apiUrl, `/repository/tags/${encodedGitTag}/release`), {
74+
await got.post(urlJoin(gitlabApiUrl, `/projects/${encodedRepoId}/repository/tags/${encodedGitTag}/release`), {
7675
...apiOptions,
7776
body: {tag_name: gitTag, description: notes}, // eslint-disable-line camelcase
7877
});
@@ -81,7 +80,7 @@ module.exports = async (pluginConfig, context) => {
8180
await Promise.all(
8281
assetsList.map(({label, alt, url}) => {
8382
debug('Add link to asset %o', label || alt);
84-
return got.post(urlJoin(apiUrl, `/releases/${encodedGitTag}/assets/links`), {
83+
return got.post(urlJoin(gitlabApiUrl, `/projects/${encodedRepoId}/releases/${encodedGitTag}/assets/links`), {
8584
...apiOptions,
8685
body: {name: label || alt, url: urlJoin(gitlabUrl, repoId, url)},
8786
});

lib/resolve-config.js

Lines changed: 43 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,44 @@
1-
const {castArray} = require('lodash');
1+
const {castArray, isNil} = require('lodash');
2+
const urlJoin = require('url-join');
23

3-
module.exports = ({gitlabUrl, gitlabApiPathPrefix, assets}, {env}) => ({
4-
gitlabToken: env.GL_TOKEN || env.GITLAB_TOKEN,
5-
gitlabUrl: gitlabUrl || env.GL_URL || env.GITLAB_URL || 'https://gitlab.com',
6-
gitlabApiPathPrefix:
7-
typeof gitlabApiPathPrefix === 'string'
8-
? gitlabApiPathPrefix
9-
: null || typeof env.GL_PREFIX === 'string'
10-
? env.GL_PREFIX
11-
: null || typeof env.GITLAB_PREFIX === 'string'
12-
? env.GITLAB_PREFIX
13-
: null || '/api/v4',
14-
assets: assets ? castArray(assets) : assets,
15-
});
4+
module.exports = (
5+
{gitlabUrl, gitlabApiPathPrefix, assets},
6+
{
7+
envCi: {service} = {},
8+
env: {
9+
CI_PROJECT_URL,
10+
CI_PROJECT_PATH,
11+
CI_API_V4_URL,
12+
GL_TOKEN,
13+
GITLAB_TOKEN,
14+
GL_URL,
15+
GITLAB_URL,
16+
GL_PREFIX,
17+
GITLAB_PREFIX,
18+
},
19+
}
20+
) => {
21+
const userGitlabApiPathPrefix = isNil(gitlabApiPathPrefix)
22+
? isNil(GL_PREFIX)
23+
? GITLAB_PREFIX
24+
: GL_PREFIX
25+
: gitlabApiPathPrefix;
26+
const userGitlabUrl = gitlabUrl || GL_URL || GITLAB_URL;
27+
const defaultedGitlabToken =
28+
userGitlabUrl ||
29+
(service === 'gitlab' && CI_PROJECT_URL && CI_PROJECT_PATH
30+
? CI_PROJECT_URL.replace(new RegExp(`/${CI_PROJECT_PATH}$`), '')
31+
: 'https://gitlab.com');
32+
33+
return {
34+
gitlabToken: GL_TOKEN || GITLAB_TOKEN,
35+
gitlabUrl: defaultedGitlabToken,
36+
gitlabApiUrl:
37+
userGitlabUrl && userGitlabApiPathPrefix
38+
? urlJoin(userGitlabUrl, userGitlabApiPathPrefix)
39+
: service === 'gitlab' && CI_API_V4_URL
40+
? CI_API_V4_URL
41+
: urlJoin(defaultedGitlabToken, isNil(userGitlabApiPathPrefix) ? '/api/v4' : userGitlabApiPathPrefix),
42+
assets: assets ? castArray(assets) : assets,
43+
};
44+
};

lib/verify.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@ module.exports = async (pluginConfig, context) => {
2323
logger,
2424
} = context;
2525
const errors = [];
26-
const {gitlabToken, gitlabUrl, gitlabApiPathPrefix, assets} = resolveConfig(pluginConfig, context);
27-
const repoId = getRepoId(gitlabUrl, repositoryUrl);
26+
const {gitlabToken, gitlabUrl, gitlabApiUrl, assets} = resolveConfig(pluginConfig, context);
27+
const repoId = getRepoId(context, gitlabUrl, repositoryUrl);
28+
debug('apiUrl: %o', gitlabApiUrl);
2829
debug('repoId: %o', repoId);
2930

3031
if (!repoId) {
@@ -43,17 +44,14 @@ module.exports = async (pluginConfig, context) => {
4344
let projectAccess;
4445
let groupAccess;
4546

46-
const apiUrl = urlJoin(gitlabUrl, gitlabApiPathPrefix);
47-
debug('apiUrl: %o', apiUrl);
48-
49-
logger.log('Verify GitLab authentication (%s)', apiUrl);
47+
logger.log('Verify GitLab authentication (%s)', gitlabApiUrl);
5048

5149
try {
5250
({
5351
body: {
5452
permissions: {project_access: projectAccess, group_access: groupAccess},
5553
},
56-
} = await got.get(urlJoin(apiUrl, `/projects/${encodeURIComponent(repoId)}`), {
54+
} = await got.get(urlJoin(gitlabApiUrl, `/projects/${encodeURIComponent(repoId)}`), {
5755
json: true,
5856
headers: {'Private-Token': gitlabToken},
5957
}));

test/get-repo-id.test.js

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,57 @@ import test from 'ava';
22
import getRepoId from '../lib/get-repo-id';
33

44
test('Parse repo id with https URL', t => {
5-
t.is(getRepoId('https://gitlbab.com', 'https://gitlab.com/owner/repo.git'), 'owner/repo');
6-
t.is(getRepoId('https://gitlbab.com', 'https://gitlab.com/owner/repo'), 'owner/repo');
5+
t.is(getRepoId({env: {}}, 'https://gitlbab.com', 'https://gitlab.com/owner/repo.git'), 'owner/repo');
6+
t.is(getRepoId({env: {}}, 'https://gitlbab.com', 'https://gitlab.com/owner/repo'), 'owner/repo');
77
});
88

99
test('Parse repo id with git URL', t => {
10-
t.is(getRepoId('https://gitlab.com', 'git+ssh://[email protected]/owner/repo.git'), 'owner/repo');
11-
t.is(getRepoId('https://gitlab.com', 'git+ssh://[email protected]/owner/repo'), 'owner/repo');
10+
t.is(getRepoId({env: {}}, 'https://gitlab.com', 'git+ssh://[email protected]/owner/repo.git'), 'owner/repo');
11+
t.is(getRepoId({env: {}}, 'https://gitlab.com', 'git+ssh://[email protected]/owner/repo'), 'owner/repo');
1212
});
1313

1414
test('Parse repo id with context in repo URL', t => {
15-
t.is(getRepoId('https://gitlbab.com/context', 'https://gitlab.com/context/owner/repo.git'), 'owner/repo');
16-
t.is(getRepoId('https://gitlab.com/context', 'git+ssh://[email protected]/context/owner/repo.git'), 'owner/repo');
15+
t.is(getRepoId({env: {}}, 'https://gitlbab.com/context', 'https://gitlab.com/context/owner/repo.git'), 'owner/repo');
16+
t.is(
17+
getRepoId({env: {}}, 'https://gitlab.com/context', 'git+ssh://[email protected]/context/owner/repo.git'),
18+
'owner/repo'
19+
);
1720
});
1821

1922
test('Parse repo id with context not in repo URL', t => {
20-
t.is(getRepoId('https://gitlbab.com/context', 'https://gitlab.com/owner/repo.git'), 'owner/repo');
21-
t.is(getRepoId('https://gitlab.com/context', 'git+ssh://[email protected]/owner/repo.git'), 'owner/repo');
23+
t.is(getRepoId({env: {}}, 'https://gitlbab.com/context', 'https://gitlab.com/owner/repo.git'), 'owner/repo');
24+
t.is(getRepoId({env: {}}, 'https://gitlab.com/context', 'git+ssh://[email protected]/owner/repo.git'), 'owner/repo');
2225
});
2326

2427
test('Parse repo id with organization and subgroup', t => {
2528
t.is(
26-
getRepoId('https://gitlbab.com/context', 'https://gitlab.com/orga/subgroup/owner/repo.git'),
29+
getRepoId({env: {}}, 'https://gitlbab.com/context', 'https://gitlab.com/orga/subgroup/owner/repo.git'),
2730
'orga/subgroup/owner/repo'
2831
);
2932
t.is(
30-
getRepoId('https://gitlab.com/context', 'git+ssh://[email protected]/orga/subgroup/owner/repo.git'),
33+
getRepoId({env: {}}, 'https://gitlab.com/context', 'git+ssh://[email protected]/orga/subgroup/owner/repo.git'),
3134
'orga/subgroup/owner/repo'
3235
);
3336
});
37+
38+
test('Get repo id from GitLab CI', t => {
39+
t.is(
40+
getRepoId(
41+
{envCi: {service: 'gitlab'}, env: {CI_PROJECT_PATH: 'other-owner/other-repo'}},
42+
'https://gitlbab.com',
43+
'https://gitlab.com/owner/repo.git'
44+
),
45+
'other-owner/other-repo'
46+
);
47+
});
48+
49+
test('Ignore CI_PROJECT_PATH if not on GitLab CI', t => {
50+
t.is(
51+
getRepoId(
52+
{envCi: {service: 'travis'}, env: {CI_PROJECT_PATH: 'other-owner/other-repo'}},
53+
'https://gitlbab.com',
54+
'https://gitlab.com/owner/repo.git'
55+
),
56+
'owner/repo'
57+
);
58+
});

0 commit comments

Comments
 (0)