name: Release

on:
  push:
    tags:
      - "v*"

permissions:
  contents: write

jobs:
  publish:
    name: Create GitHub Release
    runs-on: ubuntu-latest
    steps:
      - name: Create release if missing
        uses: actions/github-script@v7
        with:
          script: |
            const tag = context.ref.replace("refs/tags/", "");

            try {
              const existing = await github.rest.repos.getReleaseByTag({
                owner: context.repo.owner,
                repo: context.repo.repo,
                tag
              });
              core.info(`Release already exists for ${tag}: ${existing.data.html_url}`);
            } catch (error) {
              if (error.status !== 404) {
                throw error;
              }

              const created = await github.rest.repos.createRelease({
                owner: context.repo.owner,
                repo: context.repo.repo,
                tag_name: tag,
                name: tag,
                generate_release_notes: true,
                draft: false,
                prerelease: false
              });
              core.info(`Created release for ${tag}: ${created.data.html_url}`);
            }
