--- name: pre-commit on: # workflow called by the parent workflow ci.yml workflow_call: inputs: gh_event: required: true type: string # can run job manually workflow_dispatch: concurrency: group: pre-commit-${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }} cancel-in-progress: true env: gh_event: ${{ inputs.gh_event || github.event_name }} jobs: pre-commit: runs-on: ubuntu-latest env: RAW_LOG: pre-commit.log CS_XML: pre-commit.xml steps: #- name: Install required tools # run: sudo apt-get update && sudo apt-get install cppcheck # if: false # Checkout git sources to analyze - uses: actions/checkout@v6 # Try to get the list of modified files into steps.changed-php.outputs.all_changed_files #- name: Get changed files # id: changed-files # uses: actions/github-script@v7 # with: # script: | # const base = context.payload.pull_request?.base?.sha; # const head = context.payload.pull_request?.head?.sha; # const response = await github.rest.repos.compareCommits({ # owner: context.repo.owner, # repo: context.repo.repo, # base, # head # }); # # const changedFiles = response.data.files.map(file => file.filename); # core.setOutput("all_changed_files", changedFiles.join(" ")); # Another method to get the list of changed files # It sets the variable steps.changed-php.outputs.all_changed_files for other steps - name: Get all changed php files (if PR) id: changed-php if: env.gh_event == 'pull_request' env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} run: ./.github/scripts/get_changed_php.sh #- name: Print changed files # run: echo "Changed files are ${{ steps.changed-files.outputs.all_changed_files }}" # Action setup-python needs a requirements.txt or pyproject.toml # This ensures one of them exists. - name: Create requirements.txt if no requirements.txt or pyproject.toml run: |- [ -r requirements.txt ] || [ -r pyproject.toml ] || touch requirements.txt # Install python and pre-commit tool - uses: actions/setup-python@v6 with: cache: pip python-version: "3.11" - run: python -m pip install pre-commit # Restore previous cache of precommit - uses: actions/cache/restore@v5 with: path: ~/.cache/pre-commit/ key: pre-commit-4|${{ env.pythonLocation }}|${{ hashFiles('.pre-commit-config.yaml') }} # Run all the precommit tools (defined in pre-commit-config.yaml). - name: Extract PHP version id: extract-php-version run: | # shellcheck disable=SC2016 PHP_VERSION=$(sed -n 's/.*\$arrayphpmaxversionwarning\s*=\s*array\s*(\s*\([0-9]\+\)\s*,\s*\([0-9]\+\).*/\1.\2/p' htdocs/install/check.php) echo "PHP_VERSION=$PHP_VERSION" >> "$GITHUB_ENV" - name: Setup PHPCS uses: shivammathur/setup-php@v2 # Install proper php version, and also install phpcs which may be needed if: | ! cancelled() && ( steps.changed-php.outputs.any_changed == 'true' || ( env.gh_event == 'push' && ( github.event.ref == 'refs/heads/develop' || endsWith(github.event.ref, '.0') ) ) ) with: php-version: ${{ env.PHP_VERSION }} # Version from check.php coverage: none # disable xdebug, pcov tools: phpcs # Install perltidy and perlcritic - name: Install perltidy and perlcritic run: sudo apt-get update && sudo apt-get install -y perltidy libperl-critic-perl # Run all the precommit tools (defined into pre-commit-config.yaml). # We can force exclusion of some of them here. - name: Run pre-commit hooks env: # SKIP is used by pre-commit to not execute certain hooks SKIP: no-commit-to-branch,php-cs,php-cbf,trailing-whitespace,end-of-file-fixer run: | set -o pipefail pre-commit gc pre-commit run --show-diff-on-failure --color=always --all-files | tee "${RAW_LOG}" # The next uses git, which is slow for a bit repo. # - name: Get all changed php files (if PR) # id: changed-php # uses: tj-actions/changed-files@v45 # if: env.gh_event == 'pull_request' # with: # files: | # **.php - name: Run some pre-commit hooks on selected changed files only if: "! cancelled() && steps.changed-php.outputs.any_changed == 'true'" env: ALL_CHANGED_FILES: ${{ steps.changed-php.outputs.all_changed_files }} run: | set -o pipefail # shellcheck disable=2086 pre-commit run php-cs --files ${ALL_CHANGED_FILES} | tee -a "${RAW_LOG}" - name: Run some pre-commit hooks on all files on push to "main" branches if: | env.gh_event == 'push' && ( github.event.ref == 'refs/heads/develop' || endsWith(github.event.ref, '.0') ) run: | set -o pipefail 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}" pre-commit run --hook-stage manual -a sqlfluff-lint | tee -a "${RAW_LOG}" ls -l ~/.cache/pre-commit/ - name: Convert Raw Log to Annotations uses: mdeweerd/logToCheckStyle@v2025.11.2 if: ${{ failure() }} with: in: ${{ env.RAW_LOG }} # Save the precommit cache - uses: actions/cache/save@v5 if: ${{ ! cancelled() }} with: path: ~/.cache/pre-commit/ 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@v6 if: ${{ ! cancelled() }} with: name: precommit-logs path: | ${{ env.RAW_LOG }} ${{ env.CS_XML }}