name: Backend CI Jobs

on:
  workflow_call:

jobs:

  code-checks-backend:
    name: "Code Style Checks"
    runs-on: ubuntu-20.04
    steps:
      -
        name: Checkout
        uses: actions/checkout@v3
      -
        name: Install checkers
        run: |
          pipx install reorder-python-imports
          pipx install yesqa
          pipx install add-trailing-comma
          pipx install flake8
      -
        name: Run reorder-python-imports
        run: |
          find src/ -type f -name '*.py' ! -path "*/migrations/*" | xargs reorder-python-imports
      -
        name: Run yesqa
        run: |
          find src/ -type f -name '*.py' ! -path "*/migrations/*" | xargs yesqa
      -
        name: Run add-trailing-comma
        run: |
          find src/ -type f -name '*.py' ! -path "*/migrations/*" | xargs add-trailing-comma
      # black is placed after add-trailing-comma because it may format differently
      # if a trailing comma is added
      -
        name: Run black
        uses: psf/black@stable
        with:
          options: "--check --diff"
          version: "22.3.0"
      -
        name: Run flake8 checks
        run: |
          cd src/
          flake8 --max-line-length=88 --ignore=E203,W503

  tests-backend:
    name: "Tests (${{ matrix.python-version }})"
    runs-on: ubuntu-20.04
    needs:
      - code-checks-backend
    strategy:
      matrix:
        python-version: ['3.8', '3.9', '3.10']
      fail-fast: false
    steps:
      -
        name: Checkout
        uses: actions/checkout@v3
        with:
          fetch-depth: 2
      -
        name: Install pipenv
        run: pipx install pipenv
      -
        name: Set up Python
        uses: actions/setup-python@v3
        with:
          python-version: "${{ matrix.python-version }}"
          cache: "pipenv"
          cache-dependency-path: 'Pipfile.lock'
      -
        name: Install system dependencies
        run: |
          sudo apt-get update -qq
          sudo apt-get install -qq --no-install-recommends unpaper tesseract-ocr imagemagick ghostscript optipng libzbar0 poppler-utils
      -
        name: Install Python dependencies
        run: |
          pipenv sync --dev
      -
        name: Tests
        run: |
          cd src/
          pipenv run pytest
      -
        name: Get changed files
        id: changed-files-specific
        uses: tj-actions/changed-files@v22.1
        with:
          files: |
            src/**
      -
        name: List all changed files
        run: |
          for file in ${{ steps.changed-files-specific.outputs.all_changed_files }}; do
            echo "${file} was changed"
          done
      -
        name: Publish coverage results
        if: matrix.python-version == '3.9' && steps.changed-files-specific.outputs.any_changed == 'true'
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        # https://github.com/coveralls-clients/coveralls-python/issues/251
        run: |
          cd src/
          pipenv run coveralls --service=github
