Skip to content

Commit e1bd8aa

Browse files
authored
Merge pull request #607 from phuocnb/use-channel-as-generic-package-name
2 parents 341aca0 + 8fcf5c1 commit e1bd8aa

File tree

3 files changed

+131
-13
lines changed

3 files changed

+131
-13
lines changed

README.md

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -102,15 +102,16 @@ If you need to bypass the proxy for some hosts, configure the `NO_PROXY` environ
102102
Can be a [glob](https://github.com/isaacs/node-glob#glob-primer) or and `Array` of
103103
[globs](https://github.com/isaacs/node-glob#glob-primer) and `Object`s with the following properties:
104104

105-
| Property | Description | Default |
106-
| ---------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------ |
107-
| `path` | **Required**, unless `url` is set. A [glob](https://github.com/isaacs/node-glob#glob-primer) to identify the files to upload. Supports [Lodash templating](https://lodash.com/docs#template). | - |
108-
| `url` | Alternative to setting `path` this provides the ability to add links to releases, e.g. URLs to container images. Supports [Lodash templating](https://lodash.com/docs#template). | - |
109-
| `label` | Short description of the file displayed on the GitLab release. Ignored if `path` matches more than one file. Supports [Lodash templating](https://lodash.com/docs#template). | File name extracted from the `path`. |
110-
| `type` | Asset type displayed on the GitLab release. Can be `runbook`, `package`, `image` and `other` (see official documents on [link types](https://docs.gitlab.com/user/project/releases/release_fields/#link-types)). Supports [Lodash templating](https://lodash.com/docs#template). | `other` |
111-
| `filepath` | A filepath for creating a permalink pointing to the asset (requires GitLab 12.9+, see official documents on [permanent links](https://docs.gitlab.com/user/project/releases/release_fields/#permanent-links-to-release-assets)). Ignored if `path` matches more than one file. Supports [Lodash templating](https://lodash.com/docs#template). | - |
112-
| `target` | Controls where the file is uploaded to. Can be set to `project_upload` for storing the file as [project upload](https://docs.gitlab.com/api/project_markdown_uploads/#upload-a-file) or `generic_package` for storing the file as [generic package](https://docs.gitlab.com/user/packages/generic_packages/). | `project_upload` |
113-
| `status` | This is only applied, if `target` is set to `generic_package`. The generic package status. Can be `default` and `hidden` (see official documents on [generic packages](https://docs.gitlab.com/user/packages/generic_packages/)). | `default` |
105+
| Property | Description | Default |
106+
| ------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------ |
107+
| `path` | **Required**, unless `url` is set. A [glob](https://github.com/isaacs/node-glob#glob-primer) to identify the files to upload. Supports [Lodash templating](https://lodash.com/docs#template). | - |
108+
| `url` | Alternative to setting `path` this provides the ability to add links to releases, e.g. URLs to container images. Supports [Lodash templating](https://lodash.com/docs#template). | - |
109+
| `label` | Short description of the file displayed on the GitLab release. Ignored if `path` matches more than one file. Supports [Lodash templating](https://lodash.com/docs#template). | File name extracted from the `path`. |
110+
| `type` | Asset type displayed on the GitLab release. Can be `runbook`, `package`, `image` and `other` (see official documents on [link types](https://docs.gitlab.com/user/project/releases/release_fields/#link-types)). Supports [Lodash templating](https://lodash.com/docs#template). | `other` |
111+
| `filepath` | A filepath for creating a permalink pointing to the asset (requires GitLab 12.9+, see official documents on [permanent links](https://docs.gitlab.com/user/project/releases/release_fields/#permanent-links-to-release-assets)). Ignored if `path` matches more than one file. Supports [Lodash templating](https://lodash.com/docs#template). | - |
112+
| `target` | Controls where the file is uploaded to. Can be set to `project_upload` for storing the file as [project upload](https://docs.gitlab.com/api/project_markdown_uploads/#upload-a-file) or `generic_package` for storing the file as [generic package](https://docs.gitlab.com/user/packages/generic_packages/). | `project_upload` |
113+
| `packageName` | This is only applied if `target` is set to `generic_package`. It defines the package name (`:package_name`) to upload asset file to. More information could be found at [Publish a package](https://docs.gitlab.com/user/packages/generic_packages/#publish-a-package) | `release` |
114+
| `status` | This is only applied, if `target` is set to `generic_package`. The generic package status. Can be `default` and `hidden` (see official documents on [generic packages](https://docs.gitlab.com/user/packages/generic_packages/)). | `default` |
114115

115116
Each entry in the `assets` `Array` is globbed individually. A [glob](https://github.com/isaacs/node-glob#glob-primer)
116117
can be a `String` (`"dist/**/*.js"` or `"dist/mylib.js"`) or an `Array` of `String`s that will be globbed together

lib/publish.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ export default async (pluginConfig, context) => {
2828
const { projectPath, projectApiUrl } = getProjectContext(context, gitlabUrl, gitlabApiUrl, repositoryUrl);
2929

3030
const encodedGitTag = encodeURIComponent(gitTag);
31-
const encodedVersion = encodeURIComponent(version);
3231
const apiOptions = {
3332
headers: {
3433
"PRIVATE-TOKEN": gitlabToken,
@@ -76,6 +75,7 @@ export default async (pluginConfig, context) => {
7675
const filepath = asset.filepath ? template(asset.filepath)(context) : undefined;
7776
const target = asset.target ? template(asset.target)(context) : undefined;
7877
const status = asset.status ? template(asset.status)(context) : undefined;
78+
const packageName = asset.packageName ? template(asset.packageName)(context) : "release";
7979

8080
if (_url) {
8181
assetsList.push({ label, rawUrl: _url, type, filepath });
@@ -103,18 +103,21 @@ export default async (pluginConfig, context) => {
103103
debug("file filepath: %o", filepath);
104104
debug("file target: %o", target);
105105
debug("file status: %o", status);
106+
debug("package name: %o", packageName);
106107

107108
let uploadEndpoint;
108109
let response;
109110

110111
if (target === "generic_package") {
111112
const finalLabel = label ?? pathlib.basename(file);
112113
// Upload generic packages
114+
const encodedVersion = encodeURIComponent(version);
115+
const encodedPackageName = encodeURIComponent(packageName);
113116
const encodedLabel = encodeURIComponent(finalLabel);
114117
// https://docs.gitlab.com/ee/user/packages/generic_packages/#publish-a-package-file
115118
uploadEndpoint = urlJoin(
116119
projectApiUrl,
117-
`packages/generic/release/${encodedVersion}/${encodedLabel}?${
120+
`packages/generic/${encodedPackageName}/${encodedVersion}/${encodedLabel}?${
118121
status ? `status=${status}&` : ""
119122
}select=package_file`
120123
);
@@ -129,9 +132,12 @@ export default async (pluginConfig, context) => {
129132
}
130133

131134
// https://docs.gitlab.com/ee/user/packages/generic_packages/#download-package-file
132-
const url = urlJoin(projectApiUrl, `packages/generic/release/${encodedVersion}/${encodedLabel}`);
135+
const url = urlJoin(
136+
projectApiUrl,
137+
`packages/generic/${encodedPackageName}/${encodedVersion}/${encodedLabel}`
138+
);
133139

134-
assetsList.push({ label: finalLabel, alt: "release", url, type: "package", filepath });
140+
assetsList.push({ label: finalLabel, alt: packageName, url, type: "package", filepath });
135141

136142
logger.log("Uploaded file: %s (%s)", url, response.file.url);
137143
} else {

test/publish.test.js

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,117 @@ test.serial("Publish a release with generics", async (t) => {
176176
t.true(gitlab.isDone());
177177
});
178178

179+
test.serial("Publish a release with generics: with asset.packageName (fixed text)", async (t) => {
180+
const cwd = "test/fixtures/files";
181+
const owner = "test_user";
182+
const repo = "test_repo";
183+
const env = { GITLAB_TOKEN: "gitlab_token" };
184+
const nextRelease = { gitHead: "123", gitTag: "v1.0.0", notes: "Test release note body", version: "1.0.0" };
185+
const options = { repositoryUrl: `https://gitlab.com/${owner}/${repo}.git` };
186+
const encodedProjectPath = encodeURIComponent(`${owner}/${repo}`);
187+
const encodedGitTag = encodeURIComponent(nextRelease.gitTag);
188+
const encodedVersion = encodeURIComponent(nextRelease.version);
189+
const uploaded = { file: { url: "/uploads/file.css" } };
190+
const generic = {
191+
path: "file.css",
192+
label: "Style package",
193+
target: "generic_package",
194+
status: "hidden",
195+
packageName: "microk8s",
196+
};
197+
const assets = [generic];
198+
const encodedLabel = encodeURIComponent(generic.label);
199+
const encodedPackageName = encodeURIComponent(generic.packageName);
200+
const expectedUrl = `https://gitlab.com/api/v4/projects/${encodedProjectPath}/packages/generic/${encodedPackageName}/${encodedVersion}/${encodedLabel}`;
201+
const gitlab = authenticate(env)
202+
.post(`/projects/${encodedProjectPath}/releases`, {
203+
tag_name: nextRelease.gitTag,
204+
description: nextRelease.notes,
205+
assets: {
206+
links: [
207+
{
208+
name: "Style package",
209+
url: expectedUrl,
210+
link_type: "package",
211+
},
212+
],
213+
},
214+
})
215+
.reply(200);
216+
const gitlabUpload = authenticate(env)
217+
.put(
218+
`/projects/${encodedProjectPath}/packages/generic/${encodedPackageName}/${encodedVersion}/${encodedLabel}?status=${generic.status}&select=package_file`,
219+
/\.test\s\{\}/gm
220+
)
221+
.reply(200, uploaded);
222+
223+
const result = await publish({ assets }, { env, cwd, options, nextRelease, logger: t.context.logger });
224+
225+
t.is(result.url, `https://gitlab.com/${owner}/${repo}/-/releases/${encodedGitTag}`);
226+
t.deepEqual(t.context.log.args[0], ["Uploaded file: %s (%s)", expectedUrl, uploaded.file.url]);
227+
t.deepEqual(t.context.log.args[1], ["Published GitLab release: %s", nextRelease.gitTag]);
228+
t.true(gitlabUpload.isDone());
229+
t.true(gitlab.isDone());
230+
});
231+
232+
test.serial("Publish a release with generics: with asset.packageName (template)", async (t) => {
233+
const cwd = "test/fixtures/files";
234+
const owner = "test_user";
235+
const repo = "test_repo";
236+
const env = { GITLAB_TOKEN: "gitlab_token" };
237+
const nextRelease = {
238+
gitHead: "123",
239+
gitTag: "v1.0.0-alpha.1",
240+
notes: "Test release note body",
241+
version: "1.0.0-alpha.1",
242+
channel: "alpha",
243+
};
244+
const options = { repositoryUrl: `https://gitlab.com/${owner}/${repo}.git` };
245+
const encodedProjectPath = encodeURIComponent(`${owner}/${repo}`);
246+
const encodedGitTag = encodeURIComponent(nextRelease.gitTag);
247+
const encodedVersion = encodeURIComponent(nextRelease.version);
248+
const uploaded = { file: { url: "/uploads/file.css" } };
249+
const generic = {
250+
path: "file.css",
251+
label: "Style package",
252+
target: "generic_package",
253+
status: "hidden",
254+
packageName: "${nextRelease.channel}",
255+
};
256+
const assets = [generic];
257+
const encodedLabel = encodeURIComponent(generic.label);
258+
const expectedUrl = `https://gitlab.com/api/v4/projects/${encodedProjectPath}/packages/generic/alpha/${encodedVersion}/${encodedLabel}`;
259+
const gitlab = authenticate(env)
260+
.post(`/projects/${encodedProjectPath}/releases`, {
261+
tag_name: nextRelease.gitTag,
262+
description: nextRelease.notes,
263+
assets: {
264+
links: [
265+
{
266+
name: "Style package",
267+
url: expectedUrl,
268+
link_type: "package",
269+
},
270+
],
271+
},
272+
})
273+
.reply(200);
274+
const gitlabUpload = authenticate(env)
275+
.put(
276+
`/projects/${encodedProjectPath}/packages/generic/alpha/${encodedVersion}/${encodedLabel}?status=${generic.status}&select=package_file`,
277+
/\.test\s\{\}/gm
278+
)
279+
.reply(200, uploaded);
280+
281+
const result = await publish({ assets }, { env, cwd, options, nextRelease, logger: t.context.logger });
282+
283+
t.is(result.url, `https://gitlab.com/${owner}/${repo}/-/releases/${encodedGitTag}`);
284+
t.deepEqual(t.context.log.args[0], ["Uploaded file: %s (%s)", expectedUrl, uploaded.file.url]);
285+
t.deepEqual(t.context.log.args[1], ["Published GitLab release: %s", nextRelease.gitTag]);
286+
t.true(gitlabUpload.isDone());
287+
t.true(gitlab.isDone());
288+
});
289+
179290
test.serial("Publish a release with generics (without label - issue #823)", async (t) => {
180291
const cwd = "test/fixtures/files";
181292
const owner = "test_user";

0 commit comments

Comments
 (0)