---
name: Generate PR
run-name: Generate PR for ${{ inputs.branch || github.ref_name }} by @${{ github.actor }}
on:
  workflow_dispatch:
    inputs:
      draft_pr:
        description: "Create a Draft PR."
        required: false
        type: boolean
        default: 'false'
      pr_title:
        description: "Title of PR"
        required: false
      update_package_files:
        description: "Update Package Files."
        required: false
        type: boolean
        default: 'true'
      branch:
        description: "Branch of chromebrew/chromebrew to run on, if different from this branch."
        required: false
      pr_label:
        description: "PR Label (Not Title)"
        required: false
      max_build_time:
        description: "Maximum Build Time (hours)"
        required: false
        type: number
        default: 5.5
env:
  BRANCH: ${{ inputs.branch || github.ref_name }}
permissions:
  actions: write
  contents: write
  packages: write
  pull-requests: write
  repository-projects: read
jobs:
  debug:
    if: ${{ ( github.repository_owner == 'chromebrew' ) }}
    runs-on: ubuntu-24.04
    steps:
      - name: Dump GitHub context
        env:
          GITHUB_CONTEXT: ${{ toJson(github) }}
        run: echo "$GITHUB_CONTEXT"
      - name: Dump job context
        env:
          JOB_CONTEXT: ${{ toJson(job) }}
        run: echo "$JOB_CONTEXT"
      - name: Dump steps context
        env:
          STEPS_CONTEXT: ${{ toJson(steps) }}
        run: echo "$STEPS_CONTEXT"
  setup:
    if: ${{ ( github.repository_owner == 'chromebrew' ) && ( inputs.branch != 'master' ) }}
    runs-on: ubuntu-24.04
    outputs:
      current_head: ${{ steps.get-current-head.outputs.CURRENT_HEAD }}
      changed_packages: ${{ steps.changed-packages.outputs.CHANGED_PACKAGES }}
      glibc_232_compat: ${{ steps.get-compatibility.outputs.GLIBC_232_COMPATIBLE_PACKAGES }}
      glibc_237_compat: ${{ steps.get-compatibility.outputs.GLIBC_237_COMPATIBLE_PACKAGES }}
      i686_packages: ${{ steps.get-compatibility.outputs.i686_PACKAGES }}
      x86_64_packages: ${{ steps.get-compatibility.outputs.x86_64_PACKAGES }}
      armv7l_packages: ${{ steps.get-compatibility.outputs.armv7l_PACKAGES }}
      matrix: ${{ steps.set-generate-matrix.outputs.matrix }}
    steps:
      - uses: actions/checkout@v5
        with:
          fetch-depth: 0
          persist-credentials: true
          ref: ${{ inputs.branch || github.ref_name }}
      - name: Set PR to Draft
        id: set-to-draft
        env:
          GH_TOKEN: ${{ secrets.CREW_PR_TOKEN }}
        run: |
          PR_NUMBER=$(gh pr list -L 1 -s open -H ${{ inputs.branch || github.ref_name }} | cut -f1)
          if [[ -n ${PR_NUMBER} ]]; then
            echo "Setting ${PR_NUMBER} to Draft while workflow runs."
            gh pr ready --undo || true
          fi
          if [[ -n ${PR_NUMBER} ]]; then
            # Try to update branch immediately.
            gh pr update-branch || true
          else
            gh pr create -d -b WIP -t "WIP ${{ inputs.branch || github.ref_name }}"
            # Create a draft PR and immediately try to update the branch
            # before continuing.
            gh pr update-branch || true
          fi
      - name: Get Current HEAD hash
        id: get-current-head
        run: |
          echo "CURRENT_HEAD=$(git ls-remote https://github.com/${{ github.repository }}.git | head -1 | sed 's/HEAD//')" >> "$GITHUB_OUTPUT"
          echo "CURRENT_HEAD is $(git ls-remote https://github.com/${{ github.repository }}.git | head -1 | sed 's/HEAD//')"
      - name: Get changed files
        id: changed-files
        uses: tj-actions/changed-files@v47
        with:
          base_sha: ${{ steps.get-current-head.outputs.CURRENT_HEAD }}
          files_yaml: |
            packages:
              - packages/*.rb
          since_last_remote_commit: true
      - name: Export variables to github context
        id: changed-packages
        env:
          CHANGED_FILES: ${{ steps.changed-files.outputs.packages_all_changed_files }}
        run: |
            if [[ -n "${CHANGED_FILES}" ]]; then
              # Convert "packages/foo.rb packages/bar.rb" (from steps.changed-files.outputs.packages_all_changed_files) into "foo bar"
              echo "CHANGED_PACKAGES=$(echo "${CHANGED_FILES}" | xargs basename -s .rb | xargs)" >> "$GITHUB_ENV"
              echo "CHANGED_PACKAGES=$(echo "${CHANGED_FILES}" | xargs basename -s .rb | xargs)" >> "$GITHUB_OUTPUT"
            else
              echo "No Changed Files."
            fi
      - name: Determine glibc and architecture package compatibility
        id: get-compatibility
        run: |
            # If a package doesnt have a min_glibc value, or if it is below 2.32, add it to GLIBC_232_COMPATIBLE_PACKAGES.
            GLIBC_232_COMPATIBLE_PACKAGES="$(for i in ${CHANGED_PACKAGES} ; do grep min_glibc packages/"${i}".rb | tr -d \' | awk '{exit $2 <= 2.32}' || echo "${i}" ; done | xargs)"
            export GLIBC_232_COMPATIBLE_PACKAGES
            if [[ -n ${GLIBC_232_COMPATIBLE_PACKAGES} ]]; then
              echo "GLIBC_232_COMPATIBLE_PACKAGES=${GLIBC_232_COMPATIBLE_PACKAGES}" >> "$GITHUB_ENV"
              echo "GLIBC_232_COMPATIBLE_PACKAGES=${GLIBC_232_COMPATIBLE_PACKAGES}" >> "$GITHUB_OUTPUT"
              echo "Branch ${{ inputs.branch || github.ref_name }} has these possibly Glibc 2.32 compatible packages: ${GLIBC_232_COMPATIBLE_PACKAGES}"
            fi

            # If a package doesnt have a min_glibc value, or if it is below 2.37, add it to GLIBC_237_COMPATIBLE_PACKAGES.
            GLIBC_237_COMPATIBLE_PACKAGES="$(for i in ${CHANGED_PACKAGES} ; do grep min_glibc packages/"${i}".rb | tr -d \' | awk '{exit $2 <= 2.37}' || echo "${i}" ; done | xargs)"
            export GLIBC_237_COMPATIBLE_PACKAGES
            if [[ -n ${GLIBC_237_COMPATIBLE_PACKAGES} ]]; then
              echo "GLIBC_237_COMPATIBLE_PACKAGES=${GLIBC_237_COMPATIBLE_PACKAGES}" >> "$GITHUB_ENV"
              echo "GLIBC_237_COMPATIBLE_PACKAGES=${GLIBC_237_COMPATIBLE_PACKAGES}" >> "$GITHUB_OUTPUT"
              echo "Branch ${{ inputs.branch || github.ref_name }} has these possibly Glibc 2.37 compatible packages: ${GLIBC_237_COMPATIBLE_PACKAGES}"
            fi

            # If a package has a compatibility of 'all' or one that includes 'x86_64', add it to x86_64_PACKAGES.
            x86_64_PACKAGES="$(for i in ${CHANGED_PACKAGES}; do grep -q "[[:space:]]compatibility.*all\|[[:space:]]compatibility.*x86_64" packages/"${i}".rb && echo "${i}"; done | xargs)"
            export x86_64_PACKAGES
            if [[ -n ${x86_64_PACKAGES} ]]; then
              echo "x86_64_PACKAGES=${x86_64_PACKAGES}" >> "$GITHUB_ENV"
              echo "x86_64_PACKAGES=${x86_64_PACKAGES}" >> "$GITHUB_OUTPUT"
              echo "Branch ${{ inputs.branch || github.ref_name }} has these x86_64 compatible packages: ${x86_64_PACKAGES}"
            fi

            ## If a package has a compatibility of 'all' or one that includes 'armv7l', add it to armv7l_PACKAGES.
            armv7l_PACKAGES="$(for i in ${CHANGED_PACKAGES}; do grep -q "[[:space:]]compatibility.*all\|[[:space:]]compatibility.*armv7l" packages/"${i}".rb && echo "${i}"; done | xargs)"
            export armv7l_PACKAGES
            if [[ -n ${armv7l_PACKAGES} ]]; then
              echo "armv7l_PACKAGES=${armv7l_PACKAGES}" >> "$GITHUB_ENV"
              echo "armv7l_PACKAGES=${armv7l_PACKAGES}" >> "$GITHUB_OUTPUT"
              echo "Branch ${{ inputs.branch || github.ref_name }} has these armv7l compatible packages: ${armv7l_PACKAGES}"
            fi

            ## If a package has a compatibility of 'all' or one that includes 'i686', add it to i686_PACKAGES.
            i686_PACKAGES="$(for i in ${CHANGED_PACKAGES}; do grep -q "[[:space:]]compatibility.*all\|[[:space:]]compatibility.*i686" packages/"${i}".rb && echo "${i}"; done | xargs)"
            export i686_PACKAGES
            if [[ -n ${i686_PACKAGES} ]]; then
              echo "i686_PACKAGES=${i686_PACKAGES}" >> "$GITHUB_ENV"
              echo "i686_PACKAGES=${i686_PACKAGES}" >> "$GITHUB_OUTPUT"
              echo "Branch ${{ inputs.branch || github.ref_name }} has these i686 compatible packages: ${i686_PACKAGES}"
            fi
      - name: Generate Creation Matrix
        id: set-generate-matrix
        env:
          i686_PACKAGES: ${{ steps.get-compatibility.outputs.i686_PACKAGES }}
          x86_64_PACKAGES: ${{ steps.get-compatibility.outputs.x86_64_PACKAGES }}
          armv7l_PACKAGES: ${{ steps.get-compatibility.outputs.armv7l_PACKAGES }}
        run: |
          function join_by { local IFS="$1"; shift; echo "$*"; }
          [[ -n "${i686_PACKAGES}" ]] && export CONTAINER_ARCH+=( "\"i686\"" )
          # Always run workflow on the x86_64 container as a fallback.
          export CONTAINER_ARCH+=( "\"x86_64\"" )
          [[ -n "${armv7l_PACKAGES}" ]] && export CONTAINER_ARCH+=( "\"armv7l\"" )
          export ARCHES="$(join_by , "${CONTAINER_ARCH[@]}")"
          echo "matrix=[${ARCHES}]" >> $GITHUB_OUTPUT
          echo "matrix=[${ARCHES}]"
  update-package-files:
    strategy:
      max-parallel: 1
      matrix:
        arch: ${{ fromJSON(needs.setup.outputs.matrix) }}
        runner:
          - ubuntu-24.04
          - ubuntu-24.04-arm
        exclude:
          - arch: x86_64
            runner: ubuntu-24.04-arm
          - arch: i686
            runner: ubuntu-24.04-arm
          - arch: armv7l
            runner: ubuntu-24.04
    runs-on: ${{ matrix.runner }}
    needs:
      - setup
    env:
      CREW_REPO: ${{ github.event.repository.clone_url }}
      CREW_BRANCH: ${{ inputs.branch || github.ref_name }}
      TARGET_ARCH: ${{ matrix.arch }}
      GLIBC_232_COMPATIBLE_PACKAGES: ${{ needs.setup.outputs.glibc_232_compat }}
      GLIBC_237_COMPATIBLE_PACKAGES: ${{ needs.setup.outputs.glibc_237_compat }}
      i686_PACKAGES: ${{ needs.setup.outputs.i686_packages }}
      x86_64_PACKAGES: ${{ needs.setup.outputs.x86_64_packages }}
      armv7l_PACKAGES: ${{ needs.setup.outputs.armv7l_packages }}
    if: ${{ !cancelled() && ( inputs.update_package_files ) && needs.setup.outputs.changed_packages }}
    concurrency:
      group: ${{ matrix.arch }}-${{ github.workflow }}-${{ inputs.branch || github.ref_name }}
      cancel-in-progress: true
    steps:
      - name: Fail if build jobs failed
        if: ${{ contains(needs.*.result, 'failure') }}
        run: exit 1
      - name: Dump GitHub context
        env:
          GITHUB_CONTEXT: ${{ toJson(github) }}
        run: echo "$GITHUB_CONTEXT"
      - name: Dump job context
        env:
          JOB_CONTEXT: ${{ toJson(job) }}
        run: echo "$JOB_CONTEXT"
      - name: Dump steps context
        env:
          STEPS_CONTEXT: ${{ toJson(steps) }}
        run: echo "$STEPS_CONTEXT"
      - name: Dump runner context
        env:
          RUNNER_CONTEXT: ${{ toJson(runner) }}
        run: echo "$RUNNER_CONTEXT"
      - name: Dump strategy context
        env:
          STRATEGY_CONTEXT: ${{ toJson(strategy) }}
        run: echo "$STRATEGY_CONTEXT"
      - name: Dump matrix context
        env:
          MATRIX_CONTEXT: ${{ toJson(matrix) }}
        run: echo "$MATRIX_CONTEXT"
      - uses: actions/checkout@v5
        with:
          fetch-depth: 0
          persist-credentials: true
      - name: Export target docker container to github context
        run: |
          case $TARGET_ARCH in
              x86_64)
                # Export the x86_64 container depending on whether this branch updates packages with appropriate minimum glibc.
                if [[ $GLIBC_232_COMPATIBLE_PACKAGES ]]; then
                    echo "CONTAINER=satmandu/crewbuild:nocturne-x86_64.m97" >> "$GITHUB_ENV"
                elif [[ $GLIBC_237_COMPATIBLE_PACKAGES ]]; then
                    echo "CONTAINER=satmandu/crewbuild:hatch-x86_64.m142" >> "$GITHUB_ENV"
                else
                    echo "CONTAINER=satmandu/crew-pre-glibc-standalone:nocturne-x86_64.m90" >> "$GITHUB_ENV"
                fi
                echo "PLATFORM=linux/amd64" >> "$GITHUB_ENV"
                echo "LIB_SUFFIX=64" >> "$GITHUB_ENV"
              ;;
              armv7l)
                # Export the armv7l container depending on whether this branch updates packages with appropriate minimum glibc.
                if [[ $GLIBC_232_COMPATIBLE_PACKAGES ]]; then
                    echo "CONTAINER=satmandu/crewbuild:fievel-armv7l.m97" >> "$GITHUB_ENV"
                elif [[ $GLIBC_237_COMPATIBLE_PACKAGES ]]; then
                    echo "CONTAINER=satmandu/crewbuild:strongbad-armv7l.m142" >> "$GITHUB_ENV"
                else
                    echo "CONTAINER=satmandu/crew-pre-glibc-standalone:fievel-armv7l.m91" >> "$GITHUB_ENV"
                fi
                echo "PLATFORM=linux/arm/v7" >> "$GITHUB_ENV"
                echo "LIB_SUFFIX=" >> "$GITHUB_ENV"
              ;;
              i686)
                # There is only one i686 container based upon M58 with glibc 2.23.
                echo "CONTAINER=satmandu/crew-pre-glibc-standalone:alex-i686.m58" >> "$GITHUB_ENV"
                echo "PLATFORM=linux/386" >> "$GITHUB_ENV"
                echo "LIB_SUFFIX=" >> "$GITHUB_ENV"
              ;;
            esac
      - name: Run Updater in container
        id: run-updater
        if: ${{ ( inputs.update_package_files ) && ( contains(needs.*.result, 'failure') ||  !cancelled() ) }}
        env:
          CREW_MAX_BUILD_TIME_INPUT: ${{ inputs.max_build_time }}
          UPDATE_PACKAGE_FILES: ${{ github.event.inputs.update_package_files }}
        run: |
            [[ "$UPDATE_PACKAGE_FILES" == 'false' ]] && exit 0
            if [[ -n ${CREW_MAX_BUILD_TIME_INPUT} ]]; then
              # Convert CREW_MAX_BUILD_TIME_INPUT to seconds.
              CREW_MAX_BUILD_TIME="$(bc <<<"scale=0;$CREW_MAX_BUILD_TIME_INPUT*3600/1")"
              echo "Maximum build time is $(bc <<<"scale=0;$CREW_MAX_BUILD_TIME_INPUT*60/1") minutes."
            fi
            [[ -n ${CI} ]] && echo 'CI variable is set.'
            if [ "$PLATFORM" == 'linux/arm/v7'  ] && [ -z "${armv7l_PACKAGES}" ]; then
              # Exit the arm container if there are not armv7l compatible packages.
              echo "Skipping armv7l container builds &/or package file updates."
              exit 0
            elif [ "$PLATFORM" == 'linux/amd64' ] && [ -z "${x86_64_PACKAGES}" ]; then
              # Exit the x86_64 container if there are not x86_64 compatible packages.
              echo "Skipping x86_64 container builds &/or package file updates."
              exit 0
            elif [ "$PLATFORM" == 'linux/386' ] && [ -z "${i686_PACKAGES}" ]; then
              # Exit the i686 container if there are not i686 compatible packages.
              echo "Skipping i686 container builds &/or package file updates."
              exit 0
            fi
            # Container Cleanup
            # Adapted from https://github.com/BRAINSia/free-disk-space/blob/main/action.yml
            echo "Running some background container cleanups from BRAINSia/free-disk-space."
            ( sudo apt-get purge -qq -o=Dpkg::Use-Pty=0 -y man-db manpages manpages-dev 'aspnetcore-*' 'dotnet-*' 'llvm-*' '*php*' 'mongodb-*' 'mysql-*' azure-cli google-chrome-stable firefox powershell mono-devel libgl1-mesa-dri 'google-cloud-*' 'gcloud-*' &>/dev/null ; \
            sudo apt-get autoremove -qq -o=Dpkg::Use-Pty=0 -y &>/dev/null) &

            git fetch origin
            git checkout "${{ inputs.branch || github.ref_name }}"
            git reset --hard "origin/${{ inputs.branch || github.ref_name }}"
            git log --oneline -10
            docker pull --platform "${PLATFORM}" "${CONTAINER}"
            # Detection of /output/pkg_cache dir triggers setting
            # CREW_CACHE_DIR=1 and CREW_CACHE_ENABLED=1 in the build
            # container. Without these, upload fails.
            mkdir /tmp/pkg_cache
            sudo setfacl -R -m u:1000:rwx .
            sudo setfacl -R -m u:1000:rwx /tmp/pkg_cache
            # See https://github.com/containerd/containerd/pull/7566#issuecomment-1461134737 for why we set ulimit.
            if [ -z ${CI+x} ]; then
              echo "CI is not set."
            else
              CI_PASSTHROUGH=-e
              CI_PASSTHROUGH+=" "
              CI_PASSTHROUGH+=NESTED_CI=${CI}
            fi
            if [ -z ${CREW_MAX_BUILD_TIME+x} ]; then
              echo "CREW_MAX_BUILD_TIME is not set."
            else
              CREW_MAX_BUILD_TIME_PASSTHROUGH=-e
              CREW_MAX_BUILD_TIME_PASSTHROUGH+=" "
              CREW_MAX_BUILD_TIME_PASSTHROUGH+=CREW_MAX_BUILD_TIME=${CREW_MAX_BUILD_TIME}
            fi
            docker run \
              --rm \
              --platform "${PLATFORM}" \
              -e PUID=1000 \
              -e PGID=1000 \
              --privileged \
              -e GCONV_PATH="/usr/local/lib${LIB_SUFFIX}/gconv" \
              -e CREW_BUILD_NO_PACKAGE_FILE_HASH_UPDATES="${CREW_BUILD_NO_PACKAGE_FILE_HASH_UPDATES}" \
              -e CREW_REPO="${CREW_REPO}" \
              -e CREW_BRANCH="${{ inputs.branch || github.ref_name }}" \
              -e GITLAB_TOKEN="${{ secrets.GITLAB_TOKEN }}" \
              -e GITLAB_TOKEN_USERNAME="${{ secrets.GITLAB_TOKEN_USERNAME }}" \
              ${CI_PASSTHROUGH} \
              ${CREW_MAX_BUILD_TIME_PASSTHROUGH} \
              -v /tmp/pkg_cache:/usr/local/tmp/packages:rshared \
              -v "$(pwd)":/output:rshared \
              --tmpfs /tmp \
              --ulimit "nofile=$(ulimit -Sn):$(ulimit -Hn)" \
              "${CONTAINER}" \
              /bin/chromebrewstart "/output/tools/github_actions_update_builder.sh --continue-after-failed-builds"  > >(tee -a /tmp/build.log) 2> >(tee -a /tmp/build.log >&2)
              grep "Built and Uploaded:" /tmp/build.log || true
              echo "Deleting build output directories."
              sudo rm -rf release pkg_cache
      - name: Add updated packages to branch.
        id: push-check
        run: |
          if [ -n "$(git status --porcelain)" ]; then
            git config user.name "${{ github.actor }}"
            git config user.email "${{ github.actor }}@users.noreply.github.com"
            git stash || true
            git fetch origin ${{ inputs.branch || github.ref_name }}
            git stash pop || true
            git commit -am "${{ inputs.branch || github.ref_name }}: Package File Update Run on ${PLATFORM} container." && git push origin ${{ inputs.branch || github.ref_name }}
            git log --oneline -10
          fi
  build-check:
    runs-on: ubuntu-24.04-arm
    needs:
      - setup
      - update-package-files
    if: ${{ ( always() && !cancelled() ) && needs.setup.result == 'success' && ((( inputs.update_package_files ) && needs.update-package-files.result == 'success') || !( inputs.update_package_files )) }}
    outputs:
      changed_packages: ${{ steps.changed-packages.outputs.CHANGED_PACKAGES }}
      current_head: ${{ steps.get-current-head.outputs.CURRENT_HEAD }}
    steps:
      - name: Fail if update or build jobs failed, otherwise create a PR
        if: ${{ contains(needs.*.result, 'failure') }}
        run: exit 1
      - name: Report update & build success
        run: echo "Update & build jobs succeeded. Creating a PR."
      - uses: actions/checkout@v5
        with:
          fetch-depth: 0
          persist-credentials: true
          ref: ${{ inputs.branch || github.ref_name }}
      - name: Get GH Token
        id: get_workflow_token
        uses: peter-murray/workflow-application-token-action@v4
        with:
          application_id: ${{ secrets.APPLICATION_ID }}
          application_private_key: ${{ secrets.APPLICATION_PRIVATE_KEY }}
          organization: chromebrew
          revoke_token: true
      - name: Save git log
        id: save-git-log
        env:
          GH_TOKEN: ${{ steps.get_workflow_token.outputs.token }}
        run: |
          PR_NUMBER=$(gh pr list -L 1 -s open -H ${{ inputs.branch || github.ref_name }} | cut -f1)
          if [[ -n ${PR_NUMBER} ]]; then
            # Try to update branch immediately.
            gh pr update-branch --rebase || true
          else
            gh pr create -d -b WIP -t "WIP ${{ inputs.branch || github.ref_name }}"
            # Create a draft PR and immediately try to update the branch
            # before continuing.
            gh pr update-branch --rebase || true
          fi
          git config user.name "${{ github.actor }}"
          git config user.email "${{ github.actor }}@users.noreply.github.com"
          git checkout master && git fetch origin master
          git checkout "${{ inputs.branch || github.ref_name }}"
          git fetch origin "${{ inputs.branch || github.ref_name }}"
          git log --no-merges --oneline -10
          git log --no-merges --oneline master..${{ inputs.branch || github.ref_name }} | grep -v "Merge branch 'master'\|Build Run on\|Package File Update Run on\|lint$" | tr '\n' '\0' | xargs -0 -n1 echo "- $*" >> /tmp/commits.txt
      - name: Get Current HEAD hash
        id: get-current-head
        run: |
          echo "CURRENT_HEAD=$(git ls-remote https://github.com/${{ github.repository }}.git | head -1 | sed 's/HEAD//')" >> "$GITHUB_OUTPUT"
          echo "CURRENT_HEAD is $(git ls-remote https://github.com/${{ github.repository }}.git | head -1 | sed 's/HEAD//')"
      - name: Get changed files
        id: changed-files
        uses: tj-actions/changed-files@v47
        with:
          base_sha: ${{ steps.get-current-head.outputs.CURRENT_HEAD }}
          files_yaml: |
            manifest:
              - manifest/**
            packages:
              - packages/*.rb
            github:
              - .github/**
            other:
              - ./**
              - '!manifest/**'
              - '!packages/*.rb'
              - '!.github/**'
      - name: Export variables to github context
        id: changed-packages
        env:
          CHANGED_FILES: ${{ steps.changed-files.outputs.packages_all_changed_files }}
        run: |
            if [[ -n "${CHANGED_FILES}" ]]; then
              # Convert "packages/foo.rb packages/bar.rb" (from steps.changed-files.outputs.packages_all_changed_files) into "foo bar"
              echo "CHANGED_PACKAGES=$(echo "${CHANGED_FILES}" | xargs basename -s .rb | xargs)" >> "$GITHUB_ENV"
              echo "CHANGED_PACKAGES=$(echo "${CHANGED_FILES}" | xargs basename -s .rb | xargs)" >> "$GITHUB_OUTPUT"
            else
              echo "No Changed Files."
            fi
      - name: Create Pull Request
        env:
          CHANGED_GITHUB_CONFIG_FILES: ${{ steps.changed-files.outputs.github_all_changed_files }}
          CHANGED_MANIFEST_FILES: ${{ steps.changed-files.outputs.manifest_all_changed_files }}
          CHANGED_OTHER_FILES: ${{ steps.changed-files.outputs.other_all_changed_files }}
          CHANGED_PACKAGES: ${{ steps.changed-packages.outputs.CHANGED_PACKAGES }}
          CREW_BRANCH: ${{ inputs.branch || github.ref_name }}
          DRAFT_PR: ${{ inputs.draft_pr }}
          GH_TOKEN: ${{ steps.get_workflow_token.outputs.token }}
          PR_LABEL: ${{ inputs.pr_label }}
          PR_TITLE: ${{ inputs.pr_title }}
          i686_PACKAGES: ${{ needs.setup.outputs.i686_packages }}
          x86_64_PACKAGES: ${{ needs.setup.outputs.x86_64_packages }}
          armv7l_PACKAGES: ${{ needs.setup.outputs.armv7l_packages }}
        run: |
          # Make untouched clone of repo so we can get the existing versions of packages.
          (cd /tmp ; git clone --depth=1 https://github.com/${{ github.repository }}.git chromebrew_main )
          sudo gem install -N json --conservative
          sudo gem install -N ptools --conservative
          sudo gem install -N highline --conservative
          sudo gem install -N concurrent-ruby --conservative
          sudo gem install -N method_source --conservative
          sudo gem install -N cgi --conservative
          sudo gem install -N ruby-libversion --conservative
          # Make crew do automatic gem installs before looking for deps.
          LD_LIBRARY_PATH=/usr/local/lib ruby bin/crew version
          # Force creation of temporary device.json.
          LD_LIBRARY_PATH=/usr/local/lib ruby bin/crew deps zstd_static
          function join_by { local IFS="$1"; shift; echo "$*"; }
          rm -rf /tmp/pr.txt
          echo -e "## Description" >> /tmp/pr.txt
          echo -e "#### Commits:" >> /tmp/pr.txt
          # Get git log from rebase-and-git-log step.
          cat /tmp/commits.txt >> /tmp/pr.txt
          if [[ "${CHANGED_GITHUB_CONFIG_FILES}" != "" ]]; then
            echo -e "### Updated GitHub configuration files:" >> /tmp/pr.txt
            for file in ${CHANGED_GITHUB_CONFIG_FILES}
              do
              echo "- ${file}" >> /tmp/pr.txt
            done
          fi
          if [[ "${CHANGED_PACKAGES}" != "" ]]; then
            echo -e "### Packages with Updated versions or Changed package files:" >> /tmp/pr.txt
            for file in ${CHANGED_PACKAGES}
              do
              # Get version before PR, but do not let a broken version stop us.
              main_version_string="$(cd /tmp/chromebrew_main ; LD_LIBRARY_PATH=/usr/local/lib ruby bin/crew version ${file} || true)"
              [[ -n "$main_version_string" ]] && main_version_string=": $main_version_string"
              pkg_version="$(LD_LIBRARY_PATH=/usr/local/lib ruby bin/crew version ${file})"
              upstream_version="$(LD_LIBRARY_PATH=/usr/local/lib ruby tools/version.rb -j "${file}" | jq -r '.[]|.upstream_version' || true)"
              if [[ -z "$pkg_version" ]]; then
                echo "- \`${file}\`${main_version_string}" >> /tmp/pr.txt
                PKG_DELTA+=( "${file}${main_version_string}" )
              elif [[ -z "$upstream_version" ]]; then
                echo "- \`${file}\`${main_version_string} &rarr; ${pkg_version}" >> /tmp/pr.txt
                PKG_DELTA+=( "${file}${main_version_string} → ${pkg_version}" )
              elif [[ "$pkg_version" == "$upstream_version" ]]; then
                echo "- \`${file}\`${main_version_string} &rarr; ${pkg_version}" >> /tmp/pr.txt
                PKG_DELTA+=( "${file}${main_version_string} → ${pkg_version}" )
              else
                echo "- \`${file}\`${main_version_string} &rarr; ${pkg_version} (current version is ${upstream_version})" >> /tmp/pr.txt
                PKG_DELTA+=( "${file}${main_version_string} → ${pkg_version}" )
              fi
            done
            echo -e "##\nBuilds attempted for:" >> /tmp/pr.txt
            [ -n "${x86_64_PACKAGES}" ] && echo -e "- [x] \`x86_64\`" >> /tmp/pr.txt
            [ -n "${i686_PACKAGES}" ] && echo -e "- [x] \`i686\`" >> /tmp/pr.txt
            [ -n "${armv7l_PACKAGES}" ] && echo -e "- [x] \`armv7l\`" >> /tmp/pr.txt
          fi
          if [[ "${CHANGED_OTHER_FILES}" != "" ]]; then
            echo -e "### Other changed files:" >> /tmp/pr.txt
            for file in ${CHANGED_OTHER_FILES}
              do
              echo "- ${file}" >> /tmp/pr.txt
            done
          fi
          if [[ "${CHANGED_MANIFEST_FILES}" == "" ]]; then
            echo -e "##\n- [x] This PR has no manifest .filelist changes. _(Package changes have neither added nor removed files.)_" >> /tmp/pr.txt
          fi
          echo -e "##\n### Run the following to get this pull request's changes locally for testing.\n\`\`\`bash" >> /tmp/pr.txt
          echo -e "CREW_REPO=https://github.com/${{ github.repository }}.git CREW_BRANCH=${{ inputs.branch || github.ref_name }} crew update \\" >> /tmp/pr.txt
          echo -e "&& yes | crew upgrade\n\`\`\`" >> /tmp/pr.txt
          [[ -n "${PKG_DELTA[*]}" ]] && PKG_DELTAS="— $(join_by , "${PKG_DELTA[@]}")"
          echo "PKG_DELTAS: $PKG_DELTAS"
          cat /tmp/pr.txt
          PR_NUMBER=$(gh pr list -L 1 -s open -H ${{ inputs.branch || github.ref_name }} | cut -f1)
          PR_TITLE_INPUT="${{ inputs.pr_title }}"
          if [[ -z ${PR_TITLE_INPUT} ]]; then
            PR_TITLE="$(git log --oneline master..${{ inputs.branch || github.ref_name }} | grep -v "Merge branch 'master'\|Build Run on\|Package File Update Run on" | tail -n 1 | cut -c 11-)"
            TITLE="$(echo "${PR_TITLE}" | sed -e "s/^'//" -e "s/'$//") ${PKG_DELTAS}"
          else
            PR_TITLE="${{ inputs.pr_title }}"
            TITLE="${PR_TITLE}"
          fi
          echo "PR title is: ${TITLE}"
          if [[ -n "${PR_LABEL}" ]]; then
              export PR_CREATE_LABEL_FLAG="--label ${{ inputs.pr_label }}"
              export PR_EDIT_LABEL_FLAG="--add-label ${{ inputs.pr_label }}"
          fi
          if [[ -z ${PR_NUMBER} ]]; then
            PR_NUMBER=$(gh pr create -d --reviewer chromebrew/active $PR_CREATE_LABEL_FLAG --title "${TITLE}" -F /tmp/pr.txt | rev | cut -d"/" -f1  | rev)
          else
            gh pr edit --add-reviewer chromebrew/active $PR_EDIT_LABEL_FLAG --title "${TITLE}" -F /tmp/pr.txt
          fi
          # Try to update branch before finishing.
          gh pr update-branch --rebase || true
          # Check to see if the PR is marked as WIP, in which case it should stay as draft.
          if gh pr list -l "wip 🚧" --json number | jq --arg PR_NUMBER "$PR_NUMBER"  -e -r '.[]|select(.number=='$PR_NUMBER')' &>/dev/null; then
            echo "PR ${PR_NUMBER} is set as WIP, so will be set or kept as Draft."
            export DRAFT_PR=true
          fi
          # Draft PRs can not be set to automerge.
          if [[ ${DRAFT_PR} == 'true' ]]; then
            gh pr ready --undo || true
          else
            gh pr ready || true
            gh pr merge --auto || true
          fi
          echo "PR_NUMBER is ${PR_NUMBER}"
          echo "PR_NUMBER=${PR_NUMBER}" >> "$GITHUB_ENV"
          # Trigger workflow runs:
          # gh workflow run Unit-Test.yml -R ${{ github.repository }} -r ${{ inputs.branch || github.ref_name }}
          # gh workflow run Linter-Handoff.yml -R ${{ github.repository }} -r ${{ inputs.branch || github.ref_name }}
