Skip to content

Commit 71591eb

Browse files
authored
fix: Fix pull request issues (#10)
### Added - debug info (list of branches) - outputs `changed-files` with a comma-separated list of files changed by phpcbf ### Fixed - Prevents issues with changing files in detached HEAD states (during pull request) by committing to the source branch of the pull request.
1 parent cc77a00 commit 71591eb

File tree

5 files changed

+79
-10
lines changed

5 files changed

+79
-10
lines changed

.github/workflows/phpcs-phpcbf.yml

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

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ on:
1212
# notest branches to ignore testing of partial online commits
1313
- "notest/**"
1414

15+
schedule:
16+
# Run the workflow at 6:30 AM UTC on the 18th of every month
17+
- cron: "30 6 18 * *"
18+
# Scheduled runs do not commit-changes automatically to the same branch
19+
1520
permissions:
1621
# only prettier-fix needs write permission, for others read is enough
1722
contents: read
@@ -28,7 +33,7 @@ jobs:
2833
- name: Invoke the Prettier fix
2934
uses: WorkOfStan/[email protected]
3035
with:
31-
commit-changes: true
36+
commit-changes: ${{ github.event_name != 'schedule' }}
3237

3338
super-linter:
3439
needs: prettier-fix

CHANGELOG.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,19 @@ 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.2] - 2025-05-31
23+
24+
fix: Fix pull request issues
25+
26+
### Added
27+
28+
- debug info (list of branches)
29+
- outputs `changed-files` with a comma-separated list of files changed by phpcbf
30+
31+
### Fixed
32+
33+
- Prevents issues with changing files in detached HEAD states (during pull request) by committing to the source branch of the pull request.
34+
2235
## [1.0.1] - 2025-02-09
2336

2437
### Added
@@ -38,6 +51,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
3851
- 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.)
3952
- Cached `vendor/` (for a unique combination of php-version and composer.json) after a successful run in order to speed up further runs.
4053

