From 5aae615920cfaaee97a71690112b6071a18ae1a4 Mon Sep 17 00:00:00 2001 From: William Petit Date: Mon, 18 May 2020 12:15:23 +0200 Subject: [PATCH] Add gitea-release task --- .env.dist | 3 + .gitignore | 3 +- Makefile | 3 + misc/script/gitea-release | 153 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 161 insertions(+), 1 deletion(-) create mode 100644 .env.dist create mode 100755 misc/script/gitea-release diff --git a/.env.dist b/.env.dist new file mode 100644 index 0000000..d9ce341 --- /dev/null +++ b/.env.dist @@ -0,0 +1,3 @@ +GITEA_RELEASE_PROJECT= +GITEA_RELEASE_ORG= +GITEA_RELEASE_USERNAME= \ No newline at end of file diff --git a/.gitignore b/.gitignore index cdaaada..b9d64f1 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ /bin rice-box.go /release -/vendor \ No newline at end of file +/vendor +/.env \ No newline at end of file diff --git a/Makefile b/Makefile index 36dc56e..40d1a35 100644 --- a/Makefile +++ b/Makefile @@ -22,6 +22,9 @@ vendor: release: clean vendor ./misc/script/release +gitea-release: + ./misc/script/gitea-release + clean: rm -rf ./bin ./release ./coverage diff --git a/misc/script/gitea-release b/misc/script/gitea-release new file mode 100755 index 0000000..34d90b4 --- /dev/null +++ b/misc/script/gitea-release @@ -0,0 +1,153 @@ +#!/bin/bash + +set -eo pipefail + +GITEA_RELEASE_PROJECT=${GITEA_RELEASE_PROJECT} +GITEA_RELEASE_ORG=${GITEA_RELEASE_ORG} +GITEA_RELEASE_BASE_URL=${GITEA_BASE_URL:-https://forge.cadoles.com} +GITEA_RELEASE_USERNAME=${GITEA_RELEASE_USERNAME} +GITEA_RELEASE_PASSWORD=${GITEA_RELEASE_PASSWORD} +GITEA_RELEASE_VERSION=${GITEA_RELEASE_VERSION} +GITEA_RELEASE_COMMITISH_TARGET=${GITEA_RELEASE_COMMITISH_TARGET} +GITEA_RELEASE_IS_DRAFT=${GITEA_RELEASE_IS_DRAFT:-false} +GITEA_RELEASE_IS_PRERELEASE=${GITEA_RELEASE_IS_PRERELEASE:-true} +GITEA_RELEASE_BODY=${GITEA_RELEASE_BODY} +GITEA_RELEASE_ATTACHMENTS=${GITEA_RELEASE_ATTACHMENTS} + +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_RELEASE_PROJECT + assert_environment GITEA_RELEASE_ORG + assert_environment GITEA_RELEASE_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_RELEASE_USERNAME" ]; then + echo -n "Username: " + read GITEA_RELEASE_USERNAME + + fi + if [ -z "$GITEA_RELEASE_PASSWORD" ]; then + echo -n "Password: " + stty -echo + read GITEA_RELEASE_PASSWORD + stty echo + echo + fi +} + +function retrieve_version { + if [ ! -z "$GITEA_RELEASE_VERSION" ]; then + return + fi + + set +e + GITEA_RELEASE_VERSION=$(git describe --abbrev=0 --tags 2>/dev/null) + GITEA_RELEASE_VERSION=${GITEA_RELEASE_VERSION} + set -e +} + +function retrieve_commitish_target { + if [ ! -z "$GITEA_RELEASE_COMMITISH_TARGET" ]; then + return + fi + + GITEA_RELEASE_COMMITISH_TARGET=$(git log -n 1 --pretty="format:%h") +} + +function create_release { + local payload={} + + payload=$(json_set "$payload" body "\"$GITEA_RELEASE_BODY\"") + payload=$(json_set "$payload" draft $GITEA_RELEASE_IS_DRAFT) + payload=$(json_set "$payload" name "\"$GITEA_RELEASE_VERSION\"") + payload=$(json_set "$payload" prerelease $GITEA_RELEASE_IS_PRERELEASE) + payload=$(json_set "$payload" tag_name "\"${GITEA_RELEASE_VERSION:-$GITEA_RELEASE_COMMITISH_TARGET}\"") + payload=$(json_set "$payload" target_commitish "\"$GITEA_RELEASE_COMMITISH_TARGET\"") + + gitea_api "/repos/$GITEA_RELEASE_ORG/$GITEA_RELEASE_PROJECT/releases" \ + -H "Content-Type:application/json" \ + -d "$payload" +} + +function json_set { + local data=$1 + local key=$2 + local value=$3 + echo $data | jq -cr --argjson v "$value" --arg k "$key" '.[$k] = $v' +} + +function upload_release_attachments { + local release="$1" + local release_id=$(echo "$release" | jq -r .id) + + if [ -z "$GITEA_RELEASE_ATTACHMENTS" ]; then + set +e + GITEA_RELEASE_ATTACHMENTS="$(ls release/*.{tar.gz,zip} 2>/dev/null)" + set -e + fi + + for file in $GITEA_RELEASE_ATTACHMENTS; do + local filename=$(basename "$file") + gitea_api "/repos/$GITEA_RELEASE_ORG/$GITEA_RELEASE_PROJECT/releases/$release_id/assets?name=$filename" \ + -H "Content-Type:multipart/form-data" \ + -F "attachment=@$file" + done +} + +function gitea_api { + local path=$1 + local args=${@:2} + + curl -L \ + --fail \ + -u "$GITEA_RELEASE_USERNAME:$GITEA_RELEASE_PASSWORD" \ + ${args} \ + "$GITEA_RELEASE_BASE_URL/api/v1$path" +} + +function main { + check_dependencies + source_env_file + check_environment + ask_credentials + retrieve_commitish_target + retrieve_version + local release=$(create_release) + upload_release_attachments "$release" +} + +main \ No newline at end of file