---
name: "Refresh pinned dependencies"

"on":
  workflow_call:
    inputs:
      # Commit messae and PR title
      message:
        type: string
        required: true
      # Branch to create PR from
      pr-branch:
        type: string
        required: true
      # Branch to base PR on
      base-branch:
        type: string
        required: true
      # Nox session to call
      nox-args:
        type: string
        required: true
      # Files to commit
      changed-files:
        default: "tests/*.txt"
        type: string
        required: false
      # Reset branch
      reset-branch:
        type: boolean
        default: false
      labels:
        type: string
        default: ""
      python-versions:
        type: string
        required: true
jobs:
  refresh:
    runs-on: ubuntu-latest
    environment: github-bot
    steps:
      - name: Generate temp GITHUB_TOKEN
        id: create_token
        uses: actions/create-github-app-token@v2
        with:
          app-id: ${{ secrets.BOT_APP_ID }}
          private-key: ${{ secrets.BOT_APP_KEY }}
      - name: Check out repo
        uses: actions/checkout@v5
        with:
          fetch-depth: 0
          ref: "${{ inputs.base-branch }}"
          token: "${{ steps.create_token.outputs.token }}"
      - name: Fetch required contents of ansible-core
        run: |
          python docs/bin/clone-core.py
      - name: Set up nox
        uses: wntrblm/nox@2025.10.16
        with:
          python-versions: "${{ inputs.python-versions }}"
      - name: Set up git committer
        run: |
          hacking/get_bot_user.sh "ansible-documentation-bot" "Ansible Documentation Bot"
      - name: "Use a branch named ${{ inputs.pr-branch }}"
        env:
          base_branch: "${{ inputs.base-branch }}"
          pr_branch: "${{ inputs.pr-branch }}"
        id: branch
        run: |
          set -x
          if git branch -r | grep "origin/${pr_branch}"; then
            echo "branch-exists=true" >> "${GITHUB_OUTPUT}"
            git switch "${pr_branch}"
            ${{ inputs.reset-branch && 'git reset --hard' || 'git rebase' }} \
              "${base_branch}"
          else
            echo "branch-exists=false" >> "${GITHUB_OUTPUT}"
            git switch -c "${pr_branch}"
          fi
      - name: "Run nox ${{ inputs.nox-args }}"
        env:
          # Ensure the latest pip version is used
          VIRTUALENV_DOWNLOAD: '1'
          #
        run: |
          nox ${{ inputs.nox-args }}
      - name: Push new dependency versions and create a PR
        env:
          GITHUB_TOKEN: ${{ steps.create_token.outputs.token }}
          base_branch: "${{ inputs.base-branch }}"
          pr_branch: "${{ inputs.pr-branch }}"
          message: "${{ inputs.message }}"
          pr_title: "[${{ inputs.base-branch }}] ${{ inputs.message }}"
          changed_files: "${{ inputs.changed-files }}"
          labels: "${{ inputs.labels }}"
          branch_exists: "${{ steps.branch.outputs.branch-exists }}"
        run: |
          set -x
          git diff || :
          # shellcheck disable=SC2086
          git add ${changed_files}
          # shellcheck disable=SC2086
          if git diff-index --quiet HEAD ${changed_files}; then
            echo "Nothing to do!"
            exit
          fi

          git commit -m "${message}"
          git push --force origin "${pr_branch}"

          if [ "${branch_exists}" = "true" ]; then
            # https://github.com/cli/cli/discussions/5792#discussioncomment-10410197
            current_pr="$(gh pr list \
              --head "${pr_branch}"\
              --app ansible-documentation-bot \
              --state open \
              --json url --jq .[].url \
            )"
            if [ -n "${current_pr}" ]; then
              echo "PR already exists: ${current_pr}"
              exit
            fi
          fi
          command=(gh pr create
            --base "${base_branch}"
            --title "${pr_title}"
            --body ""
            --label dependency_update
          )
          # Add custom labels to the command.
          old_ifs="$IFS"
          IFS=","
          for label in ${labels}; do
            command+=("--label" "${label}")
          done
          IFS="${old_ifs}"
          "${command[@]}"
