From 651ffc13df97b5e0eb4f05a4cd8b3a92acc3f37e Mon Sep 17 00:00:00 2001
From: su-ex <codeworks@supercable.onl>
Date: Wed, 16 Dec 2020 00:38:28 +0100
Subject: [PATCH] [WIP] Deploy scripts

---
 deploy/.gitignore                     |  1 +
 deploy/update_aur-bin.sh              | 33 ++++++++++++++
 deploy/update_flathub.sh              | 44 ++++++++++++++++++
 deploy/upload-github-release-asset.sh | 64 +++++++++++++++++++++++++++
 deploy/upload_github.sh               | 14 ++++++
 5 files changed, 156 insertions(+)
 create mode 100644 deploy/.gitignore
 create mode 100755 deploy/update_aur-bin.sh
 create mode 100755 deploy/update_flathub.sh
 create mode 100755 deploy/upload-github-release-asset.sh
 create mode 100755 deploy/upload_github.sh

diff --git a/deploy/.gitignore b/deploy/.gitignore
new file mode 100644
index 0000000..08935a3
--- /dev/null
+++ b/deploy/.gitignore
@@ -0,0 +1 @@
+repos/*
\ No newline at end of file
diff --git a/deploy/update_aur-bin.sh b/deploy/update_aur-bin.sh
new file mode 100755
index 0000000..4eb98d8
--- /dev/null
+++ b/deploy/update_aur-bin.sh
@@ -0,0 +1,33 @@
+#!/bin/bash
+
+set -e
+set -x
+
+DEPLOY_ROOT="$(dirname "$(realpath "$0")")"
+
+version="$1"
+debpath="$2"
+
+repopath="$DEPLOY_ROOT/repos/aur-bin"
+repourl="ssh://aur@aur.archlinux.org/schildichat-desktop-bin.git"
+
+sha256sum=($(sha256sum $debpath))
+
+[ -d "$repopath" ] || git clone $repourl $repopath
+
+pushd "$repopath" > /dev/null
+
+git fetch
+git reset --hard origin/master
+
+sed -i "s|^pkgver=.*$|pkgver=$version|" PKGBUILD
+sed -i "s|^sha256sums=('.*'$|sha256sums=('$sha256sum'|" PKGBUILD
+
+makepkg --printsrcinfo > .SRCINFO
+
+git add .SRCINFO PKGBUILD
+git commit -m "Bump version to v$version"
+
+#git push
+
+popd > /dev/null
diff --git a/deploy/update_flathub.sh b/deploy/update_flathub.sh
new file mode 100755
index 0000000..b0a096f
--- /dev/null
+++ b/deploy/update_flathub.sh
@@ -0,0 +1,44 @@
+#!/bin/bash
+
+set -e
+set -x
+
+DEPLOY_ROOT="$(dirname "$(realpath "$0")")"
+
+version="$1"
+debpath="$2"
+
+repopath="$DEPLOY_ROOT/repos/flathub"
+repourl="git@github.com:flathub/chat.schildi.desktop.git"
+
+downloadurl="https://github.com/SchildiChat/schildichat-desktop/releases/download/v${version}/schildichat-desktop_${version}_amd64.deb"
+sha256sum=($(sha256sum $debpath))
+debsize=($(wc -c $debpath))
+debdate=$(date +%Y-%m-%d -r $debpath)
+
+[ -d "$repopath" ] || git clone $repourl $repopath
+
+pushd "$repopath" > /dev/null
+
+git fetch
+git reset --hard origin/master
+
+jsonFile="chat.schildi.desktop.json"
+jsonString=$(jq -r "." $jsonFile)
+
+xmlFile="chat.schildi.desktop.appdata.xml"
+
+jsonString=$(echo $jsonString | jq -r ".modules[]? |= ((select(.name?==\"schildichat\") | .sources[0].url = \"${downloadurl}\") // .)")
+jsonString=$(echo $jsonString | jq -r ".modules[]? |= ((select(.name?==\"schildichat\") | .sources[0].sha256 = \"${sha256sum}\") // .)")
+jsonString=$(echo $jsonString | jq -r ".modules[]? |= ((select(.name?==\"schildichat\") | .sources[0].size = ${debsize}) // .)")
+
+echo $jsonString | jq --indent 4 "." > $jsonFile
+
+sed -i "s|^\s\s<releases>$|  <releases>\n    <release version=\"$version\" date=\"$debdate\"/>'|" $xmlFile
+
+git add $jsonFile $xmlFile
+git commit -m "Bump version to v$version"
+
+#git push
+
+popd > /dev/null
diff --git a/deploy/upload-github-release-asset.sh b/deploy/upload-github-release-asset.sh
new file mode 100755
index 0000000..206e3cd
--- /dev/null
+++ b/deploy/upload-github-release-asset.sh
@@ -0,0 +1,64 @@
+#!/usr/bin/env bash
+#
+# Author: Stefan Buck
+# License: MIT
+# https://gist.github.com/stefanbuck/ce788fee19ab6eb0b4447a85fc99f447
+#
+#
+# This script accepts the following parameters:
+#
+# * owner
+# * repo
+# * tag
+# * filename
+# * github_api_token
+#
+# Script to upload a release asset using the GitHub API v3.
+#
+# Example:
+#
+# upload-github-release-asset.sh github_api_token=TOKEN owner=stefanbuck repo=playground tag=v0.1.0 filename=./build.zip
+#
+
+# Check dependencies.
+set -e
+xargs=$(which gxargs || which xargs)
+
+# Validate settings.
+[ "$TRACE" ] && set -x
+
+CONFIG=$@
+
+for line in $CONFIG; do
+  eval "$line"
+done
+
+# Define variables.
+GH_API="https://api.github.com"
+GH_REPO="$GH_API/repos/$owner/$repo"
+GH_TAGS="$GH_REPO/releases/tags/$tag"
+AUTH="Authorization: token $github_api_token"
+WGET_ARGS="--content-disposition --auth-no-challenge --no-cookie"
+CURL_ARGS="-LJO#"
+
+if [[ "$tag" == 'LATEST' ]]; then
+  GH_TAGS="$GH_REPO/releases/latest"
+fi
+
+# Validate token.
+curl -o /dev/null -sH "$AUTH" $GH_REPO || { echo "Error: Invalid repo, token or network issue!";  exit 1; }
+
+# Read asset tags.
+response=$(curl -sH "$AUTH" $GH_TAGS)
+
+# Get ID of the asset based on given filename.
+eval $(echo "$response" | grep -m 1 "id.:" | grep -w id | tr : = | tr -cd '[[:alnum:]]=')
+[ "$id" ] || { echo "Error: Failed to get release id for tag: $tag"; echo "$response" | awk 'length($0)<100' >&2; exit 1; }
+
+# Upload asset
+echo "Uploading asset... "
+
+# Construct url
+GH_ASSET="https://uploads.github.com/repos/$owner/$repo/releases/$id/assets?name=$(basename $filename)"
+
+curl "$GITHUB_OAUTH_BASIC" --data-binary @"$filename" -H "Authorization: token $github_api_token" -H "Content-Type: application/octet-stream" $GH_ASSET
diff --git a/deploy/upload_github.sh b/deploy/upload_github.sh
new file mode 100755
index 0000000..f18bbcb
--- /dev/null
+++ b/deploy/upload_github.sh
@@ -0,0 +1,14 @@
+#!/bin/bash
+
+set -e
+set -x
+
+DEPLOY_ROOT="$(dirname "$(realpath "$0")")"
+
+version="$1"
+releasepath="$2"
+
+find "$releasepath" -type f | while read p; do
+    echo "Uploading $p ..."
+    $DEPLOY_ROOT/upload-github-release-asset.sh github_api_token=$GITHUB_TOKEN owner=SchildiChat repo=schildichat-desktop tag=v$version filename=$p
+done
\ No newline at end of file