41-
[Unreleased]: https://github.com/WorkOfStan/phpcs-fix/compare/v1.0.1...HEAD?w=1
54+
[Unreleased]: https://github.com/WorkOfStan/phpcs-fix/compare/v1.0.2...HEAD?w=1
55+
[1.0.2]: https://github.com/WorkOfStan/phpcs-fix/compare/v1.0.1...v1.0.2?w=1
4256
[1.0.1]: https://github.com/WorkOfStan/phpcs-fix/compare/v1.0.0...v1.0.1?w=1
4357
[1.0.0]: https://github.com/WorkOfStan/phpcs-fix/releases/tag/v1.0.0

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ permissions:
3939
| -------------------- | ---------------------------------------------------------------------------------------------------------------------------- | ------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
4040
| `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` |
4141
| `commit-message` | Commit message to use if the action commits changes. | String | `"PHP Code Beautifier fixes applied automatically"` |
42+
| `debug` | Enable extra debug output (list of branches). | Boolean | `false` |
4243
| `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) |
4344
| `ignore` | Ignore files based on a comma-separated list of patterns matching files and/or directories. | String | `vendor/` |
4445
| `php-version` | The PHP version to use, e.g. `"8.2"`. | String | `"8.2"` |
@@ -47,9 +48,10 @@ permissions:
4748

4849
### Outputs
4950

50-
| Output | Description |
51-
| ------------- | ----------------------------------- |
52-
| `branch-name` | The name of the branch created/used |
51+
| Output | Description |
52+
| --------------- | ----------------------------------------------- |
53+
| `branch-name` | The name of the branch created/used |
54+
| `changed-files` | Comma-separated list of files changed by phpcbf |
5355

5456
## Caching Mechanism
5557

action.yml

Lines changed: 52 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ inputs:
2020
description: "Commit message for the changes"
2121
type: string
2222
default: "PHP Code Beautifier fixes applied automatically"
23+
debug:
24+
description: "Enable extra debug output"
25+
type: boolean
26+
default: false
2327
# https://github.com/PHPCSStandards/PHP_CodeSniffer/wiki/Advanced-Usage#specifying-valid-file-extensions
2428
# By default, PHP_CodeSniffer will check any file it finds with a .inc, .php, .js or .css extension, although not all standards will actually check all these file types.
2529
# Empty value means no checking at all.
@@ -53,6 +57,8 @@ inputs:
5357
outputs:
5458
branch-name:
5559
description: "The branch where changes were committed"
60+
changed-files:
61+
description: "Comma-separated list of files changed by phpcbf"
5662

5763
runs:
5864
using: "composite"
@@ -91,6 +97,19 @@ runs:
9197
run: composer require --dev squizlabs/php_codesniffer --prefer-dist --no-progress
9298
shell: bash
9399

100+
- name: Debug branches
101+
if: ${{ inputs.debug == 'true' }}
102+
run: |
103+
echo "Debug 1. List All Local Branches"
104+
git branch
105+
echo "Debug 2. List All Remote Branches"
106+
git branch -r
107+
echo "Debug 3. List All References (Including Detached HEADs)"
108+
git show-ref
109+
echo "Debug 4. Check Your Current Checked-out Reference"
110+
git status
111+
shell: bash
112+
94113
- name: Run PHP Code Sniffer
95114
id: phpcs
96115
run: |
@@ -113,10 +132,11 @@ runs:
113132
114133
# Default the variable if no issues were found
115134
: "${HAS_ISSUES:=false}"
116-
echo "HAS_ISSUES=$HAS_ISSUES" >> "$GITHUB_ENV"
135+
echo "HAS_ISSUES=$HAS_ISSUES" >> $GITHUB_ENV
117136
shell: bash
118137

119138
- name: Run PHP Code Beautifier if needed
139+
id: fix
120140
if: env.HAS_ISSUES == 'true'
121141
run: |
122142
echo "phpcbf standard=${{ env.USE_STANDARD }}"
@@ -141,19 +161,34 @@ runs:
141161
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."
142162
if [ "${{ inputs.stop-on-manual-fix }}" = "true" ]; then
143163
# Indicates an error
164+
# This sets the output named branch-name to an empty value. Which is useful to signal “no branch was created” in a way that downstream steps can detect reliably.
165+
echo "branch-name=" >> $GITHUB_OUTPUT
166+
echo "changed-files=" >> $GITHUB_OUTPUT
144167
exit 1
145168
else
146169
# Exiting gracefully
170+
echo "branch-name=" >> $GITHUB_OUTPUT
171+
echo "changed-files=" >> $GITHUB_OUTPUT
147172
exit 0
148173
fi
149174
# The rest of the script must still be within the same step to really stop the execution
150175
fi
151176
152-
git add .
177+
# Only add changed files, safe for spaces
178+
git status --porcelain | awk '{print $2}' | while IFS= read -r file; do git add "$file"; done
153179
154180
# Determine branch name based on commit-changes input
155-
if ${{ inputs.commit-changes }}; then
156-
BRANCH_NAME=$(git branch --show-current)
181+
if [ "${{ inputs.commit-changes }}" == "true" ]; then
182+
BRANCH_NAME=$(git rev-parse --abbrev-ref HEAD 2>/dev/null)
183+
if [ "$BRANCH_NAME" == "HEAD" ] || [ -z "$BRANCH_NAME" ]; then
184+
echo "Detached HEAD detected. Checking out the PR branch directly from origin..."
185+
# Fetch the PR branch tip and force a local branch matching the remote head
186+
# Note: ${{ github.head_ref }} is a GitHub Actions built-in variable that contains the name of the source branch of a pull request (PR).
187+
# It is only available during workflow runs triggered by pull_request events.
188+
git fetch origin ${{ github.head_ref }} --depth=1
189+
git checkout -B "${{ github.head_ref }}" "origin/${{ github.head_ref }}"
190+
BRANCH_NAME=$(git rev-parse --abbrev-ref HEAD)
191+
fi
157192
NOTICE_MESSAGE="A PHPCBF commit was successfully added to the current branch: $BRANCH_NAME"
158193
else
159194
# name includes timestamp and short_hash
@@ -174,11 +209,23 @@ runs:
174209
COMMIT_URL=${{ github.server_url }}/${{ github.repository }}/commit/$(git rev-parse HEAD)
175210
echo "::notice title=View the PHPCBF commit::${COMMIT_URL}"
176211
177-
# Set branch name as output
212+
# Set outputs
213+
CHANGED_FILES=$(git diff-tree --no-commit-id --name-only -r HEAD | tr '\n' ',' | sed 's/,$//')
178214
echo "branch-name=$BRANCH_NAME" >> $GITHUB_OUTPUT
215+
echo "changed-files=${CHANGED_FILES}" >> $GITHUB_OUTPUT
216+
if [ "${{ inputs.debug }}" = "true" ]; then
217+
echo "changed-files=${CHANGED_FILES}"
218+
fi
179219
180220
## Stop with an error if some manual fixes are required on top of automatic fixes
181221
if [ "${{ inputs.stop-on-manual-fix }}" = "true" ] && [ "${MORE_ISSUES}" = "true" ]; then
182222
exit 1
183223
fi
184224
shell: bash
225+
226+
- name: No changes output (when no fix needed)
227+
if: steps.fix.outcome == 'skipped'
228+
run: |
229+
echo "branch-name=" >> $GITHUB_OUTPUT
230+
echo "changed-files=" >> $GITHUB_OUTPUT
231+
shell: bash

0 commit comments

Comments
 (0)