name: Sync python/ to adamlui/python-utils/ai-personas/

on:
  push:
    branches: [main]
    paths: [python/**]

jobs:
  build:
    if: (github.repository == 'KudoAI/ai-personas') && !contains(github.event.head_commit.message, '[auto-sync')
    runs-on: ubuntu-24.04
    permissions:
      contents: read
    env:
      TZ: PST8PDT

    steps:

      - name: Checkout KudoAI/ai-personas
        uses: actions/checkout@v6.0.2
        with:
          token: ${{ secrets.REPO_SYNC_PAT }}
          path: KudoAI/ai-personas

      - name: Checkout adamlui/python-utils
        uses: actions/checkout@v6.0.2
        with:
          token: ${{ secrets.REPO_SYNC_PAT }}
          repository: adamlui/python-utils
          path: adamlui/python-utils

      - name: Sync python/ to adamlui/python-utils/ai-personas
        run: |
          rsync -avhc --delete \
            ${{ github.workspace }}/KudoAI/ai-personas/python/ \
            ${{ github.workspace }}/adamlui/python-utils/ai-personas/

      - name: Escape backticks in commit msg
        env:
          COMMIT_MSG: ${{ github.event.head_commit.message }}
        run: |
          DELIM="EOF_$(uuidgen)"
          echo "ESCAPED_MSG<<$DELIM" >> "$GITHUB_ENV"
          echo "$COMMIT_MSG" | sed 's/`/\`/g' >> "$GITHUB_ENV"
          echo "$DELIM" >> "$GITHUB_ENV"

      - name: Config committer
        env:
          GPG_PRIVATE_KEY: ${{ secrets.GPG_PRIVATE_KEY }}
          GPG_PRIVATE_ID: ${{ secrets.GPG_PRIVATE_ID }}
        run: |
          gpg --batch --import <(echo "$GPG_PRIVATE_KEY")
          git config --global commit.gpgsign true
          git config --global user.name "kudo-sync-bot"
          git config --global user.email "auto-sync@kudoai.com"
          git config --global user.signingkey "$GPG_PRIVATE_ID"

      - name: Push changes to adamlui/python-utils
        run: |
          cd ${{ github.workspace }}/adamlui/python-utils
          git add . && git commit -n -m \
            "$ESCAPED_MSG ↞ [auto-sync from https://github.com/KudoAI/ai-personas/tree/main/python]" || true
          git pull --rebase
          git push
