name: docgen

on:
  push:
    branches-ignore:
      - main
  pull_request_target:
    branches:
      - main

env:
 BRANCH_NAME: ${{ github.head_ref || github.ref_name }} 

jobs:
  generate-docs:
    name: Generate docs
    strategy:
      matrix:
        os: [ubuntu-latest]
        neovim_version: ["v0.11.0"]
    runs-on: ${{ matrix.os }}
    steps:
      - uses: actions/checkout@v4
        with:
          token: ${{secrets.WORKFLOW_TOKEN}}
          fetch-depth: 0
          ref: ${{ env.BRANCH_NAME }}
      - name: Setup neovim
        uses: rhysd/action-setup-vim@v1
        with:
          neovim: true
          version: ${{ matrix.neovim_version }}
      - name: Generate the docs
        run: | 
          make documentation

      - name: Update documentation
        env:
          GITHUB_TOKEN: ${{ secrets.WORKFLOW_TOKEN }}
          COMMIT_MSG: |
            [docgen] Update docs
            skip-checks: true
        run: |
          git config user.email "actions@github"
          git config user.name "Github Actions"
          git remote set-url origin https://x-access-token:${GITHUB_TOKEN}@github.com/${GITHUB_REPOSITORY}.git
          git add doc/
          # Only commit and push if we have changes
          git diff --quiet && git diff --staged --quiet || (git commit -m "${COMMIT_MSG}"; git push origin HEAD:${GITHUB_REF})
