pre-commit.yml 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. LOG_TO_CS: .github/logToCs.py
  11. RAW_LOG: pre-commit.log
  12. CS_XML: pre-commit.xml
  13. steps:
  14. - name: Install required tools
  15. run: sudo apt-get update && sudo apt-get install cppcheck
  16. if: false
  17. # The next uses the git API because there is no clone yet.
  18. # This is faster for a big repo.
  19. - name: Get all changed php files (if PR)
  20. id: changed-php
  21. uses: tj-actions/changed-files@v42
  22. if: github.event_name == 'pull_request'
  23. with:
  24. files: |
  25. **.php
  26. # Checkout git sources to analyze
  27. - uses: actions/checkout@v4
  28. # Action setup-python needs a requirements.txt or pyproject.toml
  29. # This ensures one of them exists.
  30. - name: Create requirements.txt if no requirements.txt or pyproject.toml
  31. run: |-
  32. [ -r requirements.txt ] || [ -r pyproject.toml ] || touch requirements.txt
  33. # Install python and pre-commit tool
  34. - uses: actions/setup-python@v5
  35. with:
  36. cache: pip
  37. python-version: "3.11"
  38. - run: python -m pip install pre-commit regex
  39. # Restore previous cache of precommit
  40. - uses: actions/cache/restore@v4
  41. with:
  42. path: ~/.cache/pre-commit/
  43. key: pre-commit-4|${{ env.pythonLocation }}|${{ hashFiles('.pre-commit-config.yaml') }}
  44. # Run all the precommit tools (defined into pre-commit-config.yaml).
  45. # We can force exclusion of some of them here.
  46. - name: Run pre-commit hooks
  47. env:
  48. # SKIP is used by pre-commit to not execute certain hooks
  49. 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
  50. run: |
  51. set -o pipefail
  52. pre-commit gc
  53. pre-commit run --show-diff-on-failure --color=always --all-files | tee ${RAW_LOG}
  54. # The next uses git, which is slow for a bit repo.
  55. # - name: Get all changed php files (if PR)
  56. # id: changed-php
  57. # uses: tj-actions/changed-files@v42
  58. # if: github.event_name == 'pull_request'
  59. # with:
  60. # files: |
  61. # **.php
  62. - name: Setup PHPCS
  63. uses: shivammathur/setup-php@v2
  64. # Install when we're going to run phpcs
  65. if: |
  66. steps.changed-php.outputs.any_changed == 'true'
  67. ||
  68. (
  69. github.event_name == 'push'
  70. && (
  71. github.event.ref == 'refs/heads/develop'
  72. || endsWith(github.event.ref, '.0')
  73. )
  74. )
  75. with:
  76. php-version: 8.1
  77. coverage: none # disable xdebug, pcov
  78. tools: phpcs
  79. - name: Run some pre-commit hooks on selected changed files only
  80. if: steps.changed-php.outputs.any_changed == 'true'
  81. env:
  82. ALL_CHANGED_FILES: ${{ steps.changed-php.outputs.all_changed_files }}
  83. run: |
  84. set -o pipefail
  85. pre-commit run php-cs --files ${ALL_CHANGED_FILES} | tee -a ${RAW_LOG}
  86. - name: Run some pre-commit hooks on all files on push to "main" branches
  87. if: |
  88. github.event_name == 'push'
  89. && (
  90. github.event.ref == 'refs/heads/develop'
  91. || endsWith(github.event.ref, '.0')
  92. )
  93. run: |
  94. set -o pipefail
  95. ln -sf ~/.cache .cache # Absolute path in .pre-commit-config.yaml
  96. pre-commit run --hook-stage manual -a php-cs-with-cache | tee -a ${RAW_LOG}
  97. ls -l ~/.cache/pre-commit/
  98. # If error, we convert log in the checkstyle format
  99. - name: Convert Raw Log to CheckStyle format
  100. if: ${{ failure() }}
  101. run: |
  102. python ${LOG_TO_CS} ${RAW_LOG} ${CS_XML}
  103. # Annotate the git sources with the log messages
  104. - name: Annotate Source Code with Messages
  105. uses: staabm/annotate-pull-request-from-checkstyle-action@v1
  106. if: ${{ failure() }}
  107. with:
  108. files: ${{ env.CS_XML }}
  109. notices-as-warnings: true # optional
  110. prepend-filename: true # optional
  111. # Save the precommit cache
  112. - uses: actions/cache/save@v4
  113. if: ${{ ! cancelled() }}
  114. with:
  115. path: ~/.cache/pre-commit/
  116. key: pre-commit-4|${{ env.pythonLocation }}|${{ hashFiles('.pre-commit-config.yaml') }}
  117. # Upload result log files of precommit into the Artifact shared store
  118. - name: Provide log as artifact
  119. uses: actions/upload-artifact@v4
  120. if: ${{ ! cancelled() }}
  121. with:
  122. name: precommit-logs
  123. path: |
  124. ${{ env.RAW_LOG }}
  125. ${{ env.CS_XML }}
  126. retention-days: 2