name: Lighthouse Report

on:
  pull_request:
    branches:
      - main
      - docusaurus-v**

permissions:
  contents: read

concurrency:
  group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
  cancel-in-progress: true

jobs:
  lighthouse-report:
    permissions:
      pull-requests: write # for marocchino/sticky-pull-request-comment
    name: Lighthouse Report
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # v6.0.1

      - name: Use Node.js
        uses: actions/setup-node@395ad3262231945c25e8478fd5baf05154b1d79f # v6.1.0
        with:
          node-version: lts/*
          cache: yarn

      - name: Install dependencies
        run: yarn || yarn || yarn

      - name: Build website fast
        run: yarn build:website:fast

      - name: Audit URLs using Lighthouse
        id: lighthouse_audit
        uses: treosh/lighthouse-ci-action@fcd65974f7c4c2bf0ee9d09b84d2489183c29726 # 12.6.1
        with:
          urls: |
            http://localhost:3000
            http://localhost:3000/docs/installation
            http://localhost:3000/docs/category/getting-started
            http://localhost:3000/blog
            http://localhost:3000/blog/preparing-your-site-for-docusaurus-v3
            http://localhost:3000/blog/tags/release
            http://localhost:3000/blog/tags
          configPath: ./.github/workflows/lighthouserc.json
          uploadArtifacts: true
          temporaryPublicStorage: true

      - name: Format lighthouse score
        id: format_lighthouse_score
        uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # 8.0.0
        with:
          github-token: ${{ secrets.GITHUB_TOKEN }}
          script: |
            const results = ${{ steps.lighthouse_audit.outputs.manifest }}
            const links = ${{ steps.lighthouse_audit.outputs.links }}
            const createLighthouseReport = (await import(`${process.env.GITHUB_WORKSPACE}/admin/scripts/formatLighthouseReport.js`)).default;
            const comment = createLighthouseReport({ results, links });
            core.setOutput("comment", comment);

      - name: Add Lighthouse stats as comment
        id: comment_to_pr
        uses: marocchino/sticky-pull-request-comment@773744901bac0e8cbb5a0dc842800d45e9b2b405 # 2.9.4
        with:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          number: ${{ github.event.pull_request.number }}
          header: lighthouse
          message: ${{ steps.format_lighthouse_score.outputs.comment }}
