diff --git a/.github/workflows/phpcs-phpcbf.yml b/.github/workflows/phpcs-phpcbf.yml index fad77df..a5e3ea0 100644 --- a/.github/workflows/phpcs-phpcbf.yml +++ b/.github/workflows/phpcs-phpcbf.yml @@ -16,4 +16,5 @@ jobs: uses: WorkOfStan/phpcs-fix@main with: commit-changes: true + #debug: true stop-on-manual-fix: true diff --git a/.github/workflows/polish-the-code.yml b/.github/workflows/polish-the-code.yml index 6359e24..2629278 100644 --- a/.github/workflows/polish-the-code.yml +++ b/.github/workflows/polish-the-code.yml @@ -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 @@ -28,7 +33,7 @@ jobs: - name: Invoke the Prettier fix uses: WorkOfStan/prettier-fix@v1.1.3 with: - commit-changes: true + commit-changes: ${{ github.event_name != 'schedule' }} super-linter: needs: prettier-fix diff --git a/CHANGELOG.md b/CHANGELOG.md index 9f20e67..7630dbe 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 @@ -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 diff --git a/README.md b/README.md index b5d0c51..2c99aec 100644 --- a/README.md +++ b/README.md @@ -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"` | @@ -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 diff --git a/action.yml b/action.yml index 94f8082..3388c7f 100644 --- a/action.yml +++ b/action.yml @@ -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. @@ -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" @@ -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: | @@ -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 }}" @@ -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 @@ -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