Skip to content

Commit

Permalink
feat(assets): allow type templating
Browse files Browse the repository at this point in the history
  • Loading branch information
kaerbr committed Jun 21, 2022
1 parent e232fc8 commit 8c9e1c4
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ Can be a [glob](https://github.com/isaacs/node-glob#glob-primer) or and `Array`
| `path` | **Required**, unless `url` is set. A [glob](https://github.com/isaacs/node-glob#glob-primer) to identify the files to upload. | - |
| `url` | Alternative to setting `path` this provides the ability to add links to releases, e.g. URLs to container images. Can be dynamically adjusted with [Lodash template](https://lodash.com/docs#template). Allows same variables as [`successComment`](#successComment) + all given environment variables. | - |
| `label` | Short description of the file displayed on the GitLab release. Can be dynamically adjusted with [Lodash template](https://lodash.com/docs#template). Allows same variables as [`successComment`](#successComment) + all given environment variables. 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://docs.gitlab.com/ee/user/project/releases/#release-assets)). | `other` |
| `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)). Can be dynamically adjusted with [Lodash template](https://lodash.com/docs#template). Allows same variables as [`successComment`](#successComment) + all given environment variables. | `other` |
| `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. | - |

Each entry in the `assets` `Array` is globbed individually. A [glob](https://github.com/isaacs/node-glob#glob-primer)
Expand Down
3 changes: 2 additions & 1 deletion lib/publish.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,10 @@ module.exports = async (pluginConfig, context) => {

await Promise.all(
allAssets.map(async (asset) => {
const {path, type, filepath} = isPlainObject(asset) ? asset : {path: asset};
const {path, filepath} = isPlainObject(asset) ? asset : {path: asset};
const _url = asset.url ? template(asset.url)(context) : undefined;
const label = asset.label ? template(asset.label)(context) : undefined;
const type = asset.type ? template(asset.type)(context) : undefined;
if (_url) {
assetsList.push({label, rawUrl: _url, type, filepath});
debug('use link from release setting: %s', _url);
Expand Down
40 changes: 40 additions & 0 deletions test/publish.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,46 @@ test.serial('Publish a release (with an link) with a template url', async (t) =>
t.true(gitlab.isDone());
});

test.serial('Publish a release (with an link) with a template type', async (t) => {
process.env.TYPE = 'other';
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', version: '1.0.0'};
const options = {repositoryUrl: `https://gitlab.com/${owner}/${repo}.git`};
const encodedRepoId = encodeURIComponent(`${owner}/${repo}`);
const encodedGitTag = encodeURIComponent(nextRelease.gitTag);
const assets = [
{
label: 'README.md',
type: `${process.env.TYPE}`,
url: 'https://gitlab.com/gitlab-org/gitlab/-/blob/master/README.md',
},
];
const gitlab = authenticate(env)
.post(`/projects/${encodedRepoId}/releases`, {
tag_name: nextRelease.gitTag,
description: nextRelease.notes,
assets: {
links: [
{
name: 'README.md',
url: `https://gitlab.com/gitlab-org/gitlab/-/blob/master/README.md`,
link_type: 'other',
},
],
},
})
.reply(200);

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

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

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

0 comments on commit 8c9e1c4

Please sign in to comment.