name: "Unit test coverage"
on:
  push:
  pull_request:
env:
  MIN_COVERAGE_PERCENTAGE: 85
  DEBIAN_FRONTEND: noninteractive
  TZ: UTC
jobs:
  # gradle test for modules
  pytest:
    runs-on: general
    container:
      image: ubuntu:24.04
    steps:
      # need to setup node and git
      - run: |
          apt-get update
          apt-get install -y nodejs git curl libsqlite3-0
      # check out code and set up python
      - uses: actions/checkout@v4
      - uses: https://github.com/actions/setup-python@v5
        with:
          python-version: '3.11'
      # install dependencies
      - run: pip install -e .[dev]
      # run pytest with coverage
      - run: pytest --cov=amqp --cov-report=xml
      # process coverage report
      - name: Collect coverage report
        id: coverage
        run: |
          COVERAGE_PERCENTAGE=$(coverage report --format=total)
          echo "Code coverage: ${COVERAGE_PERCENTAGE}%"
          echo "Minimal coverage: ${MIN_COVERAGE_PERCENTAGE}%"
          echo "percentage=${COVERAGE_PERCENTAGE}" >> $GITHUB_OUTPUT
      - name: Post coverage to RP
        if: github.event_name == 'pull_request'
        run: |
          curl --fail \
            -X POST '${{github.api_url}}/repos/${{github.repository}}/issues/${{github.event.number}}/comments' \
            -H "Content-Type: application/json" \
            -H "Authorization: token ${{github.token}}" \
            -d '{"body":"Coverage is ${{steps.coverage.outputs.percentage}}%"}'
      - name: Check coverage rate
        run: |
          coverage report --fail-under=${MIN_COVERAGE_PERCENTAGE}