name: CLI Docs

# Runs CI when code merges to the CLI on main
on:
  push:
    branches:
      - main
    paths:
      - "main.go"

jobs:
  check-updates:
    name: Check for updates to the CLI docs
    runs-on: ubuntu-latest
    permissions:
      contents: write
      pull-requests: write
    steps:
      - uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
        with:
          persist-credentials: false
      - uses: actions/setup-go@7a3fe6cf4cb3a834922a1244abfce67bcef6a0c5 # v6.2.0
        with:
          go-version-file: 'go.mod'
      - name: Generate CLI docs
        id: changed-files
        run: |
          set +e
          go run . gendocs
          git diff --exit-code --quiet
          changedfiles=$?
          echo "changedfiles=$changedfiles" >> $GITHUB_OUTPUT
      - name: Open pull request for changes
        if: steps.changed-files.outputs.changedfiles == '1'
        uses: peter-evans/create-pull-request@98357b18bf14b5342f975ff684046ec3b2a07725 # v8.0.0
        with:
          token: ${{ secrets.GITHUB_TOKEN }}
          branch: cli-docs
          commit-message: Update CLI documentation
          title: Update CLI documentation
          body: |
            Auto-generated documentation update for the opkssh CLI commands.
          labels: |
            docs
