-
Notifications
You must be signed in to change notification settings - Fork 35
Refactor GitLab integration #1116
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 24 commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
88a5619
Add Gtilab functionalities
alessandrozago 8672ba9
Fix new gitlab files using eslint
alessandrozago 5dbe093
Add tests for GitLab class and format fixes
alessandrozago b67e361
Remove existing GitLab repository in the config
alessandrozago 46567ed
Add EUPL-1.2 copyright
alessandrozago 3a64bf0
Use only a single template for README
alessandrozago d46c68c
Fix release zip upload not working
alessandrozago e423dd3
Update changelog
alessandrozago 61b20a6
Merge branch 'main' into main
alessandrozago 3b964ca
Fix typo on changelog
alessandrozago 07aac6f
Remove explicit EUPL-1.2 copyright in files
alessandrozago bbf5e58
Align logging messages format with latest releases
alessandrozago 00a3076
Merge branch 'main' into main
alessandrozago bb72636
Merge branch 'main' into main
alessandrozago 3b92517
Factorize code between Gitlab and GitHub reporters
Ndpnt 48ad093
Factorize code between Gitlab and GitHub publisher
Ndpnt ca58f97
Fix deprecated method
Ndpnt a6d8d29
Improve changelog
Ndpnt 64d1060
Improve changelog entry
Ndpnt 8769d0f
Add release funders
Ndpnt aed17f3
Clarify token precedence between GitHub and GitLab
Ndpnt cd411eb
Update changelog entry
Ndpnt c9fa05e
Add backward compatibility for legacy config
Ndpnt 4496f52
Use proper configuration key
Ndpnt 24875eb
Improve test maintainability
Ndpnt 0bdd637
Improve comment
Ndpnt 356a8b8
Implement no-op clearCache in GitLab reporter
Ndpnt 2dda758
Remove obsolete log
Ndpnt 7df61b6
Improve wording
Ndpnt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 }); | ||
MattiSG marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
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 or GitLab token found in environment variables (OTA_ENGINE_GITHUB_TOKEN or OTA_ENGINE_GITLAB_TOKEN). Cannot publish the dataset without authentication.'); | ||
Ndpnt marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
} |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.