From 691f91598c29a76f2791557faf671241172cf3de Mon Sep 17 00:00:00 2001 From: Anton Afanasyeu Date: Wed, 10 Jun 2026 21:08:56 +0200 Subject: [PATCH] sync be --- .../backend/scripts/gitea/README.md | 8 ++- .../scripts/gitea/gitea_attach_remote.sh | 37 ++++++++-- .../backend/scripts/gitea/gitea_ini.sh | 69 +++++++++++++++---- 3 files changed, 93 insertions(+), 21 deletions(-) diff --git a/examples/crash_reporter/backend/scripts/gitea/README.md b/examples/crash_reporter/backend/scripts/gitea/README.md index 42bcfd1..5a71ede 100644 --- a/examples/crash_reporter/backend/scripts/gitea/README.md +++ b/examples/crash_reporter/backend/scripts/gitea/README.md @@ -65,7 +65,13 @@ export GITEA_ADMIN_TOKEN='gitea_…' **Why auth?** Gitea rejects anonymous `git push`. The token (or CLI-generated token) proves write access to `foxx/android_cast`. -**Prerequisite:** user **`foxx`** must exist in Gitea (the script creates the repo, not the user). +**Prerequisite:** a **Gitea user** must exist (Site Admin → Users). Default script owner is `foxx`; if that user does not exist in Gitea, the script auto-picks the first admin (override with `GITEA_OWNER=admin` or whatever you created at install). + +Gitea usernames are **not** Linux users, FE logins, or crash-console `admin` — list them: + +```bash +gitea -c /etc/gitea/app.ini admin user list +``` **`remote: Not found`** = repo did not exist yet and nothing created it. Re-sync the updated script and re-run. diff --git a/examples/crash_reporter/backend/scripts/gitea/gitea_attach_remote.sh b/examples/crash_reporter/backend/scripts/gitea/gitea_attach_remote.sh index 47ed72d..c44a544 100755 --- a/examples/crash_reporter/backend/scripts/gitea/gitea_attach_remote.sh +++ b/examples/crash_reporter/backend/scripts/gitea/gitea_attach_remote.sh @@ -79,6 +79,24 @@ test_remote_reachable() { return 1 } +resolve_gitea_owner() { + if gitea_user_exists "$GITEA_OWNER"; then + log "Gitea owner user: $GITEA_OWNER" + return 0 + fi + warn "Gitea has no user '$GITEA_OWNER' (not Linux/FE login — Gitea accounts are separate)" + log "Gitea users on this instance:" + gitea_list_usernames | while read -r _u; do + [ -n "$_u" ] && log " - $_u" + done + _fallback="$(gitea_pick_owner_username)" + if [ -z "$_fallback" ]; then + die "no Gitea users — open Gitea UI, create admin, then re-run (or GITEA_OWNER=username)" + fi + warn "using Gitea user '$_fallback' as repo owner (override: GITEA_OWNER=...)" + GITEA_OWNER="$_fallback" +} + resolve_push_token() { if [ -n "${GITEA_ADMIN_TOKEN:-}" ]; then printf '%s' "$GITEA_ADMIN_TOKEN" @@ -90,10 +108,14 @@ resolve_push_token() { "--token-name" "attach-$$" \ "--scopes" "all" \ "--raw" 2>/dev/null)" && [ -n "$_token" ] && printf '%s' "$_token" && return 0 - _token="$(gitea_cli "admin" "user" "generate-access-token" \ - "--username" "$GITEA_OWNER" \ - "--token-name" "attach-$$" \ - "--raw" 2>/dev/null)" && [ -n "$_token" ] && printf '%s' "$_token" && return 0 + _admin="$(gitea_pick_owner_username)" + if [ -n "$_admin" ] && [ "$_admin" != "$GITEA_OWNER" ]; then + _token="$(gitea_cli "admin" "user" "generate-access-token" \ + "--username" "$_admin" \ + "--token-name" "attach-$$" \ + "--scopes" "all" \ + "--raw" 2>/dev/null)" && [ -n "$_token" ] && printf '%s' "$_token" && return 0 + fi return 1 } @@ -156,7 +178,10 @@ migrate_via_api() { _token="${GITEA_ADMIN_TOKEN:-}" [ -n "$_token" ] || _token="$(resolve_push_token)" || return 1 - _uid="$(gitea_owner_uid)" || die "could not resolve uid for owner $GITEA_OWNER" + _uid="$(gitea_owner_uid)" || { + warn "API migrate skipped: no uid for owner $GITEA_OWNER" + return 1 + } _api="$(gitea_api_base)" _payload=$(cat <&2 + echo "ERROR: cannot read $gitea_ini_file (set GITEA_INI=...)" >&2 return 1 fi GITEA_APP_INI="$gitea_ini_file" - GITEA_REPO_ROOT="$(ini_get "$gitea_ini_file" repository ROOT)" - GITEA_APP_DATA="$(ini_get "$gitea_ini_file" server APP_DATA_PATH)" - GITEA_ROOT_URL="$(ini_get "$gitea_ini_file" server ROOT_URL)" - GITEA_HTTP_PORT="$(ini_get "$gitea_ini_file" server HTTP_PORT)" - GITEA_DOMAIN="$(ini_get "$gitea_ini_file" server DOMAIN)" - GITEA_DB_TYPE="$(ini_get "$gitea_ini_file" database DB_TYPE)" - GITEA_DB_HOST="$(ini_get "$gitea_ini_file" database HOST)" - GITEA_DB_NAME="$(ini_get "$gitea_ini_file" database NAME)" - GITEA_DB_USER="$(ini_get "$gitea_ini_file" database USER)" - GITEA_DB_PASS="$(ini_get "$gitea_ini_file" database PASSWD)" - GITEA_DB_PATH="$(ini_get "$gitea_ini_file" database PATH)" - GITEA_LFS_PATH="$(ini_get "$gitea_ini_file" lfs PATH)" - GITEA_LOG_ROOT="$(ini_get "$gitea_ini_file" log ROOT_PATH)" + _load_ini_keys() { + _f="$1" + GITEA_REPO_ROOT="$(ini_get "$_f" repository ROOT)" + GITEA_APP_DATA="$(ini_get "$_f" server APP_DATA_PATH)" + GITEA_ROOT_URL="$(ini_get "$_f" server ROOT_URL)" + GITEA_HTTP_PORT="$(ini_get "$_f" server HTTP_PORT)" + GITEA_DOMAIN="$(ini_get "$_f" server DOMAIN)" + GITEA_DB_TYPE="$(ini_get "$_f" database DB_TYPE)" + GITEA_DB_HOST="$(ini_get "$_f" database HOST)" + GITEA_DB_NAME="$(ini_get "$_f" database NAME)" + GITEA_DB_USER="$(ini_get "$_f" database USER)" + GITEA_DB_PASS="$(ini_get "$_f" database PASSWD)" + GITEA_DB_PATH="$(ini_get "$_f" database PATH)" + GITEA_LFS_PATH="$(ini_get "$_f" lfs PATH)" + GITEA_LOG_ROOT="$(ini_get "$_f" log ROOT_PATH)" + } + _load_ini_keys "$gitea_ini_file" [ -n "$GITEA_APP_DATA" ] || GITEA_APP_DATA="/var/lib/gitea" + # Live config may live under APP_DATA (Alpine package sometimes keeps /etc stub minimal). + if [ -z "$GITEA_ROOT_URL" ] && [ -r "$GITEA_APP_DATA/custom/conf/app.ini" ]; then + _load_ini_keys "$GITEA_APP_DATA/custom/conf/app.ini" + GITEA_APP_INI="$GITEA_APP_DATA/custom/conf/app.ini" + fi + [ -n "$GITEA_REPO_ROOT" ] || GITEA_REPO_ROOT="$GITEA_APP_DATA/data/gitea-repositories" [ -n "$GITEA_HTTP_PORT" ] || GITEA_HTTP_PORT="3000" [ -n "$GITEA_DB_TYPE" ] || GITEA_DB_TYPE="sqlite3" + [ -n "$GITEA_ROOT_URL" ] || GITEA_ROOT_URL="${GITEA_ROOT_URL_DEFAULT:-https://apps.f0xx.org/app/androidcast_project/git/}" export GITEA_APP_INI GITEA_REPO_ROOT GITEA_APP_DATA GITEA_ROOT_URL GITEA_HTTP_PORT export GITEA_DOMAIN GITEA_DB_TYPE GITEA_DB_HOST GITEA_DB_NAME GITEA_DB_USER GITEA_DB_PASS @@ -113,3 +128,27 @@ gitea_cli() { return 1 fi } + +# One username per line from `gitea admin user list`. +gitea_list_usernames() { + gitea_cli "admin" "user" "list" 2>/dev/null \ + | awk 'NR > 1 { print $2 }' +} + +gitea_user_exists() { + _u="$1" + gitea_list_usernames | grep -Fx "$_u" >/dev/null 2>&1 +} + +# First admin account (site admin), else first user. +gitea_pick_owner_username() { + _pick="" + _pick="$(gitea_cli "admin" "user" "list" 2>/dev/null \ + | awk 'NR > 1 && ($5 == "true" || $5 == "1") { print $2; exit }')" + if [ -n "$_pick" ]; then + printf '%s' "$_pick" + return 0 + fi + gitea_list_usernames | head -n 1 +} +