name: CI

on:
  pull_request:
    types: [labeled]

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}-vrt
  cancel-in-progress: true

jobs:
  e2e:
    name: E2E Visual Regression Tests
    runs-on: ubuntu-latest
    timeout-minutes: 15
    if: github.event.label.name == 'vrt' || contains(github.event.pull_request.labels.*.name, 'vrt')

    permissions:
      id-token: write
      contents: write

    steps:
      - name: Checkout code
        uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
        with:
          fetch-depth: 0
          persist-credentials: false

      - name: Setup Node & pnpm
        uses: ./.github/actions/setup-node

      - name: Install Playwright
        run: |
          sudo npx playwright install-deps
          pnpm exec playwright install

      - name: Set up Git
        uses: ./.github/actions/setup-git
        with:
          github_token: ${{ secrets.GITHUB_TOKEN }}
          origin: ${{ github.head_ref }}

      - name: Build project
        run: pnpm build

      - name: Capture screenshots
        timeout-minutes: 3
        run: |
          if [ -d "e2e/snapshots" ]; then
            rm -rf e2e/snapshots
          fi
          pnpm e2e
        env:
          MAX_CONCURRENCY: 10

      - name: Commit screenshots
        run: |
          set -x

          git add e2e/snapshots
          git commit -m 'ci(snapshots): update screenshots'
          git push origin HEAD:${{ github.head_ref }}
