name: Release
on:
  workflow_dispatch:
  pull_request:
    types:
      - closed
    branches:
      - 'main'

jobs:
  release:
    name: Release
    runs-on: ubuntu-latest
    permissions:
      contents: write # to be able to publish a GitHub release
      issues: write # to be able to comment on released issues
      pull-requests: write # to be able to comment on released pull requests
      id-token: write # to enable use of OIDC for npm provenance
    steps:
      - name: Generate token
        id: generate_token
        uses: actions/create-github-app-token@v1.11.0
        with:
          app-id: ${{ secrets.DATABLE_MERGE_BOT_APP_ID }}
          private-key: ${{ secrets.DATABLE_MERGE_BOT_PRIVATE_KEY }}
      - name: Checkout
        uses: actions/checkout@v3
        with:
          fetch-depth: 0
          token: ${{ steps.generate_token.outputs.token }}
      - name: Setup Node.js
        uses: actions/setup-node@v3
        with:
          node-version: "20"
      - name: Install dependencies
        run: npm ci
      - name: Build
        run: npm run build
      # - name: Verify the integrity of provenance attestations and registry signatures for installed dependencies
      #   run: npm audit signatures

      - name: Release
        env:
          GITHUB_TOKEN: ${{ steps.generate_token.outputs.token }}
          GH_TOKEN: ${{ steps.generate_token.outputs.token }}
          NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
          NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
        run: |
          git config --global user.name "${GITHUB_ACTOR}"
          git config --global user.email "${GITHUB_ACTOR}@users.noreply.github.com"
          npx release-it --ci
