name: Release

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

permissions:
  id-token: write
  contents: write

env:
  HUSKY: '0'

jobs:
  build:
    if: github.repository == 'release-drafter/release-drafter'
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6
        with:
          fetch-depth: 0
      - uses: ./.github/actions/common-setup

      - name: test
        run: npm run test

      - name: publish
        run: npm publish # Publishes to npmjs, using package.json's version

      - name: version
        id: version
        run: |
          tag=${GITHUB_REF/refs\/tags\//}
          version=${tag#v}
          major=${version%%.*}
          echo "tag=${tag}" >> $GITHUB_OUTPUT
          echo "version=${version}" >> $GITHUB_OUTPUT
          echo "major=${major}" >> $GITHUB_OUTPUT

      - uses: release-drafter/release-drafter@master
        with:
          version: ${{ steps.version.outputs.version }}
          publish: true
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      - name: force update major tag
        run: |
          git tag v${{ steps.version.outputs.major }} ${{ steps.version.outputs.tag }} -f
          git push origin refs/tags/v${{ steps.version.outputs.major }} -f
