Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/phpcs-phpcbf.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ jobs:
uses: WorkOfStan/phpcs-fix@main
with:
commit-changes: true
#debug: true
stop-on-manual-fix: true
7 changes: 6 additions & 1 deletion .github/workflows/polish-the-code.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ on:
# notest branches to ignore testing of partial online commits
- "notest/**"

schedule:
# Run the workflow at 6:30 AM UTC on the 18th of every month
- cron: "30 6 18 * *"
# Scheduled runs do not commit-changes automatically to the same branch

permissions:
# only prettier-fix needs write permission, for others read is enough
contents: read
Expand All @@ -28,7 +33,7 @@ jobs:
- name: Invoke the Prettier fix
uses: WorkOfStan/[email protected]
with:
commit-changes: true
commit-changes: ${{ github.event_name != 'schedule' }}

super-linter:
needs: prettier-fix
Expand Down
16 changes: 15 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,19 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### `Security` in case of vulnerabilities

## [1.0.2] - 2025-05-31

fix: Fix pull request issues

### 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.0.1] - 2025-02-09

### Added
Expand All @@ -38,6 +51,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- 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.

[Unreleased]: https://github.com/WorkOfStan/phpcs-fix/compare/v1.0.1...HEAD?w=1
[Unreleased]: https://github.com/WorkOfStan/phpcs-fix/compare/v1.0.2...HEAD?w=1
[1.0.2]: https://github.com/WorkOfStan/phpcs-fix/compare/v1.0.1...v1.0.2?w=1
[1.0.1]: https://github.com/WorkOfStan/phpcs-fix/compare/v1.0.0...v1.0.1?w=1
[1.0.0]: https://github.com/WorkOfStan/phpcs-fix/releases/tag/v1.0.0
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ permissions:
| -------------------- | ---------------------------------------------------------------------------------------------------------------------------- | ------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `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` |
| `commit-message` | Commit message to use if the action commits changes. | String | `"PHP Code Beautifier fixes applied automatically"` |
| `debug` | Enable extra debug output (list of branches). | Boolean | `false` |
| `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) |
| `ignore` | Ignore files based on a comma-separated list of patterns matching files and/or directories. | String | `vendor/` |
| `php-version` | The PHP version to use, e.g. `"8.2"`. | String | `"8.2"` |
Expand All @@ -47,9 +48,10 @@ permissions:

### Outputs

| Output | Description |
| ------------- | ----------------------------------- |
| `branch-name` | The name of the branch created/used |
| Output | Description |
| --------------- | ----------------------------------------------- |
| `branch-name` | The name of the branch created/used |
| `changed-files` | Comma-separated list of files changed by phpcbf |

## Caching Mechanism

Expand Down
57 changes: 52 additions & 5 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ inputs:
description: "Commit message for the changes"
type: string
default: "PHP Code Beautifier fixes applied automatically"
debug:
description: "Enable extra debug output"
type: boolean
default: false
# https://github.com/PHPCSStandards/PHP_CodeSniffer/wiki/Advanced-Usage#specifying-valid-file-extensions
# 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.
# Empty value means no checking at all.
Expand Down Expand Up @@ -53,6 +57,8 @@ inputs:
outputs:
branch-name:
description: "The branch where changes were committed"
changed-files:
description: "Comma-separated list of files changed by phpcbf"

runs:
using: "composite"
Expand Down Expand Up @@ -91,6 +97,19 @@ runs:
run: composer require --dev squizlabs/php_codesniffer --prefer-dist --no-progress
shell: bash

- name: Debug branches
if: ${{ inputs.debug == 'true' }}
run: |
echo "Debug 1. List All Local Branches"
git branch
echo "Debug 2. List All Remote Branches"
git branch -r
echo "Debug 3. List All References (Including Detached HEADs)"
git show-ref
echo "Debug 4. Check Your Current Checked-out Reference"
git status
shell: bash

- name: Run PHP Code Sniffer
id: phpcs
run: |
Expand All @@ -113,10 +132,11 @@ runs:

# Default the variable if no issues were found
: "${HAS_ISSUES:=false}"
echo "HAS_ISSUES=$HAS_ISSUES" >> "$GITHUB_ENV"
echo "HAS_ISSUES=$HAS_ISSUES" >> $GITHUB_ENV
shell: bash

- name: Run PHP Code Beautifier if needed
id: fix
if: env.HAS_ISSUES == 'true'
run: |
echo "phpcbf standard=${{ env.USE_STANDARD }}"
Expand All @@ -141,19 +161,34 @@ runs:
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."
if [ "${{ inputs.stop-on-manual-fix }}" = "true" ]; then
# Indicates an error
# 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.
echo "branch-name=" >> $GITHUB_OUTPUT
echo "changed-files=" >> $GITHUB_OUTPUT
exit 1
else
# Exiting gracefully
echo "branch-name=" >> $GITHUB_OUTPUT
echo "changed-files=" >> $GITHUB_OUTPUT
exit 0
fi
# The rest of the script must still be within the same step to really stop the execution
fi

git add .
# Only add changed files, safe for spaces
git status --porcelain | awk '{print $2}' | while IFS= read -r file; do git add "$file"; done

# Determine branch name based on commit-changes input
if ${{ inputs.commit-changes }}; then
BRANCH_NAME=$(git branch --show-current)
if [ "${{ inputs.commit-changes }}" == "true" ]; then
BRANCH_NAME=$(git rev-parse --abbrev-ref HEAD 2>/dev/null)
if [ "$BRANCH_NAME" == "HEAD" ] || [ -z "$BRANCH_NAME" ]; then
echo "Detached HEAD detected. Checking out the PR branch directly from origin..."
# Fetch the PR branch tip and force a local branch matching the remote head
# Note: ${{ github.head_ref }} is a GitHub Actions built-in variable that contains the name of the source branch of a pull request (PR).
# It is only available during workflow runs triggered by pull_request events.
git fetch origin ${{ github.head_ref }} --depth=1
git checkout -B "${{ github.head_ref }}" "origin/${{ github.head_ref }}"
BRANCH_NAME=$(git rev-parse --abbrev-ref HEAD)
fi
NOTICE_MESSAGE="A PHPCBF commit was successfully added to the current branch: $BRANCH_NAME"
else
# name includes timestamp and short_hash
Expand All @@ -174,11 +209,23 @@ runs:
COMMIT_URL=${{ github.server_url }}/${{ github.repository }}/commit/$(git rev-parse HEAD)
echo "::notice title=View the PHPCBF commit::${COMMIT_URL}"

# Set branch name as output
# Set outputs
CHANGED_FILES=$(git diff-tree --no-commit-id --name-only -r HEAD | tr '\n' ',' | sed 's/,$//')
echo "branch-name=$BRANCH_NAME" >> $GITHUB_OUTPUT
echo "changed-files=${CHANGED_FILES}" >> $GITHUB_OUTPUT
if [ "${{ inputs.debug }}" = "true" ]; then
echo "changed-files=${CHANGED_FILES}"
fi

## Stop with an error if some manual fixes are required on top of automatic fixes
if [ "${{ inputs.stop-on-manual-fix }}" = "true" ] && [ "${MORE_ISSUES}" = "true" ]; then
exit 1
fi
shell: bash

- name: No changes output (when no fix needed)
if: steps.fix.outcome == 'skipped'
run: |
echo "branch-name=" >> $GITHUB_OUTPUT
echo "changed-files=" >> $GITHUB_OUTPUT
shell: bash