diff --git a/.github/workflows/phpcs-phpcbf.yml b/.github/workflows/phpcs-phpcbf.yml index f1947a0..fad77df 100644 --- a/.github/workflows/phpcs-phpcbf.yml +++ b/.github/workflows/phpcs-phpcbf.yml @@ -16,3 +16,4 @@ jobs: uses: WorkOfStan/phpcs-fix@main with: commit-changes: true + stop-on-manual-fix: true diff --git a/.github/workflows/polish-the-code.yml b/.github/workflows/polish-the-code.yml index d259027..613e58f 100644 --- a/.github/workflows/polish-the-code.yml +++ b/.github/workflows/polish-the-code.yml @@ -19,14 +19,14 @@ permissions: jobs: prettier-fix: # Note: runs-on doesn't accept all expressions, so a string is used - runs-on: "ubuntu-24.04" + runs-on: "ubuntu-latest" permissions: contents: write # Limit the running time timeout-minutes: 10 steps: - name: Invoke the Prettier fix - uses: WorkOfStan/prettier-fix@v1.1.0 + uses: WorkOfStan/prettier-fix@v1.1.1 with: commit-changes: true @@ -34,4 +34,4 @@ jobs: needs: prettier-fix uses: WorkOfStan/seablast-actions/.github/workflows/linter.yml@v0.2.1 with: - runs-on: "ubuntu-24.04" + runs-on: "ubuntu-latest" diff --git a/CHANGELOG.md b/CHANGELOG.md index c50511a..cd0d802 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,7 +19,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### `Security` in case of vulnerabilities -## [1.0.0] - 2025-01-18 +## [1.0.0] - 2025-02-01 - 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. - 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 - **Branch Name Output**: The `branch-name` is provided as an output for downstream workflows. - Notice about a successful commit. - **Checkout code**: Fetches the latest changes so that modifications from a previous job are included, enabling seamless job chaining. +- 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.0...HEAD?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 a4b4e04..40988e5 100644 --- a/README.md +++ b/README.md @@ -9,14 +9,19 @@ This action can either commit changes directly to the current branch or create a - **Automatic Fixes:** Check PHP files for coding standard violations and attempts to automatically fix them when possible. - **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.) - **Configurable:** Adjust extensions, ignore and standard parameters of phpcs. Adjust PHP version, or commit-message. +- **Caching:** Cache `vendor/` (for a unique combination of php-version and composer.json) after a successful run in order to speed up further runs. +- **GitHub Action Chain:** Either leave this GitHub Action non-blocking or set `stop-on-manual-fix` according to your automation process needs. ## Usage 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. -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. +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. -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. +Note 1: This action is non-blocking, so the status remains green even when changes are proposed via a new branch. +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. + +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`. ### Permissions @@ -29,14 +34,15 @@ permissions: ### Inputs (all optional) -| Input | Description | Type | Default | -| ---------------- | ---------------------------------------------------------------------------------------------------------------------------- | ------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -| `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"` | -| `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"` | -| `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 copied to [.github/linters/super-linter-templates-phpcs.xml](.github/linters/super-linter-templates-phpcs.xml) will be used. | +| Input | Description | Type | Default | +| -------------------- | ---------------------------------------------------------------------------------------------------------------------------- | ------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| `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"` | +| `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"` | +| `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 copied to [.github/linters/super-linter-templates-phpcs.xml](.github/linters/super-linter-templates-phpcs.xml) will be used. | +| `stop-on-manual-fix` | If true, the execution will stop when manual fixes are necessary. | Boolean | `false` | ### Outputs diff --git a/action.yml b/action.yml index 3cc2134..1a5175b 100644 --- a/action.yml +++ b/action.yml @@ -45,6 +45,10 @@ inputs: required: false type: string default: "" # if left empty, .github/linters/phpcs.xml or https://github.com/super-linter/super-linter/blob/main/TEMPLATES/phpcs.xml will be used + stop-on-manual-fix: + description: "If true, the execution will stop when manual fixes are necessary." + type: boolean + default: false outputs: branch-name: @@ -70,8 +74,20 @@ runs: php-version: "${{ inputs.php-version }}" tools: composer:v2 - # todo consider using cache + # https://github.com/actions/cache + # 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. + # Caches that are not accessed within the last week will also be evicted. + - name: Cache composer dependencies + uses: actions/cache@v4 + id: vendor-cache + with: + # path to Checkout working directory is /home/runner/work/repo-name/repo-name , so just add /vendor/ + path: ${{ github.workspace }}/vendor/ + # Use composer.json for key, if composer.lock is not committed. + key: phpcs-fix-${{ runner.os }}-PHP${{ inputs.php-version }}-vendor-${{ hashFiles('**/composer.json') }} + - name: "Use the latest PHPCS version" + if: ${{ steps.vendor-cache.outputs.cache-hit != 'true' }} run: composer require --dev squizlabs/php_codesniffer --prefer-dist --no-progress shell: bash @@ -104,16 +120,32 @@ runs: if: env.HAS_ISSUES == 'true' run: | echo "phpcbf standard=${{ env.USE_STANDARD }}" - 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." + PHPCBF_EXIT_CODE=0 + MORE_ISSUES=false + vendor/bin/phpcbf --extensions="${{ inputs.extensions }}" . --standard="${{ env.USE_STANDARD }}" --ignore="${{ inputs.ignore }}" || PHPCBF_EXIT_CODE=$? + if [ ${PHPCBF_EXIT_CODE} -ne 0 ]; then + echo "::notice title=phpcbf wasn't idle::Non-zero code exit of phpcbf = ${PHPCBF_EXIT_CODE} ... phpcbf wasn't idle" + + vendor/bin/phpcs --extensions="${{ inputs.extensions }}" . --standard="${{ env.USE_STANDARD }}" --ignore="${{ inputs.ignore }}" || MORE_ISSUES=true + # because of `||`, the previous command always exit 0 + if [ "${MORE_ISSUES}" = "true" ]; then + echo "::warning title=Some phpcs issues remained.::Some phpcs issues remained. Manual fix necessary." + fi + fi # restore composer.json before staging the changes git checkout composer.json # Check for fixable changes if [ -z "$(git status --porcelain)" ]; then - echo "::warning title=No fixable errors were found by phpcbf::Manual fix necessary. Compare to the PHP Code Sniffer section above." - # Exiting gracefully - exit 0 + 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 + exit 1 + else + # Exiting gracefully + exit 0 + fi # The rest of the script must still be within the same step to really stop the execution fi @@ -127,7 +159,7 @@ runs: # name includes timestamp and short_hash BRANCH_NAME="phpcbf/fix-$(date +'%y%m%d%H%M%S')-$(git rev-parse --short HEAD)" git checkout -b "$BRANCH_NAME" - echo "::warning title=New branch created::Consider pull request for a new branch: $BRANCH_NAME (or delete it)" + echo "::warning title=New branch created::New branch created: Consider pull request for a new branch: $BRANCH_NAME (or delete it)" NOTICE_MESSAGE="A PHPCBF commit was successfully added to the new branch: $BRANCH_NAME" fi @@ -142,4 +174,9 @@ runs: # Set branch name as output echo "branch-name=$BRANCH_NAME" >> $GITHUB_OUTPUT + + ## 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