fix(ci): try multiple gitea auth url formats

This commit is contained in:
Tracewebstudio Dev 2026-04-30 20:08:50 +02:00
parent c1b08bd194
commit 921b97f2c3

View file

@ -35,36 +35,51 @@ jobs:
REPO_NAME="${{ github.event.repository.name }}" REPO_NAME="${{ github.event.repository.name }}"
BRANCH_NAME="${{ github.ref_name }}" BRANCH_NAME="${{ github.ref_name }}"
CANDIDATE_OWNERS="${GITEA_OWNER:-} Admin ${{ github.repository_owner }}" CANDIDATE_OWNERS="${GITEA_OWNER:-} Admin ${{ github.repository_owner }}"
TARGET_FULL_NAME=""
TARGET_URL="" TARGET_URL=""
for owner in $CANDIDATE_OWNERS; do for owner in $CANDIDATE_OWNERS; do
[ -n "$owner" ] || continue [ -n "$owner" ] || continue
candidate_url="https://${GITEA_USERNAME}:${GITEA_TOKEN}@${GITEA_HOST}/${owner}/${REPO_NAME}.git" candidate_full_name="${owner}/${REPO_NAME}"
if git ls-remote "$candidate_url" >/dev/null 2>&1; then candidate_url="https://${GITEA_HOST}/${candidate_full_name}.git"
TARGET_URL="$candidate_url" if git ls-remote "https://${GITEA_USERNAME}:${GITEA_TOKEN}@${GITEA_HOST}/${candidate_full_name}.git" >/dev/null 2>&1; then
TARGET_FULL_NAME="$candidate_full_name"
echo "Using Gitea target owner: $owner" echo "Using Gitea target owner: $owner"
break break
fi fi
done done
if [ -z "$TARGET_URL" ]; then if [ -z "$TARGET_FULL_NAME" ]; then
echo "Owner guess failed; searching accessible repos via Gitea API" echo "Owner guess failed; searching accessible repos via Gitea API"
API_URL="https://${GITEA_HOST}/api/v1/repos/search?q=${REPO_NAME}&limit=100" API_URL="https://${GITEA_HOST}/api/v1/repos/search?q=${REPO_NAME}&limit=100"
API_JSON="$(curl -fsS -H "Authorization: token ${GITEA_TOKEN}" "${API_URL}" || true)" API_JSON="$(curl -fsS -H "Authorization: token ${GITEA_TOKEN}" "${API_URL}" || true)"
TARGET_FULL_NAME="$(printf '%s' "$API_JSON" | jq -r --arg repo "$REPO_NAME" '[.data[]?.full_name | select((ascii_downcase | endswith("/" + ($repo | ascii_downcase))))][0] // empty')" TARGET_FULL_NAME="$(printf '%s' "$API_JSON" | jq -r --arg repo "$REPO_NAME" '[.data[]?.full_name | select((ascii_downcase | endswith("/" + ($repo | ascii_downcase))))][0] // empty')"
if [ -n "$TARGET_FULL_NAME" ]; then if [ -n "$TARGET_FULL_NAME" ]; then
TARGET_URL="https://${GITEA_USERNAME}:${GITEA_TOKEN}@${GITEA_HOST}/${TARGET_FULL_NAME}.git"
fi
if [ -n "$TARGET_URL" ]; then
echo "Resolved Gitea target via API" echo "Resolved Gitea target via API"
fi fi
fi fi
if [ -z "$TARGET_URL" ]; then if [ -z "$TARGET_FULL_NAME" ]; then
echo "Could not access target repo on Gitea for owners: $CANDIDATE_OWNERS" echo "Could not access target repo on Gitea for owners: $CANDIDATE_OWNERS"
exit 1 exit 1
fi fi
for auth_url in \
"https://${GITEA_USERNAME}:${GITEA_TOKEN}@${GITEA_HOST}/${TARGET_FULL_NAME}.git" \
"https://${GITEA_TOKEN}@${GITEA_HOST}/${TARGET_FULL_NAME}.git" \
"https://oauth2:${GITEA_TOKEN}@${GITEA_HOST}/${TARGET_FULL_NAME}.git"; do
if git ls-remote "$auth_url" >/dev/null 2>&1; then
TARGET_URL="$auth_url"
echo "Using Gitea credential mode for ${TARGET_FULL_NAME}"
break
fi
done
if [ -z "$TARGET_URL" ]; then
echo "Resolved repo path but authentication to Gitea git remote failed"
exit 1
fi
git remote add gitea "$TARGET_URL" git remote add gitea "$TARGET_URL"
git fetch gitea "$BRANCH_NAME" || true git fetch gitea "$BRANCH_NAME" || true