diff --git a/examples/crash_reporter/backend/scripts/gitea/README.md b/examples/crash_reporter/backend/scripts/gitea/README.md index 10f5d4d..2c58e91 100644 --- a/examples/crash_reporter/backend/scripts/gitea/README.md +++ b/examples/crash_reporter/backend/scripts/gitea/README.md @@ -365,6 +365,53 @@ Rejects on `master` / `next`: branch delete, non-fast-forward (force-push). Othe `gitea_polish_project.sh` runs branch protection automatically unless `GITEA_SKIP_BRANCH_PROTECT=1`. +Apply the same Gitea rules to other AndroidCast repos: + +```bash +GITEA_REPO=url-shortener GITEA_PROTECT_BRANCHES="next master" \ + sudo ./examples/crash_reporter/backend/scripts/gitea/gitea_protect_branches.sh +``` + +(Branches that do not exist yet are skipped when using `gitea_attach_url_shortener.sh`.) + +## url-shortener (first-party submodule mirror) + +The url-shortener service lives in its **own** canonical repo +`git://f0xx.org/androidcast_project/url-shortener` (submodule `backend/url-shortener`). +It is **not** mirrored by `gitea_mirror_submodules.sh` (local `f0xx.org` URLs are skipped). + +**One command on BE** (org + pull mirror + default `next` + protect `master`/`next` when present): + +```bash +sudo ./examples/crash_reporter/backend/scripts/gitea/gitea_attach_url_shortener.sh +``` + +This reuses `gitea_attach_remote.sh`, `gitea_set_default_branch.sh`, and `gitea_protect_branches.sh` — no duplicate mirror logic. + +| Step | What | +|------|------| +| Org | Ensures **AndroidCast** + **Owners** (`admin`, `foxx`) | +| Transfer | `admin/url-shortener` or `androidcast_project/url-shortener` → `AndroidCast/url-shortener` if present | +| Mirror | Pull from `git://f0xx.org/androidcast_project/url-shortener` (`GITEA_PRIVATE=false` — team-visible under public org) | +| Default branch | `next` | +| Protection | `next` and `master` only (skips branches not on canonical remote yet) | + +Clone URL: + +`https://apps.f0xx.org/app/androidcast_project/git/AndroidCast/url-shortener.git` + +**FE canonical hook** (primary enforcement for `git push` to `git://f0xx.org/...`): + +```bash +cp examples/git/hooks/pre-receive.android_cast.example \ + /path/to/url-shortener.git/hooks/pre-receive +chmod +x /path/to/url-shortener.git/hooks/pre-receive +``` + +Ensure `app.ini` `[migrations]` allows **`f0xx.org`** (in addition to `10.7.0.10` for the main repo). + +`gitea_polish_project.sh` calls `gitea_attach_url_shortener.sh` automatically when the mirror is missing. + ## Polish (finish line — safe BE run) ```bash diff --git a/examples/crash_reporter/backend/scripts/gitea/gitea_attach_url_shortener.sh b/examples/crash_reporter/backend/scripts/gitea/gitea_attach_url_shortener.sh new file mode 100755 index 0000000..ff5fb7e --- /dev/null +++ b/examples/crash_reporter/backend/scripts/gitea/gitea_attach_url_shortener.sh @@ -0,0 +1,121 @@ +#!/bin/ash +# +# Import url-shortener into the AndroidCast Gitea org (pull mirror, team-visible, protected branches). +# Thin orchestrator — reuses gitea_attach_remote.sh, gitea_set_default_branch.sh, +# gitea_protect_branches.sh (same rules as android_cast). +# +# Canonical remote: git://f0xx.org/androidcast_project/url-shortener +# Gitea UI: …/git/AndroidCast/url-shortener +# +# Usage (Alpine BE as root): +# sudo ./gitea_attach_url_shortener.sh +# +# Override: +# GITEA_MIRROR_URL=git://f0xx.org/androidcast_project/url-shortener sudo ./gitea_attach_url_shortener.sh +# +# Prerequisites: app.ini [migrations] must allow f0xx.org (see scripts/gitea/README.md). +# +set -eu + +SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" +# shellcheck source=gitea_ini.sh +. "$SCRIPT_DIR/gitea_ini.sh" +# shellcheck source=gitea_mirror_lib.sh +. "$SCRIPT_DIR/gitea_mirror_lib.sh" +# shellcheck source=gitea_org_lib.sh +. "$SCRIPT_DIR/gitea_org_lib.sh" + +GITEA_ORG="${GITEA_ORG:-AndroidCast}" +GITEA_OWNER="${GITEA_OWNER:-$GITEA_ORG}" +GITEA_REPO="${GITEA_REPO:-url-shortener}" +GITEA_MIRROR_URL="${GITEA_MIRROR_URL:-git://f0xx.org/androidcast_project/url-shortener}" +GITEA_DEFAULT_BRANCH="${GITEA_DEFAULT_BRANCH:-next}" +GITEA_MIRROR_INTERVAL="${GITEA_MIRROR_INTERVAL:-10m}" +GITEA_PRIVATE="${GITEA_PRIVATE:-false}" +# Personal or legacy owners to re-home under AndroidCast (space-separated). +GITEA_TRANSFER_SOURCES="${GITEA_TRANSFER_SOURCES:-admin androidcast_project}" + +log() { printf '[gitea-url-shortener] %s\n' "$*"; } +warn() { printf '[gitea-url-shortener] WARN: %s\n' "$*" >&2; } + +case "${1:-}" in + -h|--help|help) + sed -n '2,18p' "$0" + exit 0 + ;; +esac + +gitea_load_config || exit 1 +gitea_ensure_cli_access || exit 1 + +log "=== $GITEA_REPO → $GITEA_OWNER/$GITEA_REPO ===" +log "mirror: $GITEA_MIRROR_URL" +log "public: ${GITEA_ROOT_URL%/}/$GITEA_OWNER/$GITEA_REPO" + +# 1) Org + Owners team (admin, foxx) — repos under the org are team-visible. +gitea_org_ensure_androidcast "$GITEA_ORG" || warn "org setup incomplete" + +# 2) Transfer if mirror was created under a personal/legacy account. +for _from in $GITEA_TRANSFER_SOURCES; do + [ "$_from" = "$GITEA_OWNER" ] && continue + if gitea_repo_exists_api "$_from" "$GITEA_REPO"; then + log "transfer $_from/$GITEA_REPO → $GITEA_OWNER/$GITEA_REPO" + gitea_transfer_repo_api "$_from" "$GITEA_REPO" "$GITEA_OWNER" \ + && log " transferred" \ + || warn " transfer failed" + fi +done + +# 3) Pull mirror (attach when missing). +if gitea_repo_exists_api "$GITEA_OWNER" "$GITEA_REPO"; then + log "repo exists — trigger mirror sync" + gitea_sync_mirror_api "$GITEA_OWNER" "$GITEA_REPO" || warn "mirror sync API failed — try gitea_sync_mirror.sh" +else + log "attach pull mirror via gitea_attach_remote.sh ..." + GITEA_OWNER="$GITEA_OWNER" GITEA_REPO="$GITEA_REPO" \ + GITEA_MIRROR_URL="$GITEA_MIRROR_URL" \ + GITEA_MIRROR_INTERVAL="$GITEA_MIRROR_INTERVAL" \ + GITEA_PRIVATE="$GITEA_PRIVATE" \ + sh "$SCRIPT_DIR/gitea_attach_remote.sh" "$GITEA_MIRROR_URL" || exit 1 + gitea_sync_mirror_api "$GITEA_OWNER" "$GITEA_REPO" || warn "initial sync pending — check [migrations] / git daemon" +fi + +# 4) Default branch next (integration; master when release branch exists). +GITEA_OWNER="$GITEA_OWNER" GITEA_REPO_NAMES="$GITEA_REPO" \ + GITEA_DEFAULT_BRANCH="$GITEA_DEFAULT_BRANCH" \ + sh "$SCRIPT_DIR/gitea_set_default_branch.sh" || warn "default branch set failed (sync mirror first)" + +# 5) Branch protection — master + next when present on canonical remote (GIT_FLOW §7). +_branches="" +for _b in next master; do + if gitea_git_remote_has_branch "$GITEA_MIRROR_URL" "$_b"; then + _branches="$_branches $_b" + else + log "skip Gitea protection for $_b (not on $GITEA_MIRROR_URL yet)" + fi +done +_branches="$(echo "$_branches" | sed 's/^ //')" +if [ -n "$_branches" ]; then + GITEA_OWNER="$GITEA_OWNER" GITEA_REPO="$GITEA_REPO" \ + GITEA_PROTECT_BRANCHES="$_branches" \ + sh "$SCRIPT_DIR/gitea_protect_branches.sh" || warn "branch protection failed — run manually" +else + warn "no branches to protect — mirror may be empty" +fi + +cat </dev/null 2>&1 || return 1 + git ls-remote "$_url" "refs/heads/$_branch" 2>/dev/null | grep -q . +} + # True if commit SHA is reachable on the mirror (after sync). gitea_mirror_has_commit() { _owner="$1" diff --git a/examples/crash_reporter/backend/scripts/gitea/gitea_polish_project.sh b/examples/crash_reporter/backend/scripts/gitea/gitea_polish_project.sh index 8086c1b..ea78afb 100755 --- a/examples/crash_reporter/backend/scripts/gitea/gitea_polish_project.sh +++ b/examples/crash_reporter/backend/scripts/gitea/gitea_polish_project.sh @@ -101,6 +101,16 @@ sync_all_mirrors() { done } +ensure_url_shortener_mirror() { + gitea_repo_exists_api "$GITEA_ORG" url-shortener && { + log "url-shortener mirror present under $GITEA_ORG" + return 0 + } + log "url-shortener missing — running gitea_attach_url_shortener.sh ..." + sh "$SCRIPT_DIR/gitea_attach_url_shortener.sh" \ + || warn "url-shortener attach failed — run: sudo ./gitea_attach_url_shortener.sh" +} + mirror_submodules_if_missing() { _checkout="${1:-}" _repo_dir="" @@ -194,6 +204,9 @@ sync_all_mirrors # 5) Submodules mirror_submodules_if_missing "$_checkout" +# 5b) First-party submodule url-shortener (local git:// — not gitea_mirror_submodules) +ensure_url_shortener_mirror + # 6) Default branch if command -v "$SCRIPT_DIR/gitea_set_default_branch.sh" >/dev/null 2>&1; then GITEA_OWNER="$GITEA_ORG" GITEA_DEFAULT_BRANCH="$GITEA_DEFAULT_BRANCH" \ @@ -211,7 +224,7 @@ cat <