Skip to content

Commit 9be4ada

Browse files
committed
fix: file labels in GitLab releases, to ensure they're unique.
Previously GitLab lables were just the basename for files uploaded as part of the release. This is problematic because GitLab doesn't allow conflicting labels -- a condition that could be caused by uploading a release with two files by the same name in different directories. This would generate a 409 Conflict error. This changes the labels for files uploaded as part of a release to the name relative to pkgRoot, or the package. A project may look like this pkg pkg \ foo \ baz pkg \ bar \ baz This would previously result in two conflicting labels of 'baz'. Now you would have {"foo/baz", "bar/baz"} with no conflict. GitHub issues: #265, #158
1 parent 2671c06 commit 9be4ada

File tree

4 files changed

+32
-22
lines changed

4 files changed

+32
-22
lines changed

lib/glob-assets.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
const path = require('path');
2-
const {basename} = require('path');
32
const {isPlainObject, castArray, uniqWith, uniq} = require('lodash');
43
const dirGlob = require('dir-glob');
54
const globby = require('globby');
65
const debug = require('debug')('semantic-release:gitlab');
76

8-
module.exports = async ({cwd}, assets) =>
9-
uniqWith(
7+
module.exports = async ({pkgRoot, cwd}, assets) => {
8+
return uniqWith(
109
[]
1110
.concat(
1211
...(await Promise.all(
@@ -42,7 +41,7 @@ module.exports = async ({cwd}, assets) =>
4241
// - `filepath` ignored (also to avoid duplicates)
4342
// - other properties of the original asset definition
4443
const {filepath, ...others} = asset;
45-
return globbed.map(file => ({...others, path: file, label: basename(file)}));
44+
return globbed.map(file => ({...others, path: file, label: path.relative(pkgRoot || '.', file)}));
4645
}
4746

4847
// If asset is an Object, output an Object definition with:
@@ -66,3 +65,4 @@ module.exports = async ({cwd}, assets) =>
6665
// Compare `path` property if Object definition, value itself if String
6766
(a, b) => path.resolve(cwd, isPlainObject(a) ? a.path : a) === path.resolve(cwd, isPlainObject(b) ? b.path : b)
6867
);
68+
};

lib/publish.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ module.exports = async (pluginConfig, context) => {
3030
debug('milestones: %o', milestones);
3131

3232
if (assets && assets.length > 0) {
33-
const globbedAssets = await getAssets(context, assets);
33+
const globbedAssets = await getAssets({cwd: context.cwd, pkgRoot: pluginConfig.pkgRoot}, assets);
3434
debug('globbed assets: %o', globbedAssets);
3535

3636
await Promise.all(

lib/resolve-config.js

+16-17
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,21 @@
11
const {castArray, isNil} = require('lodash');
22
const urlJoin = require('url-join');
33

4-
module.exports = (
5-
{gitlabUrl, gitlabApiPathPrefix, assets, milestones},
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-
) => {
4+
module.exports = (pluginConfig, context) => {
5+
const {gitlabUrl, gitlabApiPathPrefix, assets, milestones, pkgRoot} = pluginConfig;
6+
const {service} = context.envCi || {service: undefined};
7+
const {
8+
CI_PROJECT_URL,
9+
CI_PROJECT_PATH,
10+
CI_API_V4_URL,
11+
GL_TOKEN,
12+
GITLAB_TOKEN,
13+
GL_URL,
14+
GITLAB_URL,
15+
GL_PREFIX,
16+
GITLAB_PREFIX,
17+
} = context.env;
18+
2119
const userGitlabApiPathPrefix = isNil(gitlabApiPathPrefix)
2220
? isNil(GL_PREFIX)
2321
? GITLAB_PREFIX
@@ -31,6 +29,7 @@ module.exports = (
3129
: 'https://gitlab.com');
3230

3331
return {
32+
pkgRoot,
3433
gitlabToken: GL_TOKEN || GITLAB_TOKEN,
3534
gitlabUrl: defaultedGitlabUrl,
3635
gitlabApiUrl:

test/resolve-config.test.js

+11
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ test('Returns user config', t => {
1414
gitlabApiUrl: urlJoin(gitlabUrl, gitlabApiPathPrefix),
1515
assets,
1616
milestones: undefined,
17+
pkgRoot: undefined,
1718
});
1819
});
1920

@@ -35,6 +36,7 @@ test('Returns user config via environment variables', t => {
3536
gitlabApiUrl: urlJoin(gitlabUrl, gitlabApiPathPrefix),
3637
assets,
3738
milestones,
39+
pkgRoot: undefined,
3840
}
3941
);
4042
});
@@ -53,6 +55,7 @@ test('Returns user config via alternative environment variables', t => {
5355
gitlabApiUrl: urlJoin(gitlabUrl, gitlabApiPathPrefix),
5456
assets,
5557
milestones: undefined,
58+
pkgRoot: undefined,
5659
}
5760
);
5861
});
@@ -68,6 +71,7 @@ test('Returns default config', t => {
6871
gitlabApiUrl: urlJoin('https://gitlab.com', '/api/v4'),
6972
assets: undefined,
7073
milestones: undefined,
74+
pkgRoot: undefined,
7175
});
7276

7377
t.deepEqual(resolveConfig({gitlabApiPathPrefix}, {env: {GL_TOKEN: gitlabToken}}), {
@@ -76,6 +80,7 @@ test('Returns default config', t => {
7680
gitlabApiUrl: urlJoin('https://gitlab.com', gitlabApiPathPrefix),
7781
assets: undefined,
7882
milestones: undefined,
83+
pkgRoot: undefined,
7984
});
8085

8186
t.deepEqual(resolveConfig({gitlabUrl}, {env: {GL_TOKEN: gitlabToken}}), {
@@ -84,6 +89,7 @@ test('Returns default config', t => {
8489
gitlabApiUrl: urlJoin(gitlabUrl, '/api/v4'),
8590
assets: undefined,
8691
milestones: undefined,
92+
pkgRoot: undefined,
8793
});
8894
});
8995

@@ -107,6 +113,7 @@ test('Returns default config via GitLab CI/CD environment variables', t => {
107113
gitlabApiUrl: CI_API_V4_URL,
108114
assets: undefined,
109115
milestones: undefined,
116+
pkgRoot: undefined,
110117
}
111118
);
112119
});
@@ -134,6 +141,7 @@ test('Returns user config over GitLab CI/CD environment variables', t => {
134141
gitlabApiUrl: urlJoin(gitlabUrl, gitlabApiPathPrefix),
135142
assets,
136143
milestones: undefined,
144+
pkgRoot: undefined,
137145
}
138146
);
139147
});
@@ -167,6 +175,7 @@ test('Returns user config via environment variables over GitLab CI/CD environmen
167175
gitlabApiUrl: urlJoin(gitlabUrl, gitlabApiPathPrefix),
168176
assets: undefined,
169177
milestones: undefined,
178+
pkgRoot: undefined,
170179
}
171180
);
172181
});
@@ -200,6 +209,7 @@ test('Returns user config via alternative environment variables over GitLab CI/C
200209
gitlabApiUrl: urlJoin(gitlabUrl, gitlabApiPathPrefix),
201210
assets: undefined,
202211
milestones: undefined,
212+
pkgRoot: undefined,
203213
}
204214
);
205215
});
@@ -224,6 +234,7 @@ test('Ignore GitLab CI/CD environment variables if not running on GitLab CI/CD',
224234
gitlabApiUrl: urlJoin('https://gitlab.com', '/api/v4'),
225235
assets: undefined,
226236
milestones: undefined,
237+
pkgRoot: undefined,
227238
}
228239
);
229240
});

0 commit comments

Comments
 (0)