# Auto-create a GitHub release when a vX.Y.Z tag is pushed.
#
# Uses the built-in GITHUB_TOKEN that GitHub Actions provides;
# no setup required.  Reads the annotated-tag message as the
# release body.
name: Release on tag push

on:
  push:
    tags:
      - 'v[0-9]+.[0-9]+.[0-9]+'
  workflow_dispatch:
    inputs:
      tag:
        description: 'Tag name to release (e.g. v0.8.8)'
        required: true
        type: string

permissions:
  contents: write

jobs:
  release:
    runs-on: ubuntu-24.04
    steps:
      - name: Checkout (full, for tags)
        uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Extract release notes from annotated tag
        id: notes
        run: |
          if [ "${GITHUB_EVENT_NAME}" = "workflow_dispatch" ]; then
            tag="${{ inputs.tag }}"
          else
            tag="${GITHUB_REF#refs/tags/}"
          fi
          echo "tag=$tag" >> "$GITHUB_OUTPUT"
          # Pick the longer of commit-message-body and tag-annotation
          # (matches the Codeberg workflow's selection so both forges
          # produce identical release bodies).
          commit_body="$(git log -1 --format='%b' "$tag")"
          tag_body="$(git tag -l --format='%(contents)' "$tag")"
          if [ "${#commit_body}" -ge "${#tag_body}" ]; then
            body="$commit_body"
          else
            body="$tag_body"
          fi
          if [ -z "$body" ]; then
            body="Release $tag"
          fi
          {
            echo "body<<RELEASE_BODY_EOF"
            echo "$body"
            echo "RELEASE_BODY_EOF"
          } >> "$GITHUB_OUTPUT"

      - name: Create GitHub release
        uses: softprops/action-gh-release@v2
        with:
          tag_name: ${{ steps.notes.outputs.tag }}
          name: Lime ${{ steps.notes.outputs.tag }}
          body: ${{ steps.notes.outputs.body }}
          draft: false
          prerelease: false
