Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support asset type and filepath #242

Merged
merged 4 commits into from
Jun 17, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ Can be a [glob](https://github.com/isaacs/node-glob#glob-primer) or and `Array`
| -------- | ----------------------------------------------------------------------------------------------------------- | ------------------------------------ |
| `path` | **Required.** A [glob](https://github.com/isaacs/node-glob#glob-primer) to identify the files to upload. | - |
| `label` | Short description of the file displayed on the GitLab release. Ignored if `path` matches more than one file.| File name extracted from the `path`. |
| `type` | Asset type displayed on the GitLab release. Can be `runbook`, `package`, `image` and `other` (see official documents on [release assets](https://git.bdaa.pro/help/user/project/releases/index#release-assets)). | `other` |
nfriend marked this conversation as resolved.
Show resolved Hide resolved
| `filepath` | A filepath for creating a permalink pointing to the asset (requires GitLab 12.9+, see official documents on [permanent links](https://git.bdaa.pro/help/user/project/releases/index#permanent-links-to-release-assets)). | - |
nfriend marked this conversation as resolved.
Show resolved Hide resolved

Each entry in the `assets` `Array` is globbed individually. A [glob](https://github.com/isaacs/node-glob#glob-primer)
can be a `String` (`"dist/**/*.js"` or `"dist/mylib.js"`) or an `Array` of `String`s that will be globbed together
Expand Down
8 changes: 5 additions & 3 deletions lib/publish.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ module.exports = async (pluginConfig, context) => {

await Promise.all(
globbedAssets.map(async asset => {
const {path, label} = isPlainObject(asset) ? asset : {path: asset};
const {path, label, type, filepath} = isPlainObject(asset) ? asset : {path: asset};
const file = resolve(cwd, path);
let fileStat;

Expand All @@ -61,7 +61,7 @@ module.exports = async (pluginConfig, context) => {
.post(urlJoin(gitlabApiUrl, `/projects/${encodedRepoId}/uploads`), {...apiOptions, body: form})
.json();

assetsList.push({label, alt, url});
assetsList.push({label, alt, url, type, filepath});

logger.log('Uploaded file: %s', url);
})
Expand All @@ -77,10 +77,12 @@ module.exports = async (pluginConfig, context) => {
description: notes && notes.trim() ? notes : gitTag,
milestones,
assets: {
links: assetsList.map(({label, alt, url}) => {
links: assetsList.map(({label, alt, url, type, filepath}) => {
return {
name: label || alt,
url: urlJoin(gitlabUrl, repoId, url),
link_type: type,
filepath,
nfriend marked this conversation as resolved.
Show resolved Hide resolved
};
}),
},
Expand Down
46 changes: 46 additions & 0 deletions test/publish.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,52 @@ test.serial('Publish a release with assets', async t => {
t.true(gitlab.isDone());
});

test.serial('Publish a release with asset type and permalink', async t => {
const cwd = 'test/fixtures/files';
const owner = 'test_user';
const repo = 'test_repo';
const env = {GITLAB_TOKEN: 'gitlab_token'};
const nextRelease = {gitHead: '123', gitTag: 'v1.0.0', notes: 'Test release note body'};
const options = {repositoryUrl: `https://gitlab.com/${owner}/${repo}.git`};
const encodedRepoId = encodeURIComponent(`${owner}/${repo}`);
const encodedGitTag = encodeURIComponent(nextRelease.gitTag);
const uploaded = {url: '/uploads/file.css', alt: 'file.css', link_type: 'package', filepath: '/dist/file.css'};
const assets = [
{
path: 'file.css',
type: 'package',
filepath: '/dist/file.css',
},
];
const gitlab = authenticate(env)
.post(`/projects/${encodedRepoId}/releases`, {
tag_name: nextRelease.gitTag,
description: nextRelease.notes,
assets: {
links: [
{
name: uploaded.alt,
url: `https://gitlab.com/${owner}/${repo}${uploaded.url}`,
link_type: uploaded.link_type,
filepath: uploaded.filepath,
},
],
},
})
.reply(200);
const gitlabUpload = authenticate(env)
.post(`/projects/${encodedRepoId}/uploads`, /filename="file.css"/gm)
.reply(200, uploaded);

const result = await publish({assets}, {env, cwd, options, nextRelease, logger: t.context.logger});

t.is(result.url, `https://gitlab.com/${encodedRepoId}/-/releases/${encodedGitTag}`);
t.deepEqual(t.context.log.args[0], ['Uploaded file: %s', uploaded.url]);
t.deepEqual(t.context.log.args[1], ['Published GitLab release: %s', nextRelease.gitTag]);
t.true(gitlabUpload.isDone());
t.true(gitlab.isDone());
});

test.serial('Publish a release with a milestone', async t => {
const owner = 'test_user';
const repo = 'test_repo';
Expand Down