name: E2E - Generate Timings

on:
  # run this workflow every night at 3am
  schedule:
    - cron: '28 3 * * *'
  # or when the user triggers it from GitHub Actions page
  workflow_dispatch:

concurrency: ${{ github.workflow }}-${{ github.ref }}

permissions:
  contents: write
  pull-requests: write

jobs:
  timings:
    runs-on: ubuntu-latest
    container:
      image: cypress/browsers:node-20.11.0-chrome-121.0.6167.85-1-ff-120.0-edge-121.0.2277.83-1
      options: --user 1001
    steps:
      - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
      - uses: pnpm/action-setup@a7487c7e89a18df4991f7f222e4898a00d66ddda # v4.1.0
      - name: Setup Node.js
        uses: actions/setup-node@1d0ff469b7ec7b3cb9d8673fde0c81c44821de2a # v4.2.0
        with:
          node-version-file: '.node-version'
      - name: Install dependencies
        uses: cypress-io/github-action@18a6541367f4580a515371905f499a27a44e8dbe # v6.7.12
        with:
          runTests: false

      - name: Cypress run
        uses: cypress-io/github-action@18a6541367f4580a515371905f499a27a44e8dbe # v6.7.12
        id: cypress
        with:
          install: false
          start: pnpm run dev:coverage
          wait-on: 'http://localhost:9000'
          browser: chrome
          publish-summary: false
        env:
          VITEST_COVERAGE: true
          CYPRESS_COMMIT: ${{ github.sha }}
          SPLIT: 1
          SPLIT_INDEX: 0
          SPLIT_FILE: 'cypress/timings.json'

      - name: Compare timings
        id: compare
        run: |
          OUTPUT=$(pnpm tsx scripts/compare-timings.ts)
          echo "$OUTPUT" >> $GITHUB_STEP_SUMMARY

          echo "output<<EOF" >> $GITHUB_OUTPUT
          echo "$OUTPUT" >> $GITHUB_OUTPUT
          echo "EOF" >> $GITHUB_OUTPUT

      - name: Commit and create pull request
        uses: peter-evans/create-pull-request@07cbaebb4bfc9c5d7db426ea5a5f585df29dd0a0
        with:
          add-paths: |
            cypress/timings.json
          commit-message: 'chore: update E2E timings'
          branch: update-timings
          title: Update E2E Timings
          body: ${{ steps.compare.outputs.output }}
          delete-branch: true
          sign-commits: true
