-
Notifications
You must be signed in to change notification settings - Fork 0
104 lines (93 loc) · 7.07 KB
/
Copy pathphpcs.yml
File metadata and controls
104 lines (93 loc) · 7.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
name: 'PHPCS'
on:
workflow_call:
secrets:
access-token:
description: 'GitHub Access Token'
required: true
inputs:
ref:
description: 'Git Commit Ref (branch, tag, or hash)'
required: true
type: string
php_version:
description: 'PHP Version'
required: false
type: string
default: '7.4'
change_permissions:
description: 'Whether to change file permissions to root'
required: false
type: boolean
default: true
jobs:
phpcs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
ref: ${{ inputs.ref }}
fetch-depth: 0
# ------------------------------------------------------------------------------
# Get changed files — runs right after checkout so we can skip remaining
# steps entirely when no PHP files changed
# ------------------------------------------------------------------------------
- name: Get list of changed files
id: files
run: |
CHANGED_FILES=$(git diff --name-only --diff-filter=AM ${{ github.event.pull_request.base.sha }} ${{ github.event.pull_request.head.sha }} -- . ':!tests' | grep '\.php$' || true)
{
echo 'CHANGED_FILES<<EOF'
echo "$CHANGED_FILES"
echo EOF
} >> $GITHUB_ENV
if [ -n "$CHANGED_FILES" ]; then
echo "has_php_files=true" >> $GITHUB_OUTPUT
else
echo "No PHP files changed, skipping phpcs"
echo "has_php_files=false" >> $GITHUB_OUTPUT
fi
- uses: shivammathur/setup-php@v2
if: steps.files.outputs.has_php_files == 'true'
with:
php-version: ${{ inputs.php_version }}
- uses: ramsey/composer-install@v3
if: steps.files.outputs.has_php_files == 'true'
with:
composer-options: "--ignore-platform-reqs"
- name: "Give permissions"
if: ${{ inputs.change_permissions }}
run: |
sudo chown -R root:root $GITHUB_WORKSPACE
# ------------------------------------------------------------------------------
# PHPCS
# ------------------------------------------------------------------------------
- uses: reviewdog/action-setup@v1
if: steps.files.outputs.has_php_files == 'true'
with:
reviewdog_version: latest # Optional. [latest,nightly,v.X.Y.Z]
- name: Run reviewdog
if: steps.files.outputs.has_php_files == 'true'
env:
REVIEWDOG_GITHUB_API_TOKEN: ${{ secrets.access-token }}
run: |
mapfile -t FILES <<< "${{ env.CHANGED_FILES }}"
# phpcs's exit code covers the whole file (incl. pre-existing issues on
# untouched lines), so it must not gate the job. set +e stops bash -e
# from killing the step on that non-zero exit.
set +e
CHECKSTYLE_REPORT=$(vendor/bin/phpcs --report=checkstyle -q "${FILES[@]}")
PHPCS_EXIT_CODE=$?
set -e
# No report + non-zero exit means phpcs itself failed to run.
if [ -z "$CHECKSTYLE_REPORT" ] && [ "$PHPCS_EXIT_CODE" -ne 0 ]; then
echo "phpcs failed to run (exit code $PHPCS_EXIT_CODE)"
exit "$PHPCS_EXIT_CODE"
fi
# reviewdog gates the job: -filter-mode=added + -fail-level=any make it
# exit 1 only for violations on lines this PR changed.
REVIEWDOG_EXIT_CODE=0
echo "$CHECKSTYLE_REPORT" \
| reviewdog -f=checkstyle -name="phpcs" -filter-mode="added" -fail-level=any -reporter=github-pr-review \
|| REVIEWDOG_EXIT_CODE=$?
exit "$REVIEWDOG_EXIT_CODE"