name: Playwright Report Comment

on:
  workflow_run:
    workflows: ["CI"]
    types:
      - completed

permissions:
  contents: read
  pull-requests: write
  issues: write
  actions: read

jobs:
  comment:
    if: ${{ github.event.workflow_run.event == 'pull_request' && github.event.workflow_run.pull_requests[0].number }}
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4
        with:
          ref: ${{ github.event.workflow_run.base_ref }}

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: lts/*

      - name: Download Playwright HTML report
        uses: actions/download-artifact@v4
        with:
          name: html-report--attempt-${{ github.event.workflow_run.run_attempt }}
          path: playwright-report
          github-token: ${{ github.token }}
          repository: ${{ github.event.workflow_run.repository.full_name }}
          run-id: ${{ github.event.workflow_run.id }}
          if-no-artifact-found: warn

      - name: Download blob reports
        uses: actions/download-artifact@v4
        with:
          path: all-blob-reports
          pattern: blob-report-*
          merge-multiple: true
          github-token: ${{ github.token }}
          repository: ${{ github.event.workflow_run.repository.full_name }}
          run-id: ${{ github.event.workflow_run.id }}
          if-no-artifact-found: warn

      - name: Generate Playwright summary comment
        uses: actions/github-script@v7
        env:
          PR_NUMBER: ${{ github.event.workflow_run.pull_requests[0].number }}
          PLAYWRIGHT_RUN_ID: ${{ github.event.workflow_run.id }}
        with:
          script: |
            const { run } = require('./scripts/generate-playwright-summary.js');
            await run({ github, context, core });
