version: 2.1

setup: true

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

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

jobs:
  publish:
    docker:
      - image: cimg/python:3.10
    steps:
      - checkout
      - run:
          name: Publish Python package
          command: make publish
      - run:
          name: Update API Documentation
          command: |
            pip install pdoc sqlmesh
            make api-docs
            git add docs
            git config --local user.email "41898282+github-actions[bot]@users.noreply.github.com"
            git config --local user.name "github-actions[bot]"
            git commit -m "CI: Auto-generated documentation" -a | exit 0
      - run:
          name: Deploy API Documentation
          command: |
            GITHUB_TOKEN="$GITHUB_TOKEN" \
            git push origin main

  gh-release:
    docker:
      - image: cimg/node:16.14
    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

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 python true
            \.circleci/.*|Makefile|\.pre-commit-config\.yaml common true

  release:
    jobs:
      - gh-release:
          <<: *on_tag_filter
      - publish:
          <<: *on_tag_filter
          requires:
            - gh-release
