#!/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/ 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 [--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)): # - Additionally closes any OTHER still-open PR for this recipe (superseded), # then pushes the upgrade branch and opens the new PR IN ITS PLACE. # # --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 [--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=$(python3 -c "import urllib.parse,sys;print(urllib.parse.quote(sys.argv[1],safe=''))" "${GITEA_PASSWORD}") 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=$(python3 -c "import json;print(json.dumps({'name':'${RECIPE}','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() { # 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 "$(python3 -c "import json,sys;print(json.dumps({'body':sys.argv[1]}))" "Auto-closed by /recipe-upgrade: ${reason}")" || 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 "indexhead_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" elif [ -n "${BRANCH}" ]; then close_pr "${IDX}" "superseded by ${BRANCH} (fresh upgrade against current upstream main)" else echo " • PR #${IDX} (${HEAD_REF}) still open vs upstream main — left as-is (reconcile-only)" fi done < <(echo "${OPEN_PRS}" | python3 -c " import json,sys try: data=json.load(sys.stdin) except Exception: data=[] for pr in data: print(f\"{pr['number']}\t{pr['head']['ref']}\") ") 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=$(python3 -c " import json,sys print(json.dumps({'title':sys.argv[1],'body':sys.argv[2],'head':sys.argv[3],'base':'main','reviewers':['trav','notplants']}))" \ "${LATEST_MSG}" "${PR_BODY}" "${BRANCH}") 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=$(python3 -c "import json;print(json.load(open('${RESP}'))['html_url'])")" 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}"