feat(gitea): add download() method
This commit is contained in:
parent
522deb3c6a
commit
190b01fa6d
|
@ -0,0 +1,161 @@
|
|||
#!/bin/bash
|
||||
|
||||
set -eo pipefail
|
||||
|
||||
GITEA_DOWNLOAD_PROJECT=${GITEA_DOWNLOAD_PROJECT}
|
||||
GITEA_DOWNLOAD_ORG=${GITEA_DOWNLOAD_ORG}
|
||||
GITEA_DOWNLOAD_BASE_URL=${GITEA_BASE_URL:-https://forge.cadoles.com}
|
||||
GITEA_DOWNLOAD_USERNAME=${GITEA_DOWNLOAD_USERNAME}
|
||||
GITEA_DOWNLOAD_PASSWORD=${GITEA_DOWNLOAD_PASSWORD}
|
||||
GITEA_DOWNLOAD_RELEASE_NAME=${GITEA_DOWNLOAD_RELEASE_NAME:-latest}
|
||||
GITEA_DOWNLOAD_TARGET_DIRECTORY=${GITEA_DOWNLOAD_TARGET_DIRECTORY:-gitea-dl}
|
||||
|
||||
function check_dependencies {
|
||||
assert_command_available 'curl'
|
||||
assert_command_available 'jq'
|
||||
}
|
||||
|
||||
function assert_command_available {
|
||||
local command=$1
|
||||
local command_path=$(which $command)
|
||||
|
||||
if [ -z "$command_path" ]; then
|
||||
echo "The '$command' command could not be found. Please install it before using this script." 1>&2
|
||||
exit 1
|
||||
fi
|
||||
}
|
||||
|
||||
function check_environment {
|
||||
assert_environment GITEA_DOWNLOAD_PROJECT
|
||||
assert_environment GITEA_DOWNLOAD_ORG
|
||||
assert_environment GITEA_DOWNLOAD_BASE_URL
|
||||
}
|
||||
|
||||
function source_env_file {
|
||||
if [ ! -f '.env' ]; then
|
||||
return 0
|
||||
fi
|
||||
set -o allexport
|
||||
source .env
|
||||
set +o allexport
|
||||
}
|
||||
|
||||
function assert_environment {
|
||||
local name=$1
|
||||
local value=${!name}
|
||||
|
||||
if [ -z "$value" ]; then
|
||||
echo "The $"$name" environment variable is empty." 1>&2
|
||||
exit 1
|
||||
fi
|
||||
}
|
||||
|
||||
function ask_credentials {
|
||||
if [ -z "$GITEA_DOWNLOAD_USERNAME" ]; then
|
||||
echo -n "Username: "
|
||||
read GITEA_DOWNLOAD_USERNAME
|
||||
|
||||
fi
|
||||
if [ -z "$GITEA_DOWNLOAD_PASSWORD" ]; then
|
||||
echo -n "Password: "
|
||||
stty -echo
|
||||
read GITEA_DOWNLOAD_PASSWORD
|
||||
stty echo
|
||||
echo
|
||||
fi
|
||||
}
|
||||
|
||||
function retrieve_release_name {
|
||||
if [ ! -z "$GITEA_DOWNLOAD_RELEASE_NAME" ]; then
|
||||
return
|
||||
fi
|
||||
|
||||
echo -n "Release name: "
|
||||
read GITEA_DOWNLOAD_RELEASE_NAME
|
||||
}
|
||||
|
||||
function retrieve_target_directory {
|
||||
if [ ! -z "$GITEA_DOWNLOAD_TARGET_DIRECTORY" ]; then
|
||||
return
|
||||
fi
|
||||
|
||||
echo -n "Target directory: "
|
||||
read GITEA_DOWNLOAD_TARGET_DIRECTORY
|
||||
}
|
||||
|
||||
function json_set {
|
||||
local data=$1
|
||||
local key=$2
|
||||
local value=$3
|
||||
local use_raw_file=$4
|
||||
|
||||
if [ "$use_raw_file" != "true" ]; then
|
||||
echo $data | jq -cr --argjson v "$value" --arg k "$key" '.[$k] = $v'
|
||||
else
|
||||
local tmpfile=$(mktemp)
|
||||
echo "$value" > "$tmpfile"
|
||||
echo $data | jq -cr --rawfile v "$tmpfile" --arg k "$key" '.[$k] = $v'
|
||||
rm -f "$tmpfile"
|
||||
fi
|
||||
}
|
||||
|
||||
function gitea_api {
|
||||
local path=$1
|
||||
local args=${@:2}
|
||||
|
||||
curl -L \
|
||||
--fail \
|
||||
--ipv4 \
|
||||
-u "$GITEA_DOWNLOAD_USERNAME:$GITEA_DOWNLOAD_PASSWORD" \
|
||||
${args} \
|
||||
"$GITEA_DOWNLOAD_BASE_URL/api/v1$path"
|
||||
}
|
||||
|
||||
function gitea_download {
|
||||
local attachment_id=$1
|
||||
local output=$2
|
||||
|
||||
curl -L \
|
||||
--fail \
|
||||
--ipv4 \
|
||||
-u "$GITEA_DOWNLOAD_USERNAME:$GITEA_DOWNLOAD_PASSWORD" \
|
||||
--output "$output" \
|
||||
"$GITEA_DOWNLOAD_BASE_URL/attachments/$attachment_id"
|
||||
}
|
||||
|
||||
function download_release_files {
|
||||
local releases=$(gitea_api "/repos/${GITEA_DOWNLOAD_ORG}/${GITEA_DOWNLOAD_PROJECT}/releases")
|
||||
|
||||
local assets
|
||||
if [ "$GITEA_DOWNLOAD_RELEASE_NAME" == "latest" ]; then
|
||||
assets=$(echo $releases | jq -r '. | sort_by(.id) | reverse | .[0].assets')
|
||||
else
|
||||
assets=$(echo $releases | jq -r --arg name "$GITEA_DOWNLOAD_RELEASE_NAME" '. | map(select( .name == $name)) | .[0].assets')
|
||||
fi
|
||||
|
||||
if [ "$assets" == "null" ]; then
|
||||
echo 1>&2 "No release found."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
mkdir -p "$GITEA_DOWNLOAD_TARGET_DIRECTORY"
|
||||
|
||||
local attachment_uuids=$(echo $assets | jq -r '.[].uuid')
|
||||
|
||||
for uuid in $attachment_uuids; do
|
||||
local filename=$(echo $assets | jq -r --arg uuid "$uuid" '. | map(select( .uuid == $uuid)) | .[0].name')
|
||||
gitea_download "$uuid" "$GITEA_DOWNLOAD_TARGET_DIRECTORY/$filename"
|
||||
done
|
||||
}
|
||||
|
||||
function main {
|
||||
check_dependencies
|
||||
source_env_file
|
||||
check_environment
|
||||
ask_credentials
|
||||
retrieve_release_name
|
||||
retrieve_target_directory
|
||||
download_release_files
|
||||
}
|
||||
|
||||
main
|
|
@ -44,15 +44,15 @@ def commentPullRequest(String repo, String issueId, String comment, Integer comm
|
|||
}
|
||||
|
||||
// Effectue une "release" sur Gitea pour le <ORG>/<PROJET> donné.
|
||||
def release(String credentialsId, String org, String project, Map options = [:]) {
|
||||
def isDraft = options.get('isDraft', false)
|
||||
def baseUrl = options.get('baseUrl', 'https://forge.cadoles.com')
|
||||
def defaultVersion = sh(returnStdout: true, script: 'git describe --always').trim()
|
||||
def releaseVersion = options.get('releaseVersion', defaultVersion)
|
||||
def releaseName = options.get('releaseName', releaseVersion)
|
||||
def commitishTarget = options.get('commitishTarget', env.GIT_COMMIT)
|
||||
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)
|
||||
|
||||
def defaultIsPrerelease = true
|
||||
Boolean defaultIsPrerelease = true
|
||||
try {
|
||||
sh(script: "git describe --exact-match ${GIT_COMMIT}")
|
||||
defaultIsPrerelease = false
|
||||
|
@ -60,16 +60,16 @@ def release(String credentialsId, String org, String project, Map options = [:])
|
|||
println "Could not find tag associated with commit '${GIT_COMMIT}' ! Using 'prerelease' as default."
|
||||
}
|
||||
|
||||
def isPrerelease = options.get('isPrerelease', defaultIsPrerelease)
|
||||
def body = options.get('body', '')
|
||||
def attachments = options.get('attachments', [])
|
||||
Boolean isPrerelease = options.get('isPrerelease', defaultIsPrerelease)
|
||||
String body = options.get('body', '')
|
||||
List<String> attachments = options.get('attachments', [])
|
||||
|
||||
def scriptTempDir = ".gitea-release-script-${System.currentTimeMillis()}"
|
||||
String scriptTempDir = ".gitea-release-script-${System.currentTimeMillis()}"
|
||||
sh("mkdir -p '${scriptTempDir}'")
|
||||
|
||||
def giteaReleaseScript = "${scriptTempDir}/gitea-release.sh"
|
||||
String giteaReleaseScript = "${scriptTempDir}/gitea-release.sh"
|
||||
|
||||
def giteaReleaseScriptContent = libraryResource 'com/cadoles/gitea/gitea-release.sh'
|
||||
String giteaReleaseScriptContent = libraryResource 'com/cadoles/gitea/gitea-release.sh'
|
||||
writeFile file: giteaReleaseScript, text:giteaReleaseScriptContent
|
||||
sh("chmod +x '${giteaReleaseScript}'")
|
||||
|
||||
|
@ -102,3 +102,43 @@ def release(String credentialsId, String org, String project, Map options = [:])
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 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()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue