#!/usr/bin/env bash # fm-gitea-pr.sh - Push a reviewed crewmate branch to Gitea and open a PR via tea. # # Used after the first mate (opencode) reviews a crewmate's local branch in # local-only mode: this helper pushes the branch to the Gitea remote and opens # a pull request for the record. The review decision already happened in # opencode; this is the publish step. # # Usage: # fm-gitea-pr.sh [--title ""] [--body "<body>"] [--base <base>] # # Defaults: --title = branch name, --body = "(reviewed by first mate)", --base = main # # Prerequisites (one-time per machine, not automatable in nix - secret): # tea login add --name unraid --host http://unraid.local:3003 --token <GITEA_TOKEN> # # The Gitea remote is read from the current git repo's `origin` URL. set -euo pipefail BRANCH="" TITLE="" BODY="(reviewed by first mate - tests green, Primary-Rule + ADR-0009 lint passed)" BASE="main" while [ $# -gt 0 ]; do case "$1" in --title) TITLE="$2"; shift 2 ;; --body) BODY="$2"; shift 2 ;; --base) BASE="$2"; shift 2 ;; -*) echo "unknown flag: $1" >&2; exit 1 ;; *) [ -z "$BRANCH" ] && BRANCH="$1" || { echo "unexpected extra arg: $1" >&2; exit 1; }; shift ;; esac done [ -n "$BRANCH" ] || { echo "usage: fm-gitea-pr.sh <branch> [--title <t>] [--body <b>] [--base <b>]" >&2; exit 1; } [ -z "$TITLE" ] && TITLE="$BRANCH" command -v tea >/dev/null 2>&1 || { echo "error: tea not installed. brew install tea" >&2; exit 1; } # Parse the Gitea owner/repo from the origin remote URL. # Handles http://user:token@host:port/owner/repo(.git) and https://host/owner/repo.git REMOTE=$(git remote get-url origin 2>/dev/null) || { echo "error: no git origin remote" >&2; exit 1; } # Strip credentials and .git suffix, then take the path after the host. PATH_PART=${REMOTE#*://} PATH_PART=${PATH_PART#*@} # drop user:pass@ PATH_PART=${PATH_PART#*/} # drop host:port/ (first slash) PATH_PART=${PATH_PART%.git} OWNER_REPO=${PATH_PART} case "$OWNER_REPO" in */*) : ;; # owner/repo form *) echo "error: could not parse owner/repo from remote: $REMOTE" >&2; exit 1 ;; esac # Push the branch to origin (Gitea). --push-branch uses the configured remote. git push -u origin "$BRANCH" 2>&1 || { echo "error: git push failed for branch $BRANCH" >&2; exit 1; } # Open the PR via tea. tea resolves the login by host automatically. PR_URL=$(tea pulls create \ --repo "$OWNER_REPO" \ --head "$BRANCH" \ --base "$BASE" \ --title "$TITLE" \ --description "$BODY" 2>&1) || { echo "error: tea pulls create failed" >&2 echo "$PR_URL" >&2 exit 1 } # tea prints the PR URL on success; echo it for the caller. echo "$PR_URL"