#!/usr/bin/env bash # Open a pull request against main from the current branch. # # scripts/pr.sh "feat(ingest): parse Bryton FIT activities" [--draft] # # Requires GITEA_TOKEN (scopes: write:repository, write:issue) and optionally GITEA_URL. set -euo pipefail GITEA_URL="${GITEA_URL:-http://192.168.0.3:3000}" OWNER="${GITEA_OWNER:-BBergle}" REPO="${GITEA_REPO:-bike-app}" BASE="${PR_BASE:-main}" die() { echo "error: $*" >&2; exit 1; } [ -n "${GITEA_TOKEN:-}" ] || die "GITEA_TOKEN is not set. See CONTRIBUTING.md." command -v jq >/dev/null || die "jq is required (brew install jq)." TITLE="${1:-}" [ -n "$TITLE" ] || die "usage: scripts/pr.sh \"\" [--draft]" DRAFT=false [ "${2:-}" = "--draft" ] && DRAFT=true BRANCH="$(git rev-parse --abbrev-ref HEAD)" [ "$BRANCH" != "$BASE" ] || die "refusing to open a PR from $BASE onto itself." git diff --quiet && git diff --cached --quiet || die "working tree is dirty; commit or stash first." if ! git rev-parse --verify "origin/$BRANCH" >/dev/null 2>&1; then echo "Branch not on the remote yet; pushing." git push -u origin "$BRANCH" fi # Prefer the PR template; otherwise summarise the commits on this branch. if [ -f .gitea/PULL_REQUEST_TEMPLATE.md ]; then BODY="$(cat .gitea/PULL_REQUEST_TEMPLATE.md)" else BODY="$(git log --format='- %s' "origin/$BASE..$BRANCH")" fi RESPONSE="$(jq -n \ --arg title "$TITLE" --arg body "$BODY" \ --arg head "$BRANCH" --arg base "$BASE" \ --argjson draft "$DRAFT" \ '{title:$title, body:$body, head:$head, base:$base}' \ | curl -sS -X POST \ -H "Authorization: token $GITEA_TOKEN" \ -H "Content-Type: application/json" \ -d @- \ "$GITEA_URL/api/v1/repos/$OWNER/$REPO/pulls")" if URL="$(echo "$RESPONSE" | jq -er '.html_url' 2>/dev/null)"; then echo "PR opened: $URL" else echo "Gitea rejected the request:" >&2 echo "$RESPONSE" | jq . >&2 2>/dev/null || echo "$RESPONSE" >&2 exit 1 fi