|
| 1 | +import fsApi from 'fs'; |
| 2 | +import path from 'path'; |
| 3 | + |
| 4 | +import config from 'config'; |
| 5 | +import dotenv from 'dotenv'; |
| 6 | +import FormData from 'form-data'; |
| 7 | +import nodeFetch from 'node-fetch'; |
| 8 | + |
| 9 | +import GitLab from '../../../../src/reporter/gitlab/index.js'; |
| 10 | +import * as readme from '../../assets/README.template.js'; |
| 11 | +import logger from '../../logger/index.js'; |
| 12 | + |
| 13 | +dotenv.config(); |
| 14 | + |
| 15 | +export default async function publish({ |
| 16 | + archivePath, |
| 17 | + releaseDate, |
| 18 | + stats, |
| 19 | +}) { |
| 20 | + let projectId = null; |
| 21 | + const gitlabAPIUrl = config.get('@opentermsarchive/engine.dataset.apiBaseURL'); |
| 22 | + |
| 23 | + const [ owner, repo ] = new URL(config.get('@opentermsarchive/engine.dataset.versionsRepositoryURL')) |
| 24 | + .pathname |
| 25 | + .split('/') |
| 26 | + .filter(Boolean); |
| 27 | + const commonParams = { owner, repo }; |
| 28 | + |
| 29 | + try { |
| 30 | + const repositoryPath = `${commonParams.owner}/${commonParams.repo}`; |
| 31 | + |
| 32 | + const options = GitLab.baseOptionsHttpReq(process.env.OTA_ENGINE_GITLAB_RELEASES_TOKEN); |
| 33 | + |
| 34 | + options.method = 'GET'; |
| 35 | + options.headers = { |
| 36 | + 'Content-Type': 'application/json', |
| 37 | + ...options.headers, |
| 38 | + }; |
| 39 | + |
| 40 | + const response = await nodeFetch( |
| 41 | + `${gitlabAPIUrl}/projects/${encodeURIComponent(repositoryPath)}`, |
| 42 | + options, |
| 43 | + ); |
| 44 | + const res = await response.json(); |
| 45 | + |
| 46 | + projectId = res.id; |
| 47 | + } catch (error) { |
| 48 | + logger.error(`Error while obtaining projectId: ${error}`); |
| 49 | + projectId = null; |
| 50 | + } |
| 51 | + |
| 52 | + const tagName = `${path.basename(archivePath, path.extname(archivePath))}`; // use archive filename as Git tag |
| 53 | + |
| 54 | + try { |
| 55 | + let options = GitLab.baseOptionsHttpReq(process.env.OTA_ENGINE_GITLAB_RELEASES_TOKEN); |
| 56 | + |
| 57 | + options.method = 'POST'; |
| 58 | + options.body = { |
| 59 | + ref: 'main', |
| 60 | + tag_name: tagName, |
| 61 | + name: readme.title({ releaseDate }), |
| 62 | + description: readme.body(stats), |
| 63 | + }; |
| 64 | + options.headers = { |
| 65 | + 'Content-Type': 'application/json', |
| 66 | + ...options.headers, |
| 67 | + }; |
| 68 | + |
| 69 | + options.body = JSON.stringify(options.body); |
| 70 | + |
| 71 | + const releaseResponse = await nodeFetch( |
| 72 | + `${gitlabAPIUrl}/projects/${projectId}/releases`, |
| 73 | + options, |
| 74 | + ); |
| 75 | + const releaseRes = await releaseResponse.json(); |
| 76 | + |
| 77 | + const releaseId = releaseRes.commit.id; |
| 78 | + |
| 79 | + logger.info(`Created release with releaseId: ${releaseId}`); |
| 80 | + |
| 81 | + // Upload the package |
| 82 | + options = GitLab.baseOptionsHttpReq(process.env.OTA_ENGINE_GITLAB_RELEASES_TOKEN); |
| 83 | + options.method = 'PUT'; |
| 84 | + options.body = fsApi.createReadStream(archivePath); |
| 85 | + |
| 86 | + // restrict characters to the ones allowed by GitLab APIs |
| 87 | + const packageName = config.get('@opentermsarchive/engine.dataset.title').replace(/[^a-zA-Z0-9.\-_]/g, '-'); |
| 88 | + const packageVersion = tagName.replace(/[^a-zA-Z0-9.\-_]/g, '-'); |
| 89 | + const packageFileName = archivePath.replace(/[^a-zA-Z0-9.\-_/]/g, '-'); |
| 90 | + |
| 91 | + logger.debug(`packageName: ${packageName}, packageVersion: ${packageVersion} packageFileName: ${packageFileName}`); |
| 92 | + |
| 93 | + const packageResponse = await nodeFetch( |
| 94 | + `${gitlabAPIUrl}/projects/${projectId}/packages/generic/${packageName}/${packageVersion}/${packageFileName}?status=default&select=package_file`, |
| 95 | + options, |
| 96 | + ); |
| 97 | + const packageRes = await packageResponse.json(); |
| 98 | + |
| 99 | + const packageFilesId = packageRes.id; |
| 100 | + |
| 101 | + logger.debug(`package file id: ${packageFilesId}`); |
| 102 | + |
| 103 | + // use the package id to build the download url for the release |
| 104 | + const publishedPackageUrl = `${config.get('@opentermsarchive/engine.dataset.versionsRepositoryURL')}/-/package_files/${packageFilesId}/download`; |
| 105 | + |
| 106 | + // Create the release and link the package |
| 107 | + const formData = new FormData(); |
| 108 | + |
| 109 | + formData.append('name', archivePath); |
| 110 | + formData.append('url', publishedPackageUrl); |
| 111 | + formData.append('file', fsApi.createReadStream(archivePath), { filename: path.basename(archivePath) }); |
| 112 | + |
| 113 | + options = GitLab.baseOptionsHttpReq(process.env.OTA_ENGINE_GITLAB_RELEASES_TOKEN); |
| 114 | + options.method = 'POST'; |
| 115 | + options.headers = { |
| 116 | + ...formData.getHeaders(), |
| 117 | + ...options.headers, |
| 118 | + }; |
| 119 | + options.body = formData; |
| 120 | + |
| 121 | + const uploadResponse = await nodeFetch( |
| 122 | + `${gitlabAPIUrl}/projects/${projectId}/releases/${tagName}/assets/links`, |
| 123 | + options, |
| 124 | + ); |
| 125 | + const uploadRes = await uploadResponse.json(); |
| 126 | + const releaseUrl = uploadRes.direct_asset_url; |
| 127 | + |
| 128 | + return releaseUrl; |
| 129 | + } catch (error) { |
| 130 | + logger.error('Failed to create release or upload ZIP file:', error); |
| 131 | + throw error; |
| 132 | + } |
| 133 | +} |
0 commit comments