name: Lighthouse Report

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

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

jobs:
  lighthouse-report:
    name: Lighthouse Report
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@2541b1294d2704b0964813337f33b291d3f8596b # v3
      - name: Wait for the Netlify Preview
        uses: jakepartusch/wait-for-netlify-action@7ccf91c9ba3d64aa4389c0d3adcba0a6e77e5421 # v1
        id: netlify
        with:
          site_name: docusaurus-2
          max_timeout: 600
      - name: Audit URLs using Lighthouse
        id: lighthouse_audit
        uses: treosh/lighthouse-ci-action@b4dfae3eb959c5226e2c5c6afd563d493188bfaf # 9.3.0
        with:
          urls: |
            https://deploy-preview-$PR_NUMBER--docusaurus-2.netlify.app/
            https://deploy-preview-$PR_NUMBER--docusaurus-2.netlify.app/docs/installation
          configPath: ./.github/workflows/lighthouserc.json
          uploadArtifacts: true
          temporaryPublicStorage: true
        env:
          PR_NUMBER: ${{ github.event.pull_request.number}}
      - name: Format lighthouse score
        id: format_lighthouse_score
        uses: actions/github-script@7a5c598405937d486b0331594b5da2b14db670da # v6
        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@39c5b5dc7717447d0cba270cd115037d32d28443 # v2.2.0
        with:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          number: ${{ github.event.pull_request.number }}
          header: lighthouse
          message: ${{ steps.format_lighthouse_score.outputs.comment }}
