From 961dd6eb7800ed077a237f5e3ccb0f5d41793f4d Mon Sep 17 00:00:00 2001 From: Yu Yin Date: Wed, 2 Jun 2021 00:07:37 +0800 Subject: [PATCH 1/3] feat: support asset permalink --- README.md | 2 ++ lib/publish.js | 8 +++++--- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 9b2a6d91..1b0856cd 100644 --- a/README.md +++ b/README.md @@ -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` | +| `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)). | - | 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 diff --git a/lib/publish.js b/lib/publish.js index dd5d7169..73051b73 100644 --- a/lib/publish.js +++ b/lib/publish.js @@ -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; @@ -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); }) @@ -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 || 'other', + filepath, }; }), }, From 732a34f9b330b21a66aa4730573139d48ab4917b Mon Sep 17 00:00:00 2001 From: Yu Yin Date: Sat, 5 Jun 2021 23:28:30 +0800 Subject: [PATCH 2/3] fix: link_type default to undefined and fix test --- lib/publish.js | 2 +- test/publish.test.js | 46 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/lib/publish.js b/lib/publish.js index 73051b73..db28244d 100644 --- a/lib/publish.js +++ b/lib/publish.js @@ -81,7 +81,7 @@ module.exports = async (pluginConfig, context) => { return { name: label || alt, url: urlJoin(gitlabUrl, repoId, url), - link_type: type || 'other', + link_type: type, filepath, }; }), diff --git a/test/publish.test.js b/test/publish.test.js index cc512fee..a65de2d8 100644 --- a/test/publish.test.js +++ b/test/publish.test.js @@ -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'; From b8a3de93d47e723a943396c00ad9eedf53ad1c4b Mon Sep 17 00:00:00 2001 From: Yu Yin Date: Fri, 11 Jun 2021 23:10:46 +0800 Subject: [PATCH 3/3] fix: ignore filepath when uploading multiple assets --- README.md | 4 ++-- lib/glob-assets.js | 4 +++- lib/publish.js | 2 ++ test/glob-assets.test.js | 18 ++++++++++++++++-- test/publish.test.js | 2 +- 5 files changed, 24 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 1b0856cd..31126641 100644 --- a/README.md +++ b/README.md @@ -74,8 +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` | -| `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)). | - | +| `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. | - | 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 diff --git a/lib/glob-assets.js b/lib/glob-assets.js index 2231de7e..e8914489 100644 --- a/lib/glob-assets.js +++ b/lib/glob-assets.js @@ -39,8 +39,10 @@ module.exports = async ({cwd}, assets) => // Output an Object definition for each file matched and set each one with: // - `path` of the matched file // - `label` based on the actual file name (to avoid assets with duplicate `label`s) + // - `filepath` ignored (also to avoid duplicates) // - other properties of the original asset definition - return globbed.map(file => ({...asset, path: file, label: basename(file)})); + const {filepath, ...others} = asset; + return globbed.map(file => ({...others, path: file, label: basename(file)})); } // If asset is an Object, output an Object definition with: diff --git a/lib/publish.js b/lib/publish.js index db28244d..19e03293 100644 --- a/lib/publish.js +++ b/lib/publish.js @@ -53,6 +53,8 @@ module.exports = async (pluginConfig, context) => { debug('file path: %o', path); debug('file label: %o', label); + debug('file type: %o', type); + debug('file filepath: %o', filepath); // Uploaded assets to the project const form = new FormData(); diff --git a/test/glob-assets.test.js b/test/glob-assets.test.js index ac5c8238..7e8adf3c 100644 --- a/test/glob-assets.test.js +++ b/test/glob-assets.test.js @@ -70,7 +70,7 @@ test('Favor Object over String values when removing duplicates', async t => { 'upload.txt', {path: 'upload.txt', name: 'upload_name'}, 'upload.txt', - {path: 'upload_other.txt', name: 'upload_other_name'}, + {path: 'upload_other.txt', name: 'upload_other_name', filepath: '/path/to/other'}, 'upload.txt', 'upload_other.txt', ]); @@ -79,7 +79,7 @@ test('Favor Object over String values when removing duplicates', async t => { sortAssets(globbedAssets), sortAssets([ {path: 'upload.txt', name: 'upload_name'}, - {path: 'upload_other.txt', name: 'upload_other_name'}, + {path: 'upload_other.txt', name: 'upload_other_name', filepath: '/path/to/other'}, ]) ); }); @@ -138,6 +138,20 @@ test('Replace name by filename for Object that match multiple files', async t => ); }); +test('Ignore filepath for Object that match multiple files', async t => { + const cwd = tempy.directory(); + await copy(fixtures, cwd); + const globbedAssets = await globAssets({cwd}, [{path: '*.txt', filepath: '/path/to/file'}]); + + t.deepEqual( + sortAssets(globbedAssets), + sortAssets([ + {path: 'upload.txt', label: 'upload.txt'}, + {path: 'upload_other.txt', label: 'upload_other.txt'}, + ]) + ); +}); + test('Include dotfiles', async t => { const cwd = tempy.directory(); await copy(fixtures, cwd); diff --git a/test/publish.test.js b/test/publish.test.js index a65de2d8..af1f795b 100644 --- a/test/publish.test.js +++ b/test/publish.test.js @@ -95,7 +95,7 @@ test.serial('Publish a release with asset type and permalink', async t => { const uploaded = {url: '/uploads/file.css', alt: 'file.css', link_type: 'package', filepath: '/dist/file.css'}; const assets = [ { - path: 'file.css', + path: ['**', '!**/*.txt', '!.dotfile'], type: 'package', filepath: '/dist/file.css', },