From 581ccba72e5730ce17e1871f42ae03dd03eeddcd Mon Sep 17 00:00:00 2001 From: Joakim Olsson Date: Mon, 23 Mar 2026 16:51:47 +0100 Subject: [PATCH] fix: make release PR creation robust against race conditions Two fixes: - Use BASE_BRANCH (with fallback) instead of DEFAULT_BRANCH for the PR base field, preventing empty base causing 404 - Replace fixed sleep with branch readiness poll (up to 10 attempts) and add retry loop for PR creation (up to 5 attempts), fixing intermittent 404 when Gitea hasn't indexed the branch yet Co-Authored-By: Claude Opus 4.6 (1M context) --- .gitea/workflows/Release.yml | 64 +++++++++++++++++++++++++----------- 1 file changed, 44 insertions(+), 20 deletions(-) diff --git a/.gitea/workflows/Release.yml b/.gitea/workflows/Release.yml index 72695cc..5a2e115 100644 --- a/.gitea/workflows/Release.yml +++ b/.gitea/workflows/Release.yml @@ -189,27 +189,51 @@ jobs: fi echo "Creating new PR..." - echo "Waiting for branch to be ready..." - sleep 3 + echo "Waiting for next-release branch to be ready..." + for i in $(seq 1 10); do + BRANCH_STATUS=$(curl -s --retry 3 --retry-delay 2 --retry-connrefused \ + -w "%{http_code}" -o /dev/null \ + -H "Authorization: token ${TOKEN}" \ + "${API_URL}/branches/next-release") + if [ "${BRANCH_STATUS}" = "200" ]; then + echo "Branch ready after ${i} attempt(s)" + break + fi + if [ "${i}" = "10" ]; then + echo "Branch next-release not found after 10 attempts, giving up" + exit 1 + fi + echo "Branch not ready yet (attempt ${i}/10), waiting..." + sleep 3 + done - RESPONSE=$(curl -s --retry 3 --retry-delay 3 --retry-all-errors --retry-connrefused \ - -w "\n%{http_code}" -X POST \ - -H "Authorization: token ${TOKEN}" \ - -H "Content-Type: application/json" \ - --data "$(jq -n \ - --arg title "${TITLE}" \ - --arg body "${DESCRIPTION}" \ - --arg head "next-release" \ - --arg base "${DEFAULT_BRANCH}" \ - '{title: $title, body: $body, head: $head, base: $base}')" \ - "${API_URL}/pulls") - HTTP_CODE=$(echo "${RESPONSE}" | tail -1) - BODY=$(echo "${RESPONSE}" | sed '$d') - if [ "${HTTP_CODE}" -ge 400 ]; then - echo "Error creating PR (HTTP ${HTTP_CODE}): ${BODY}" - exit 1 - fi - echo "PR created successfully" + PR_DATA=$(jq -n \ + --arg title "${TITLE}" \ + --arg body "${DESCRIPTION}" \ + --arg head "next-release" \ + --arg base "${BASE_BRANCH}" \ + '{title: $title, body: $body, head: $head, base: $base}') + + for i in $(seq 1 5); do + RESPONSE=$(curl -s --retry 3 --retry-delay 2 --retry-connrefused \ + -w "\n%{http_code}" -X POST \ + -H "Authorization: token ${TOKEN}" \ + -H "Content-Type: application/json" \ + --data "${PR_DATA}" \ + "${API_URL}/pulls") + HTTP_CODE=$(echo "${RESPONSE}" | tail -1) + BODY=$(echo "${RESPONSE}" | sed '$d') + if [ "${HTTP_CODE}" -lt 400 ]; then + echo "PR created successfully" + break + fi + if [ "${i}" = "5" ]; then + echo "Error creating PR after 5 attempts (HTTP ${HTTP_CODE}): ${BODY}" + exit 1 + fi + echo "PR creation attempt ${i}/5 failed (HTTP ${HTTP_CODE}), retrying..." + sleep 3 + done create-release: name: Create Release -- 2.52.0