From 214604e229f22f49942c624de36d908238fea182 Mon Sep 17 00:00:00 2001 From: MDW Date: Thu, 8 Feb 2024 18:38:19 +0100 Subject: [PATCH] Qual: ci: Run pre-commit/php-cs with cache This adds a hook to .pre-commit-config.yaml and updates the workflow to run php-cs with cache when it is run for all files. When running on changed files only, the cache is not useful. The php-codesniffer ruleset.xml was cleaned up (duplicates removal/formatted) --- .github/workflows/pre-commit.yml | 45 ++-- .pre-commit-config.yaml | 36 ++- dev/setup/codesniffer/ruleset.xml | 415 +++++++++++++++--------------- 3 files changed, 258 insertions(+), 238 deletions(-) diff --git a/.github/workflows/pre-commit.yml b/.github/workflows/pre-commit.yml index 95016b925b776..9127b43c0056b 100644 --- a/.github/workflows/pre-commit.yml +++ b/.github/workflows/pre-commit.yml @@ -24,7 +24,7 @@ jobs: if: github.event_name == 'pull_request' with: files: | - **.php + **.php # Checkout git sources to analyze - uses: actions/checkout@v4 @@ -37,7 +37,7 @@ jobs: - uses: actions/setup-python@v5 with: cache: pip - python-version: '3.11' + python-version: "3.11" - run: python -m pip install pre-commit regex # Restore previous cache of precommit - uses: actions/cache/restore@v4 @@ -68,18 +68,18 @@ jobs: uses: shivammathur/setup-php@v2 # Install when we're going to run phpcs if: | - steps.changed-php.outputs.any_changed == 'true' - || - ( - github.event_name == 'push' - && ( - github.event.ref == 'refs/heads/develop' - || endsWith(github.event.ref, '.0') - ) - ) + steps.changed-php.outputs.any_changed == 'true' + || + ( + github.event_name == 'push' + && ( + github.event.ref == 'refs/heads/develop' + || endsWith(github.event.ref, '.0') + ) + ) with: php-version: 8.1 - coverage: none # disable xdebug, pcov + coverage: none # disable xdebug, pcov tools: phpcs - name: Run some pre-commit hooks on selected changed files only @@ -92,14 +92,16 @@ jobs: - name: Run some pre-commit hooks on all files on push to "main" branches if: | - github.event_name == 'push' - && ( - github.event.ref == 'refs/heads/develop' - || endsWith(github.event.ref, '.0') - ) + github.event_name == 'push' + && ( + github.event.ref == 'refs/heads/develop' + || endsWith(github.event.ref, '.0') + ) run: | set -o pipefail - pre-commit run -a php-cs | tee -a ${RAW_LOG} + ln -sf ~/.cache .cache # Absolute path in .pre-commit-config.yaml + pre-commit run --hook-stage manual -a php-cs-with-cache | tee -a ${RAW_LOG} + ls -l ~/.cache/pre-commit/ # If error, we convert log in the checkstyle format - name: Convert Raw Log to CheckStyle format @@ -112,15 +114,14 @@ jobs: if: ${{ failure() }} with: files: ${{ env.CS_XML }} - notices-as-warnings: true # optional - prepend-filename: true # optional + notices-as-warnings: true # optional + prepend-filename: true # optional # Save the precommit cache - uses: actions/cache/save@v4 if: ${{ ! cancelled() }} with: path: ~/.cache/pre-commit/ - key: pre-commit-4|${{ env.pythonLocation }}|${{ hashFiles('.pre-commit-config.yaml') - }} + key: pre-commit-4|${{ env.pythonLocation }}|${{ hashFiles('.pre-commit-config.yaml') }} # Upload result log files of precommit into the Artifact shared store - name: Provide log as artifact uses: actions/upload-artifact@v4 diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index e88b7d2eeca5d..47ffeb5e28f8f 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -18,7 +18,8 @@ repos: - id: check-merge-conflict - id: check-executables-have-shebangs - id: check-shebang-scripts-are-executable - exclude: (?x)^( dev/tools/dolibarr-postgres2mysql.php |test/other/test_serialize.php + exclude: + (?x)^( dev/tools/dolibarr-postgres2mysql.php |test/other/test_serialize.php |test/phpunit/textutf8.txt |test/phpunit/textiso.txt |htdocs/includes/.* |htdocs/modulebuilder/template/.* |build/debian/dolibarr.postrm |build/debian/dolibarr.postinst |build/debian/dolibarr.config )$ @@ -53,14 +54,32 @@ repos: # Check PHP syntax - repo: https://github.com/mdeweerd/pre-commit-php - rev: v1.6.3 + rev: v1.6.5 hooks: - id: php-cbf files: \.(php)$ args: [--standard=dev/setup/codesniffer/ruleset.xml] - id: php-cs files: \.(php)$ - args: [--standard=dev/setup/codesniffer/ruleset.xml, --report=emacs, --severity=5] + args: + [ + --standard=dev/setup/codesniffer/ruleset.xml, + --report=emacs, + --severity=5, + ] + - alias: php-cs-with-cache + id: php-cs + # Configuration for ci - run on all files with cache + stages: [manual] + args: + [ + --standard=dev/setup/codesniffer/ruleset.xml, + --report=emacs, + --severity=5, + --cache=.cache/pre-commit/dolibarr-php-cs.cache, + ., + ] + pass_filenames: false # Run on all files - id: php-lint - id: php-stan stages: [manual] @@ -72,7 +91,8 @@ repos: hooks: - id: prettier stages: [manual] - exclude: (?x)^( .*\.(phar |min\.css |lock) |htdocs/(includes|theme/common)/.* + exclude: + (?x)^( .*\.(phar |min\.css |lock) |htdocs/(includes|theme/common)/.* )$ exclude_types: - php @@ -96,7 +116,7 @@ repos: args: - --no-warnings - -d - - '{extends: relaxed, rules: {line-length: {max: 120}}}' + - "{extends: relaxed, rules: {line-length: {max: 120}}}" # Execute codespell to fix typo errors (setup of codespell into dev/tools/codespell/) - repo: https://github.com/codespell-project/codespell @@ -109,7 +129,7 @@ repos: # dev/tools/codespell/addCodespellIgnores.sh args: - -D - - '-' + - "-" - -D - dev/tools/codespell/codespell-dict.txt - -I @@ -129,7 +149,7 @@ repos: files: ^htdocs/langs/en_US/.*$ args: - -D - - '-' + - "-" - -D - dev/tools/codespell/codespell-dict.txt - -L @@ -148,4 +168,4 @@ repos: rev: v0.9.0.6 hooks: - id: shellcheck - args: [-W, '100'] + args: [-W, "100"] diff --git a/dev/setup/codesniffer/ruleset.xml b/dev/setup/codesniffer/ruleset.xml index c9d264b50924e..5b7dade0a981e 100644 --- a/dev/setup/codesniffer/ruleset.xml +++ b/dev/setup/codesniffer/ruleset.xml @@ -3,6 +3,7 @@ Dolibarr coding standard. + build/html build/aps @@ -17,8 +18,7 @@ */nltechno* source .git - htdocs/includes - htdocs/install/doctemplates/websites + .cache @@ -32,41 +32,41 @@ - - - - - - + + + + + + - - + + - + - + - + - - + + 0 - - + + 0 - - + + 0 - - + + 0 - - + + 0 - - + + 0 - + @@ -80,14 +80,14 @@ - - + + - - + + @@ -105,57 +105,57 @@ - + - - + - + - + - + 0 - - - - - - - - - - - - - + + + + + + + + + + + + + @@ -163,20 +163,20 @@ - - - 0 - + + + 0 + - + - - - - 0 - + + + + 0 + @@ -194,123 +194,122 @@ - - - - + + + + - - + + - + - + - - - - - - - - - - - 0 - + + + + + + + + + + + 0 + - - + + - - - + + + - - + + - - + + + + + + - - - - - - - 0 - - + + 0 + + - + - + - + - + - + - - 0 - + + 0 + - - 0 - - - 0 - - - 0 - - - 0 - + + 0 + + + 0 + + + 0 + + + 0 + - + - + - + - - 0 - - - 0 - + + 0 + + + 0 + - + - + - - 0 - - - 0 - - - 0 - - - 0 - - - 0 - - - 0 - + + 0 + + + 0 + + + 0 + + + 0 + + + 0 + + + 0 + - + - - 0 - + + 0 + - - - 0 - + + + 0 + - - 0 - + + 0 + - - - 0 - + + + 0 + - - - 0 - + + + 0 + - - - 0 - + + + 0 + @@ -362,14 +361,14 @@ 0 - - + + - 0 + 0 @@ -378,7 +377,7 @@ - 0 + 0 @@ -386,26 +385,26 @@ - 0 + 0 - 0 + 0 - 0 + 0 - + - 0 + 0 @@ -413,35 +412,35 @@ - 0 - + 0 + - + - - - - - - + + + + + + - - + +