name: Post coverage comment

on:
  workflow_call:
    inputs:
      # this is the workflow run ID of the `CI.yml` workflow
      workflow_run_id:
        required: true
        type: string
jobs:
  test:
    name: "Display code coverage"
    runs-on: ubuntu-latest
    permissions:
      pull-requests: write
      contents: write # needed to edit the comment vs opening multiple ones
      actions: read
    steps:
      - name: "Get the triggering workflow run details"
        id: get-run
        uses: octokit/request-action@b91aabaa861c777dcdb14e2387e30eddf04619ae
        with:
          route: GET /repos/"${REPO}"/actions/runs/"${WORKFLOW_RUN_ID}"
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          REPO: ${{ github.repository }}
          WORKFLOW_RUN_ID: ${{ inputs.workflow_run_id }}

      # this needs the .coverage file so we download from the CI workflow artifacts
      - name: "Download coverage data 📥"
        uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c
        with:
          pattern: coverage-data-*
          merge-multiple: true
          github-token: ${{ secrets.GITHUB_TOKEN }}
          run-id: ${{ inputs.workflow_run_id }}

      - name: "Check downloaded files"
        run: ls -R

      - name: "Post coverage comment 💬"
        uses: py-cov-action/python-coverage-comment-action@7188638f871f721a365d644f505d1ff3df20d683 # 3.38.0
        with:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          GITHUB_PR_RUN_ID: ${{ inputs.workflow_run_id }}
