# Generated by `starci compile --target circleci` 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 project's CircleCI cache, which expires.
#    starci's own store does not.

version: 2.1

jobs:
  lint:
    machine:
      image: ubuntu-2404:current
    resource_class: medium
    steps:
      - checkout
      - run:
          name: "Keep only this step's inputs"
          command: |
            git sparse-checkout set --no-cone '/pyproject.toml' '/src' '/tests'
      - run:
          name: "Identify the inputs"
          command: |
            git ls-files -s -- ':(top,literal)pyproject.toml' ':(top,literal)src' ':(top,literal)tests' > /tmp/starci-inputs
      - restore_cache:
          name: Has this already passed with these inputs?
          keys:
            - starci-passed-lint-d87a15139de0a6d7-{{ checksum "/tmp/starci-inputs" }}
      - run:
          name: "Stop here if it has"
          command: |
            if [ -e ~/starci-passed/at ]; then
              echo "Passed before with the same inputs, on $(cat ~/starci-passed/at). Nothing to do."
              circleci-agent step halt
            fi
      - run:
          name: "lint"
          environment:
            STARCI_RUN: |-
              pip install -q ruff && ruff check .
          command: |
            docker run --rm \
              -v "$PWD:/workspace" \
              -w /workspace \
              python:3.13-slim sh -c "$STARCI_RUN"
      - run:
          name: "Remember this pass"
          command: |
            mkdir -p ~/starci-passed && date > ~/starci-passed/at
      - save_cache:
          key: starci-passed-lint-d87a15139de0a6d7-{{ checksum "/tmp/starci-inputs" }}
          paths:
            - ~/starci-passed

  api-tests:
    machine:
      image: ubuntu-2404:current
    resource_class: medium
    steps:
      - checkout
      - run:
          name: "Files for the services"
          command: |
            mkdir -p "$(dirname "$HOME/starci-services"'/postgres/db/roles.sql')"
            cp -R 'db/roles.sql' "$HOME/starci-services"'/postgres/db/roles.sql'
      - run:
          name: "Keep only this step's inputs"
          command: |
            git sparse-checkout set --no-cone '/pyproject.toml' '/src' '/tests/api' '/tests/conftest.py'
      - run:
          name: "Identify the inputs"
          command: |
            git ls-files -s -- ':(top,literal)pyproject.toml' ':(top,literal)src' ':(top,literal)tests/api' ':(top,literal)tests/conftest.py' ':(top,literal)db/roles.sql' > /tmp/starci-inputs
      - restore_cache:
          name: Has this already passed with these inputs?
          keys:
            - starci-passed-api-tests-1b86b1a2577c889f-{{ checksum "/tmp/starci-inputs" }}
      - run:
          name: "Stop here if it has"
          command: |
            if [ -e ~/starci-passed/at ]; then
              echo "Passed before with the same inputs, on $(cat ~/starci-passed/at). Nothing to do."
              circleci-agent step halt
            fi
      - run:
          name: "Services: postgres"
          command: |
            docker network create "starci-$CIRCLE_BUILD_NUM-api-tests"
            docker run -d --name "starci-$CIRCLE_BUILD_NUM-api-tests-postgres" --network "starci-$CIRCLE_BUILD_NUM-api-tests" --network-alias postgres --hostname postgres -e 'POSTGRES_DB=app' -e 'POSTGRES_PASSWORD=app' -e 'POSTGRES_USER=app' -v "$HOME/starci-services"'/postgres/db/roles.sql:/docker-entrypoint-initdb.d/10-roles.sql':ro postgres:17
            until=$(($(date +%s) + 120))
            until timeout 10 docker exec "starci-$CIRCLE_BUILD_NUM-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-$CIRCLE_BUILD_NUM-api-tests-postgres"
                exit 1
              fi
              sleep 0.5
            done
      - run:
          name: "api tests"
          environment:
            STARCI_RUN: |-
              pip install -q -e '.[test]' && pytest -q tests/api
          command: |
            docker run --rm \
              -v "$PWD:/workspace" \
              -w /workspace \
              --network "starci-$CIRCLE_BUILD_NUM-api-tests" \
              -e 'DATABASE_URL=postgresql://app:app@postgres:5432/app' \
              python:3.13-slim sh -c "$STARCI_RUN"
      - run:
          name: Stop the services
          when: always
          command: |
            docker rm -f -v "starci-$CIRCLE_BUILD_NUM-api-tests-postgres" || true
            docker network rm "starci-$CIRCLE_BUILD_NUM-api-tests" || true
      - run:
          name: "Remember this pass"
          command: |
            mkdir -p ~/starci-passed && date > ~/starci-passed/at
      - save_cache:
          key: starci-passed-api-tests-1b86b1a2577c889f-{{ checksum "/tmp/starci-inputs" }}
          paths:
            - ~/starci-passed

  storage-tests:
    machine:
      image: ubuntu-2404:current
    resource_class: medium
    steps:
      - checkout
      - run:
          name: "Files for the services"
          command: |
            mkdir -p "$(dirname "$HOME/starci-services"'/postgres/db/roles.sql')"
            cp -R 'db/roles.sql' "$HOME/starci-services"'/postgres/db/roles.sql'
      - run:
          name: "Keep only this step's inputs"
          command: |
            git sparse-checkout set --no-cone '/pyproject.toml' '/src' '/tests/conftest.py' '/tests/storage'
      - run:
          name: "Identify the inputs"
          command: |
            git ls-files -s -- ':(top,literal)pyproject.toml' ':(top,literal)src' ':(top,literal)tests/conftest.py' ':(top,literal)tests/storage' ':(top,literal)db/roles.sql' > /tmp/starci-inputs
      - restore_cache:
          name: Has this already passed with these inputs?
          keys:
            - starci-passed-storage-tests-9dd714af153780db-{{ checksum "/tmp/starci-inputs" }}
      - run:
          name: "Stop here if it has"
          command: |
            if [ -e ~/starci-passed/at ]; then
              echo "Passed before with the same inputs, on $(cat ~/starci-passed/at). Nothing to do."
              circleci-agent step halt
            fi
      - run:
          name: "Services: postgres"
          command: |
            docker network create "starci-$CIRCLE_BUILD_NUM-storage-tests"
            docker run -d --name "starci-$CIRCLE_BUILD_NUM-storage-tests-postgres" --network "starci-$CIRCLE_BUILD_NUM-storage-tests" --network-alias postgres --hostname postgres -e 'POSTGRES_DB=app' -e 'POSTGRES_PASSWORD=app' -e 'POSTGRES_USER=app' -v "$HOME/starci-services"'/postgres/db/roles.sql:/docker-entrypoint-initdb.d/10-roles.sql':ro postgres:17
            until=$(($(date +%s) + 120))
            until timeout 10 docker exec "starci-$CIRCLE_BUILD_NUM-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-$CIRCLE_BUILD_NUM-storage-tests-postgres"
                exit 1
              fi
              sleep 0.5
            done
      - run:
          name: "storage tests"
          environment:
            STARCI_RUN: |-
              pip install -q -e '.[test]' && pytest -q tests/storage
          command: |
            docker run --rm \
              -v "$PWD:/workspace" \
              -w /workspace \
              --network "starci-$CIRCLE_BUILD_NUM-storage-tests" \
              -e 'DATABASE_URL=postgresql://app:app@postgres:5432/app' \
              python:3.13-slim sh -c "$STARCI_RUN"
      - run:
          name: Stop the services
          when: always
          command: |
            docker rm -f -v "starci-$CIRCLE_BUILD_NUM-storage-tests-postgres" || true
            docker network rm "starci-$CIRCLE_BUILD_NUM-storage-tests" || true
      - run:
          name: "Remember this pass"
          command: |
            mkdir -p ~/starci-passed && date > ~/starci-passed/at
      - save_cache:
          key: starci-passed-storage-tests-9dd714af153780db-{{ checksum "/tmp/starci-inputs" }}
          paths:
            - ~/starci-passed

workflows:
  ci:
    jobs:
      - lint
      - api-tests
      - storage-tests
