name: Performance
on:
  pull_request:
    branches: [main]

permissions:
  contents: read
  pull-requests: write

jobs:
  budgets:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v7
      - uses: actions/setup-node@v7
        with:
          node-version: 22
      - name: Check every file's size
        run: node scripts/check-size.mjs
      - name: Run Lighthouse 5 times on each page
        if: ${{ !cancelled() }}
        run: npx --yes @lhci/cli@0.16.1 collect
      - name: Check each page's medians
        if: ${{ !cancelled() }}
        run: npx --yes @lhci/cli@0.16.1 assert
      - name: Save the reports
        if: ${{ !cancelled() }}
        run: npx --yes @lhci/cli@0.16.1 upload
      - name: Write the report
        if: ${{ !cancelled() }}
        run: |
          node scripts/report.mjs > report.md
          cat report.md >> "$GITHUB_STEP_SUMMARY"
      - name: Comment on the pull request
        if: ${{ !cancelled() }}
        continue-on-error: true
        run: gh pr comment "$PR" --body-file report.md --edit-last --create-if-none
        env:
          GH_TOKEN: ${{ github.token }}
          PR: ${{ github.event.pull_request.html_url }}
      - name: Keep the Lighthouse reports
        if: ${{ !cancelled() }}
        uses: actions/upload-artifact@v7
        with:
          name: lighthouse-reports
          path: lhci-reports
