Jenkins/resources/com/cadoles/gitea/gitea-download.sh

170 lines
4.3 KiB
Bash

#!/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}
GITEA_DOWNLOAD_ATTACHMENTS_FILTER="${GITEA_DOWNLOAD_ATTACHMENTS_FILTER:-.*}"
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 \
-k \
-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 \
-k \
-u "$GITEA_DOWNLOAD_USERNAME:$GITEA_DOWNLOAD_PASSWORD" \
--output "$output" \
$GITEA_DOWNLOAD_CURL_ARGS \
"$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')
if [[ "$filename" =~ $GITEA_DOWNLOAD_ATTACHMENTS_FILTER ]]; then
echo "Downloading attachment '$filename'"
gitea_download "$uuid" "$GITEA_DOWNLOAD_TARGET_DIRECTORY/$filename"
else
echo "Ignoring attachment '$filename'"
fi
done
}
function main {
check_dependencies
source_env_file
check_environment
ask_credentials
retrieve_release_name
retrieve_target_directory
download_release_files
}
main