name: Update cmdpriv-template

on:
  push:
    paths:
      - 'pkg/qqbot/cmds/**'
  pull_request:
    types: [closed]
    paths:
      - 'pkg/qqbot/cmds/**'

jobs:
  update-cmdpriv-template:
    if: github.event.pull_request.merged == true || github.event_name == 'push'
    runs-on: ubuntu-latest

    steps:
    - name: Checkout repository
      uses: actions/checkout@v2

    - name: Set up Python
      uses: actions/setup-python@v2
      with:
        python-version: 3.x

    - name: Install dependencies
      run: |
        python -m pip install --upgrade pip
        python -m pip install --upgrade  yiri-mirai openai colorlog func_timeout dulwich Pillow

    - name: Copy Scripts
      run: |
        cp res/scripts/generate_cmdpriv_template.py .  

    - name: Generate Files
      run: |
        python main.py

    - name: Run generate_cmdpriv_template.py
      run: python3 generate_cmdpriv_template.py

    - name: Check for changes in cmdpriv-template.json
      id: check_changes
      run: |
        if git diff --name-only | grep -q "res/templates/cmdpriv-template.json"; then
          echo "::set-output name=changes_detected::true"
        else
          echo "::set-output name=changes_detected::false"
        fi

    - name: Commit changes to cmdpriv-template.json
      if: steps.check_changes.outputs.changes_detected == 'true'
      run: |
        git config --global user.name "GitHub Actions Bot"
        git config --global user.email "<github-actions@github.com>"
        git add res/templates/cmdpriv-template.json
        git commit -m "Update cmdpriv-template.json"
        git push
