---
name: Ansible porting guide creation
on:
  workflow_dispatch:
    inputs:
      ansible-build-data-branch:
        description: >-
          Release Branch name from the Ansible Build data PR.
          (e.g. `refs/pull/1/merge`)
        required: true
      ansible-version:
        description: >-
          Exact release version. For example, 12.1.0
        required: true
permissions:
  contents: read

jobs:
  upload-porting-guide:
    name: Extract the porting guide
    runs-on: ubuntu-latest
    environment: github-bot
    env:
      GIT_BRANCH: "release/porting-guide-${{ inputs.ansible-version }}"
      ANSIBLE_VERSION_FULL: ${{ inputs.ansible-version }}
      CI_COMMIT_MESSAGE: >-
        Add the Ansible community ${{ inputs.ansible-version }} porting guide

    steps:
      - name: Extract the major version
        run: echo "ANSIBLE_VERSION_MAJOR=${ANSIBLE_VERSION_FULL%%.*}" >> "${GITHUB_ENV}"
        shell: bash --noprofile --norc -O extglob -eEuo pipefail {0}

      - name: Generate temp GITHUB_TOKEN
        id: create_token
        uses: actions/create-github-app-token@v2
        with:
          app-id: ${{ secrets.BOT_APP_ID }} # From github-bot environment
          private-key: ${{ secrets.BOT_APP_KEY }} # From github-bot environment

      - name: Check out this repo src
        uses: actions/checkout@v6
        with:
          token: ${{ steps.create_token.outputs.token }}
          persist-credentials: true  # Needed to push to the repo

      - name: Check out ansible-build-data
        uses: actions/checkout@v6
        with:
          repository: ansible-community/ansible-build-data
          ref: ${{ inputs.ansible-build-data-branch }}
          path: ansible-build-data
          persist-credentials: false

      - name: Copy the RST file to the correct path
        run: >-
          cp -v
          "ansible-build-data/${ANSIBLE_VERSION_MAJOR}/porting_guide_${ANSIBLE_VERSION_MAJOR}.rst"
          docs/docsite/rst/porting_guides/

      - name: Set up git
        run: |
          git switch --create "${GIT_BRANCH}"
          ACTOR_NAME="$(curl -s https://api.github.com/users/"${GITHUB_ACTOR}" | jq --raw-output '.name // .login')"
          git config --global user.name "${ACTOR_NAME}"
          git config --global user.email "${GITHUB_ACTOR_ID}+${GITHUB_ACTOR}@users.noreply.github.com"

      - name: Add the porting guide
        run: git add docs/docsite/rst/porting_guides/"porting_guide_${ANSIBLE_VERSION_MAJOR}.rst"

      - name: Commit the porting guide
        run: >-
          git diff-index --quiet HEAD ||
          git commit -m "${CI_COMMIT_MESSAGE}"

      - name: Push to the repo
        env:
          GITHUB_TOKEN: ${{ steps.create_token.outputs.token }}
        run: git push origin "${GIT_BRANCH}"

      - name: Create the porting guide PR
        env:
          GITHUB_TOKEN: ${{ steps.create_token.outputs.token }}
          PR_BODY_MESSAGE: |-
            ##### ISSUE TYPE

            - Docs Pull Request

            ##### COMPONENT NAME

            docs/docsite/rst/porting_guides/porting_guide_${{ env.ANSIBLE_VERSION_MAJOR }}.rst
        run: >-
          gh pr create
          --base devel
          --head "${GIT_BRANCH}"
          --title "${CI_COMMIT_MESSAGE}"
          --body "${PR_BODY_MESSAGE}"
