name: Update wp-cli framework

on:
  workflow_call:
  push:
    branches:
      - main
      - master
  schedule:
    - cron:  '17 4 * * *' # Run every day on a seemly random time.

# Cancels all previous workflow runs for the same branch that have not yet completed.
concurrency:
  # The concurrency group contains the workflow name and the branch name.
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true


jobs:

  update-framework: #----------------------------------------------------------
    name: Update wp-cli framework
    runs-on: ubuntu-latest
    if: ${{ github.repository_owner == 'wp-cli' }}
    steps:
      - name: Check out source code
        uses: actions/checkout@v4

      - name: Set up PHP envirnoment
        uses: shivammathur/setup-php@v2
        with:
          php-version: '7.4'
        env:
          COMPOSER_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      - name: Check existence of composer.json file
        id: check_composer_file
        uses: andstor/file-existence-action@v2
        with:
          files: "composer.json"

      - name: Install Composer dependencies & cache dependencies
        if: steps.check_composer_file.outputs.files_exists == 'true'
        uses: "ramsey/composer-install@v2"
        env:
          COMPOSER_ROOT_VERSION: dev-${{ github.event.repository.default_branch }}
        with:
          # Bust the cache at least once a month - output format: YYYY-MM.
          custom-cache-suffix: $(date -u "+%Y-%m")

      - name: Configure git user
        run: |
          git config --global user.email "info@wp-cli.org"
          git config --global user.name "wp-make-coffee"

      - name: Check if remote branch exists
        run: echo "REMOTE_BRANCH_EXISTS=$([[ -z $(git ls-remote --heads origin update-framework) ]] && echo "0" || echo "1")" >> $GITHUB_ENV

      - name: Create branch to base pull request on
        if: env.REMOTE_BRANCH_EXISTS == 0
        run: |
          git checkout -b update-framework

      - name: Fetch existing branch to add commits to
        if: env.REMOTE_BRANCH_EXISTS == 1
        run: |
          git fetch --all --prune
          git checkout update-framework
          git pull --no-rebase

      - name: Update wp-cli framework
        run: |
          composer update wp-cli/wp-cli

      - name: Check if there are changes
        run: echo "CHANGES_DETECTED=$([[ -z $(git status --porcelain) ]] && echo "0" || echo "1")" >> $GITHUB_ENV

      - name: Commit changes
        if: env.CHANGES_DETECTED == 1
        run: |
          git add composer.lock
          git commit -m "Update wp-cli framework - $(date +'%Y-%m-%d')"
          git push origin update-framework

      - name: Create pull request
        if: |
          env.CHANGES_DETECTED == 1 &&
          env.REMOTE_BRANCH_EXISTS == 0
        uses: repo-sync/pull-request@v2
        with:
          source_branch: update-framework
          destination_branch: ${{ github.event.repository.default_branch }}
          github_token: ${{ secrets.REPO_GITHUB_TOKEN }}
          pr_title: Update wp-cli framework
          pr_body: "**This is an automated pull-request**\n\nUpdates the `wp-cli/wp-cli` framework to the latest changeset."
          pr_label: scope:framework
