name: Update Documentation

on:
  workflow_dispatch:
    inputs:
      tag:
        description: Release tag (e.g. selenium-4.21.0)
        required: true
        type: string

  workflow_call:
    inputs:
      tag:
        required: true
        type: string

env:
  GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

jobs:
  determine-language:
    runs-on: ubuntu-latest
    outputs:
      language: ${{ steps.get-language.outputs.language }}
    steps:
      - name: Parse language from tag
        id: get-language
        run: |
          tag=${{ inputs.tag }}
          language=$(echo $tag | awk -F'-' '{print $NF}')
          if [[ ! "$language" =~ ^(java|ruby|python|dotnet|node)$ ]]; then
            language="all"
          fi
          echo "language=$language" > $GITHUB_OUTPUT

  java-docs:
    runs-on: ubuntu-latest
    needs: determine-language
    steps:
      - name: Checkout the tag
        uses: actions/checkout@v4
        with:
          ref: ${{ inputs.tag }}
      - name: Prep git
        run: |
          git config --local user.email "selenium-ci@users.noreply.github.com"
          git config --local user.name "Selenium CI Bot"
      - name: Setup Java
        uses: actions/setup-java@v4
        with:
          java-version: 17
          distribution: 'temurin'
      - name: Update Documentation
        if: needs.determine-language.outputs.language == 'all' || needs.determine-language.outputs.language == 'java'
        run: ./go java:docs
      - name: Create patch for changes
        run: |
          git format-patch -1 HEAD --stdout > java-docs.patch
      - name: Upload patch
        if: needs.determine-language.outputs.language == 'all' || needs.determine-language.outputs.language == 'java'
        uses: actions/upload-artifact@v4
        with:
          name: java-docs-patch
          path: java-docs.patch

  ruby-docs:
    runs-on: ubuntu-latest
    needs: determine-language
    steps:
      - name: Checkout the tag
        uses: actions/checkout@v4
        with:
          ref: ${{ inputs.tag }}
      - name: Prep git
        run: |
          git config --local user.email "selenium-ci@users.noreply.github.com"
          git config --local user.name "Selenium CI Bot"
      - name: Setup Java
        uses: actions/setup-java@v4
        with:
          java-version: 17
          distribution: 'temurin'
      - name: Setup curl for Ubuntu
        run: sudo apt-get update && sudo apt-get install -y libcurl4-openssl-dev
      - name: Update Documentation
        if: needs.determine-language.outputs.language == 'all' || needs.determine-language.outputs.language == 'ruby'
        run: ./go rb:docs
      - name: Create patch for changes
        run: |
          git format-patch -1 HEAD --stdout > ruby-docs.patch
      - name: Upload patch
        if: needs.determine-language.outputs.language == 'all' || needs.determine-language.outputs.language == 'ruby'
        uses: actions/upload-artifact@v4
        with:
          name: ruby-docs-patch
          path: ruby-docs.patch

  python-docs:
    needs: determine-language
    runs-on: ubuntu-latest
    steps:
      - name: Checkout the tag
        uses: actions/checkout@v4
        with:
          ref: ${{ inputs.tag }}
      - name: Prep git
        run: |
          git config --local user.email "selenium-ci@users.noreply.github.com"
          git config --local user.name "Selenium CI Bot"
      - name: Set up Python 3.9
        uses: actions/setup-python@v5
        with:
          python-version: 3.9
      - name: Install dependencies
        run: |
          python -m pip install --upgrade pip
          pip install tox
      - name: Update Documentation
        if: needs.determine-language.outputs.language == 'all' || needs.determine-language.outputs.language == 'python'
        run: ./go py:docs
      - name: Create patch for changes
        run: |
          git format-patch -1 HEAD --stdout > python-docs.patch
      - name: Upload patch
        if: needs.determine-language.outputs.language == 'all' || needs.determine-language.outputs.language == 'python'
        uses: actions/upload-artifact@v4
        with:
          name: python-docs-patch
          path: python-docs.patch

  dotnet-docs:
    needs: determine-language
    runs-on: ubuntu-latest
    steps:
      - name: Checkout the tag
        uses: actions/checkout@v4
        with:
          ref: ${{ inputs.tag }}
      - name: Prep git
        run: |
          git config --local user.email "selenium-ci@users.noreply.github.com"
          git config --local user.name "Selenium CI Bot"
      - name: Update Documentation
        if: needs.determine-language.outputs.language == 'all' || needs.determine-language.outputs.language == 'dotnet'
        run: ./go dotnet:docs
      - name: Create patch for changes
        run: |
          git format-patch -1 HEAD --stdout > dotnet-docs.patch
      - name: Upload patch
        if: needs.determine-language.outputs.language == 'all' || needs.determine-language.outputs.language == 'dotnet'
        uses: actions/upload-artifact@v4
        with:
          name: dotnet-docs-patch
          path: dotnet-docs.patch

  node-docs:
    needs: determine-language
    runs-on: ubuntu-latest
    steps:
      - name: Checkout the tag
        uses: actions/checkout@v4
        with:
          ref: ${{ inputs.tag }}
      - name: Prep git
        run: |
          git config --local user.email "selenium-ci@users.noreply.github.com"
          git config --local user.name "Selenium CI Bot"
      - name: Install npm dependencies
        run: |
          npm install
          npm install --prefix javascript/node/selenium-webdriver
      - name: Update Documentation
        if: needs.determine-language.outputs.language == 'all' || needs.determine-language.outputs.language == 'node'
        run: ./go node:docs
      - name: Create patch for changes
        run: |
          git format-patch -1 HEAD --stdout > node-docs.patch
      - name: Upload patch
        if: needs.determine-language.outputs.language == 'all' || needs.determine-language.outputs.language == 'node'
        uses: actions/upload-artifact@v4
        with:
          name: node-docs-patch
          path: node-docs.patch

  merge-patches:
    runs-on: ubuntu-latest
    needs: [java-docs, ruby-docs, python-docs, dotnet-docs, node-docs]
    steps:
      - name: Checkout  documentation branch
        uses: actions/checkout@v4
        with:
          ref: 'gh-pages'
          fetch-depth: 0
          fetch-tags: true
      - name: Create and checkout new branch
        run: |
          git config --local user.email "selenium-ci@users.noreply.github.com"
          git config --local user.name "Selenium CI Bot"
          git checkout -b api-docs-${{ inputs.tag }}
      - name: Download all patches
        uses: actions/download-artifact@v4
        with:
          path: patches/
          merge-multiple: true
      - name: Show downloaded patches
        run: ls -lah patches/
      - name: Apply patches
        run: |
          for patch in patches/*.patch; do
            git am < "$patch"
          done
          rm -rf patches/
      - name: Push Branch
        run: git push origin api-docs-${{ inputs.tag }}
      - name: Documentation Pull Request
        uses: peter-evans/create-pull-request@v6
        with:
          token: ${{ secrets.SELENIUM_CI_TOKEN }}
          author: Selenium CI Bot <selenium-ci@users.noreply.github.com>
          delete-branch: true
          branch: api-docs-${{ inputs.tag }}
          base: gh-pages
          title: Update documentation for ${{ inputs.tag }}
          body: |
            This PR updates the API documentation for all bindings
            based on the provided tag name.

            - Auto-generated by [create-pull-request][1]

            [1]: https://github.com/peter-evans/create-pull-request
          labels: documentation
          draft: false
