version: 2.1

setup: true

on_main_or_tag_filter: &on_main_or_tag_filter
  filters:
    branches:
      only: main
    tags:
      only: /^v\d+\.\d+\.\d+/

on_tag_filter: &on_tag_filter
  filters:
    branches:
      ignore: /.*/
    tags:
      only: /^v\d+\.\d+\.\d+/

orbs:
  path-filtering: circleci/path-filtering@1.2.0

jobs:
  publish:
    docker:
      - image: cimg/python:3.10
    resource_class: small
    steps:
      - checkout
      - attach_workspace:
          at: web/client
      - run:
          name: Publish Python package
          command: make publish
      - run:
          name: Update pypirc
          command: ./.circleci/update-pypirc.sh
      - run:
          name: Publish Python Tests package
          command: unset TWINE_USERNAME TWINE_PASSWORD && make publish-tests
  gh-release:
    docker:
      - image: cimg/node:20.19.0
    resource_class: small
    steps:
      - run:
          name: Create release on GitHub
          command: |
            GITHUB_TOKEN="$GITHUB_TOKEN" \
            TARGET_TAG="$CIRCLE_TAG" \
            REPO_OWNER="$CIRCLE_PROJECT_USERNAME" \
            REPO_NAME="$CIRCLE_PROJECT_REPONAME" \
            CONTINUE_ON_ERROR="false" \
            npx https://github.com/TobikoData/circleci-gh-conventional-release

  ui-build:
    docker:
      - image: cimg/node:20.19.0
    resource_class: medium
    steps:
      - checkout
      - run:
          name: Install Dependencies
          command: |
            pnpm install
      - run:
          name: Build UI
          command: pnpm --prefix web/client run build
      - persist_to_workspace:
          root: web/client
          paths:
            - dist
  trigger_private_renovate:
    docker:
      - image: cimg/base:2021.11
    resource_class: small
    steps:
      - run:
          name: Trigger private renovate
          command: |
            curl --request POST \
              --url $TOBIKO_PRIVATE_CIRCLECI_URL \
              --header "Circle-Token: $TOBIKO_PRIVATE_CIRCLECI_KEY" \
              --header "content-type: application/json" \
              --data '{
                "branch":"main",
                "parameters":{
                  "run_main_pr":false,
                  "run_sqlmesh_commit":false,
                  "run_renovate":true
                  }
              }'

workflows:
  setup-workflow:
    jobs:
      - path-filtering/filter:
          mapping: |
            web/client/.* client true
            (sqlmesh|tests|examples|web/server)/.* python true
            pytest.ini|setup.cfg|setup.py|pyproject.toml python true
            \.circleci/.*|Makefile|\.pre-commit-config\.yaml common true
            vscode/extensions/.* vscode true
          tag: "3.9"
      - gh-release:
          <<: *on_tag_filter
      - ui-build:
          <<: *on_main_or_tag_filter
      - publish:
          <<: *on_main_or_tag_filter
          requires:
            - ui-build
      - trigger_private_renovate:
          <<: *on_tag_filter
          requires:
            - publish
