pre-commit.yml 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. ---
  2. name: pre-commit
  3. on:
  4. pull_request:
  5. push:
  6. jobs:
  7. pre-commit:
  8. runs-on: ubuntu-latest
  9. env:
  10. RAW_LOG: pre-commit.log
  11. CS_XML: pre-commit.xml
  12. steps:
  13. - name: Install required tools
  14. run: sudo apt-get update && sudo apt-get install cppcheck
  15. if: false
  16. # Checkout git sources to analyze
  17. - uses: actions/checkout@v4
  18. # The next uses the git API because there is no clone yet.
  19. # This is faster for a big repo.
  20. - name: Get all changed php files (if PR)
  21. id: changed-php
  22. if: env.gh_event == 'pull_request'
  23. env:
  24. GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
  25. run: ./.github/scripts/get_changed_php.sh
  26. # Action setup-python needs a requirements.txt or pyproject.toml
  27. # This ensures one of them exists.
  28. - name: Create requirements.txt if no requirements.txt or pyproject.toml
  29. run: |-
  30. [ -r requirements.txt ] || [ -r pyproject.toml ] || touch requirements.txt
  31. # Install python and pre-commit tool
  32. - uses: actions/setup-python@v5
  33. with:
  34. cache: pip
  35. python-version: "3.11"
  36. - run: python -m pip install pre-commit
  37. # Restore previous cache of precommit
  38. - uses: actions/cache/restore@v4
  39. with:
  40. path: ~/.cache/pre-commit/
  41. key: pre-commit-4|${{ env.pythonLocation }}|${{ hashFiles('.pre-commit-config.yaml') }}
  42. - name: Extract PHP version
  43. id: extract-php-version
  44. run: |
  45. PHP_VERSION=$(sed -n 's/.*\$arrayphpmaxversionwarning\s*=\s*array\s*(\s*\([0-9]\+\)\s*,\s*\([0-9]\+\).*/\1.\2/p' htdocs/install/check.php)
  46. echo "PHP_VERSION=$PHP_VERSION" >> $GITHUB_ENV
  47. - name: Setup PHPCS
  48. uses: shivammathur/setup-php@v2
  49. # Install proper php version, and also install phpcs which may be needed
  50. with:
  51. php-version: ${{ env.PHP_VERSION }} # Version from check.php
  52. coverage: none # disable xdebug, pcov
  53. tools: phpcs
  54. # Run all the precommit tools (defined into pre-commit-config.yaml).
  55. # We can force exclusion of some of them here.
  56. - name: Run pre-commit hooks
  57. env:
  58. # SKIP is used by pre-commit to not execute certain hooks
  59. SKIP: no-commit-to-branch,php-cs,php-cbf,trailing-whitespace,end-of-file-fixer,check-json,check-executables-have-shebangs,check-shebang-scripts-are-executable,beautysh,yamllint,shellcheck
  60. run: |
  61. set -o pipefail
  62. pre-commit gc
  63. pre-commit run --show-diff-on-failure --color=always --all-files | tee ${RAW_LOG}
  64. # The next uses git, which is slow for a bit repo.
  65. # - name: Get all changed php files (if PR)
  66. # id: changed-php
  67. # uses: tj-actions/changed-files@v42
  68. # if: github.event_name == 'pull_request'
  69. # with:
  70. # files: |
  71. # **.php
  72. - name: Run some pre-commit hooks on selected changed files only
  73. if: steps.changed-php.outputs.any_changed == 'true'
  74. env:
  75. ALL_CHANGED_FILES: ${{ steps.changed-php.outputs.all_changed_files }}
  76. run: |
  77. set -o pipefail
  78. pre-commit run php-cs --files ${ALL_CHANGED_FILES} | tee -a ${RAW_LOG}
  79. - name: Run some pre-commit hooks on all files on push to "main" branches
  80. if: |
  81. github.event_name == 'push'
  82. && (
  83. github.event.ref == 'refs/heads/develop'
  84. || endsWith(github.event.ref, '.0')
  85. )
  86. run: |
  87. set -o pipefail
  88. ln -sf ~/.cache .cache # Absolute path in .pre-commit-config.yaml
  89. pre-commit run --hook-stage manual -a php-cs-with-cache | tee -a ${RAW_LOG}
  90. ls -l ~/.cache/pre-commit/
  91. - name: Convert Raw Log to Annotations
  92. uses: mdeweerd/logToCheckStyle@v2024.2.9
  93. if: ${{ failure() }}
  94. with:
  95. in: ${{ env.RAW_LOG }}
  96. # Save the precommit cache
  97. - uses: actions/cache/save@v4
  98. if: ${{ ! cancelled() }}
  99. with:
  100. path: ~/.cache/pre-commit/
  101. key: pre-commit-4|${{ env.pythonLocation }}|${{ hashFiles('.pre-commit-config.yaml') }}
  102. # Upload result log files of precommit into the Artifact shared store
  103. - name: Provide log as artifact
  104. uses: actions/upload-artifact@v4
  105. if: ${{ ! cancelled() }}
  106. with:
  107. name: precommit-logs
  108. path: |
  109. ${{ env.RAW_LOG }}
  110. ${{ env.CS_XML }}
  111. retention-days: 2