From 5ac7516672e27e66da94dfbab6d68e3a58373199 Mon Sep 17 00:00:00 2001 From: moilanik Date: Sun, 14 Jun 2026 09:16:37 +0300 Subject: [PATCH] fix(retention): add retry on Gitea API errors - Retry up to 3 times (2 retries) on non-200/non-404 responses - 10 second delay between retries - Fail-safe: keep report if all retries fail --- git-pages/files/retention-cleanup.sh | 48 ++++++++++++++++------------ 1 file changed, 28 insertions(+), 20 deletions(-) diff --git a/git-pages/files/retention-cleanup.sh b/git-pages/files/retention-cleanup.sh index 8398439..82bb30b 100644 --- a/git-pages/files/retention-cleanup.sh +++ b/git-pages/files/retention-cleanup.sh @@ -16,33 +16,41 @@ curl_with_host() { declare -A BRANCH_CACHE branch_exists() { local owner="$1" repo="$2" branch="$3" key="${owner}/${repo}/${branch}" - local status - + local status attempt + [ -z "$GITEA_API_URL" ] && return 0 [ -z "$GITEA_TOKEN" ] && return 0 - + if [ "${BRANCH_CACHE[$key]:-}" = "1" ]; then return 0 fi - - status=$(curl -sS -o /dev/null -w "%{http_code}" \ - -H "Authorization: token ${GITEA_TOKEN}" \ - "${GITEA_API_URL}/api/v1/repos/${owner}/${repo}/branches/${branch}" 2>/dev/null || echo "000") - - if [ "$status" = "200" ]; then - BRANCH_CACHE[$key]=1 - return 0 - fi - # API error or branch not found - log warning and keep report (fail-safe) - if [ "$status" != "404" ]; then - echo " WARN: Gitea API error for ${owner}/${repo}/${branch} (status ${status}) - KEEPING report" - BRANCH_CACHE[$key]=1 # cache as "exists" to avoid repeated errors - return 0 - fi + # Retry up to 2 times on API errors (hardcoded) + for attempt in 1 2 3; do + status=$(curl -sS -o /dev/null -w "%{http_code}" \ + -H "Authorization: token ${GITEA_TOKEN}" \ + "${GITEA_API_URL}/api/v1/repos/${owner}/${repo}/branches/${branch}" 2>/dev/null || echo "000") + + if [ "$status" = "200" ]; then + BRANCH_CACHE[$key]=1 + return 0 + fi + + if [ "$status" = "404" ]; then + return 1 + fi + + # API error - retry if not last attempt + if [ "$attempt" -lt 3 ]; then + sleep 10 + continue + fi + done - # Actual 404 - branch truly doesn't exist - return 1 + # All retries failed - keep report (fail-safe) + echo " WARN: Gitea API error for ${owner}/${repo}/${branch} (status ${status}) after 3 attempts - KEEPING report" + BRANCH_CACHE[$key]=1 + return 0 } default_max_age=$(jq -r '.branches.default.maxAgeDays // 90' "$CONFIG")