name: build

on:
  push:
    branches: ["**"]
  pull_request:

jobs:
  test:
    runs-on: ubuntu-latest
    env:
      VROBBLER_DATABASE_URL: sqlite:///test.db
      VROBBLER_USDA_API_KEY: ${{ vars.VROBBLER_USDA_API_KEY }}

    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Setup Python
        uses: actions/setup-python@v5
        with:
          python-version: "3.11"

      - name: Cache pip/poetry
        uses: actions/cache@v4
        with:
          path: |
            ~/.cache/pip
            ~/.cache/pypoetry
          key: ${{ runner.os }}-py311-${{ hashFiles('**/poetry.lock') }}
          restore-keys: |
            ${{ runner.os }}-py311-

      - name: Install Poetry
        run: |
          python -m pip install --upgrade pip
          pip install poetry

      - name: Install deps
        run: |
          cp vrobbler.conf.test vrobbler.conf
          poetry install --with test

      - name: Pytest with coverage
        run: |
          poetry run pytest -n 5 --cov-report term:skip-covered --cov=vrobbler tests

      - name: Notify success (ntfy)
        if: success()
        run: |
          curl -fsS \
            -H "Title: vrobbler CI success" \
            -H "Priority: low" \
            -H "Tags: success,vrobbler" \
            -H "Actions: view, Changes, ${{ gitea.server_url }}/${{ gitea.repository }}/commit/${{ gitea.sha }}; view, Build, ${{ gitea.server_url }}/${{ gitea.repository }}/actions/runs/${{ gitea.run_id }}" \
            -d "✅ Build succeeded: ${{ gitea.repository }} @ ${{ gitea.sha }}" \
            https://ntfy.unbl.ink/drone

      - name: Notify failure (ntfy)
        if: failure()
        run: |
          curl -fsS \
            -H "Title: vrobbler CI failure" \
            -H "Priority: high" \
            -H "Tags: failure,vrobbler" \
            -H "Actions: view, Changes, ${{ gitea.server_url }}/${{ gitea.repository }}/commit/${{ gitea.sha }}; view, Build, ${{ gitea.server_url }}/${{ gitea.repository }}/actions/runs/${{ gitea.run_id }}" \
            -d "❌ Build failed: ${{ gitea.repository }} @ ${{ gitea.sha }}" \
            https://ntfy.unbl.ink/drone
