name: GH-Pages PR Comment

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

env:
  WEB_URL: 'https://chipsalliance.github.io/Cores-VeeR-EL2/'

jobs:
  comment:
    name: PR Comment
    runs-on: ubuntu-24.04
    permissions:
      pull-requests: write
    if: ${{ github.event.workflow_run.event == 'pull_request' && github.event.workflow_run.conclusion == 'success' }}
    steps:

      - name: Download artifact
        id: download-artifact
        uses: dawidd6/action-download-artifact@v6
        with:
          name: pr_number
          path: ./
          run_id: ${{ github.event.workflow_run.id }}

      - name: Extract PR number
        id: PR
        run: |
          cat pr_number.txt | tee "$GITHUB_OUTPUT"

      - name: Post comment (internal PR)
        if: github.event.workflow_run.head_repository.owner.login == 'chipsalliance'
        uses: actions/github-script@v6
        with:
          script: |
            github.rest.issues.createComment({
              issue_number: ${{ steps.PR.outputs.number }},
              owner: context.repo.owner,
              repo: context.repo.repo,
              body: 'Coverage report for this PR is available at ${{ env.WEB_URL }}/html/dev/${{ steps.PR.outputs.number }}/coverage_dashboard/all, documentation is available at ${{ env.WEB_URL }}/html/dev/${{ steps.PR.outputs.number }}/docs_rendered/html'
            })

      - name: Post comment (external PR)
        if: github.event.workflow_run.head_repository.owner.login != 'chipsalliance'
        uses: actions/github-script@v6
        with:
          script: |
            github.rest.issues.createComment({
              issue_number: ${{ steps.PR.outputs.number }},
              owner: context.repo.owner,
              repo: context.repo.repo,
              body: 'Coverage dashboard artifact for this PR is available in the [CI run](${{ github.event.workflow_run.html_url }})'
            })
