#!/usr/bin/env bash set -euo pipefail repo_root="$(cd "$(dirname "$0")/.." && pwd)" cd "$repo_root" if [[ -z "${GITHUB_REF_NAME:-}" ]]; then echo "GITHUB_REF_NAME is required" >&2 exit 1 fi if [[ -z "${GITHUB_REPOSITORY:-}" || -z "${GITHUB_SERVER_URL:-}" || -z "${GITHUB_TOKEN:-}" ]]; then echo "GITHUB_REPOSITORY, GITHUB_SERVER_URL, and GITHUB_TOKEN are required" >&2 exit 1 fi tag="${GITHUB_REF_NAME}" version="${tag#v}" notes_file="$(mktemp)" awk -v version="$version" ' $0 ~ ("^## \\\[" version "\\\] - ") { in_section = 1 } /^## \[/ && in_section && $0 !~ ("^## \\\[" version "\\\] - ") { exit } in_section { print } ' CHANGELOG.md > "$notes_file" if [[ ! -s "$notes_file" ]]; then printf '## [%s]\n\n- Release %s\n' "$version" "$tag" > "$notes_file" fi payload_file="$(mktemp)" jq -n \ --arg tag "$tag" \ --arg name "$tag" \ --arg target "main" \ --rawfile body "$notes_file" \ '{tag_name: $tag, target_commitish: $target, name: $name, body: $body, draft: false, prerelease: false}' > "$payload_file" api_base="${GITHUB_SERVER_URL}/api/v1/repos/${GITHUB_REPOSITORY}" status="$(curl -sS -o /tmp/release_lookup.json -w '%{http_code}' \ -H "Authorization: token ${GITHUB_TOKEN}" \ -H 'accept: application/json' \ "${api_base}/releases/tags/${tag}" || true)" if [[ "$status" == "200" ]]; then release_id="$(jq -r '.id' /tmp/release_lookup.json)" curl -fsSL -X PATCH \ -H "Authorization: token ${GITHUB_TOKEN}" \ -H 'accept: application/json' \ -H 'content-type: application/json' \ --data @"$payload_file" \ "${api_base}/releases/${release_id}" >/dev/null echo "Updated release ${tag}." else curl -fsSL -X POST \ -H "Authorization: token ${GITHUB_TOKEN}" \ -H 'accept: application/json' \ -H 'content-type: application/json' \ --data @"$payload_file" \ "${api_base}/releases" >/dev/null echo "Created release ${tag}." fi