name: 'Publish to NPM'

on:
  workflow_run:
    workflows: ['Node.js CI']
    types: [completed]
    branches: [master,main]

permissions:
  contents: write
  id-token: write

jobs:
  publish-new-version:
    runs-on: ubuntu-latest
    if: ${{ github.event.workflow_run.conclusion == 'success' }}
    steps:
      - uses: actions/checkout@v6
        with:
          fetch-depth: '0'
      - name: git setup
        run: |
          git config --local user.email "41898282+github-actions[bot]@users.noreply.github.com"
          git config --local user.name "github-actions[bot]"
      - name: setup node
        uses: actions/setup-node@v6
        with:
          node-version: '24'
          registry-url: 'https://registry.npmjs.org'
          cache: 'npm'
      - name: npm install
        run: npm ci

      - name: Should release
        id: should_release
        continue-on-error: true
        run: npm run should-release -- -v

      - name: No release
        if: steps.should_release.outcome != 'success'
        run: echo "No release required. Skipping publishing."

      - name: Version bump
        if: steps.should_release.outcome == 'success'
        run: npm run release

      - name: Publish to NPM
        if: steps.should_release.outcome == 'success'
        run: npm publish --provenance

      - name: Push commits to GitHub
        if: steps.should_release.outcome == 'success'
        uses: ad-m/github-push-action@v1.0.0
        with:
          github_token: ${{ secrets.GITHUB_TOKEN }}
          branch: ${{ github.ref }}
          tags: true
