|
| 1 | +#!/usr/bin/env bash |
| 2 | +# The guards of fork-release.yml, one subcommand each, so they can be tested without a run |
| 3 | +# (fork-release.test.sh). Every refusal exits non-zero with a ::error:: line; nothing falls back. |
| 4 | +# |
| 5 | +# resolve Preflight: picks base tag, version and tag for HEAD (key=value lines). |
| 6 | +# remove-drafts TAG Refuses a published TAG, then deletes drafts with TAG left by earlier runs. |
| 7 | +# publish ID TAG VERSION Refuses a published TAG, then publishes draft ID, marked latest only |
| 8 | +# when VERSION is above every published fork release. |
| 9 | +# check-archives DIR Every CLI archive in DIR must name REPOSITORY as its release repository. |
| 10 | +# |
| 11 | +# Environment: REPOSITORY (owner/name of this fork; GITHUB_REPOSITORY in a run), UPSTREAM |
| 12 | +# (owner/name, resolve looks it up), UPSTREAM_URL (git URL of upstream, default GitHub), ORIGIN |
| 13 | +# (git remote or URL of this fork, default origin). Output goes to $GITHUB_OUTPUT, else stdout. |
| 14 | +set -euo pipefail |
| 15 | + |
| 16 | +REPOSITORY="${REPOSITORY:-${GITHUB_REPOSITORY:?REPOSITORY or GITHUB_REPOSITORY}}" |
| 17 | +ORIGIN="${ORIGIN:-origin}" |
| 18 | +OUTPUT="${GITHUB_OUTPUT:-/dev/stdout}" |
| 19 | + |
| 20 | +die() { |
| 21 | + printf '::error::%s\n' "$*" >&2 |
| 22 | + exit 1 |
| 23 | +} |
| 24 | + |
| 25 | +stable_version_re='^v[0-9]+\.[0-9]+\.[0-9]+$' |
| 26 | +fork_version_re='^v?[0-9]+\.[0-9]+\.[0-9]+-fork\.[0-9]+$' |
| 27 | + |
| 28 | +# version_gt A B: A sorts above B. Takes X.Y.Z or X.Y.Z-fork.N (leading v allowed). A fork |
| 29 | +# release of X.Y.Z sorts above X.Y.Z itself here, which is what this fork's ordering needs. |
| 30 | +version_gt() { |
| 31 | + local a="${1#v}" b="${2#v}" i |
| 32 | + local -a x y |
| 33 | + IFS=. read -r -a x <<< "${a/-fork./.}" |
| 34 | + IFS=. read -r -a y <<< "${b/-fork./.}" |
| 35 | + for i in 0 1 2 3; do |
| 36 | + if (( 10#${x[i]:-0} != 10#${y[i]:-0} )); then |
| 37 | + (( 10#${x[i]:-0} > 10#${y[i]:-0} )) |
| 38 | + return |
| 39 | + fi |
| 40 | + done |
| 41 | + return 1 |
| 42 | +} |
| 43 | + |
| 44 | +# Prints the releases of this fork through a jq filter; a failed listing stops everything. |
| 45 | +releases() { |
| 46 | + gh api "repos/$REPOSITORY/releases" --paginate --jq "$1" \ |
| 47 | + || die "Could not list the releases of $REPOSITORY; nothing was changed." |
| 48 | +} |
| 49 | + |
| 50 | +# A published (non-draft) release or a tag with this name means the version is taken: "Re-run failed |
| 51 | +# jobs" reuses the preflight's version, and publishing again would replace a live release's files. |
| 52 | +refuse_published() { |
| 53 | + local tag="$1" published |
| 54 | + if git ls-remote --exit-code --tags "$ORIGIN" "refs/tags/$tag" > /dev/null; then |
| 55 | + die "Tag $tag already exists, so $tag is published or being published. Start a new run (Re-run all jobs) for the next number; nothing was changed." |
| 56 | + else |
| 57 | + # 2 means no such ref; anything else is a failed lookup, which must not read as "free". |
| 58 | + [[ $? == 2 ]] || die "Could not list the tags of $ORIGIN." |
| 59 | + fi |
| 60 | + published="$(releases ".[] | select((.draft | not) and .tag_name == \"$tag\") | .id")" |
| 61 | + if [[ -n "$published" ]]; then |
| 62 | + die "A published release ($published) already has tag $tag. Start a new run (Re-run all jobs); nothing was changed." |
| 63 | + fi |
| 64 | +} |
| 65 | + |
| 66 | +cmd_resolve() { |
| 67 | + local upstream="${UPSTREAM:-}" |
| 68 | + if [[ -z "$upstream" ]]; then |
| 69 | + upstream="$(gh api "repos/$REPOSITORY" --jq '.parent.full_name // empty')" |
| 70 | + [[ -n "$upstream" ]] || die "$REPOSITORY is not a fork, so there is no upstream release to build on." |
| 71 | + fi |
| 72 | + local url="${UPSTREAM_URL:-https://github.com/$upstream.git}" |
| 73 | + local head |
| 74 | + head="$(git rev-parse HEAD)" |
| 75 | + |
| 76 | + # Read-only fetch of upstream's tags and default branch into refs this run alone uses. |
| 77 | + git fetch --quiet --no-tags "$url" "+refs/tags/v*:refs/upstream-tags/v*" "+HEAD:refs/upstream-head" |
| 78 | + |
| 79 | + # Lists are read into variables first: a failure inside <(...) would go unnoticed. |
| 80 | + local refs tag |
| 81 | + local -a contained=() |
| 82 | + refs="$(git for-each-ref --format='%(refname:strip=2)' refs/upstream-tags)" |
| 83 | + for tag in $(grep -E "$stable_version_re" <<< "$refs" | sort -rV || true); do |
| 84 | + if git merge-base --is-ancestor "refs/upstream-tags/$tag^{commit}" HEAD; then |
| 85 | + contained+=("$tag") |
| 86 | + fi |
| 87 | + done |
| 88 | + (( ${#contained[@]} > 0 )) || die "$head contains no stable $upstream release tag." |
| 89 | + local base_tag="${contained[0]}" |
| 90 | + local base |
| 91 | + base="$(git rev-parse "refs/upstream-tags/$base_tag^{commit}")" |
| 92 | + |
| 93 | + # The base must be the newest stable tag in the series' history, not just the highest name: a |
| 94 | + # stray tag on an old commit (say v1.0.0) would otherwise outrank every later real release. |
| 95 | + for tag in "${contained[@]:1}"; do |
| 96 | + git merge-base --is-ancestor "refs/upstream-tags/$tag^{commit}" "$base" \ |
| 97 | + || die "$base_tag sorts highest but is not the newest stable $upstream tag in this history ($tag is not part of it). Delete or fix the stray tag; nothing was built." |
| 98 | + done |
| 99 | + |
| 100 | + # Upstream's own ordering: never above the release upstream marks latest. |
| 101 | + local upstream_latest |
| 102 | + upstream_latest="$(gh api "repos/$upstream/releases/latest" --jq '.tag_name')" |
| 103 | + [[ "$upstream_latest" =~ $stable_version_re ]] \ |
| 104 | + || die "$upstream's latest release is '$upstream_latest', not a stable vX.Y.Z tag." |
| 105 | + if version_gt "$base_tag" "$upstream_latest"; then |
| 106 | + die "$base_tag sorts above $upstream's latest release $upstream_latest. Only released upstream versions are built." |
| 107 | + fi |
| 108 | + |
| 109 | + # Releases only: every commit after the base tag must be the fork's own. Upstream commits there |
| 110 | + # are unreleased upstream code (for example a branch that still equals upstream's default branch). |
| 111 | + local total own |
| 112 | + total="$(git rev-list --count "$base..HEAD")" |
| 113 | + own="$(git rev-list --count "$base..HEAD" --not refs/upstream-head)" |
| 114 | + if [[ "$total" != "$own" ]]; then |
| 115 | + die "$head holds $((total - own)) unreleased $upstream commits after $base_tag. Build a branch of $base_tag plus the fork's own commits." |
| 116 | + fi |
| 117 | + |
| 118 | + local version="${base_tag#v}" last |
| 119 | + last="$(git ls-remote --tags --refs "$ORIGIN" "v$version-fork.*" \ |
| 120 | + | sed -n "s|.*refs/tags/v$version-fork\.\([0-9]\+\)$|\1|p" | sort -n | tail -n1)" |
| 121 | + version="$version-fork.$(( ${last:-0} + 1 ))" |
| 122 | + { |
| 123 | + echo "ref=$head" |
| 124 | + echo "upstream=$upstream" |
| 125 | + echo "base_tag=$base_tag" |
| 126 | + echo "version=$version" |
| 127 | + echo "tag=v$version" |
| 128 | + } >> "$OUTPUT" |
| 129 | + echo "Building $version from $(git rev-parse --short HEAD) on $upstream $base_tag." \ |
| 130 | + | tee -a "${GITHUB_STEP_SUMMARY:-/dev/null}" >&2 |
| 131 | +} |
| 132 | + |
| 133 | +cmd_remove_drafts() { |
| 134 | + local tag="${1:?tag}" id |
| 135 | + refuse_published "$tag" |
| 136 | + local ids |
| 137 | + ids="$(releases ".[] | select(.draft and .tag_name == \"$tag\") | .id")" |
| 138 | + for id in $ids; do |
| 139 | + gh api -X DELETE "repos/$REPOSITORY/releases/$id" > /dev/null |
| 140 | + echo "Removed leftover draft $id for $tag." |
| 141 | + done |
| 142 | + [[ -n "$ids" ]] || echo "No leftover draft for $tag." |
| 143 | +} |
| 144 | + |
| 145 | +cmd_publish() { |
| 146 | + local id="${1:?release id}" tag="${2:?tag}" version="${3:?version}" |
| 147 | + [[ "$version" =~ $fork_version_re ]] || die "'$version' is not a fork version." |
| 148 | + refuse_published "$tag" |
| 149 | + # Latest only when above every published fork release: building an older ref must not move |
| 150 | + # /releases/latest backwards, where the desktop updater would then find nothing newer. |
| 151 | + local latest=true other published |
| 152 | + published="$(releases '.[] | select((.draft or .prerelease) | not) | .tag_name')" |
| 153 | + for other in $published; do |
| 154 | + [[ "$other" =~ $fork_version_re ]] || continue |
| 155 | + if ! version_gt "$version" "$other"; then |
| 156 | + latest=false |
| 157 | + echo "$other is published and not below $version, so $tag is not marked latest." |
| 158 | + break |
| 159 | + fi |
| 160 | + done |
| 161 | + gh api -X PATCH "repos/$REPOSITORY/releases/$id" -F draft=false -f make_latest="$latest" > /dev/null |
| 162 | + echo "Published $tag (latest: $latest)." |
| 163 | + { |
| 164 | + echo "published=true" |
| 165 | + echo "latest=$latest" |
| 166 | + } >> "$OUTPUT" |
| 167 | +} |
| 168 | + |
| 169 | +cmd_check_archives() { |
| 170 | + local dir="${1:?directory}" archive work found=0 |
| 171 | + shopt -s nullglob |
| 172 | + for archive in "$dir"/t3-*.tar.gz "$dir"/t3-*.zip; do |
| 173 | + found=1 |
| 174 | + work="$(mktemp -d)" |
| 175 | + case "$archive" in |
| 176 | + *.tar.gz) tar -xzf "$archive" -C "$work" ;; |
| 177 | + *.zip) python3 -I -m zipfile -e "$archive" "$work" ;; |
| 178 | + esac |
| 179 | + # The `t3` executable bundles the server again (release-desktop.yml build-exe), so this checks |
| 180 | + # the code that ships rather than the separate JS bundle. |
| 181 | + grep -rqaF "\"$REPOSITORY\"" "$work" \ |
| 182 | + || die "$(basename "$archive") does not name $REPOSITORY as its release repository, so it would update from elsewhere." |
| 183 | + rm -rf "$work" |
| 184 | + echo "$(basename "$archive") names $REPOSITORY." |
| 185 | + done |
| 186 | + (( found )) || die "No CLI archive in $dir." |
| 187 | +} |
| 188 | + |
| 189 | +command="${1:-}" |
| 190 | +shift || true |
| 191 | +case "$command" in |
| 192 | + resolve) cmd_resolve "$@" ;; |
| 193 | + remove-drafts) cmd_remove_drafts "$@" ;; |
| 194 | + publish) cmd_publish "$@" ;; |
| 195 | + check-archives) cmd_check_archives "$@" ;; |
| 196 | + version-gt) version_gt "$@" ;; |
| 197 | + *) die "usage: $0 resolve | remove-drafts TAG | publish ID TAG VERSION | check-archives DIR" ;; |
| 198 | +esac |
0 commit comments