From 7e17aa1b859b7cd86b480775711ada3fa5edf250 Mon Sep 17 00:00:00 2001 From: Lectom C Han Date: Wed, 17 Dec 2025 09:44:24 +0900 Subject: [PATCH] =?UTF-8?q?gitea=20API=20=EC=98=A4=EB=A5=98=EC=8B=9C=20?= =?UTF-8?q?=EC=9E=AC=EC=8B=9C=EB=8F=84=20=EB=A1=9C=EC=A7=81=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitea/workflows/mirror.yml | 50 ++++++++++++++++++++++++++++++------- README.md | 1 + to-do.md | 2 ++ 3 files changed, 44 insertions(+), 9 deletions(-) diff --git a/.gitea/workflows/mirror.yml b/.gitea/workflows/mirror.yml index 6b084ec..1ed9879 100644 --- a/.gitea/workflows/mirror.yml +++ b/.gitea/workflows/mirror.yml @@ -91,10 +91,21 @@ jobs: set_default_branch_main() { local repo_name="$1" - local response http_status body - response=$(curl -s -w "\n%{http_code}" -X PATCH -H "Content-Type: application/json" -H "${AUTH_HEADER}" -d "{\"default_branch\":\"main\"}" "${BASE_GITEA_URL}/api/v1/repos/${CENTER_ORG}/${repo_name}") - http_status=$(echo "${response}" | tail -n1) - body=$(echo "${response}" | sed '$d') + local response http_status body attempt max_api_retry + max_api_retry=3 + attempt=1 + while (( attempt<=max_api_retry )); do + response=$(curl -s -w "\n%{http_code}" -X PATCH -H "Content-Type: application/json" -H "${AUTH_HEADER}" -d "{\"default_branch\":\"main\"}" "${BASE_GITEA_URL}/api/v1/repos/${CENTER_ORG}/${repo_name}") + http_status=$(echo "${response}" | tail -n1) + body=$(echo "${response}" | sed '$d') + if [[ "${http_status}" == "000" || "${http_status}" =~ ^5 || "${http_status}" == "429" ]]; then + echo "::warning::Retrying default branch set (HTTP ${http_status}) for ${repo_name} (${attempt}/3)..." + sleep 5 + ((attempt++)) + continue + fi + break + done if [[ "${http_status}" != "200" ]]; then echo "::warning::Failed to set default branch to 'main' for ${CENTER_ORG}/${repo_name} (status ${http_status})" @@ -107,7 +118,7 @@ jobs: } process_entry() { - local entry_raw entry alias_name branch_name source_repo source_repo_url start_epoch backup_mode + local entry_raw entry alias_name branch_name source_repo source_repo_url start_epoch backup_mode attempt max_api_retry entry_raw="$1" # Remove inline comments and trim entry="${entry_raw%%#*}" @@ -177,18 +188,39 @@ jobs: # Check if repository exists on Gitea repo_exists=false just_created=false - http_status=$(curl -s -o /dev/null -w "%{http_code}" -H "${AUTH_HEADER}" "${BASE_GITEA_URL}/api/v1/repos/${CENTER_ORG}/${repo_name}") + max_api_retry=3 + attempt=1 + while (( attempt<=max_api_retry )); do + http_status=$(curl -s -o /dev/null -w "%{http_code}" -H "${AUTH_HEADER}" "${BASE_GITEA_URL}/api/v1/repos/${CENTER_ORG}/${repo_name}") + if [[ "${http_status}" == "000" || "${http_status}" =~ ^5 || "${http_status}" == "429" ]]; then + echo "::warning::Repo check HTTP ${http_status} for ${repo_name} (${attempt}/3); retrying in 5s..." + sleep 5 + ((attempt++)) + continue + fi + break + done if [ "${http_status}" == "404" ]; then echo "Repository 'center_dev/${repo_name}' does not exist. Creating it..." create_tmp="$(mktemp)" - create_status=$(curl -s -o "${create_tmp}" -w "%{http_code}" -X POST -H "Content-Type: application/json" -H "${AUTH_HEADER}" -d "{\"name\":\"${repo_name}\",\"private\":true,\"default_branch\":\"main\"}" "${BASE_GITEA_URL}/api/v1/orgs/${CENTER_ORG}/repos") + attempt=1 + while (( attempt<=max_api_retry )); do + create_status=$(curl -s -o "${create_tmp}" -w "%{http_code}" -X POST -H "Content-Type: application/json" -H "${AUTH_HEADER}" -d "{\"name\":\"${repo_name}\",\"private\":true,\"default_branch\":\"main\"}" "${BASE_GITEA_URL}/api/v1/orgs/${CENTER_ORG}/repos") + if [[ "${create_status}" == "000" || "${create_status}" =~ ^5 || "${create_status}" == "429" ]]; then + echo "::warning::Repo create HTTP ${create_status} for ${repo_name} (${attempt}/3); retrying in 5s..." + sleep 5 + ((attempt++)) + continue + fi + break + done if [[ "${create_status}" != "201" ]]; then echo "::error::Failed to create repository. HTTP ${create_status}" cat "${create_tmp}" rm -f "${create_tmp}" notify_status "error" "${repo_name}" "${branch_name}" "${backup_mode}" "${start_epoch}" "repo 생성 실패 (HTTP ${create_status})" - exit 1 + return fi echo "Repository created successfully." repo_exists=true @@ -197,7 +229,7 @@ jobs: elif [ "${http_status}" != "200" ]; then echo "::error::Error checking repository. HTTP status: ${http_status}" notify_status "error" "${repo_name}" "${branch_name}" "${backup_mode}" "${start_epoch}" "repo 조회 실패 (HTTP ${http_status})" - exit 1 + return else echo "Repository 'center_dev/${repo_name}' already exists." repo_exists=true diff --git a/README.md b/README.md index d0a20ab..1a7efce 100644 --- a/README.md +++ b/README.md @@ -43,6 +43,7 @@ 6) 태그 동기화가 켜져 있으면 태그 fetch/push(강제+prune) 7) 기본 브랜치를 `main`으로 패치, 임시 디렉터리 정리 8) 소스 서버가 `--shallow-exclude`를 지원하지 않으면 타겟 `main`을 얕게(depth=50) 받아 시드한 뒤 일반 fetch로 재시도 +9) Gitea API(조회/생성/기본브랜치 설정)는 최대 3회 재시도 후 실패 시 해당 브랜치를 건너뛰고 다음 브랜치로 진행 ## 알림 - `NOTIFY_WEBHOOK` 설정 시 시작/성공/실패/건너뛰기 상태를 KST 타임스탬프와 소요시간, 모드(신규/증분)와 함께 전송 diff --git a/to-do.md b/to-do.md index fb91576..4985d36 100644 --- a/to-do.md +++ b/to-do.md @@ -28,3 +28,5 @@ 2025-12-17 08:27:54 KST 추가 업데이트: 스케줄 cron을 UTC 기준임을 명시하고 02:07 KST 실행을 위해 `7 17 * * *`로 설정, 동기화 결과 요약(총/성공/건너뜀/오류) 메시지를 마지막에 출력하고 알림 웹훅으로 전송하도록 추가. 2025-12-17 08:35:58 KST 추가 업데이트: 소스 서버가 `--shallow-exclude`를 지원하지 않을 때 타겟 main을 depth=50으로 시드한 후 일반 fetch로 재시도하도록 폴백을 추가, README 동작 개요 반영. 2025-12-17 08:48:00 KST 추가 업데이트: 동일 커밋 건너뛰기 시 모드를 “변경 없음”으로 분리하고 shallow-exclude 실패 시 fetch 로그 tail을 실행 로그에 남기며 폴백 모드를 표시하도록 개선. +2025-12-17 09:38:49 KST 추가 업데이트: Gitea API 오류가 발생해도 전체 작업이 중단되지 않도록 exit 대신 return 처리하여 다음 브랜치로 계속 진행하도록 수정. +2025-12-17 09:50:00 KST 추가 업데이트: Gitea API(조회/생성/기본 브랜치 설정)에 최대 3회 재시도(5초 대기)를 추가해 일시 오류 발생 시에도 작업이 이어지도록 개선, README 반영.