name: Submit coverage

# read-write repo token
# access to secrets
on:
  workflow_run:
    workflows: [Build and test]
    types: [completed]

env:
  COVERALLS_REPO_TOKEN: ${{ secrets.COVERALLS_REPO_TOKEN }}

jobs:
  upload:
    runs-on: ubuntu-latest
    if: >
      github.event.workflow_run.event == 'pull_request' &&
      github.event.workflow_run.conclusion == 'success'
    steps:
      - name: Download artifact
        uses: dawidd6/action-download-artifact@v2
        with:
          workflow: ${{ github.event.workflow_run.workflow_id }}
          workflow_conclusion: success
          name: coveralls.json


      - name: 'Prepare dependencies'
        run:   |
          sudo apt-get update && \
          sudo apt-get install -yq \
            jq \
            curl
        shell: bash

      - name: 'Generate full report'
        run: |
          cat coveralls.json | jq --arg repo_token $COVERALLS_REPO_TOKEN '. += {repo_token: $repo_token}' > coveralls_full.json
        shell: bash

      - name: 'Upload final report'
        run:   |
          curl --request POST \
            --url https://coveralls.io/api/v1/jobs \
            --header 'Content-Type: multipart/form-data' \
            --form json_file=@./coveralls_full.json
        shell: bash
