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

Retrieve link name from link when using link asset #401

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ Can be a [glob](https://github.com/isaacs/node-glob#glob-primer) or and `Array`
| Property | Description | Default |
| -------- | ----------------------------------------------------------------------------------------------------------- | ------------------------------------ |
| `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. | - |
| `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). Ignored if `path` matches more than one file. | File name extracted from the `path`. |
| `url` | Alternative to setting `path` this provides the ability to add links to releases, e.g. URLs to container images. The url must be unique within the release. | - |
awcjack marked this conversation as resolved.
Show resolved Hide resolved
| `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). Ignored if `path` matches more than one file. The Label must be unique within the release when `url` is set. | File name extracted from the `path`. File name extracted from `url` |
awcjack marked this conversation as resolved.
Show resolved Hide resolved
| `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` |
| `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. | - |

Expand Down
9 changes: 8 additions & 1 deletion lib/publish.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,14 @@ module.exports = async (pluginConfig, context) => {
const label = asset.label ? template(asset.label)(context) : undefined;
const _url = asset.url;
if (_url) {
assetsList.push({label, rawUrl: _url, type, filepath});
let _label = label;
if (!label) {
const parsedUrl = new URL(_url);
const file = pathlib.basename(parsedUrl.pathname);
_label = file ? file : 'Unknown asset';
Copy link
Contributor

@fgreinacher fgreinacher Jun 14, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like the fallback here. What about using parsedUrl.hostname?

Also can any of these throw an exception that we should handle?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You mean replacing 'Unknown asset' by parsedUrl.hostname?
Actually I am thinking any better handling here too since the link name must be unique within the release.

So when user have urls like below will cause exception during publish
https://server1.com/1.txt
https://server2.com/1.txt

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could include the index from .map, e.g.:

_label = file ? file : `${parsedUrl.hostname}_${index}`;

}

assetsList.push({label: _label, rawUrl: _url, type, filepath});
debug('use link from release setting: %s', _url);
} else {
const file = pathlib.resolve(cwd, path);
Expand Down
105 changes: 105 additions & 0 deletions test/publish.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -332,3 +332,108 @@ test.serial('Publish a release with an asset link', async (t) => {
t.deepEqual(t.context.log.args[0], ['Published GitLab release: %s', nextRelease.gitTag]);
t.true(gitlab.isDone());
});

test.serial('Publish a release with an asset link without label & type', 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 link = {
url: 'https://gitlab.com/gitlab-org/gitlab/-/blob/master/README.md',
};
const assets = [link];
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`,
},
],
},
})
.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 an asset link 2 without label & type', 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 link = {
url: 'https://gitlab.com/gitlab-org/gitlab/-/blob/master/README.md/',
};
const assets = [link];
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/`,
},
],
},
})
.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 an asset link 3 without label & type', 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 link = {
url: 'https://gitlab.com',
};
const assets = [link];
const gitlab = authenticate(env)
.post(`/projects/${encodedRepoId}/releases`, {
tag_name: nextRelease.gitTag,
description: nextRelease.notes,
assets: {
links: [
{
name: 'Unknown asset',
url: `https://gitlab.com`,
},
],
},
})
.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());
});