chadtmiller.com/dual-forge-test
Test: one main on two forges
0
0
#!/usr/bin/env bash
# Lands one pull, opened on either forge, on both mains as one commit:
#
# bot/land.sh github <number>
# bot/land.sh knotless <at-uri>
#
# The bot is the only writer of main on both forges. It takes a lock so
# lands run one at a time, checks both mains name the same commit, squashes
# the pull's head onto it, runs ./ci.sh on the result, pushes that commit to
# both mains (a plain push: refused if either main moved), then closes the
# pull with the commit. A pull that conflicts or fails CI is told so and
# left open.
set -eEuo pipefail
trap 'echo "land.sh: failed at line $LINENO: $BASH_COMMAND" >&2' ERR
GH_REPO=${GH_REPO:-bigmoves/dual-forge-test}
KN_REPO=${KN_REPO:-knotless://chadtmiller.com/dual-forge-test}
WORK=${BOT_WORK:-$HOME/.cache/dual-forge-bot}
forge=${1:?usage: bot/land.sh github <number> | knotless <at-uri>}
id=${2:?usage: bot/land.sh github <number> | knotless <at-uri>}
# One land at a time, across every forge: mkdir is atomic.
mkdir -p "$WORK"
lock=$WORK/lock
until mkdir "$lock" 2>/dev/null; do sleep 1; done
trap 'rm -rf "$lock"' EXIT
repo=$WORK/repo
if [ ! -d "$repo" ]; then
git init -q -b main "$repo"
git -C "$repo" remote add gh "https://github.com/$GH_REPO.git"
git -C "$repo" remote add kn "$KN_REPO"
fi
cd "$repo"
git config user.name "dual-forge bot"
git config user.email "bot@dual-forge.invalid"
# The pull: its title, body, link, and where its head is.
case $forge in
github)
pr=$(gh pr view "$id" -R "$GH_REPO" --json state,title,body,url)
[ "$(jq -r .state <<<"$pr")" = OPEN ] || { echo "github #$id is not open" >&2; exit 1; }
title=$(jq -r .title <<<"$pr"); body=$(jq -r .body <<<"$pr"); url=$(jq -r .url <<<"$pr")
# The base repository holds every pull's head, forks' too.
git fetch -q gh "+refs/pull/$id/head:refs/land/head"
say() { gh pr comment "$id" -R "$GH_REPO" -b "$1" >/dev/null; }
close() { gh pr close "$id" -R "$GH_REPO" -c "$1" >/dev/null; }
;;
knotless)
author=${id#at://}; author=${author%%/*}; rkey=${id##*/}
case $author in
did:plc:*) doc=https://plc.directory/$author ;;
did:web:*) doc=https://${author#did:web:}/.well-known/did.json ;;
*) echo "$id: author is no did:plc or did:web" >&2; exit 1 ;;
esac
pds=$(curl -fsS "$doc" | jq -r '.service[] | select(.id | endswith("#atproto_pds")) | .serviceEndpoint')
record=$(curl -fsS "$pds/xrpc/com.atproto.repo.getRecord?repo=$author&collection=dev.knotless.pull&rkey=$rkey")
show=$(knotless pull show "$KN_REPO" "$id")
case $show in open\ *) ;; *) echo "knotless $id is not open" >&2; exit 1 ;; esac
title=$(jq -r .value.title <<<"$record"); body=$(jq -r '.value.body // ""' <<<"$record")
url="$KN_REPO (pull $id)"
# at://<owner>/dev.knotless.repo/<name> is knotless://<owner>/<name>.
head_repo=$(jq -r .value.head.repo.uri <<<"$record")
owner=${head_repo#at://}; owner=${owner%%/*}
git fetch -q "knotless://$owner/${head_repo##*/}" "+refs/heads/$(jq -r .value.head.branch <<<"$record"):refs/land/head"
say() { knotless comment add "$KN_REPO" "$id" "$1" >/dev/null; }
close() { say "$1"; knotless pull close "$KN_REPO" "$id" >/dev/null; }
;;
*) echo "forge is github or knotless, not $forge" >&2; exit 2 ;;
esac
# Both mains must name the same commit: the bot is their only writer, so
# anything else is a push that went around it.
git fetch -q gh +main:refs/land/gh-main
git fetch -q kn +main:refs/land/kn-main
base=$(git rev-parse refs/land/gh-main)
if [ "$base" != "$(git rev-parse refs/land/kn-main)" ]; then
echo "mains differ: github $base, knotless $(git rev-parse refs/land/kn-main); landing nothing" >&2
exit 1
fi
# Squash the head onto main, as its last commit's author.
git checkout -q --detach "$base"
git reset -q --hard
if ! git merge -q --squash refs/land/head >/dev/null 2>&1; then
git reset -q --hard
say "Not landed: it conflicts with main at ${base:0:7}. Merge main into it and ask again."
echo "conflicts with main" >&2; exit 1
fi
if git diff --cached --quiet; then
say "Not landed: it changes nothing on main at ${base:0:7}."
echo "nothing to land" >&2; exit 1
fi
who=$(git log -1 --format='%an <%ae>' refs/land/head)
msg=$(printf '%s\n\n%s\n\nPull: %s\n' "$title" "$body" "$url")
git commit -q --author="$who" -m "$msg"
sha=$(git rev-parse HEAD)
if ! out=$(./ci.sh 2>&1); then
say "$(printf 'Not landed: CI failed on it squashed onto main at %s.\n\n```\n%s\n```' "${base:0:7}" "$out")"
echo "ci failed: $out" >&2; exit 1
fi
# Knotless first, then GitHub. A refused push means a main moved under the
# bot; the first refused lands nothing. A failure between the two leaves
# knotless ahead, which the next land refuses on until GitHub is pushed.
git push -q kn "$sha:refs/heads/main"
for try in 1 2 3; do
git push -q gh "$sha:refs/heads/main" && break
[ "$try" = 3 ] && { echo "knotless main is $sha, github's push failed: push it to github by hand" >&2; exit 1; }
sleep $((try * 5))
done
close "Landed on both mains as $sha."
echo "$forge $id landed as $sha"