on: [pull_request]

name: Test coverage report # main.yml

permissions:
    # allow dependabot to execute this workflow
    pull-requests: write

jobs:
    hello_world_job:
        runs-on: ubuntu-latest
        name: Test and report
        steps:
            - name: Checkout
              uses: actions/checkout@v6
              with:
                  ref: ${{ github.event.pull_request.head.sha }}
                  fetch-depth: 1000

            - uses: actions/setup-node@v6
              with:
                  node-version-file: ./package.json

            - run: npm ci --ignore-scripts

            - name: Run mcdev-tests with coverage
              run: npm run coverage

            - name: Prepare for Report Coverage
              run: |
                  npx c8 report --reporter json-summary --exclude-after-remap false
                  npm i svelte-to-html@1.0.9
                  git fetch origin ${{ github.event.pull_request.base.ref }} --depth=1000

            - name: Upload coverage artifact
              uses: actions/upload-artifact@v7
              with:
                  name: test-coverage-output
                  path: coverage
                  overwrite: true
                  retention-days: 90

            - name: Download coverage artifact
              uses: actions/download-artifact@v8
              with:
                  path: artifacts/test-coverage-output

            - name: Get Run ID
              id: get_run_id
              run: |
                  echo "name=run_id::$(\
                    gh run list \
                      --workflow "${{ github.event.pull_request.base.ref == 'main' && 'coverage-main-branch.yml' || github.event.pull_request.base.ref == 'develop' && 'coverage-develop-branch.yml' || 'coverage.yml' }}" \
                      --json conclusion,headSha,status,databaseId \
                      --jq ".[] | select( .conclusion == \"success\" and .headSha == \"${{github.event.pull_request.base.sha}}\") | .databaseId" \
                  )" >> $GITHUB_OUTPUT
              env:
                  GITHUB_TOKEN: ${{ github.token }}

            - name: Download artifact for base branch if available, previous
              uses: dawidd6/action-download-artifact@v19
              continue-on-error: true
              with:
                  workflow: ${{ github.event.pull_request.base.ref == 'main' && 'coverage-main-branch.yml' || github.event.pull_request.base.ref == 'develop' && 'coverage-develop-branch.yml' || 'coverage.yml' }}
                  run_id: ${{steps.get_run_id.outputs.run_id}}
                  name: 'test-coverage-output'
                  path: base-artifacts
                  search_artifacts: true

            - name: Check file existence
              id: check_files
              uses: andstor/file-existence-action@v3
              with:
                  files: 'base-artifacts/coverage-summary.json, artifacts/test-coverage-output/coverage-summary.json'

            - name: Report coverage
              uses: sidx1024/report-nyc-coverage-github-action@v1.2.7
              id: report
              with:
                  coverage_file: 'artifacts/test-coverage-output/coverage-summary.json'
                  base_coverage_file: ${{steps.check_files.outputs.files_exists == 'true' && 'base-artifacts/coverage-summary.json' || ''}}
                  comment_template_file: '.coverage-comment-template.svelte'
