# Generated by `starci compile --target circleci` from changed-files.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:
#  - The script reads ctx.changed_files, so its plan depends on the commit.
#    This file is the plan for a commit about which nothing is known, and is
#    the same for every push: steps the script would add or drop for a
#    particular branch or set of changed files are not represented.
#  - Passes are remembered in this project's CircleCI cache, which expires.
#    starci's own store does not.

version: 2.1

jobs:
  api-tests:
    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 '/api'
      - run:
          name: "Identify the inputs"
          command: |
            git ls-files -s -- ':(top,literal)api' > /tmp/starci-inputs
      - restore_cache:
          name: Has this already passed with these inputs?
          keys:
            - starci-passed-api-tests-5bc1e5c11433e410-{{ 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: "api tests"
          environment:
            STARCI_RUN: |-
              cd api && pip install -e . && pytest -q
          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-api-tests-5bc1e5c11433e410-{{ checksum "/tmp/starci-inputs" }}
          paths:
            - ~/starci-passed

  terraform-fmt:
    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 '/terraform'
      - run:
          name: "Identify the inputs"
          command: |
            git ls-files -s -- ':(top,literal)terraform' > /tmp/starci-inputs
      - restore_cache:
          name: Has this already passed with these inputs?
          keys:
            - starci-passed-terraform-fmt-0e31302fd31c38ab-{{ 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: "terraform fmt"
          environment:
            STARCI_RUN: |-
              terraform fmt -check -recursive terraform
          command: |
            docker run --rm \
              -v "$PWD:/workspace" \
              -w /workspace \
              hashicorp/terraform:1.9 sh -c "$STARCI_RUN"
      - run:
          name: "Remember this pass"
          command: |
            mkdir -p ~/starci-passed && date > ~/starci-passed/at
      - save_cache:
          key: starci-passed-terraform-fmt-0e31302fd31c38ab-{{ checksum "/tmp/starci-inputs" }}
          paths:
            - ~/starci-passed

workflows:
  ci:
    jobs:
      - api-tests
      - terraform-fmt
