Skip to content

Commit 11109d3

Browse files
authored
Stop on manual fix (#3)
### Added - The new boolean input `stop-on-manual-fix` will cause the workflow to stop (fail) if manual fixes are necessary. (Also stops with an error if some manual fixes are required on top of automatic fixes.) - Cached `vendor/` (for a unique combination of php-version and composer.json) after a successful run in order to speed up further runs.
1 parent 989cef4 commit 11109d3

File tree

5 files changed

+66
-20
lines changed

5 files changed

+66
-20
lines changed

.github/workflows/phpcs-phpcbf.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@ jobs:
1616
uses: WorkOfStan/phpcs-fix@main
1717
with:
1818
commit-changes: true
19+
stop-on-manual-fix: true

.github/workflows/polish-the-code.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,19 @@ permissions:
1919
jobs:
2020
prettier-fix:
2121
# Note: runs-on doesn't accept all expressions, so a string is used
22-
runs-on: "ubuntu-24.04"
22+
runs-on: "ubuntu-latest"
2323
permissions:
2424
contents: write
2525
# Limit the running time
2626
timeout-minutes: 10
2727
steps:
2828
- name: Invoke the Prettier fix
29-
uses: WorkOfStan/[email protected].0
29+
uses: WorkOfStan/[email protected].1
3030
with:
3131
commit-changes: true
3232

3333
super-linter:
3434
needs: prettier-fix
3535
uses: WorkOfStan/seablast-actions/.github/workflows/[email protected]
3636
with:
37-
runs-on: "ubuntu-24.04"
37+
runs-on: "ubuntu-latest"

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1919

2020
### `Security` in case of vulnerabilities
2121

22-
## [1.0.0] - 2025-01-18
22+
## [1.0.0] - 2025-02-01
2323

2424
- This GitHub Action automates PHPCS formatting across your project, ensuring consistent code styling by creating a new branch for review when necessary. It simplifies integrating PHPCS/PHPCBF into your workflow.
2525
- A warning appears in the GitHub Actions Annotations section if changes occur and therefore a new branch is created.
@@ -29,6 +29,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2929
- **Branch Name Output**: The `branch-name` is provided as an output for downstream workflows.
3030
- Notice about a successful commit.
3131
- **Checkout code**: Fetches the latest changes so that modifications from a previous job are included, enabling seamless job chaining.
32+
- The new boolean input `stop-on-manual-fix` will cause the workflow to stop (fail) if manual fixes are necessary. (Also stops with an error if some manual fixes are required on top of automatic fixes.)
33+
- Cached `vendor/` (for a unique combination of php-version and composer.json) after a successful run in order to speed up further runs.
3234

3335
[Unreleased]: https://github.com/WorkOfStan/phpcs-fix/compare/v1.0.0...HEAD?w=1
3436
[1.0.0]: https://github.com/WorkOfStan/phpcs-fix/releases/tag/v1.0.0

README.md

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,19 @@ This action can either commit changes directly to the current branch or create a
99
- **Automatic Fixes:** Check PHP files for coding standard violations and attempts to automatically fix them when possible.
1010
- **Flexible Committing:** Creates a new branch (prefixed with `phpcbf/fix`) if changes are required, making it easy to review and merge. Or you can set the input parameter `commit-changes: true` to commit to the current branch. (Recommendation: start with committing to another branch and review in order to avoid surprises. Then commit to the same branch for incremental changes.)
1111
- **Configurable:** Adjust extensions, ignore and standard parameters of phpcs. Adjust PHP version, or commit-message.
12+
- **Caching:** Cache `vendor/` (for a unique combination of php-version and composer.json) after a successful run in order to speed up further runs.
13+
- **GitHub Action Chain:** Either leave this GitHub Action non-blocking or set `stop-on-manual-fix` according to your automation process needs.
1214

1315
## Usage
1416

1517
This action is designed to be used as a _reusable workflow_. You can call it from another workflow in your repository or simply add [the provided YAML configuration](.github/workflows/phpcs-phpcbf.yml) to your repository.
1618

17-
By default, if needed, a new branch with a name starting with `prettier/fix` will be created, making it easy to review and merge the fixes into your main branch.
19+
By default, if necessary, a new branch with a name starting with `phpcbf/fix` will be created, making it easy to review and merge fixes into your main branch.
1820

19-
Note: This action is not blocking, so the status remains green even if changes are proposed in the form of a new branch. Then it’s up to you to either create a pull request to merge the changes or delete the branch.
21+
Note 1: This action is non-blocking, so the status remains green even when changes are proposed via a new branch.
22+
A notice message is displayed, and it is then up to you to either create a pull request to merge the changes or delete the branch.
23+
24+
Note 2: However, you can mandate the action to stop if manual fixes are necessary by setting the input parameter `stop-on-manual-fix: true`.
2025

2126
### Permissions
2227

@@ -29,14 +34,15 @@ permissions:
2934
3035
### Inputs (all optional)
3136
32-
| Input | Description | Type | Default |
33-
| ---------------- | ---------------------------------------------------------------------------------------------------------------------------- | ------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
34-
| `commit-changes` | If set to `true`, the action will commit changes to the current branch; otherwise a new branch is created for manual review. | Boolean | `false` |
35-
| `commit-message` | Commit message to use if the action commits changes. | String | `"PHP Code Beautifier fixes applied automatically"` |
36-
| `extensions` | Comma-delimited list of file extensions to be sniffed. Note: an empty value will disable checking. | String | `"php"` (defaults to PHP only; other file types must be specified) |
37-
| `ignore` | Ignore files based on a comma-separated list of patterns matching files and/or directories. | String | `vendor/` |
38-
| `php-version` | The PHP version to use, e.g. `"8.2"`. | String | `"8.2"` |
39-
| `standard` | The name of, or the path to, the coding standard to use. Can be a comma-separated list specifying multiple standards. | String | The project's `.github/linters/phpcs.xml` (where Super-linter expects it) with fallback to <https://github.com/super-linter/super-linter/blob/main/TEMPLATES/phpcs.xml> copied to [.github/linters/super-linter-templates-phpcs.xml](.github/linters/super-linter-templates-phpcs.xml) will be used. |
37+
| Input | Description | Type | Default |
38+
| -------------------- | ---------------------------------------------------------------------------------------------------------------------------- | ------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
39+
| `commit-changes` | If set to `true`, the action will commit changes to the current branch; otherwise a new branch is created for manual review. | Boolean | `false` |
40+
| `commit-message` | Commit message to use if the action commits changes. | String | `"PHP Code Beautifier fixes applied automatically"` |
41+
| `extensions` | Comma-delimited list of file extensions to be sniffed. Note: an empty value will disable checking. | String | `"php"` (defaults to PHP only; other file types must be specified) |
42+
| `ignore` | Ignore files based on a comma-separated list of patterns matching files and/or directories. | String | `vendor/` |
43+
| `php-version` | The PHP version to use, e.g. `"8.2"`. | String | `"8.2"` |
44+
| `standard` | The name of, or the path to, the coding standard to use. Can be a comma-separated list specifying multiple standards. | String | The project's `.github/linters/phpcs.xml` (where Super-linter expects it) with fallback to <https://github.com/super-linter/super-linter/blob/main/TEMPLATES/phpcs.xml> copied to [.github/linters/super-linter-templates-phpcs.xml](.github/linters/super-linter-templates-phpcs.xml) will be used. |
45+
| `stop-on-manual-fix` | If true, the execution will stop when manual fixes are necessary. | Boolean | `false` |
4046

4147
### Outputs
4248

action.yml

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ inputs:
4545
required: false
4646
type: string
4747
default: "" # if left empty, .github/linters/phpcs.xml or https://github.com/super-linter/super-linter/blob/main/TEMPLATES/phpcs.xml will be used
48+
stop-on-manual-fix:
49+
description: "If true, the execution will stop when manual fixes are necessary."
50+
type: boolean
51+
default: false
4852

4953
outputs:
5054
branch-name:
@@ -70,8 +74,20 @@ runs:
7074
php-version: "${{ inputs.php-version }}"
7175
tools: composer:v2
7276

73-
# todo consider using cache
77+
# https://github.com/actions/cache
78+
# A repository can have up to 10 GB of caches. Once the 10 GB limit is reached, older caches will be evicted based on when the cache was last accessed.
79+
# Caches that are not accessed within the last week will also be evicted.
80+
- name: Cache composer dependencies
81+
uses: actions/cache@v4
82+
id: vendor-cache
83+
with:
84+
# path to Checkout working directory is /home/runner/work/repo-name/repo-name , so just add /vendor/
85+
path: ${{ github.workspace }}/vendor/
86+
# Use composer.json for key, if composer.lock is not committed.
87+
key: phpcs-fix-${{ runner.os }}-PHP${{ inputs.php-version }}-vendor-${{ hashFiles('**/composer.json') }}
88+
7489
- name: "Use the latest PHPCS version"
90+
if: ${{ steps.vendor-cache.outputs.cache-hit != 'true' }}
7591
run: composer require --dev squizlabs/php_codesniffer --prefer-dist --no-progress
7692
shell: bash
7793

@@ -104,16 +120,32 @@ runs:
104120
if: env.HAS_ISSUES == 'true'
105121
run: |
106122
echo "phpcbf standard=${{ env.USE_STANDARD }}"
107-
vendor/bin/phpcbf --extensions="${{ inputs.extensions }}" . --standard="${{ env.USE_STANDARD }}" --ignore="${{ inputs.ignore }}" || echo "::warning title=Some phpcs issues remained::Manual fix necessary. Compare to the PHP Code Sniffer section above."
123+
PHPCBF_EXIT_CODE=0
124+
MORE_ISSUES=false
125+
vendor/bin/phpcbf --extensions="${{ inputs.extensions }}" . --standard="${{ env.USE_STANDARD }}" --ignore="${{ inputs.ignore }}" || PHPCBF_EXIT_CODE=$?
126+
if [ ${PHPCBF_EXIT_CODE} -ne 0 ]; then
127+
echo "::notice title=phpcbf wasn't idle::Non-zero code exit of phpcbf = ${PHPCBF_EXIT_CODE} ... phpcbf wasn't idle"
128+
129+
vendor/bin/phpcs --extensions="${{ inputs.extensions }}" . --standard="${{ env.USE_STANDARD }}" --ignore="${{ inputs.ignore }}" || MORE_ISSUES=true
130+
# because of `||`, the previous command always exit 0
131+
if [ "${MORE_ISSUES}" = "true" ]; then
132+
echo "::warning title=Some phpcs issues remained.::Some phpcs issues remained. Manual fix necessary."
133+
fi
134+
fi
108135
109136
# restore composer.json before staging the changes
110137
git checkout composer.json
111138
112139
# Check for fixable changes
113140
if [ -z "$(git status --porcelain)" ]; then
114-
echo "::warning title=No fixable errors were found by phpcbf::Manual fix necessary. Compare to the PHP Code Sniffer section above."
115-
# Exiting gracefully
116-
exit 0
141+
echo "::warning title=No fixable errors were found by phpcbf::No fixable errors were found by phpcbf: Manual fix necessary. Compare to the PHP Code Sniffer section above."
142+
if [ "${{ inputs.stop-on-manual-fix }}" = "true" ]; then
143+
# Indicates an error
144+
exit 1
145+
else
146+
# Exiting gracefully
147+
exit 0
148+
fi
117149
# The rest of the script must still be within the same step to really stop the execution
118150
fi
119151
@@ -127,7 +159,7 @@ runs:
127159
# name includes timestamp and short_hash
128160
BRANCH_NAME="phpcbf/fix-$(date +'%y%m%d%H%M%S')-$(git rev-parse --short HEAD)"
129161
git checkout -b "$BRANCH_NAME"
130-
echo "::warning title=New branch created::Consider pull request for a new branch: $BRANCH_NAME (or delete it)"
162+
echo "::warning title=New branch created::New branch created: Consider pull request for a new branch: $BRANCH_NAME (or delete it)"
131163
NOTICE_MESSAGE="A PHPCBF commit was successfully added to the new branch: $BRANCH_NAME"
132164
fi
133165
@@ -142,4 +174,9 @@ runs:
142174
143175
# Set branch name as output
144176
echo "branch-name=$BRANCH_NAME" >> $GITHUB_OUTPUT
177+
178+
## Stop with an error if some manual fixes are required on top of automatic fixes
179+
if [ "${{ inputs.stop-on-manual-fix }}" = "true" ] && [ "${MORE_ISSUES}" = "true" ]; then
180+
exit 1
181+
fi
145182
shell: bash

0 commit comments

Comments
 (0)