name: backend
on:
  push:
    branches:
      - master
      - releases/**
  pull_request:

jobs:
  test:
    name: backend test
    runs-on: ubuntu-20.04
    timeout-minutes: 20
    strategy:
      matrix:
        python-version: [3.8.12]
        # XXX: When updating this, make sure you also update MATRIX_INSTANCE_TOTAL.
        instance: [0, 1, 2]

    env:
      # XXX: MATRIX_INSTANCE_TOTAL must be hardcoded to the length of strategy.matrix.instance.
      MATRIX_INSTANCE_TOTAL: 3
      MIGRATIONS_TEST_MIGRATE: 1

    steps:
      - uses: actions/checkout@v2

        with:
          # Avoid codecov error message related to SHA resolution:
          # https://github.com/codecov/codecov-bash/blob/7100762afbc822b91806a6574658129fe0d23a7d/codecov#L891
          fetch-depth: '2'

      # If we make these jobs "required" to merge on GH, then on every PR, GitHub automatically
      # creates a status check in the "pending" state. This means that the workflow needs to run
      # for every PR in order to update the status checks.
      #
      # In order to optimize CI usage, we want the tests to only run when python files change,
      # since frontend changes should have no effect on these test suites. We cannot use GH workflow
      # path filters because entire workflow would be skipped vs skipping individual jobs which
      # would still allow this status check to pass.
      - name: Check for python file changes
        uses: getsentry/paths-filter@v2
        id: changes
        with:
          token: ${{ github.token }}
          filters: .github/file-filters.yml

      - name: Setup sentry env (python ${{ matrix.python-version }})
        uses: ./.github/actions/setup-sentry
        if: steps.changes.outputs.backend == 'true'
        id: setup
        with:
          python-version: ${{ matrix.python-version }}
          pip-cache-version: ${{ secrets.PIP_CACHE_VERSION }}
          snuba: true

      - name: Run backend test (${{ steps.setup.outputs.matrix-instance-number }} of ${{ steps.setup.outputs.matrix-instance-total }})
        if: steps.changes.outputs.backend == 'true'
        run: |
          # Note: `USE_SNUBA` is not used for backend tests because there are a few failing tests with Snuba enabled.
          unset USE_SNUBA
          make test-python-ci

      - name: Handle artifacts
        uses: ./.github/actions/artifacts