#!/usr/bin/env bash set -euo pipefail if [ -z "${GITEA_URL:-}" ]; then echo "gitea-api: GITEA_URL is not set" >&2 exit 1 fi if [ -z "${GITEA_TOKEN:-}" ]; then echo "gitea-api: GITEA_TOKEN is not set" >&2 exit 1 fi method="${1:-GET}" path="${2:-}" body="${3:-}" if [ -z "$path" ]; then echo "Usage: gitea-api METHOD /path [JSON]" >&2 exit 2 fi base="${GITEA_URL%/}/api/v1" tmp="$(mktemp)" cleanup() { rm -f "$tmp" } trap cleanup EXIT args=( -sS -o "$tmp" -w "%{http_code}" -X "$method" -H "Authorization: token ${GITEA_TOKEN}" -H "Accept: application/json" ) if [ -n "$body" ]; then args+=( -H "Content-Type: application/json" --data "$body" ) fi http_code="$(curl "${args[@]}" "${base}${path}")" curl_status=$? cat "$tmp" if [ "$curl_status" -ne 0 ]; then echo >&2 echo "gitea-api: curl failed with exit code $curl_status" >&2 exit "$curl_status" fi case "$http_code" in 2??) exit 0 ;; *) echo >&2 echo "gitea-api: HTTP $http_code" >&2 exit 1 ;; esac