From c00d003ddbb24b09bc24aa8543ff01b7d644829a Mon Sep 17 00:00:00 2001 From: mfoti Date: Fri, 17 Dec 2021 14:29:16 +0100 Subject: [PATCH 1/3] feat: package support --- lib/publish.js | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/lib/publish.js b/lib/publish.js index d2384766..9db47091 100644 --- a/lib/publish.js +++ b/lib/publish.js @@ -1,5 +1,5 @@ const {createReadStream} = require('fs'); -const {resolve} = require('path'); +const {resolve, basename} = require('path'); const {stat} = require('fs-extra'); const {isPlainObject} = require('lodash'); const FormData = require('form-data'); @@ -60,23 +60,23 @@ module.exports = async (pluginConfig, context) => { const form = new FormData(); form.append('file', createReadStream(file)); - const uploadEndpoint = urlJoin(gitlabApiUrl, `/projects/${encodedRepoId}/uploads`); - + const uploadEndpoint = urlJoin(gitlabApiUrl, `/projects/${encodedRepoId}/packages/generic/${basename(path)}/${gitTag}/${basename(path)}?select=package_file`); + debug('POST-ing the file %s to %s', file, uploadEndpoint); let response; try { - response = await got.post(uploadEndpoint, {...apiOptions, body: form}).json(); + response = await got.put(uploadEndpoint, {...apiOptions, body: form}).json(); } catch (error) { logger.error('An error occurred while uploading %s to the GitLab project uploads API:\n%O', file, error); throw error; } - const {url, alt} = response; + const {file_name} = response; - assetsList.push({label, alt, url, type, filepath}); + assetsList.push({label, file_name, uploadEndpoint, type, filepath}); - logger.log('Uploaded file: %s', url); + logger.log('Uploaded file: %s', uploadEndpoint); }) ); } @@ -91,10 +91,10 @@ module.exports = async (pluginConfig, context) => { description: notes && notes.trim() ? notes : gitTag, milestones, assets: { - links: assetsList.map(({label, alt, url, type, filepath}) => { + links: assetsList.map(({label, file_name, uploadEndpoint, type, filepath}) => { return { - name: label || alt, - url: urlJoin(gitlabUrl, repoId, url), + name: label || file_name, + url: uploadEndpoint, link_type: type, filepath, }; From c7b0a139bd321cfb62b6cb54c38244c301e577c1 Mon Sep 17 00:00:00 2001 From: mfoti Date: Thu, 6 Jan 2022 13:13:22 +0100 Subject: [PATCH 2/3] feat: package support with streaming publisher --- lib/publish.js | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/lib/publish.js b/lib/publish.js index 9db47091..0a20667a 100644 --- a/lib/publish.js +++ b/lib/publish.js @@ -1,8 +1,9 @@ +const {promisify} = require('util'); +const stream = require('stream'); const {createReadStream} = require('fs'); const {resolve, basename} = require('path'); const {stat} = require('fs-extra'); const {isPlainObject} = require('lodash'); -const FormData = require('form-data'); const urlJoin = require('url-join'); const got = require('got'); const debug = require('debug')('semantic-release:gitlab'); @@ -57,16 +58,20 @@ module.exports = async (pluginConfig, context) => { debug('file filepath: %o', filepath); // Uploaded assets to the project - const form = new FormData(); - form.append('file', createReadStream(file)); + const encodedFileName = encodeURIComponent(basename(path)); + const uploadEndpoint = urlJoin( + gitlabApiUrl, + `/projects/${encodedRepoId}/packages/generic/${encodedFileName}/${encodedGitTag}/${encodedFileName}?select=package_file` + ); - const uploadEndpoint = urlJoin(gitlabApiUrl, `/projects/${encodedRepoId}/packages/generic/${basename(path)}/${gitTag}/${basename(path)}?select=package_file`); - debug('POST-ing the file %s to %s', file, uploadEndpoint); let response; + const pipe = promisify(stream.pipeline); try { - response = await got.put(uploadEndpoint, {...apiOptions, body: form}).json(); + const uploadStream = got.stream.put(uploadEndpoint, {...apiOptions}); + await pipe(createReadStream(file), uploadStream, new stream.PassThrough()); + response = await got(uploadEndpoint, {...apiOptions}).json(); } catch (error) { logger.error('An error occurred while uploading %s to the GitLab project uploads API:\n%O', file, error); throw error; From 4b02fdc715400ca32328faa975cb85fd2986f9c1 Mon Sep 17 00:00:00 2001 From: mfoti Date: Fri, 7 Jan 2022 15:52:08 +0100 Subject: [PATCH 3/3] Fix: filename --- lib/publish.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/publish.js b/lib/publish.js index 0a20667a..d7d80042 100644 --- a/lib/publish.js +++ b/lib/publish.js @@ -71,13 +71,12 @@ module.exports = async (pluginConfig, context) => { try { const uploadStream = got.stream.put(uploadEndpoint, {...apiOptions}); await pipe(createReadStream(file), uploadStream, new stream.PassThrough()); - response = await got(uploadEndpoint, {...apiOptions}).json(); } catch (error) { logger.error('An error occurred while uploading %s to the GitLab project uploads API:\n%O', file, error); throw error; } - const {file_name} = response; + const file_name = encodedFileName assetsList.push({label, file_name, uploadEndpoint, type, filepath});