summaryrefslogtreecommitdiff
path: root/tools/update-fdroid.sh
blob: 35003482d65a76ee4aab25577a5475a76badb64d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
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