Skip to content

Commit 642ca03

Browse files
committed
add unit tests, update readme
1 parent 91c6213 commit 642ca03

File tree

3 files changed

+105
-9
lines changed

3 files changed

+105
-9
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 [release assets](https://docs.gitlab.com/ee/user/project/releases/#release-assets)). 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/ee/user/project/releases/#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/ee/api/projects.html#upload-a-file) or `generic_package` for storing the file as [generic package](https://docs.gitlab.com/ee/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/ee/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 [release assets](https://docs.gitlab.com/ee/user/project/releases/#release-assets)). 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/ee/user/project/releases/#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/ee/api/projects.html#upload-a-file) or `generic_package` for storing the file as [generic package](https://docs.gitlab.com/ee/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/ee/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: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ 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);
107+
106108

107109
let uploadEndpoint;
108110
let response;

test/publish.test.js

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,99 @@ 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 = { path: "file.css", label: "Style package", target: "generic_package", status: "hidden", packageName: "microk8s" };
191+
const assets = [generic];
192+
const encodedLabel = encodeURIComponent(generic.label);
193+
const encodedPackageName = encodeURIComponent(generic.packageName);
194+
const expectedUrl = `https://gitlab.com/api/v4/projects/${encodedProjectPath}/packages/generic/${encodedPackageName}/${encodedVersion}/${encodedLabel}`;
195+
const gitlab = authenticate(env)
196+
.post(`/projects/${encodedProjectPath}/releases`, {
197+
tag_name: nextRelease.gitTag,
198+
description: nextRelease.notes,
199+
assets: {
200+
links: [
201+
{
202+
name: "Style package",
203+
url: expectedUrl,
204+
link_type: "package",
205+
},
206+
],
207+
},
208+
})
209+
.reply(200);
210+
const gitlabUpload = authenticate(env)
211+
.put(
212+
`/projects/${encodedProjectPath}/packages/generic/${encodedPackageName}/${encodedVersion}/${encodedLabel}?status=${generic.status}&select=package_file`,
213+
/\.test\s\{\}/gm
214+
)
215+
.reply(200, uploaded);
216+
217+
const result = await publish({ assets }, { env, cwd, options, nextRelease, logger: t.context.logger });
218+
219+
t.is(result.url, `https://gitlab.com/${owner}/${repo}/-/releases/${encodedGitTag}`);
220+
t.deepEqual(t.context.log.args[0], ["Uploaded file: %s (%s)", expectedUrl, uploaded.file.url]);
221+
t.deepEqual(t.context.log.args[1], ["Published GitLab release: %s", nextRelease.gitTag]);
222+
t.true(gitlabUpload.isDone());
223+
t.true(gitlab.isDone());
224+
});
225+
226+
test.serial("Publish a release with generics: with asset.packageName (template)", async (t) => {
227+
const cwd = "test/fixtures/files";
228+
const owner = "test_user";
229+
const repo = "test_repo";
230+
const env = { GITLAB_TOKEN: "gitlab_token" };
231+
const nextRelease = { gitHead: "123", gitTag: "v1.0.0-alpha.1", notes: "Test release note body", version: "1.0.0-alpha.1", channel: "alpha"};
232+
const options = { repositoryUrl: `https://gitlab.com/${owner}/${repo}.git` };
233+
const encodedProjectPath = encodeURIComponent(`${owner}/${repo}`);
234+
const encodedGitTag = encodeURIComponent(nextRelease.gitTag);
235+
const encodedVersion = encodeURIComponent(nextRelease.version);
236+
const uploaded = { file: { url: "/uploads/file.css" } };
237+
const generic = { path: "file.css", label: "Style package", target: "generic_package", status: "hidden", packageName: "${nextRelease.channel}" };
238+
const assets = [generic];
239+
const encodedLabel = encodeURIComponent(generic.label);
240+
const expectedUrl = `https://gitlab.com/api/v4/projects/${encodedProjectPath}/packages/generic/alpha/${encodedVersion}/${encodedLabel}`;
241+
const gitlab = authenticate(env)
242+
.post(`/projects/${encodedProjectPath}/releases`, {
243+
tag_name: nextRelease.gitTag,
244+
description: nextRelease.notes,
245+
assets: {
246+
links: [
247+
{
248+
name: "Style package",
249+
url: expectedUrl,
250+
link_type: "package",
251+
},
252+
],
253+
},
254+
})
255+
.reply(200);
256+
const gitlabUpload = authenticate(env)
257+
.put(
258+
`/projects/${encodedProjectPath}/packages/generic/alpha/${encodedVersion}/${encodedLabel}?status=${generic.status}&select=package_file`,
259+
/\.test\s\{\}/gm
260+
)
261+
.reply(200, uploaded);
262+
263+
const result = await publish({ assets }, { env, cwd, options, nextRelease, logger: t.context.logger });
264+
265+
t.is(result.url, `https://gitlab.com/${owner}/${repo}/-/releases/${encodedGitTag}`);
266+
t.deepEqual(t.context.log.args[0], ["Uploaded file: %s (%s)", expectedUrl, uploaded.file.url]);
267+
t.deepEqual(t.context.log.args[1], ["Published GitLab release: %s", nextRelease.gitTag]);
268+
t.true(gitlabUpload.isDone());
269+
t.true(gitlab.isDone());
270+
});
271+
179272
test.serial("Publish a release with generics and external storage provider (http)", async (t) => {
180273
const cwd = "test/fixtures/files";
181274
const owner = "test_user";

0 commit comments

Comments
 (0)