From cbfbfef0bb679317d407dbbf368d9323dcc31834 Mon Sep 17 00:00:00 2001 From: Agent Date: Sun, 5 Apr 2026 17:50:58 +0000 Subject: [PATCH] =?UTF-8?q?fix:=20fix:=20hire-an-agent=20branch=20protecti?= =?UTF-8?q?on=20fails=20=E2=80=94=20race=20condition=20after=20initial=20p?= =?UTF-8?q?ush=20(#238)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/branch-protection.sh | 84 +++++++++++++++++++++++++++++++--------- 1 file changed, 66 insertions(+), 18 deletions(-) diff --git a/lib/branch-protection.sh b/lib/branch-protection.sh index b0ed481..e972977 100644 --- a/lib/branch-protection.sh +++ b/lib/branch-protection.sh @@ -51,14 +51,30 @@ setup_vault_branch_protection() { _bp_log "Setting up branch protection for ${branch} on ${FORGE_OPS_REPO}" - # Check if branch exists - local branch_exists - branch_exists=$(curl -s -o /dev/null -w "%{http_code}" \ - -H "Authorization: token ${FORGE_TOKEN}" \ - "${api_url}/git/branches/${branch}" 2>/dev/null || echo "0") + # Check if branch exists with retry loop (handles race condition after initial push) + local branch_exists="0" + local max_attempts=3 + local attempt=1 + + while [ "$attempt" -le "$max_attempts" ]; do + branch_exists=$(curl -s -o /dev/null -w "%{http_code}" \ + -H "Authorization: token ${FORGE_TOKEN}" \ + "${api_url}/git/branches/${branch}" 2>/dev/null || echo "0") + + if [ "$branch_exists" = "200" ]; then + _bp_log "Branch ${branch} exists on ${FORGE_OPS_REPO}" + break + fi + + if [ "$attempt" -lt "$max_attempts" ]; then + _bp_log "Branch ${branch} not indexed yet (attempt ${attempt}/${max_attempts}), waiting 2s..." + sleep 2 + fi + attempt=$((attempt + 1)) + done if [ "$branch_exists" != "200" ]; then - _bp_log "ERROR: Branch ${branch} does not exist" + _bp_log "ERROR: Branch ${branch} does not exist on ${FORGE_OPS_REPO} after ${max_attempts} attempts" return 1 fi @@ -228,14 +244,30 @@ setup_profile_branch_protection() { local api_url api_url="${FORGE_URL}/api/v1/repos/${repo}" - # Check if branch exists - local branch_exists - branch_exists=$(curl -s -o /dev/null -w "%{http_code}" \ - -H "Authorization: token ${FORGE_TOKEN}" \ - "${api_url}/git/branches/${branch}" 2>/dev/null || echo "0") + # Check if branch exists with retry loop (handles race condition after initial push) + local branch_exists="0" + local max_attempts=3 + local attempt=1 + + while [ "$attempt" -le "$max_attempts" ]; do + branch_exists=$(curl -s -o /dev/null -w "%{http_code}" \ + -H "Authorization: token ${FORGE_TOKEN}" \ + "${api_url}/git/branches/${branch}" 2>/dev/null || echo "0") + + if [ "$branch_exists" = "200" ]; then + _bp_log "Branch ${branch} exists on ${repo}" + break + fi + + if [ "$attempt" -lt "$max_attempts" ]; then + _bp_log "Branch ${branch} not indexed yet (attempt ${attempt}/${max_attempts}), waiting 2s..." + sleep 2 + fi + attempt=$((attempt + 1)) + done if [ "$branch_exists" != "200" ]; then - _bp_log "ERROR: Branch ${branch} does not exist on ${repo}" + _bp_log "ERROR: Branch ${branch} does not exist on ${repo} after ${max_attempts} attempts" return 1 fi @@ -398,14 +430,30 @@ setup_project_branch_protection() { local api_url api_url="${FORGE_URL}/api/v1/repos/${repo}" - # Check if branch exists - local branch_exists - branch_exists=$(curl -s -o /dev/null -w "%{http_code}" \ - -H "Authorization: token ${FORGE_TOKEN}" \ - "${api_url}/git/branches/${branch}" 2>/dev/null || echo "0") + # Check if branch exists with retry loop (handles race condition after initial push) + local branch_exists="0" + local max_attempts=3 + local attempt=1 + + while [ "$attempt" -le "$max_attempts" ]; do + branch_exists=$(curl -s -o /dev/null -w "%{http_code}" \ + -H "Authorization: token ${FORGE_TOKEN}" \ + "${api_url}/git/branches/${branch}" 2>/dev/null || echo "0") + + if [ "$branch_exists" = "200" ]; then + _bp_log "Branch ${branch} exists on ${repo}" + break + fi + + if [ "$attempt" -lt "$max_attempts" ]; then + _bp_log "Branch ${branch} not indexed yet (attempt ${attempt}/${max_attempts}), waiting 2s..." + sleep 2 + fi + attempt=$((attempt + 1)) + done if [ "$branch_exists" != "200" ]; then - _bp_log "ERROR: Branch ${branch} does not exist on ${repo}" + _bp_log "ERROR: Branch ${branch} does not exist on ${repo} after ${max_attempts} attempts" return 1 fi