name: Generate docs from pod

on:
  push:
    branches:
      - main

jobs:
  docs:
    runs-on: ubuntu-latest
    permissions:
      contents: write
      pull-requests: write
    steps:
      - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
      - name: Check is main protected
        id: branchCheck
        run: |
          RESPONSE=$(curl -s -H "Authorization: Bearer ${{ secrets.token }}" \
          "https://api.github.com/repos/${{ github.repository }}/branches/main")
          PROTECTED=$(echo "${RESPONSE}" | jq -r '.protected')
          echo "${PROTECTED}"
          echo "{isProtected}={${PROTECTED}}" >> "${GITHUB_OUTPUT}"
      - uses: Omochice/action-normalize-vim-plugin-name@4c0461629dd9dfbce8a5e77ef962d8d719ca1885 # v0.1.0
        id: normalize
        with:
          trim-tail-dot-vim: true
      - uses: Omochice/action-podeno@dada699f16466ed7890f9714334fd3a38f2447b8 # v0.1.1
        with:
          config: |
            [
              { type: "markdown", in: "README.pod", out: "README.md", },
              { type: "vimdoc", in: "README.pod", out: "doc/${{ steps.normalize.outputs.normalizedName }}.txt", },
            ]
      - name: Create auto commit
        uses: stefanzweifel/git-auto-commit-action@e348103e9026cc0eee72ae06630dbe30c8bf7a79 # v5.1.0
        if: ${{ steps.branchCheck.outputs.isProtected != true }}
        with:
          commit_message: "docs: update docs"
          branch: ${{ github.head_ref }}
      - name: Create Pull Request
        if: ${{ steps.branchCheck.outputs.isProtected == true }}
        uses: peter-evans/create-pull-request@271a8d0340265f705b14b6d32b9829c1cb33d45e # v7.0.8
        with:
          title: "docs: update docs"
          body: |
            This PR is created by [create-pull-request](https://github.com/peter-evans/create-pull-request).
            The docs are generated automatically.
          author: "GitHub <noreply@github.com>"
          delete-branch: true