-
Notifications
You must be signed in to change notification settings - Fork 26
104 lines (94 loc) · 4.04 KB
/
Copy pathpr-code-sniff.yml
File metadata and controls
104 lines (94 loc) · 4.04 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: Run code sniff on PR
on: pull_request
jobs:
test:
name: Code sniff (PHP 7.4, WP Latest)
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
with:
fetch-depth: 100
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: 7.4
tools: composer, cs2pr
- name: Tool versions
run: |
php --version
composer --version
- name: Get cached composer directories
uses: actions/cache@v3
with:
path: ./vendor
key: ${{ runner.os }}-${{ hashFiles('./composer.lock') }}
- name: Setup and install composer
run: composer install
- name: Get changed PHP files
id: changed
run: |
git fetch origin ${{ github.event.pull_request.base.ref }} --depth=1
git diff --name-only origin/${{ github.event.pull_request.base.ref }}...HEAD -- '*.php' > changed-php.txt || true
if [ -s changed-php.txt ]; then
echo "count=$(wc -l < changed-php.txt)" >> $GITHUB_OUTPUT
echo "Files to sniff:"
cat changed-php.txt
else
echo "count=0" >> $GITHUB_OUTPUT
echo "No PHP files changed in this PR"
fi
- name: Run code sniff
if: steps.changed.outputs.count != '0'
continue-on-error: true
run: |
./vendor/bin/phpcs --report=checkstyle --report-file=./phpcs-report.xml -s -p --file-list=changed-php.txt || true
- name: Run code sniff (no PHP changes)
if: steps.changed.outputs.count == '0'
run: echo "Skipping PHPCS - no PHP files changed."
- name: Show PHPCS results in PR (annotations)
if: steps.changed.outputs.count != '0' && hashFiles('./phpcs-report.xml') != ''
run: cs2pr ./phpcs-report.xml
- name: Generate PHPCS comment body
if: steps.changed.outputs.count != '0' && hashFiles('./phpcs-report.xml') != ''
id: phpcs_comment
run: |
node -e "
const fs = require('fs');
const xml = fs.readFileSync('phpcs-report.xml', 'utf8');
const fileBlocks = xml.match(/<file[^>]*name=\"([^\"]+)\"[^>]*>([\\s\\S]*?)<\\/file>/g) || [];
let md = '## PHPCS results\\n\\n';
let total = 0;
const rows = [];
fileBlocks.forEach(block => {
const nameMatch = block.match(/name=\"([^\"]+)\"/);
const file = nameMatch ? nameMatch[1].replace(/^.*\//, '') : '';
const errors = block.match(/<error[^>]*>/g) || [];
errors.forEach(tag => {
const line = (tag.match(/line=\"([^\"]+)\"/) || [])[1] || '';
const sev = (tag.match(/severity=\"([^\"]+)\"/) || [])[1] || '';
let msg = (tag.match(/message=\"([^\"]*)\"/) || [])[1] || '';
msg = msg.replace(/\|/g, '\\\\|').replace(/\n/g, ' ');
const source = (tag.match(/source=\"([^\"]+)\"/) || [])[1] || '';
rows.push({ file, line, sev, msg, source });
total++;
});
});
if (total === 0) {
md += '✅ No PHPCS issues found.';
} else {
md += '| File | Line | Severity | Message |\\n|------|------|----------|---------|\\n';
rows.slice(0, 100).forEach(r => { md += '| ' + r.file + ' | ' + r.line + ' | ' + r.sev + ' | ' + r.msg.substring(0, 80) + (r.msg.length > 80 ? '…' : '') + ' |\\n'; });
if (rows.length > 100) md += '\\n*… and ' + (rows.length - 100) + ' more (see annotations).*';
}
fs.writeFileSync('phpcs-comment.md', md);
"
- name: Comment PHPCS results on PR
if: steps.changed.outputs.count != '0' && hashFiles('./phpcs-report.xml') != ''
uses: peter-evans/create-or-update-comment@v4
with:
token: ${{ secrets.GITHUB_TOKEN }}
comment-id: phpcs-report
issue-number: ${{ github.event.pull_request.number }}
body-path: phpcs-comment.md
edit-mode: replace