name: panvimdoc

on:
  push:
    paths:
      - README.md
    branches:
      - master
jobs:
  docs:
    runs-on: ubuntu-latest
    permissions:
      contents: write
      pull-requests: write
    env:
      GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
    name: markdown to vimdoc
    steps:
      - uses: actions/checkout@v2
      - name: Setup git
        run: |
          git config --local user.email "41898282+github-actions[bot]@users.noreply.github.com"
          git config --local user.name "github-actions[bot]"
          printf 'VIMDOC_BRANCH=bot/vimdoc/%s\n' ${GITHUB_REF#refs/heads/} >> $GITHUB_ENV
      - name: Checkout to vimdoc branch
        run: git checkout -b ${VIMDOC_BRANCH}
      - name: panvimdoc
        uses: kdheepak/panvimdoc@v2.7.1
        with:
          vimdoc: nvim-autopairs
          description: A super powerful autopair for Neovim.
      - name: clone rules api docs
        run: |
          git clone --depth 1 https://github.com/windwp/nvim-autopairs.wiki.git ../nvim-autopairs.wiki
          cp ../nvim-autopairs.wiki/Rules-API.md ./Rules-API.md
      - name: panvimdoc
        uses: kdheepak/panvimdoc@v2.7.1
        with:
          vimdoc: nvim-autopairs-rules
          description: nvim-autopairs rules
          pandoc: "Rules-API.md"
          toc: true
      - name: Create PR
        run: |
          if ! [[ -z $(git status -s) ]]; then
            git add doc/nvim-autopairs.txt
            git add doc/nvim-autopairs-rules.txt
            git commit -m "chore: generated vimdoc"
            git push --force https://${GITHUB_ACTOR}:${GITHUB_TOKEN}@github.com/${GITHUB_REPOSITORY} ${VIMDOC_BRANCH}
            gh pr create --fill --base ${GITHUB_REF#refs/heads/} --head ${VIMDOC_BRANCH} || true
          fi
