Jenkins/vars/gitea.groovy

157 lines
5.5 KiB
Groovy

def commentPullRequest(String repo, String issueId, String comment, Integer commentIndex = -1) {
comment = comment.replaceAll('"', '\\"')
withCredentials([
string(credentialsId: 'GITEA_JENKINS_PERSONAL_TOKEN', variable: 'GITEA_TOKEN'),
]) {
writeFile(file: '.prComment', text: comment)
sh """#!/bin/bash
set -xeo pipefail
previous_comment_id=null
if [ "${commentIndex}" != "-1" ]; then
# Récupération si il existe du commentaire existant
previous_comment_id=\$(curl -v --fail \
-H "Authorization: token ${GITEA_TOKEN}" \
-H "Content-Type: application/json" \
https://forge.cadoles.com/api/v1/repos/${repo}/issues/${issueId}/comments \
| jq -c '[ .[] | select(.user.login=="jenkins") ] | .[${commentIndex}] | .id' \
)
fi
# Génération du payload pour l'API Gitea
echo '{}' | jq -c --rawfile body .prComment '.body = \$body' > payload.json
if [[ "\$previous_comment_id" == "null" ]]; then
# Création du commentaire via l'API Gitea
curl -v --fail \
-XPOST \
-H "Authorization: token ${GITEA_TOKEN}" \
-H "Content-Type: application/json" \
-d @payload.json \
https://forge.cadoles.com/api/v1/repos/${repo}/issues/${issueId}/comments
else
# Modification du commentaire existant
curl -v --fail \
-XPATCH \
-H "Authorization: token ${GITEA_TOKEN}" \
-H "Content-Type: application/json" \
-d @payload.json \
https://forge.cadoles.com/api/v1/repos/${repo}/issues/comments/\$previous_comment_id
fi
"""
}
}
// Effectue une "release" sur Gitea pour le <ORG>/<PROJET> donné.
void release(String credentialsId, String org, String project, Map options = [:]) {
Boolean isDraft = options.get('isDraft', false)
String baseUrl = options.get('baseUrl', 'https://forge.cadoles.com')
String defaultVersion = sh(returnStdout: true, script: 'git describe --always').trim()
String releaseVersion = options.get('releaseVersion', defaultVersion)
String releaseName = options.get('releaseName', releaseVersion)
String commitishTarget = options.get('commitishTarget', env.GIT_COMMIT)
Boolean defaultIsPrerelease = true
try {
sh(script: "git describe --exact-match ${GIT_COMMIT}")
defaultIsPrerelease = false
} catch (err) {
println "Could not find tag associated with commit '${GIT_COMMIT}' ! Using 'prerelease' as default."
}
Boolean isPrerelease = options.get('isPrerelease', defaultIsPrerelease)
String body = options.get('body', '')
List<String> attachments = options.get('attachments', [])
String scriptTempDir = ".gitea-release-script-${System.currentTimeMillis()}"
sh("mkdir -p '${scriptTempDir}'")
String giteaReleaseScript = "${scriptTempDir}/gitea-release.sh"
String giteaReleaseScriptContent = libraryResource 'com/cadoles/gitea/gitea-release.sh'
writeFile file: giteaReleaseScript, text:giteaReleaseScriptContent
sh("chmod +x '${giteaReleaseScript}'")
try {
withCredentials([
usernamePassword(
credentialsId: credentialsId,
usernameVariable: 'GITEA_RELEASE_USERNAME',
passwordVariable: 'GITEA_RELEASE_PASSWORD'
)
]) {
sh """
export GITEA_RELEASE_PROJECT="${project}"
export GITEA_RELEASE_ORG="${org}"
export GITEA_RELEASE_BASE_URL="${baseUrl}"
export GITEA_RELEASE_VERSION="${releaseVersion}"
export GITEA_RELEASE_NAME="${releaseName}"
export GITEA_RELEASE_COMMITISH_TARGET="${commitishTarget}"
export GITEA_RELEASE_IS_DRAFT="${isDraft}"
export GITEA_RELEASE_IS_PRERELEASE="${isPrerelease}"
export GITEA_RELEASE_BODY="${body}"
export GITEA_RELEASE_ATTACHMENTS="${attachments.join(' ')}"
${giteaReleaseScript}
"""
}
} finally {
dir(scriptTempDir) {
deleteDir()
}
}
rocketSend(
avatar: 'https://jenkins.cadol.es/static/b5f67753/images/headshot.png',
message: """
Nouvelle version publiée pour le projet `${org}/${project}`: [${releaseName}](${baseUrl}/${org}/${project}/releases/tag/${releaseVersion})
[Visualiser le job](${env.RUN_DISPLAY_URL})
@${utils.getBuildUser()}
""".stripIndent(),
rawMessage: true
)
}
// Télécharge les fichiers associés à une "version" publiée sur un projet Gitea
void download(String credentialsId, String org, String project, Map options = [:]) {
String baseUrl = options.get('baseUrl', 'https://forge.cadoles.com')
String releaseName = options.get('releaseName', 'latest')
String outputDir = options.get('outputDir', 'gitea-dl')
String scriptTempDir = ".gitea-download-script-${System.currentTimeMillis()}"
sh("mkdir -p '${scriptTempDir}'")
String giteaDownloadScript = "${scriptTempDir}/gitea-download.sh"
String giteaDownloadScriptContent = libraryResource 'com/cadoles/gitea/gitea-download.sh'
writeFile file: giteaDownloadScript, text:giteaDownloadScriptContent
sh("chmod +x '${giteaDownloadScript}'")
try {
withCredentials([
usernamePassword(
credentialsId: credentialsId,
usernameVariable: 'GITEA_DOWNLOAD_USERNAME',
passwordVariable: 'GITEA_DOWNLOAD_PASSWORD'
)
]) {
sh """
export GITEA_DOWNLOAD_PROJECT="${project}"
export GITEA_DOWNLOAD_ORG="${org}"
export GITEA_DOWNLOAD_BASE_URL="${baseUrl}"
export GITEA_DOWNLOAD_RELEASE_NAME="${releaseName}"
export GITEA_DOWNLOAD_TARGET_DIRECTORY="${outputDir}"
${giteaDownloadScript}
"""
}
} finally {
dir(scriptTempDir) {
deleteDir()
}
}
}