on:
  push:
    branches:
      - main

permissions:
  contents: write
  issues: write
  pull-requests: write

name: release-please

jobs:
  release-please:
    runs-on: ubuntu-latest
    outputs:
      prs: ${{ steps.release-please.outputs.prs }}
    steps:
      - uses: actions/create-github-app-token@bcd2ba49218906704ab6c1aa796996da409d3eb1 # v3.2.0
        id: generate-token
        with:
          client-id: ${{ vars.RELEASE_BOT_CLIENT_ID }}
          private-key: ${{ secrets.RELEASE_BOT_PRIVATE_KEY }}
      - uses: googleapis/release-please-action@45996ed1f6d02564a971a2fa1b5860e934307cf7 # v5.0.0
        id: release-please
        with:
          token: ${{ steps.generate-token.outputs.token }}

  update-static-files:
    runs-on: ubuntu-latest
    needs: release-please
    if: ${{ needs.release-please.outputs.prs }}
    steps:
      - uses: actions/create-github-app-token@bcd2ba49218906704ab6c1aa796996da409d3eb1 # v3.2.0
        id: generate-token
        with:
          client-id: ${{ vars.RELEASE_BOT_CLIENT_ID }}
          private-key: ${{ secrets.RELEASE_BOT_PRIVATE_KEY }}
      - name: Checkout PR branch
        uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
        with:
          token: ${{ steps.generate-token.outputs.token }}
          ref: ${{ fromJSON(needs.release-please.outputs.prs)[0].headBranchName }}
      - uses: astral-sh/setup-uv@c771a70e6277c0a99b617c7a806ffedaca235ff9 # v9.0.0
      - name: Run pull-static script
        run: uv run scripts/pull-static
      - name: Run pytest with rewrite mode
        run: uv run pytest --record-mode rewrite
      - name: Commit and push changes
        run: |
          git config user.name "github-actions[bot]"
          git config user.email "github-actions[bot]@users.noreply.github.com"
          git add -A
          git diff --staged --quiet || git commit -m "chore: update static files and test recordings"
          git push
