From 9a2a29d19028f0fbef1046ef904e1b92aa5b3d05 Mon Sep 17 00:00:00 2001 From: Tracewebstudio Dev Date: Thu, 30 Apr 2026 19:57:17 +0200 Subject: [PATCH] fix(ci): auto-resolve gitea target repo for sync --- .github/workflows/sync-to-gitea.yml | 64 +++++++++++++++++++++++++++-- 1 file changed, 60 insertions(+), 4 deletions(-) diff --git a/.github/workflows/sync-to-gitea.yml b/.github/workflows/sync-to-gitea.yml index c45546a..b36a70d 100644 --- a/.github/workflows/sync-to-gitea.yml +++ b/.github/workflows/sync-to-gitea.yml @@ -20,14 +20,70 @@ jobs: env: GITEA_USERNAME: Admin GITEA_TOKEN: ${{ secrets.GITEA_SECRET }} + GITEA_HOST: ci.nxtgauge.com + GITEA_OWNER: ${{ vars.GITEA_OWNER }} run: | set -euo pipefail - echo "Syncing ${{ github.event.repository.name }}:high-performance → Gitea high-performance" + echo "Syncing ${{ github.event.repository.name }}:${{ github.ref_name }} → Gitea ${{ github.ref_name }}" echo "Commit: $(git rev-parse HEAD)" - git remote add gitea "https://${GITEA_USERNAME}:${GITEA_TOKEN}@ci.nxtgauge.com/Admin/${{ github.event.repository.name }}.git" + if [ -z "${GITEA_TOKEN:-}" ]; then + echo "Missing GITEA_SECRET" + exit 1 + fi - git fetch gitea high-performance || true - git push gitea HEAD:high-performance --force-with-lease=refs/heads/high-performance + REPO_NAME="${{ github.event.repository.name }}" + BRANCH_NAME="${{ github.ref_name }}" + CANDIDATE_OWNERS="${GITEA_OWNER:-} Admin ${{ github.repository_owner }}" + TARGET_URL="" + + for owner in $CANDIDATE_OWNERS; do + [ -n "$owner" ] || continue + candidate_url="https://${GITEA_USERNAME}:${GITEA_TOKEN}@${GITEA_HOST}/${owner}/${REPO_NAME}.git" + if git ls-remote "$candidate_url" >/dev/null 2>&1; then + TARGET_URL="$candidate_url" + echo "Using Gitea target owner: $owner" + break + fi + done + + if [ -z "$TARGET_URL" ]; then + echo "Owner guess failed; searching accessible repos via Gitea API" + API_URL="https://${GITEA_HOST}/api/v1/repos/search?q=${REPO_NAME}&limit=100" + TARGET_URL=$(python3 - <<'PY' +import json +import os +import urllib.request + +api_url = os.environ["API_URL"] +token = os.environ["GITEA_TOKEN"] +repo_name = os.environ["REPO_NAME"] +username = os.environ["GITEA_USERNAME"] + +req = urllib.request.Request(api_url, headers={"Authorization": f"token {token}"}) +with urllib.request.urlopen(req, timeout=20) as resp: + data = json.loads(resp.read().decode("utf-8")) + +for repo in data.get("data", []): + full_name = str(repo.get("full_name", "")) + if full_name.lower().endswith("/" + repo_name.lower()): + print(f"https://{username}:{token}@{os.environ['GITEA_HOST']}/{full_name}.git") + break +PY +) + if [ -n "$TARGET_URL" ]; then + echo "Resolved Gitea target via API" + fi + fi + + if [ -z "$TARGET_URL" ]; then + echo "Could not access target repo on Gitea for owners: $CANDIDATE_OWNERS" + exit 1 + fi + + git remote add gitea "$TARGET_URL" + + git fetch gitea "$BRANCH_NAME" || true + git push gitea "HEAD:${BRANCH_NAME}" --force-with-lease=refs/heads/"$BRANCH_NAME" echo "Sync complete!"