name: API

on:
  push:
    branches: [main]
    paths:
      - .github/workflows/**
      - api/**
      - shared/**

  pull_request:
    branches: [main]
    paths:
      - .github/workflows/**
      - api/**
      - shared/**

jobs:
  python:
    uses: ./.github/workflows/_python.yml
    with:
      context: api
      test: false

  test-with-database:
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        release:
          - buster
          - bullseye
          - bionic
          - focal
          - jammy

    services:
      postgres:
        image: postgres
        env:
          POSTGRES_USER: libretime
          POSTGRES_PASSWORD: libretime
        options: >-
          --health-cmd pg_isready
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5

    container:
      image: ghcr.io/libretime/libretime-dev:${{ matrix.release }}
      options: --user 1001
    defaults:
      run:
        shell: bash
    env:
      LIBRETIME_DATABASE_HOST: postgres

    steps:
      - uses: actions/checkout@v3

      - uses: actions/cache@v3
        with:
          path: ~/.cache/pip
          key: ${{ matrix.release }}-pip-api-${{ hashFiles('api/**/setup.py') }}
          restore-keys: |
            ${{ matrix.release }}-pip-api

      - name: Test
        run: make test
        working-directory: api

      - name: Report coverage
        uses: codecov/codecov-action@v3
        with:
          files: api/coverage.xml
          flags: api
          name: api