Skip to content

Commit

Permalink
Qual: ci: Run pre-commit/php-cs with cache
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
mdeweerd committed Feb 9, 2024
1 parent c19ad24 commit 214604e
Show file tree
Hide file tree
Showing 3 changed files with 258 additions and 238 deletions.
45 changes: 23 additions & 22 deletions .github/workflows/pre-commit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
if: github.event_name == 'pull_request'
with:
files: |
**.php
**.php
# Checkout git sources to analyze
- uses: actions/checkout@v4
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
36 changes: 28 additions & 8 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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 )$
Expand Down Expand Up @@ -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]
Expand All @@ -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
Expand All @@ -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
Expand All @@ -109,7 +129,7 @@ repos:
# dev/tools/codespell/addCodespellIgnores.sh
args:
- -D
- '-'
- "-"
- -D
- dev/tools/codespell/codespell-dict.txt
- -I
Expand All @@ -129,7 +149,7 @@ repos:
files: ^htdocs/langs/en_US/.*$
args:
- -D
- '-'
- "-"
- -D
- dev/tools/codespell/codespell-dict.txt
- -L
Expand All @@ -148,4 +168,4 @@ repos:
rev: v0.9.0.6
hooks:
- id: shellcheck
args: [-W, '100']
args: [-W, "100"]
Loading

0 comments on commit 214604e

Please sign in to comment.