From 55578299e84f0ab41541d144b1c7a2433f9c8bda Mon Sep 17 00:00:00 2001 From: Jack Chan Date: Sat, 11 Jun 2022 12:08:57 +0800 Subject: [PATCH 1/5] fix: publish link asset allow retrieve filename from url --- lib/publish.js | 9 +++- test/publish.test.js | 105 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 113 insertions(+), 1 deletion(-) diff --git a/lib/publish.js b/lib/publish.js index eb2c408a..99e79397 100644 --- a/lib/publish.js +++ b/lib/publish.js @@ -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'; + } + + assetsList.push({label: _label, rawUrl: _url, type, filepath}); debug('use link from release setting: %s', _url); } else { const file = pathlib.resolve(cwd, path); diff --git a/test/publish.test.js b/test/publish.test.js index 2fa7d98b..aa5f3b50 100644 --- a/test/publish.test.js +++ b/test/publish.test.js @@ -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()); +}); From 3781a41aa4dbb376ef51deeaad0152a8ac12b17c Mon Sep 17 00:00:00 2001 From: Jack Chan Date: Sat, 11 Jun 2022 12:09:29 +0800 Subject: [PATCH 2/5] docs: update url asset url description --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index ecdd5c63..c23b854e 100644 --- a/README.md +++ b/README.md @@ -97,7 +97,7 @@ 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. | - | +| `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. | - | | `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`. | | `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. | - | From a6ff55bca03179e393e489624d7cbb5ff35f1162 Mon Sep 17 00:00:00 2001 From: Jack Chan Date: Sat, 11 Jun 2022 12:28:16 +0800 Subject: [PATCH 3/5] docs: update label description --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index c23b854e..8f70c3ee 100644 --- a/README.md +++ b/README.md @@ -98,7 +98,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. The url must be unique within the release. | - | -| `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`. | +| `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` | | `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. | - | From aa74c0a045bc445b3565b5f89d019cd9083b85db Mon Sep 17 00:00:00 2001 From: awcjack Date: Thu, 23 Jun 2022 17:11:13 +0000 Subject: [PATCH 4/5] Update README.md Co-authored-by: Florian Greinacher --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 71f2c350..55befbb8 100644 --- a/README.md +++ b/README.md @@ -97,7 +97,7 @@ 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. The url must be unique within the release. | - | +| `url` | Alternative to setting `path` this provides the ability to add links to releases, e.g. URLs to container images. Must be unique within the release. | - | | `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` | | `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. | - | From d89c8ca2af9e26b516d5123c02b9c951cba28a39 Mon Sep 17 00:00:00 2001 From: awcjack Date: Thu, 23 Jun 2022 17:11:20 +0000 Subject: [PATCH 5/5] Update README.md Co-authored-by: Florian Greinacher --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 55befbb8..4cdf8785 100644 --- a/README.md +++ b/README.md @@ -98,7 +98,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. Must be unique within the release. | - | -| `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` | +| `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. Must be unique within the release. | File name extracted from `path` or `url` | | `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. | - |