name: Release

on:
  push:
    tags:
      - "v*.*.*"

permissions:
  contents: write

jobs:
  release:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6
      - uses: actions/setup-node@v6
        with:
          node-version: 20.x
          cache: npm
      - run: npm ci
      - run: npm run build
      - run: npm test
      - run: npm run test:coverage
      - name: Extract release notes from CHANGELOG
        run: |
          version="${GITHUB_REF_NAME#v}"
          awk -v version="$version" '
            $0 ~ "^## \\[" version "\\]" { in_section = 1; next }
            in_section && $0 ~ "^## \\[" { exit }
            in_section { print }
          ' CHANGELOG.md > RELEASE_NOTES.md

          if [ ! -s RELEASE_NOTES.md ]; then
            echo "No changelog section found for version $version"
            exit 1
          fi
      - name: Create release
        uses: softprops/action-gh-release@v2
        with:
          body_path: RELEASE_NOTES.md
