name: Update API docs

on:
  pull_request:
    paths:
    - "src/_nebari/subcommands/**"
    - "src/_nebari/cli.py"
  push:
    branches:
      - main
    paths:
    - "src/_nebari/subcommands/**"
    - "src/_nebari/cli.py"
  workflow_dispatch:

jobs:
  update_api:
    permissions:
      contents: write
      pull-requests: write
    runs-on: ubuntu-latest
    defaults:
      run:
        shell: bash -l {0}
        working-directory: ./docs-sphinx
    steps:
      - name: Check out repository 🛎️
        uses: actions/checkout@v4

      - name: Set up Python
        uses: actions/setup-python@v5
        with:
          python-version: "3.10"

      - name: Install nebari and docs dependencies
        run: |
          python -m pip install --upgrade pip
          pip install -e "../[docs]"

      - name: Generate new API docs
        run: |
          make html

      - name: Copy cli doc
        run: |
          cp _build/html/cli.html cli.html

      - name: Look for changes to generated docs
        uses: tj-actions/verify-changed-files@v12
        id: verify-changed-files
        with:
          files: |
            docs-sphinx/cli.html

      - name: Create Pull Request in code repo
        id: create_pull_request
        uses: peter-evans/create-pull-request@v4
        if: steps.verify-changed-files.outputs.files_changed == 'true' && github.event_name != 'pull_request'
        with:
          token: ${{ secrets.NEBARI_SENSEI_API_DOCS_PR_OPENER }}
          commit-message: Update api docs
          committer: GitHub <noreply@github.com>
          author: ${{ github.actor }} <${{ github.actor }}@users.noreply.github.com>
          signoff: false
          branch: auto_cli_doc_update
          delete-branch: true
          title: '[AUTO] Update CLI doc'
          body: |
            Update CLI doc
            - Auto-generated by [create-pull-request][1]

            [1]: https://github.com/peter-evans/create-pull-request
          labels: |
            "area: documentation 📖"
          draft: false
          base: ${{ github.head_ref }}
