summaryrefslogtreecommitdiff
path: root/tools/update-fdroid.sh
diff options
context:
space:
mode:
Diffstat (limited to 'tools/update-fdroid.sh')
-rwxr-xr-xtools/update-fdroid.sh132
1 files changed, 132 insertions, 0 deletions
diff --git a/tools/update-fdroid.sh b/tools/update-fdroid.sh
new file mode 100755
index 0000000..3500348
--- /dev/null
+++ b/tools/update-fdroid.sh
@@ -0,0 +1,132 @@
+#!/usr/bin/env bash
+# Open/update the fdroiddata merge request for the current RECCoon release.
+#
+# The app is already in F-Droid; this updates metadata/com.tomcooks.reccoon.yml
+# with the build entry from f-droid/com.tomcooks.reccoon.yml and opens an MR
+# against fdroid/fdroiddata.
+#
+# Needs a GitLab Personal Access Token with the "api" scope:
+# printf '%s' 'glpat-...' > ~/.config/gitlab-token
+# chmod 600 ~/.config/gitlab-token
+#
+# The release tag and signed APK must already be pushed to the source repo.
+set -euo pipefail
+cd "$(dirname "$0")/.."
+
+API="https://gitlab.com/api/v4"
+UPSTREAM_PATH="fdroid%2Ffdroiddata"
+FILE_PATH="metadata/com.tomcooks.reccoon.yml"
+FILE_PATH_ENC="metadata%2Fcom.tomcooks.reccoon.yml"
+SOURCE_YAML="f-droid/com.tomcooks.reccoon.yml"
+
+TOKEN="${GITLAB_TOKEN:-}"
+if [ -z "$TOKEN" ] && [ -f "$HOME/.config/gitlab-token" ]; then
+ TOKEN="$(tr -d '\r\n' < "$HOME/.config/gitlab-token")"
+fi
+if [ -z "$TOKEN" ]; then
+ echo "No GitLab token found. See the comment at the top of this script." >&2
+ exit 1
+fi
+if [ ! -f "$SOURCE_YAML" ]; then
+ echo "Missing $SOURCE_YAML" >&2
+ exit 1
+fi
+
+gl() { curl -sf -H "PRIVATE-TOKEN: $TOKEN" "$@"; }
+
+VERSION="$(sed -nE 's/^CurrentVersion: *(.*)$/\1/p' "$SOURCE_YAML" | head -1)"
+BRANCH="reccoon-${VERSION}"
+if [ -z "$VERSION" ]; then
+ echo "Could not read CurrentVersion from $SOURCE_YAML" >&2
+ exit 1
+fi
+
+AUTH_CODE="$(curl -s -o /tmp/fdroid-user.json -w '%{http_code}' \
+ -H "PRIVATE-TOKEN: $TOKEN" "$API/user")"
+if [ "$AUTH_CODE" != "200" ]; then
+ echo "GitLab authentication failed (HTTP $AUTH_CODE)." >&2
+ cat /tmp/fdroid-user.json >&2
+ echo "Renew the token at https://gitlab.com/-/user_settings/personal_access_tokens" >&2
+ echo "with the api scope, then save it to ~/.config/gitlab-token." >&2
+ exit 1
+fi
+USERNAME="$(jq -r .username /tmp/fdroid-user.json)"
+UPSTREAM_ID="$(gl "$API/projects/$UPSTREAM_PATH" | jq -r .id)"
+TARGET_BRANCH="$(gl "$API/projects/$UPSTREAM_PATH" | jq -r .default_branch)"
+echo "Authenticated as @$USERNAME; target branch $TARGET_BRANCH; release $VERSION"
+
+echo "==> Checking fork"
+PROJ="$(gl "$API/projects/$USERNAME%2Ffdroiddata" 2>/dev/null || true)"
+FORK_ID="$(printf '%s' "$PROJ" | jq -r '.id // empty' 2>/dev/null || true)"
+if [ -z "$FORK_ID" ]; then
+ echo " creating fork (this can take a few minutes)"
+ curl -s -o /dev/null -H "PRIVATE-TOKEN: $TOKEN" -X POST \
+ "$API/projects/$UPSTREAM_PATH/fork" || true
+ for _ in $(seq 1 180); do
+ PROJ="$(gl "$API/projects/$USERNAME%2Ffdroiddata" 2>/dev/null || true)"
+ FORK_ID="$(printf '%s' "$PROJ" | jq -r '.id // empty' 2>/dev/null || true)"
+ EMPTY="$(printf '%s' "$PROJ" | jq -r '.empty_repo // true' 2>/dev/null || true)"
+ if [ -n "$FORK_ID" ] && [ "$EMPTY" = "false" ]; then
+ break
+ fi
+ sleep 5
+ done
+fi
+if [ -z "$FORK_ID" ]; then
+ echo "Fork is not ready; run this script again in a minute." >&2
+ exit 1
+fi
+FORK_BRANCH="$(printf '%s' "$PROJ" | jq -r '.default_branch // "master"')"
+echo " fork id: $FORK_ID (default branch $FORK_BRANCH)"
+
+echo "==> Creating branch $BRANCH from $FORK_BRANCH"
+curl -s -o /dev/null -H "PRIVATE-TOKEN: $TOKEN" -X POST \
+ "$API/projects/$FORK_ID/repository/branches?branch=$BRANCH&ref=$FORK_BRANCH" || true
+
+echo "==> Writing $FILE_PATH"
+CONTENT="$(cat "$SOURCE_YAML")"
+LAST_COMMIT=""
+LAST_COMMIT="$(gl "$API/projects/$FORK_ID/repository/files/$FILE_PATH_ENC?ref=$BRANCH" \
+ | jq -r '.last_commit_id // empty' 2>/dev/null || true)"
+BODY="$(jq -n --arg b "$BRANCH" --arg c "$CONTENT" --arg m "Update RECCoon to $VERSION" \
+ --arg l "$LAST_COMMIT" \
+ '{branch:$b, content:$c, commit_message:$m} + (if $l == "" then {} else {last_commit_id:$l} end)')"
+if [ -n "$LAST_COMMIT" ]; then
+ CODE="$(curl -s -o /tmp/fdroid-file.json -w '%{http_code}' \
+ -H "PRIVATE-TOKEN: $TOKEN" -H 'Content-Type: application/json' \
+ -X PUT "$API/projects/$FORK_ID/repository/files/$FILE_PATH_ENC" -d "$BODY")"
+else
+ CODE="$(curl -s -o /tmp/fdroid-file.json -w '%{http_code}' \
+ -H "PRIVATE-TOKEN: $TOKEN" -H 'Content-Type: application/json' \
+ -X POST "$API/projects/$FORK_ID/repository/files/$FILE_PATH_ENC" -d "$BODY")"
+fi
+if [ "$CODE" != "200" ] && [ "$CODE" != "201" ]; then
+ echo " unexpected response (HTTP $CODE):" >&2
+ cat /tmp/fdroid-file.json >&2
+ exit 1
+fi
+
+echo "==> Checking for an existing merge request"
+EXISTING="$(gl "$API/projects/$FORK_ID/merge_requests?source_branch=$BRANCH&state=opened" \
+ | jq -r '.[0].web_url // empty')"
+if [ -n "$EXISTING" ]; then
+ echo " already open: $EXISTING"
+ exit 0
+fi
+
+echo "==> Opening merge request"
+MR_BODY="$(jq -n --arg b "$BRANCH" --arg base "$TARGET_BRANCH" --argjson up "$UPSTREAM_ID" \
+ --arg v "$VERSION" \
+ '{source_branch:$b, target_branch:$base, target_project_id:$up,
+ title:("Update RECCoon to " + $v),
+ description:("Update RECCoon to " + $v + ".\n\n- Source: https://git.tomcooks.com/reccoon\n- Signed APK: https://git.tomcooks.com/reccoon/plain/releases/RECCoon-" + $v + ".apk\n- Release tag: v" + $v)}')"
+MR_CODE="$(curl -s -o /tmp/fdroid-mr.json -w '%{http_code}' \
+ -H "PRIVATE-TOKEN: $TOKEN" -H 'Content-Type: application/json' \
+ -X POST "$API/projects/$FORK_ID/merge_requests" -d "$MR_BODY")"
+if [ "$MR_CODE" = "201" ]; then
+ echo " merge request: $(jq -r .web_url /tmp/fdroid-mr.json)"
+else
+ echo " response (HTTP $MR_CODE):" >&2
+ cat /tmp/fdroid-mr.json >&2
+ exit 1
+fi