Per operator: opening a new upgrade PR should stack ON TOP of any other still-open PRs, not close them. Only PRs already merged into upstream main are closed (merging them is a no-op). This prevents the phase-7 incident where an unrelated open ghost PR was auto-closed as 'superseded'. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
163 lines
8.1 KiB
Bash
Executable File
163 lines
8.1 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
# recipe-upgrade :: reconcile the git.autonomic.zone mirror + open a recipe PR
|
|
# (adapted from recipe-maintainer's recipe-create-pr for the cc-ci context).
|
|
# -------------------------------------------------------------------------
|
|
# RUNS ON cc-ci (it has the recipe checkout at ~/.abra/recipes/<recipe> and the
|
|
# bot token). Invoke from the orchestrator, feeding creds + the script over ssh:
|
|
#
|
|
# set -a; . /srv/cc-ci/.testenv; set +a
|
|
# ssh cc-ci "GITEA_USERNAME='$GITEA_USERNAME' GITEA_PASSWORD='$GITEA_PASSWORD' \
|
|
# GITEA_URL='$GITEA_URL' bash -s <recipe> [--reconcile-only]" \
|
|
# < /srv/cc-ci/.claude/skills/recipe-upgrade/open-recipe-pr.sh
|
|
#
|
|
# Always (both modes):
|
|
# - Force-syncs the mirror's `main` to be IDENTICAL to true upstream `main`
|
|
# (origin/main of the abra checkout = git.coopcloud.tech). This guarantees
|
|
# every open PR is measured against the real current upstream.
|
|
# - Closes any open mirror PR whose changes are ALREADY IN upstream main
|
|
# (i.e. merging it is a no-op) — it was merged upstream; the mirror just
|
|
# never reflected it.
|
|
#
|
|
# Default mode (open a PR — preconditions: HEAD has the upgrade commit(s)):
|
|
# - Pushes the upgrade branch and opens the new PR ALONGSIDE any other still-open
|
|
# PRs for this recipe. It does NOT close superseded/unrelated open PRs — stacking
|
|
# a fresh PR on top of the open ones is intentional; the operator decides which to
|
|
# merge/close. (Only PRs already merged into upstream main are closed — see above.)
|
|
#
|
|
# --reconcile-only: just sync main + close merged-upstream PRs; no push, no new
|
|
# PR (used when a recipe is up to date but its mirror may be stale).
|
|
#
|
|
# NEVER merges anything.
|
|
set -o errexit -o nounset -o pipefail
|
|
|
|
RECIPE="${1:?usage: open-recipe-pr.sh <recipe> [--reconcile-only]}"
|
|
MODE="${2:-open}"
|
|
: "${GITEA_USERNAME:?missing GITEA_USERNAME (pass via ssh env)}"
|
|
: "${GITEA_PASSWORD:?missing GITEA_PASSWORD}"
|
|
: "${GITEA_URL:?missing GITEA_URL}"
|
|
NAMESPACE="${GITEA_NAMESPACE:-recipe-maintainers}"
|
|
RECIPE_DIR="${HOME}/.abra/recipes/${RECIPE}"
|
|
|
|
export PATH="/run/current-system/sw/bin:${PATH}"
|
|
PASS_ENC=$(printf '%s' "${GITEA_PASSWORD}" | jq -Rr '@uri')
|
|
API="https://${GITEA_URL}/api/v1"
|
|
AUTH=(-u "${GITEA_USERNAME}:${GITEA_PASSWORD}")
|
|
|
|
[ -d "${RECIPE_DIR}/.git" ] || { echo "ERROR: ${RECIPE_DIR} is not a git repo (run 'abra recipe fetch ${RECIPE}')"; exit 1; }
|
|
cd "${RECIPE_DIR}"
|
|
|
|
# --- The true current upstream main ---
|
|
echo "→ Fetching upstream main..."
|
|
git fetch --quiet origin main
|
|
NEW_MAIN_SHA=$(git rev-parse refs/remotes/origin/main)
|
|
MAIN_TREE=$(git rev-parse "${NEW_MAIN_SHA}^{tree}")
|
|
|
|
# --- Ensure the mirror repo exists ---
|
|
STATUS=$(curl -s -o /dev/null -w "%{http_code}" "${AUTH[@]}" "${API}/repos/${NAMESPACE}/${RECIPE}")
|
|
if [ "${STATUS}" = "404" ]; then
|
|
if [ "${MODE}" = "--reconcile-only" ]; then
|
|
echo "→ Mirror ${NAMESPACE}/${RECIPE} does not exist; nothing to reconcile."; exit 0
|
|
fi
|
|
echo "→ Mirror ${NAMESPACE}/${RECIPE} missing; creating..."
|
|
BODY=$(jq -n --arg name "${RECIPE}" '{name:$name,private:true,default_branch:"main",auto_init:false}')
|
|
CS=$(curl -s -o /dev/null -w "%{http_code}" "${AUTH[@]}" -H "Content-Type: application/json" -X POST "${API}/orgs/${NAMESPACE}/repos" -d "${BODY}")
|
|
if [ "${CS}" != "201" ]; then
|
|
CS=$(curl -s -o /dev/null -w "%{http_code}" "${AUTH[@]}" -H "Content-Type: application/json" -X POST "${API}/user/repos" -d "${BODY}")
|
|
[ "${CS}" = "201" ] || { echo "ERROR: repo create failed (HTTP ${CS})"; exit 1; }
|
|
NAMESPACE="${GITEA_USERNAME}"
|
|
fi
|
|
echo " ✓ created ${NAMESPACE}/${RECIPE}"
|
|
elif [ "${STATUS}" != "200" ]; then
|
|
echo "ERROR: unexpected HTTP ${STATUS} checking mirror repo"; exit 1
|
|
fi
|
|
|
|
REMOTE_URL="https://${GITEA_USERNAME}:${PASS_ENC}@${GITEA_URL}/${NAMESPACE}/${RECIPE}.git"
|
|
git remote | grep -qx gitea && git remote set-url gitea "${REMOTE_URL}" || git remote add gitea "${REMOTE_URL}"
|
|
|
|
# --- Mirror main := true upstream main (identical) ---
|
|
echo "→ Force-syncing gitea/main to upstream main (${NEW_MAIN_SHA:0:8})..."
|
|
git push --force gitea "${NEW_MAIN_SHA}:refs/heads/main"
|
|
git fetch --quiet gitea '+refs/heads/*:refs/remotes/gitea/*' || true
|
|
|
|
# --- Determine the branch we're about to (re)open, if in open mode ---
|
|
BRANCH=""
|
|
if [ "${MODE}" != "--reconcile-only" ]; then
|
|
DIVERGED=$(git log --oneline origin/main..HEAD 2>/dev/null || true)
|
|
[ -n "${DIVERGED}" ] || { echo "ERROR: HEAD has no commits beyond origin/main. Nothing to PR."; exit 1; }
|
|
LATEST_MSG=$(git log -1 --pretty=%s HEAD)
|
|
if echo "${LATEST_MSG}" | grep -qiE "upgrade to [0-9]"; then
|
|
VERSION=$(echo "${LATEST_MSG}" | grep -oiE "upgrade to [0-9][^[:space:]]+" | awk '{print $NF}')
|
|
BRANCH="upgrade-${VERSION}"
|
|
else
|
|
BRANCH="upgrade-$(git rev-parse --short HEAD)"
|
|
fi
|
|
fi
|
|
|
|
# --- Reconcile open PRs against the freshly-synced upstream main ---
|
|
close_pr() { # <index> <reason>
|
|
local idx="$1" reason="$2"
|
|
curl -s -o /dev/null "${AUTH[@]}" -H "Content-Type: application/json" -X POST \
|
|
"${API}/repos/${NAMESPACE}/${RECIPE}/issues/${idx}/comments" \
|
|
-d "$(jq -n --arg body "Auto-closed by /recipe-upgrade: ${reason}" '{body:$body}')" || true
|
|
curl -s -o /dev/null "${AUTH[@]}" -H "Content-Type: application/json" -X PATCH \
|
|
"${API}/repos/${NAMESPACE}/${RECIPE}/pulls/${idx}" -d '{"state":"closed"}' || true
|
|
echo " ✗ closed PR #${idx} — ${reason}"
|
|
}
|
|
|
|
echo "→ Reconciling open PRs on ${NAMESPACE}/${RECIPE}..."
|
|
OPEN_PRS=$(curl -s "${AUTH[@]}" "${API}/repos/${NAMESPACE}/${RECIPE}/pulls?state=open&base=main&limit=50")
|
|
# Emit "index<TAB>head_ref" per open PR.
|
|
while IFS=$'\t' read -r IDX HEAD_REF; do
|
|
[ -n "${IDX:-}" ] || continue
|
|
# Same branch we're re-opening? leave it — the force-push below updates it.
|
|
if [ -n "${BRANCH}" ] && [ "${HEAD_REF}" = "${BRANCH}" ]; then
|
|
echo " • PR #${IDX} is on ${BRANCH} (the branch being updated) — keeping"
|
|
continue
|
|
fi
|
|
# Is this PR already a no-op against upstream main (merged upstream)?
|
|
merged=0
|
|
if git rev-parse --verify --quiet "refs/remotes/gitea/${HEAD_REF}" >/dev/null; then
|
|
mt=$(git merge-tree --write-tree "${NEW_MAIN_SHA}" "refs/remotes/gitea/${HEAD_REF}" 2>/dev/null) || mt=""
|
|
[ -n "${mt}" ] && [ "${mt}" = "${MAIN_TREE}" ] && merged=1
|
|
fi
|
|
if [ "${merged}" = "1" ]; then
|
|
close_pr "${IDX}" "its changes are already in upstream main (merged upstream); mirror main re-synced"
|
|
else
|
|
# Do NOT auto-close superseded/unrelated open PRs. Open the new PR alongside them and
|
|
# let the operator decide which to merge/close. Only merged-upstream PRs are closed above.
|
|
echo " • PR #${IDX} (${HEAD_REF}) still open vs upstream main — leaving open (operator decides)"
|
|
fi
|
|
done < <(echo "${OPEN_PRS}" | jq -r '.[] | [(.number | tostring), .head.ref] | @tsv' 2>/dev/null || true)
|
|
|
|
if [ "${MODE}" = "--reconcile-only" ]; then
|
|
echo "✓ reconcile-only done for ${NAMESPACE}/${RECIPE} (main synced; merged-upstream PRs closed)."
|
|
exit 0
|
|
fi
|
|
|
|
# --- Push the upgrade branch and open the new PR in place of the closed ones ---
|
|
echo "→ Pushing branch '${BRANCH}'..."
|
|
git push --force gitea "HEAD:refs/heads/${BRANCH}"
|
|
|
|
PR_BODY="${RECIPE_PR_BODY:-$(printf 'Recipe upgrade.\n\nCommits on top of upstream main:\n\n%s\n' "$(git log origin/main..HEAD --pretty='- %h %s')")}"
|
|
PR_BODY="${PR_BODY}
|
|
|
|
Tested green on the cc-ci recipe CI server (full suite, cold, against this PR head). NOT merged — for operator review.
|
|
|
|
cc @trav @notplants"
|
|
PAYLOAD=$(jq -n \
|
|
--arg title "${LATEST_MSG}" \
|
|
--arg body "${PR_BODY}" \
|
|
--arg head "${BRANCH}" \
|
|
'{title:$title,body:$body,head:$head,base:"main",reviewers:["trav","notplants"]}')
|
|
|
|
RESP=$(mktemp)
|
|
PS=$(curl -s -o "${RESP}" -w "%{http_code}" "${AUTH[@]}" -H "Content-Type: application/json" -X POST "${API}/repos/${NAMESPACE}/${RECIPE}/pulls" -d "${PAYLOAD}")
|
|
if [ "${PS}" = "201" ]; then
|
|
echo "PR_URL=$(jq -r '.html_url' "${RESP}")"
|
|
elif [ "${PS}" = "409" ] || grep -q "pull request already exists" "${RESP}" 2>/dev/null; then
|
|
echo "PR_URL=https://${GITEA_URL}/${NAMESPACE}/${RECIPE}/pulls (branch ${BRANCH} already has a PR — updated by force-push)"
|
|
else
|
|
echo "ERROR: PR creation failed (HTTP ${PS}):"; cat "${RESP}"; rm -f "${RESP}"; exit 1
|
|
fi
|
|
rm -f "${RESP}"
|