# Generated by `starci compile` from services.ci.star. Edit that, not this.
#
# Each job is one step of the plan: the same image, command, tools and
# packages, given only the step's inputs. A job skips its work when it has
# already passed with the same inputs.
#
# What does not carry over:
#  - Passes are remembered in this repository's Actions cache, which GitHub
#    scopes by branch and evicts when old. starci's own store has neither
#    limit.

name: CI

on:
  push:
    branches: ['**']
    tags: ['**']
  pull_request:
  workflow_dispatch:

jobs:
  lint:
    name: "lint"
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with:
          sparse-checkout-cone-mode: false
          sparse-checkout: |
            /pyproject.toml
            /src
            /tests
      - name: Has this already passed with these inputs?
        id: passed
        uses: actions/cache@v4
        with:
          path: ${{ runner.temp }}/starci-passed
          key: starci-passed-lint-d87a15139de0a6d7-${{ hashFiles('pyproject.toml', 'pyproject.toml/**', 'src', 'src/**', 'tests', 'tests/**') }}
      - name: "lint"
        if: steps.passed.outputs.cache-hit != 'true'
        env:
          STARCI_RUN: |-
            pip install -q ruff && ruff check .
        run: |
          docker run --rm \
            -v "$PWD:/workspace" \
            -w /workspace \
            python:3.13-slim sh -c "$STARCI_RUN"
      - name: Remember this pass
        if: steps.passed.outputs.cache-hit != 'true'
        run: mkdir -p "$RUNNER_TEMP/starci-passed" && date > "$RUNNER_TEMP/starci-passed/at"

  api-tests:
    name: "api tests"
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with:
          sparse-checkout-cone-mode: false
          sparse-checkout: |
            /pyproject.toml
            /src
            /tests/api
            /tests/conftest.py
            /db/roles.sql
      - name: Has this already passed with these inputs?
        id: passed
        uses: actions/cache@v4
        with:
          path: ${{ runner.temp }}/starci-passed
          key: starci-passed-api-tests-1b86b1a2577c889f-${{ hashFiles('pyproject.toml', 'pyproject.toml/**', 'src', 'src/**', 'tests/api', 'tests/api/**', 'tests/conftest.py', 'tests/conftest.py/**', 'db/roles.sql', 'db/roles.sql/**') }}
      - name: "Services: postgres"
        if: steps.passed.outputs.cache-hit != 'true'
        run: |
          mkdir -p "$(dirname "$RUNNER_TEMP/starci-services/api-tests"'/postgres/db/roles.sql')"
          cp -R 'db/roles.sql' "$RUNNER_TEMP/starci-services/api-tests"'/postgres/db/roles.sql'
          git sparse-checkout set --no-cone '/pyproject.toml' '/src' '/tests/api' '/tests/conftest.py'
          docker network create "starci-$GITHUB_RUN_ID-${GITHUB_RUN_ATTEMPT:-1}-api-tests"
          docker run -d --name "starci-$GITHUB_RUN_ID-${GITHUB_RUN_ATTEMPT:-1}-api-tests-postgres" --network "starci-$GITHUB_RUN_ID-${GITHUB_RUN_ATTEMPT:-1}-api-tests" --network-alias postgres --hostname postgres -e 'POSTGRES_DB=app' -e 'POSTGRES_PASSWORD=app' -e 'POSTGRES_USER=app' -v "$RUNNER_TEMP/starci-services/api-tests"'/postgres/db/roles.sql:/docker-entrypoint-initdb.d/10-roles.sql':ro postgres:17
          until=$(($(date +%s) + 120))
          until timeout 10 docker exec "starci-$GITHUB_RUN_ID-${GITHUB_RUN_ATTEMPT:-1}-api-tests-postgres" sh -c 'pg_isready -h localhost -U app'; do
            if [ "$(date +%s)" -ge "$until" ]; then
              echo "service postgres was not ready after 120 seconds"
              docker logs --tail 20 "starci-$GITHUB_RUN_ID-${GITHUB_RUN_ATTEMPT:-1}-api-tests-postgres"
              exit 1
            fi
            sleep 0.5
          done
      - name: "api tests"
        if: steps.passed.outputs.cache-hit != 'true'
        env:
          STARCI_RUN: |-
            pip install -q -e '.[test]' && pytest -q tests/api
        run: |
          docker run --rm \
            -v "$PWD:/workspace" \
            -w /workspace \
            --network "starci-$GITHUB_RUN_ID-${GITHUB_RUN_ATTEMPT:-1}-api-tests" \
            -e 'DATABASE_URL=postgresql://app:app@postgres:5432/app' \
            python:3.13-slim sh -c "$STARCI_RUN"
      - name: Stop the services
        if: always() && steps.passed.outputs.cache-hit != 'true'
        run: |
          docker rm -f -v "starci-$GITHUB_RUN_ID-${GITHUB_RUN_ATTEMPT:-1}-api-tests-postgres" || true
          docker network rm "starci-$GITHUB_RUN_ID-${GITHUB_RUN_ATTEMPT:-1}-api-tests" || true
      - name: Remember this pass
        if: steps.passed.outputs.cache-hit != 'true'
        run: mkdir -p "$RUNNER_TEMP/starci-passed" && date > "$RUNNER_TEMP/starci-passed/at"

  storage-tests:
    name: "storage tests"
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with:
          sparse-checkout-cone-mode: false
          sparse-checkout: |
            /pyproject.toml
            /src
            /tests/conftest.py
            /tests/storage
            /db/roles.sql
      - name: Has this already passed with these inputs?
        id: passed
        uses: actions/cache@v4
        with:
          path: ${{ runner.temp }}/starci-passed
          key: starci-passed-storage-tests-9dd714af153780db-${{ hashFiles('pyproject.toml', 'pyproject.toml/**', 'src', 'src/**', 'tests/conftest.py', 'tests/conftest.py/**', 'tests/storage', 'tests/storage/**', 'db/roles.sql', 'db/roles.sql/**') }}
      - name: "Services: postgres"
        if: steps.passed.outputs.cache-hit != 'true'
        run: |
          mkdir -p "$(dirname "$RUNNER_TEMP/starci-services/storage-tests"'/postgres/db/roles.sql')"
          cp -R 'db/roles.sql' "$RUNNER_TEMP/starci-services/storage-tests"'/postgres/db/roles.sql'
          git sparse-checkout set --no-cone '/pyproject.toml' '/src' '/tests/conftest.py' '/tests/storage'
          docker network create "starci-$GITHUB_RUN_ID-${GITHUB_RUN_ATTEMPT:-1}-storage-tests"
          docker run -d --name "starci-$GITHUB_RUN_ID-${GITHUB_RUN_ATTEMPT:-1}-storage-tests-postgres" --network "starci-$GITHUB_RUN_ID-${GITHUB_RUN_ATTEMPT:-1}-storage-tests" --network-alias postgres --hostname postgres -e 'POSTGRES_DB=app' -e 'POSTGRES_PASSWORD=app' -e 'POSTGRES_USER=app' -v "$RUNNER_TEMP/starci-services/storage-tests"'/postgres/db/roles.sql:/docker-entrypoint-initdb.d/10-roles.sql':ro postgres:17
          until=$(($(date +%s) + 120))
          until timeout 10 docker exec "starci-$GITHUB_RUN_ID-${GITHUB_RUN_ATTEMPT:-1}-storage-tests-postgres" sh -c 'pg_isready -h localhost -U app'; do
            if [ "$(date +%s)" -ge "$until" ]; then
              echo "service postgres was not ready after 120 seconds"
              docker logs --tail 20 "starci-$GITHUB_RUN_ID-${GITHUB_RUN_ATTEMPT:-1}-storage-tests-postgres"
              exit 1
            fi
            sleep 0.5
          done
      - name: "storage tests"
        if: steps.passed.outputs.cache-hit != 'true'
        env:
          STARCI_RUN: |-
            pip install -q -e '.[test]' && pytest -q tests/storage
        run: |
          docker run --rm \
            -v "$PWD:/workspace" \
            -w /workspace \
            --network "starci-$GITHUB_RUN_ID-${GITHUB_RUN_ATTEMPT:-1}-storage-tests" \
            -e 'DATABASE_URL=postgresql://app:app@postgres:5432/app' \
            python:3.13-slim sh -c "$STARCI_RUN"
      - name: Stop the services
        if: always() && steps.passed.outputs.cache-hit != 'true'
        run: |
          docker rm -f -v "starci-$GITHUB_RUN_ID-${GITHUB_RUN_ATTEMPT:-1}-storage-tests-postgres" || true
          docker network rm "starci-$GITHUB_RUN_ID-${GITHUB_RUN_ATTEMPT:-1}-storage-tests" || true
      - name: Remember this pass
        if: steps.passed.outputs.cache-hit != 'true'
        run: mkdir -p "$RUNNER_TEMP/starci-passed" && date > "$RUNNER_TEMP/starci-passed/at"
