Skip to content

Commit f2b61d7

Browse files
authored
feat: support gitlab milestones in config (#237)
* feat: support gitlab milestones in config * chore(docs): add milestone option to readme
1 parent 0a2d64c commit f2b61d7

File tree

5 files changed

+87
-14
lines changed

5 files changed

+87
-14
lines changed

README.md

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

5959
### Options
6060

61-
| Option | Description | Default |
62-
|-----------------------|--------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
63-
| `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`. |
64-
| `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`. |
65-
| `assets` | An array of files to upload to the release. See [assets](#assets). | - |
61+
| Option | Description | Default |
62+
|-----------------------|------------------------------------------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
63+
| `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`. |
64+
| `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`. |
65+
| `assets` | An array of files to upload to the release. See [assets](#assets). | - |
66+
| `milestones` | An array of milestone titles to associate to the release. See [GitLab Release API](https://docs.gitlab.com/ee/api/releases/#create-a-release). | - |
6667

6768
#### assets
6869

lib/publish.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ module.exports = async (pluginConfig, context) => {
1717
nextRelease: {gitTag, gitHead, notes},
1818
logger,
1919
} = context;
20-
const {gitlabToken, gitlabUrl, gitlabApiUrl, assets} = resolveConfig(pluginConfig, context);
20+
const {gitlabToken, gitlabUrl, gitlabApiUrl, assets, milestones} = resolveConfig(pluginConfig, context);
2121
const assetsList = [];
2222
const repoId = getRepoId(context, gitlabUrl, repositoryUrl);
2323
const encodedRepoId = encodeURIComponent(repoId);
@@ -27,6 +27,7 @@ module.exports = async (pluginConfig, context) => {
2727
debug('repoId: %o', repoId);
2828
debug('release name: %o', gitTag);
2929
debug('release ref: %o', gitHead);
30+
debug('milestones: %o', milestones);
3031

3132
if (assets && assets.length > 0) {
3233
const globbedAssets = await getAssets(context, assets);
@@ -74,6 +75,7 @@ module.exports = async (pluginConfig, context) => {
7475
/* eslint-disable camelcase */
7576
tag_name: gitTag,
7677
description: notes && notes.trim() ? notes : gitTag,
78+
milestones,
7779
assets: {
7880
links: assetsList.map(({label, alt, url}) => {
7981
return {

lib/resolve-config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const {castArray, isNil} = require('lodash');
22
const urlJoin = require('url-join');
33

44
module.exports = (
5-
{gitlabUrl, gitlabApiPathPrefix, assets},
5+
{gitlabUrl, gitlabApiPathPrefix, assets, milestones},
66
{
77
envCi: {service} = {},
88
env: {
@@ -40,5 +40,6 @@ module.exports = (
4040
? CI_API_V4_URL
4141
: urlJoin(defaultedGitlabUrl, isNil(userGitlabApiPathPrefix) ? '/api/v4' : userGitlabApiPathPrefix),
4242
assets: assets ? castArray(assets) : assets,
43+
milestones: milestones ? castArray(milestones) : milestones,
4344
};
4445
};

test/publish.test.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,33 @@ test.serial('Publish a release with assets', async t => {
8383
t.true(gitlab.isDone());
8484
});
8585

86+
test.serial('Publish a release with a milestone', async t => {
87+
const owner = 'test_user';
88+
const repo = 'test_repo';
89+
const env = {GITLAB_TOKEN: 'gitlab_token'};
90+
const pluginConfig = {milestones: ['1.2.3']};
91+
const nextRelease = {gitHead: '123', gitTag: 'v1.0.0', notes: 'Test release note body'};
92+
const options = {repositoryUrl: `https://gitlab.com/${owner}/${repo}.git`};
93+
const encodedRepoId = encodeURIComponent(`${owner}/${repo}`);
94+
const encodedGitTag = encodeURIComponent(nextRelease.gitTag);
95+
const gitlab = authenticate(env)
96+
.post(`/projects/${encodedRepoId}/releases`, {
97+
tag_name: nextRelease.gitTag,
98+
description: nextRelease.notes,
99+
assets: {
100+
links: [],
101+
},
102+
milestones: ['1.2.3'],
103+
})
104+
.reply(200);
105+
106+
const result = await publish(pluginConfig, {env, options, nextRelease, logger: t.context.logger});
107+
108+
t.is(result.url, `https://gitlab.com/${encodedRepoId}/-/releases/${encodedGitTag}`);
109+
t.deepEqual(t.context.log.args[0], ['Published GitLab release: %s', nextRelease.gitTag]);
110+
t.true(gitlab.isDone());
111+
});
112+
86113
test.serial('Publish a release with array of missing assets', async t => {
87114
const cwd = 'test/fixtures/files';
88115
const owner = 'test_user';

test/resolve-config.test.js

Lines changed: 49 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ test('Returns user config', t => {
1313
gitlabUrl,
1414
gitlabApiUrl: urlJoin(gitlabUrl, gitlabApiPathPrefix),
1515
assets,
16+
milestones: undefined,
1617
});
1718
});
1819

@@ -21,13 +22,20 @@ test('Returns user config via environment variables', t => {
2122
const gitlabUrl = 'https://host.com';
2223
const gitlabApiPathPrefix = '/api/prefix';
2324
const assets = ['file.js'];
25+
const milestones = ['1.2.3'];
2426

2527
t.deepEqual(
2628
resolveConfig(
27-
{assets},
29+
{assets, milestones},
2830
{env: {GITLAB_TOKEN: gitlabToken, GITLAB_URL: gitlabUrl, GITLAB_PREFIX: gitlabApiPathPrefix}}
2931
),
30-
{gitlabToken, gitlabUrl, gitlabApiUrl: urlJoin(gitlabUrl, gitlabApiPathPrefix), assets}
32+
{
33+
gitlabToken,
34+
gitlabUrl,
35+
gitlabApiUrl: urlJoin(gitlabUrl, gitlabApiPathPrefix),
36+
assets,
37+
milestones,
38+
}
3139
);
3240
});
3341

@@ -39,7 +47,13 @@ test('Returns user config via alternative environment variables', t => {
3947

4048
t.deepEqual(
4149
resolveConfig({assets}, {env: {GL_TOKEN: gitlabToken, GL_URL: gitlabUrl, GL_PREFIX: gitlabApiPathPrefix}}),
42-
{gitlabToken, gitlabUrl, gitlabApiUrl: urlJoin(gitlabUrl, gitlabApiPathPrefix), assets}
50+
{
51+
gitlabToken,
52+
gitlabUrl,
53+
gitlabApiUrl: urlJoin(gitlabUrl, gitlabApiPathPrefix),
54+
assets,
55+
milestones: undefined,
56+
}
4357
);
4458
});
4559

@@ -53,20 +67,23 @@ test('Returns default config', t => {
5367
gitlabUrl: 'https://gitlab.com',
5468
gitlabApiUrl: urlJoin('https://gitlab.com', '/api/v4'),
5569
assets: undefined,
70+
milestones: undefined,
5671
});
5772

5873
t.deepEqual(resolveConfig({gitlabApiPathPrefix}, {env: {GL_TOKEN: gitlabToken}}), {
5974
gitlabToken,
6075
gitlabUrl: 'https://gitlab.com',
6176
gitlabApiUrl: urlJoin('https://gitlab.com', gitlabApiPathPrefix),
6277
assets: undefined,
78+
milestones: undefined,
6379
});
6480

6581
t.deepEqual(resolveConfig({gitlabUrl}, {env: {GL_TOKEN: gitlabToken}}), {
6682
gitlabToken,
6783
gitlabUrl: 'https://gitlab.com',
6884
gitlabApiUrl: urlJoin(gitlabUrl, '/api/v4'),
6985
assets: undefined,
86+
milestones: undefined,
7087
});
7188
});
7289

@@ -84,7 +101,13 @@ test('Returns default config via GitLab CI/CD environment variables', t => {
84101
env: {GL_TOKEN: gitlabToken, CI_PROJECT_URL, CI_PROJECT_PATH, CI_API_V4_URL},
85102
}
86103
),
87-
{gitlabToken, gitlabUrl: 'http://ci-host.com', gitlabApiUrl: CI_API_V4_URL, assets: undefined}
104+
{
105+
gitlabToken,
106+
gitlabUrl: 'http://ci-host.com',
107+
gitlabApiUrl: CI_API_V4_URL,
108+
assets: undefined,
109+
milestones: undefined,
110+
}
88111
);
89112
});
90113

@@ -105,7 +128,13 @@ test('Returns user config over GitLab CI/CD environment variables', t => {
105128
env: {GL_TOKEN: gitlabToken, CI_PROJECT_URL, CI_PROJECT_PATH, CI_API_V4_URL},
106129
}
107130
),
108-
{gitlabToken, gitlabUrl, gitlabApiUrl: urlJoin(gitlabUrl, gitlabApiPathPrefix), assets}
131+
{
132+
gitlabToken,
133+
gitlabUrl,
134+
gitlabApiUrl: urlJoin(gitlabUrl, gitlabApiPathPrefix),
135+
assets,
136+
milestones: undefined,
137+
}
109138
);
110139
});
111140

@@ -132,7 +161,13 @@ test('Returns user config via environment variables over GitLab CI/CD environmen
132161
},
133162
}
134163
),
135-
{gitlabToken, gitlabUrl, gitlabApiUrl: urlJoin(gitlabUrl, gitlabApiPathPrefix), assets: undefined}
164+
{
165+
gitlabToken,
166+
gitlabUrl,
167+
gitlabApiUrl: urlJoin(gitlabUrl, gitlabApiPathPrefix),
168+
assets: undefined,
169+
milestones: undefined,
170+
}
136171
);
137172
});
138173

@@ -159,7 +194,13 @@ test('Returns user config via alternative environment variables over GitLab CI/C
159194
},
160195
}
161196
),
162-
{gitlabToken, gitlabUrl, gitlabApiUrl: urlJoin(gitlabUrl, gitlabApiPathPrefix), assets: undefined}
197+
{
198+
gitlabToken,
199+
gitlabUrl,
200+
gitlabApiUrl: urlJoin(gitlabUrl, gitlabApiPathPrefix),
201+
assets: undefined,
202+
milestones: undefined,
203+
}
163204
);
164205
});
165206

@@ -182,6 +223,7 @@ test('Ignore GitLab CI/CD environment variables if not running on GitLab CI/CD',
182223
gitlabUrl: 'https://gitlab.com',
183224
gitlabApiUrl: urlJoin('https://gitlab.com', '/api/v4'),
184225
assets: undefined,
226+
milestones: undefined,
185227
}
186228
);
187229
});

0 commit comments

Comments
 (0)