Skip to content

Commit

Permalink
GH Actions: automate yearly update of test file
Browse files Browse the repository at this point in the history
Every year two of the `fixed` files for the tests for the `Squiz.Commenting.FileComment` sniff need to be updated to ensure they have the correct expected year.
See: squizlabs/PHP_CodeSniffer 2793, squizlabs/PHP_CodeSniffer 3525, squizlabs/PHP_CodeSniffer 3737

So far this was always done manually and all PRs created in the new year would show a failing build until that PR was merged and the other PRs rebased.

This commit introduces a new workflow to automate those updates and automatically creates a PR at 5 minutes past midnight on the new year.

All that needs to happy then is to merge the PR promptly and all will be good.

Obviously, the true test of whether this workflow works will be on January 1st, but I've done some testing by manually reverting the change from January of this year and it all seems to look good.

Refs to actions used in this workflow:
* https://github.com/jacobtomlinson/gha-find-replace
* https://github.com/peter-evans/create-pull-request
  • Loading branch information
jrfnl committed Nov 11, 2023
1 parent 265adff commit a586b63
Showing 1 changed file with 94 additions and 0 deletions.
94 changes: 94 additions & 0 deletions .github/workflows/happy-new-year.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
# 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

0 comments on commit a586b63

Please sign in to comment.