# There is one particular test for the Squiz/FileComment sniff which every year requires
# an update to the year used in a `@copyright` tag to allow the tests to keep passing.
# This workflow will automatically create a PR to the repo to handle this update.

name: "Update Squiz/FileComment Test"

on:
  # Run every year on Jan 1st at 00:05.
  schedule:
    - cron: '5 0 1 1 *'
  # And whenever this workflow is updated.
  push:
    paths:
      - '.github/workflows/happy-new-year.yml'
  pull_request:
    paths:
      - '.github/workflows/happy-new-year.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:
  update-year-in-test:
    runs-on: ubuntu-latest
    # Don't run the cron job on forks.
    if: ${{ github.event_name != 'schedule' || github.repository == 'PHPCSStandards/PHP_CodeSniffer' }}

    name: "Happy New Year"
    steps:
      - name: Set branches to use
        id: branches
        run: |
          echo "BASE=master" >> $GITHUB_OUTPUT
          echo "PR_BRANCH=feature/squiz-filecomment-update-copyright-year" >> $GITHUB_OUTPUT

      # Using "Tomorrow" to prevent accidentally getting last year if the server is not using UTC.
      - name: Grab the new year
        id: year
        run: |
          echo "PREVIOUS_YEAR=$(date --date="last month" -u "+%Y")" >> $GITHUB_OUTPUT
          echo "NEW_YEAR=$(date --date="tomorrow" -u "+%Y")" >> $GITHUB_OUTPUT

      - name: "Debug info: Show years"
        run: "echo current year: ${{ steps.year.outputs.NEW_YEAR }} - previous year: ${{ steps.year.outputs.PREVIOUS_YEAR }}"

      - name: Checkout code
        uses: actions/checkout@v4
        with:
          ref: ${{ steps.branches.outputs.BASE }}

      - name: Update the year in the copyright tag in the fixed file
        id: findreplace
        uses: jacobtomlinson/gha-find-replace@v3
        with:
          find: "* @copyright  ${{ steps.year.outputs.PREVIOUS_YEAR }} Squiz Pty Ltd (ABN 77 084 670 600)"
          replace: "* @copyright  ${{ steps.year.outputs.NEW_YEAR }} Squiz Pty Ltd (ABN 77 084 670 600)"
          include: "src/Standards/Squiz/Tests/Commenting/FileCommentUnitTest.1.*.fixed"
          regex: false

      - name: "Debug info: Show number of modified files"
        run: "echo modified files: ${{ steps.findreplace.outputs.modifiedFiles }}"

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

      - name: Fail the cron job if no files where modified
        if: ${{ github.event_name == 'schedule' && steps.findreplace.outputs.modifiedFiles == 0 }}
        run: exit 1

      - name: Create pull request
        uses: peter-evans/create-pull-request@v5
        with:
          base: ${{ steps.branches.outputs.BASE }}
          branch: ${{ steps.branches.outputs.PR_BRANCH }}
          delete-branch: true
          commit-message: "Squiz/FileComment: update year in test case fixed file"
          title: "Squiz/FileComment: update year in test case fixed file"
          # yamllint disable rule:line-length
          body: |
            The regular annual update to make sure the build still passes ;-)

            Happy new year!

            This PR is auto-generated by [create-pull-request](https://github.com/peter-evans/create-pull-request) using the [`happy-new-year.yml` workflow](https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/.github/workflows/happy-new-year.yml).
          # yamllint enable rule:line-length
          labels: |
            Type: chores/QA
          reviewers: |
            jrfnl
