name: Update PHPCS version

on:
  # Run every day at 3:40.
  schedule:
    - cron: '40 3 * * *'
  # And whenever this workflow is updated.
  pull_request:
    paths:
      - '.github/workflows/update-phpcs-versionnr.yml'
  # Also allow manually triggering the workflow.
  workflow_dispatch:

# 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:
  phpcs-version-check:
    name: "Check latest PHPCS version"
    # Don't run the cron job on forks.
    if: ${{ github.event_name != 'schedule' || github.event.repository.fork == false }}

    runs-on: ubuntu-latest
    steps:
      - name: Retrieve latest PHPCS release info
        uses: octokit/request-action@dad4362715b7fb2ddedf9772c8670824af564f0d # v2.4.0
        id: get_latest_release
        with:
          route: GET /repos/PHPCSStandards/PHP_CodeSniffer/releases/latest
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      - name: "Debug info: Show API request failure status"
        if: ${{ failure() }}
        run: "echo No release found. Request failed with status ${{ steps.get_latest_release.outputs.status }}"

      - name: Grab latest tag name from API response
        id: version
        run: |
          echo "TAG=${{ fromJson(steps.get_latest_release.outputs.data).tag_name }}" >> "$GITHUB_OUTPUT"

      - name: Show tag name found in API response
        run: "echo latest release: ${{ steps.version.outputs.TAG }}"

      - name: Set branches to use
        id: branches
        run: |
          echo "BASE=develop" >> "$GITHUB_OUTPUT"
          echo "PR_BRANCH=feature/getversiontest-update-phpcs-version" >> "$GITHUB_OUTPUT"

      - name: Checkout code
        uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
        with:
          ref: ${{ steps.branches.outputs.BASE }}
          persist-credentials: false

      - name: Update the version constant in the test file (PHPCS 3)
        if: ${{ startsWith( steps.version.outputs.TAG, '3.' ) }}
        uses: jacobtomlinson/gha-find-replace@f1069b438f125e5395d84d1c6fd3b559a7880cb5 # 3.0.5
        with:
          find: "const LATEST_3X_VERSION = '[^']+';"
          replace: "const LATEST_3X_VERSION = '${{ steps.version.outputs.TAG }}';"
          include: "Tests/BackCompat/Helper/GetVersionTest.php"
          regex: true

      - name: Update the version constant in the test file (PHPCS 4)
        if: ${{ startsWith( steps.version.outputs.TAG, '4.' ) }}
        uses: jacobtomlinson/gha-find-replace@f1069b438f125e5395d84d1c6fd3b559a7880cb5 # 3.0.5
        with:
          find: "const LATEST_4X_VERSION = '[^']+';"
          replace: "const LATEST_4X_VERSION = '${{ steps.version.outputs.TAG }}';"
          include: "Tests/BackCompat/Helper/GetVersionTest.php"
          regex: true

      - name: "Debug info: Show git status"
        run: git status -vv --untracked=all

      - name: Create pull request
        uses: peter-evans/create-pull-request@271a8d0340265f705b14b6d32b9829c1cb33d45e # v7.0.8
        with:
          base: ${{ steps.branches.outputs.BASE }}
          branch: ${{ steps.branches.outputs.PR_BRANCH }}
          delete-branch: true
          sign-commits: true
          commit-message: "GetVersionTest: update for release of PHPCS ${{ steps.version.outputs.TAG }}"
          title: "GetVersionTest: update for release of PHPCS ${{ steps.version.outputs.TAG }}"
          # yamllint disable rule:line-length
          body: |
            This PR is auto-generated by [create-pull-request](https://github.com/peter-evans/create-pull-request) using the [`update-phpcs-versionnr.yml` workflow](https://github.com/PHPCSStandards/PHPCSUtils/blob/develop/.github/workflows/update-phpcs-versionnr.yml).
          # yamllint enable rule:line-length
          labels: |
            Type: chores/QA
          reviewers: |
            jrfnl
