Skip to content
Closed
Changes from all 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
30 changes: 17 additions & 13 deletions lib/publish.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
const {promisify} = require('util');
const stream = require('stream');
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');
const urlJoin = require('url-join');
const got = require('got');
const debug = require('debug')('semantic-release:gitlab');
Expand Down Expand Up @@ -57,26 +58,29 @@ 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 uploadEndpoint = urlJoin(gitlabApiUrl, `/projects/${encodedRepoId}/uploads`);
const encodedFileName = encodeURIComponent(basename(path));
const uploadEndpoint = urlJoin(
gitlabApiUrl,
`/projects/${encodedRepoId}/packages/generic/${encodedFileName}/${encodedGitTag}/${encodedFileName}?select=package_file`
);

debug('POST-ing the file %s to %s', file, uploadEndpoint);

let response;
const pipe = promisify(stream.pipeline);
try {
response = await got.post(uploadEndpoint, {...apiOptions, body: form}).json();
const uploadStream = got.stream.put(uploadEndpoint, {...apiOptions});
await pipe(createReadStream(file), uploadStream, new stream.PassThrough());
} 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 = encodedFileName

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);
})
);
}
Expand All @@ -91,10 +95,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,
};
Expand Down