-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
1,388 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,8 @@ | ||
OTA_ENGINE_SENDINBLUE_API_KEY='xkeysib-3f51c…' | ||
OTA_ENGINE_SMTP_PASSWORD='password' | ||
|
||
# If both GitHub and GitLab tokens are defined, GitHub takes precedence for dataset publishing | ||
OTA_ENGINE_GITHUB_TOKEN=ghp_XXXXXXXXX | ||
|
||
OTA_ENGINE_GITLAB_TOKEN=XXXXXXXXXX | ||
OTA_ENGINE_GITLAB_RELEASES_TOKEN=XXXXXXXXXX |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import fsApi from 'fs'; | ||
import path from 'path'; | ||
import url from 'url'; | ||
|
||
import config from 'config'; | ||
import { Octokit } from 'octokit'; | ||
|
||
import * as readme from '../../assets/README.template.js'; | ||
|
||
export default async function publish({ archivePath, releaseDate, stats }) { | ||
const octokit = new Octokit({ auth: process.env.OTA_ENGINE_GITHUB_TOKEN }); | ||
|
||
const [ owner, repo ] = url.parse(config.get('@opentermsarchive/engine.dataset.versionsRepositoryURL')).pathname.split('/').filter(component => component); | ||
|
||
const tagName = `${path.basename(archivePath, path.extname(archivePath))}`; // use archive filename as Git tag | ||
|
||
const { data: { upload_url: uploadUrl, html_url: releaseUrl } } = await octokit.rest.repos.createRelease({ | ||
owner, | ||
repo, | ||
tag_name: tagName, | ||
name: readme.title({ releaseDate }), | ||
body: readme.body(stats), | ||
}); | ||
|
||
await octokit.rest.repos.uploadReleaseAsset({ | ||
data: fsApi.readFileSync(archivePath), | ||
headers: { | ||
'content-type': 'application/zip', | ||
'content-length': fsApi.statSync(archivePath).size, | ||
}, | ||
name: path.basename(archivePath), | ||
url: uploadUrl, | ||
}); | ||
|
||
return releaseUrl; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
import fsApi from 'fs'; | ||
import path from 'path'; | ||
|
||
import config from 'config'; | ||
import dotenv from 'dotenv'; | ||
import FormData from 'form-data'; | ||
import nodeFetch from 'node-fetch'; | ||
|
||
import GitLab from '../../../../src/reporter/gitlab/index.js'; | ||
import * as readme from '../../assets/README.template.js'; | ||
import logger from '../../logger/index.js'; | ||
|
||
dotenv.config(); | ||
|
||
export default async function publish({ | ||
archivePath, | ||
releaseDate, | ||
stats, | ||
}) { | ||
let projectId = null; | ||
const gitlabAPIUrl = config.get('@opentermsarchive/engine.dataset.apiBaseURL'); | ||
|
||
const [ owner, repo ] = new URL(config.get('@opentermsarchive/engine.dataset.versionsRepositoryURL')) | ||
.pathname | ||
.split('/') | ||
.filter(Boolean); | ||
const commonParams = { owner, repo }; | ||
|
||
try { | ||
const repositoryPath = `${commonParams.owner}/${commonParams.repo}`; | ||
|
||
const options = GitLab.baseOptionsHttpReq(process.env.OTA_ENGINE_GITLAB_RELEASES_TOKEN); | ||
|
||
options.method = 'GET'; | ||
options.headers = { | ||
'Content-Type': 'application/json', | ||
...options.headers, | ||
}; | ||
|
||
const response = await nodeFetch( | ||
`${gitlabAPIUrl}/projects/${encodeURIComponent(repositoryPath)}`, | ||
options, | ||
); | ||
const res = await response.json(); | ||
|
||
projectId = res.id; | ||
} catch (error) { | ||
logger.error(`Error while obtaining projectId: ${error}`); | ||
projectId = null; | ||
} | ||
|
||
const tagName = `${path.basename(archivePath, path.extname(archivePath))}`; // use archive filename as Git tag | ||
|
||
try { | ||
let options = GitLab.baseOptionsHttpReq(process.env.OTA_ENGINE_GITLAB_RELEASES_TOKEN); | ||
|
||
options.method = 'POST'; | ||
options.body = { | ||
ref: 'main', | ||
tag_name: tagName, | ||
name: readme.title({ releaseDate }), | ||
description: readme.body(stats), | ||
}; | ||
options.headers = { | ||
'Content-Type': 'application/json', | ||
...options.headers, | ||
}; | ||
|
||
options.body = JSON.stringify(options.body); | ||
|
||
const releaseResponse = await nodeFetch( | ||
`${gitlabAPIUrl}/projects/${projectId}/releases`, | ||
options, | ||
); | ||
const releaseRes = await releaseResponse.json(); | ||
|
||
const releaseId = releaseRes.commit.id; | ||
|
||
logger.info(`Created release with releaseId: ${releaseId}`); | ||
|
||
// Upload the package | ||
options = GitLab.baseOptionsHttpReq(process.env.OTA_ENGINE_GITLAB_RELEASES_TOKEN); | ||
options.method = 'PUT'; | ||
options.body = fsApi.createReadStream(archivePath); | ||
|
||
// restrict characters to the ones allowed by GitLab APIs | ||
const packageName = config.get('@opentermsarchive/engine.dataset.title').replace(/[^a-zA-Z0-9.\-_]/g, '-'); | ||
const packageVersion = tagName.replace(/[^a-zA-Z0-9.\-_]/g, '-'); | ||
const packageFileName = archivePath.replace(/[^a-zA-Z0-9.\-_/]/g, '-'); | ||
|
||
logger.debug(`packageName: ${packageName}, packageVersion: ${packageVersion} packageFileName: ${packageFileName}`); | ||
|
||
const packageResponse = await nodeFetch( | ||
`${gitlabAPIUrl}/projects/${projectId}/packages/generic/${packageName}/${packageVersion}/${packageFileName}?status=default&select=package_file`, | ||
options, | ||
); | ||
const packageRes = await packageResponse.json(); | ||
|
||
const packageFilesId = packageRes.id; | ||
|
||
logger.debug(`package file id: ${packageFilesId}`); | ||
|
||
// use the package id to build the download url for the release | ||
const publishedPackageUrl = `${config.get('@opentermsarchive/engine.dataset.versionsRepositoryURL')}/-/package_files/${packageFilesId}/download`; | ||
|
||
// Create the release and link the package | ||
const formData = new FormData(); | ||
|
||
formData.append('name', archivePath); | ||
formData.append('url', publishedPackageUrl); | ||
formData.append('file', fsApi.createReadStream(archivePath), { filename: path.basename(archivePath) }); | ||
|
||
options = GitLab.baseOptionsHttpReq(process.env.OTA_ENGINE_GITLAB_RELEASES_TOKEN); | ||
options.method = 'POST'; | ||
options.headers = { | ||
...formData.getHeaders(), | ||
...options.headers, | ||
}; | ||
options.body = formData; | ||
|
||
const uploadResponse = await nodeFetch( | ||
`${gitlabAPIUrl}/projects/${projectId}/releases/${tagName}/assets/links`, | ||
options, | ||
); | ||
const uploadRes = await uploadResponse.json(); | ||
const releaseUrl = uploadRes.direct_asset_url; | ||
|
||
return releaseUrl; | ||
} catch (error) { | ||
logger.error('Failed to create release or upload ZIP file:', error); | ||
throw error; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,15 @@ | ||
import fsApi from 'fs'; | ||
import path from 'path'; | ||
import url from 'url'; | ||
import publishGitHub from './github/index.js'; | ||
import publishGitLab from './gitlab/index.js'; | ||
|
||
import config from 'config'; | ||
import { Octokit } from 'octokit'; | ||
export default function publishRelease({ archivePath, releaseDate, stats }) { | ||
// If both GitHub and GitLab tokens are defined, GitHub takes precedence | ||
if (process.env.OTA_ENGINE_GITHUB_TOKEN) { | ||
return publishGitHub({ archivePath, releaseDate, stats }); | ||
} | ||
|
||
import * as readme from '../assets/README.template.js'; | ||
if (process.env.OTA_ENGINE_GITLAB_TOKEN) { | ||
return publishGitLab({ archivePath, releaseDate, stats }); | ||
} | ||
|
||
export default async function publish({ archivePath, releaseDate, stats }) { | ||
const octokit = new Octokit({ auth: process.env.OTA_ENGINE_GITHUB_TOKEN }); | ||
|
||
const [ owner, repo ] = url.parse(config.get('@opentermsarchive/engine.dataset.versionsRepositoryURL')).pathname.split('/').filter(component => component); | ||
|
||
const tagName = `${path.basename(archivePath, path.extname(archivePath))}`; // use archive filename as Git tag | ||
|
||
const { data: { upload_url: uploadUrl, html_url: releaseUrl } } = await octokit.rest.repos.createRelease({ | ||
owner, | ||
repo, | ||
tag_name: tagName, | ||
name: readme.title({ releaseDate }), | ||
body: readme.body(stats), | ||
}); | ||
|
||
await octokit.rest.repos.uploadReleaseAsset({ | ||
data: fsApi.readFileSync(archivePath), | ||
headers: { | ||
'content-type': 'application/zip', | ||
'content-length': fsApi.statSync(archivePath).size, | ||
}, | ||
name: path.basename(archivePath), | ||
url: uploadUrl, | ||
}); | ||
|
||
return releaseUrl; | ||
throw new Error('No GitHub nor GitLab token found in environment variables (OTA_ENGINE_GITHUB_TOKEN or OTA_ENGINE_GITLAB_TOKEN). Cannot publish the dataset without authentication.'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import GitHub from './github/index.js'; | ||
import GitLab from './gitlab/index.js'; | ||
|
||
export function createReporter(config) { | ||
switch (config.type) { | ||
case 'github': | ||
return new GitHub(config.repositories.declarations); | ||
case 'gitlab': | ||
return new GitLab(config.repositories.declarations, config.baseURL, config.apiBaseURL); | ||
default: | ||
throw new Error(`Unsupported reporter type: ${config.type}`); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.