name: 📝 Convert YAML to JSON

on:
  push:
    paths: ['QUERIES.yaml']
    branches: [main]
  workflow_dispatch:
  workflow_run:
    workflows: ["🌥 Sync queries from Public Templates"]
    types:
      - completed

jobs:
  build:
    runs-on: ubuntu-latest
    permissions:
      contents: write
    steps:
      - uses: actions/checkout@master
        with:
          fetch-depth: 0

      - run: pip3 install pyyaml --user

      - name: Convert YAML to JSON
        run: cat QUERIES.yaml | python3 .github/scripts/yaml2json.py > QUERIES.json

      - name: Commit and push changes
        run: |
          [[ "$(git status -s | wc -l)" == 0 ]] && exit 0

          git config --local user.email "github-actions[bot]@users.noreply.github.com"
          git config --local user.name "github-actions[bot]"

          COMMIT_MSG="build: "
          [[ "${{ github.event_name }}" == "workflow_dispatch" ]] && COMMIT_MSG+="[force] "
          COMMIT_MSG+="compile JSON [$(date "+%d/%m/%Y %T")] :robot:"

          git commit -am "${COMMIT_MSG}"
          git push origin "${GITHUB_REF_NAME}"
