name: Autodocs

on:
  workflow_dispatch:
    inputs:
      version:
        description: "Version (e.g. 2.4.x)"
        required: true
      source_branch:
        description: "Source Branch in kong/kong (e.g. release/2.4.x)"
        required: true
      target_branch:
        description: "Target Branch in kong/docs.konghq.com (e.g. release/2.4)"
        required: true
      force_build:
        description: "Ignore the build cache and build dependencies from scratch"
        type: boolean
        default: false
jobs:
  build:
    name: Build dependencies
    runs-on: ubuntu-22.04

    env:
      DOWNLOAD_ROOT: $HOME/download-root

    steps:
      - name: Set environment variables
        run: |
          echo "INSTALL_ROOT=$HOME/install-root" >> $GITHUB_ENV
          echo "DOWNLOAD_ROOT=$HOME/download-root" >> $GITHUB_ENV
          echo "LD_LIBRARY_PATH=$INSTALL_ROOT/openssl/lib:$LD_LIBRARY_PATH" >> $GITHUB_ENV

      - name: Checkout Kong source code
        uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2

      - name: Lookup build cache
        uses: actions/cache@27d5ce7f107fe9357f9df03efb73ab90386fccae # v5.0.5
        id: cache-deps
        with:
          path: ${{ env.INSTALL_ROOT }}
          key: ${{ hashFiles('.ci/setup_env_github.sh') }}-${{ hashFiles('.requirements') }}-${{ hashFiles('kong-*.rockspec') }}

      - name: Checkout kong-build-tools
        if: steps.cache-deps.outputs.cache-hit != 'true' || github.event.inputs.force_build == 'true'
        uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
        with:
          repository: Kong/kong-build-tools
          path: kong-build-tools
          ref: master

      - name: Checkout go-pluginserver
        if: steps.cache-deps.outputs.cache-hit != 'true' || github.event.inputs.force_build == 'true'
        uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
        with:
          repository: Kong/go-pluginserver
          path: go-pluginserver

      - name: Add to Path
        if: steps.cache-deps.outputs.cache-hit != 'true' || github.event.inputs.force_build == 'true'
        run: echo "$INSTALL_ROOT/openssl/bin:$INSTALL_ROOT/openresty/nginx/sbin:$INSTALL_ROOT/openresty/bin:$INSTALL_ROOT/luarocks/bin:$GITHUB_WORKSPACE/kong-build-tools/openresty-build-tools" >> $GITHUB_PATH

      - name: Install packages
        if: steps.cache-deps.outputs.cache-hit != 'true' || github.event.inputs.force_build == 'true'
        run: sudo apt update && sudo apt install libyaml-dev valgrind

      - name: Build Kong dependencies
        if: steps.cache-deps.outputs.cache-hit != 'true' || github.event.inputs.force_build == 'true'
        run: |
          source .ci/setup_env_github.sh
          make dev
  autodoc:
    runs-on: ubuntu-22.04
    needs: [build]
    steps:
      - name: Set environment variables
        run: |
          echo "INSTALL_ROOT=$HOME/install-root" >> $GITHUB_ENV
          echo "DOWNLOAD_ROOT=$HOME/download-root" >> $GITHUB_ENV
          echo "LD_LIBRARY_PATH=$INSTALL_ROOT/openssl/lib:$LD_LIBRARY_PATH" >> $GITHUB_ENV

      - name: Checkout Kong source code
        uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
        with:
          path: kong
          ref: ${{ github.event.inputs.source_branch }}

      - name: Checkout Kong Docs
        uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
        with:
          repository: kong/docs.konghq.com
          path: docs.konghq.com
          token: ${{ secrets.PAT }}
          ref: ${{ github.event.inputs.target_branch }}

      - name: Lookup build cache
        uses: actions/cache@27d5ce7f107fe9357f9df03efb73ab90386fccae # v5.0.5
        id: cache-deps
        with:
          path: ${{ env.INSTALL_ROOT }}
          key: ${{ hashFiles('kong/.ci/setup_env_github.sh') }}-${{ hashFiles('kong/.requirements') }}-${{ hashFiles('kong/kong-*.rockspec') }}

      - name: Add to Path
        run: echo "$INSTALL_ROOT/openssl/bin:$INSTALL_ROOT/openresty/nginx/sbin:$INSTALL_ROOT/openresty/bin:$INSTALL_ROOT/luarocks/bin:$GITHUB_WORKSPACE/kong-build-tools/openresty-build-tools:$INSTALL_ROOT/go-pluginserver" >> $GITHUB_PATH

      - name: Run Autodocs
        run: |
          cd kong
          eval `luarocks path`
          scripts/autodoc ../docs.konghq.com ${{ github.event.inputs.version }}

      - name: Generate branch name
        id: kong-branch
        run: |
          cd kong
          output="$(git branch --show-current)"
          echo "name=$output" >> $GITHUB_OUTPUT

      - name: Show Docs status
        run: |
          cd docs.konghq.com
          git status
          git checkout -b "autodocs-${{ steps.kong-branch.outputs.name }}"

      - name: Commit autodoc changes
        uses: stefanzweifel/git-auto-commit-action@04702edda442b2e678b25b537cec683a1493fcb9 # v7.1.0
        with:
          repository: "./docs.konghq.com"
          commit_message: "Autodocs update"
          branch: "autodocs-${{ steps.kong-branch.outputs.name }}"
          skip_fetch: true
          push_options: "--force"

      - name: Raise PR
        run: |
          cd docs.konghq.com
          echo "${{ secrets.PAT }}" | gh auth login --with-token
          gh pr create --base "${{ github.event.inputs.target_branch }}" --fill --label "review:autodoc"
