#!/bin/bash # pr.sh - Create PRs on Gitea from current branch # Usage: ./pr.sh [base_branch] [title] set -euo pipefail GITEA_API="http://10.0.0.194:3012/api/v1" GITEA_PUBLIC="https://mysources.co.uk" GITEA_TOKEN="$(grep '^GITEA_TOKEN=' /home/michal/developer/michalzxc/claude/homeassistant-alchemy/stack/.env | cut -d= -f2-)" REPO="michal/mcpctl" if [[ -z "$GITEA_TOKEN" ]]; then echo "Error: GITEA_TOKEN not found" >&2 exit 1 fi BRANCH=$(git branch --show-current) BASE="${1:-main}" TITLE="${2:-}" if [[ "$BRANCH" == "$BASE" ]]; then echo "Error: already on $BASE, switch to a feature branch first" >&2 exit 1 fi # Check for existing open PR for this branch EXISTING=$(curl -s "$GITEA_API/repos/$REPO/pulls?state=open&head=$BRANCH" \ -H "Authorization: token $GITEA_TOKEN" | jq -r '.[0].number // empty' 2>/dev/null) if [[ -n "$EXISTING" ]]; then echo "PR #$EXISTING already exists for $BRANCH" echo "$GITEA_PUBLIC/$REPO/pulls/$EXISTING" exit 0 fi # Auto-generate title from branch name if not provided if [[ -z "$TITLE" ]]; then TITLE=$(echo "$BRANCH" | sed 's|^feat/||;s|^fix/||;s|^chore/||' | tr '-' ' ' | sed 's/.*/\u&/') fi # Build body from commit messages on this branch COMMITS=$(git log "$BASE..$BRANCH" --pretty=format:"- %s" 2>/dev/null) BODY="## Summary ${COMMITS} --- Generated with [Claude Code](https://claude.com/claude-code)" # Push if needed if ! git rev-parse --verify "origin/$BRANCH" &>/dev/null; then echo "Pushing $BRANCH to origin..." git push -u origin "$BRANCH" fi # Create PR RESPONSE=$(curl -s -X POST "$GITEA_API/repos/$REPO/pulls" \ -H "Content-Type: application/json" \ -H "Authorization: token $GITEA_TOKEN" \ -d "$(jq -n --arg title "$TITLE" --arg body "$BODY" --arg head "$BRANCH" --arg base "$BASE" \ '{title: $title, body: $body, head: $head, base: $base}')") PR_NUM=$(echo "$RESPONSE" | jq -r '.number // empty') if [[ -z "$PR_NUM" ]]; then echo "Error creating PR: $(echo "$RESPONSE" | jq -r '.message // "unknown error"')" >&2 exit 1 fi echo "Created PR #$PR_NUM: $TITLE" echo "$GITEA_PUBLIC/$REPO/pulls/$PR_NUM"