Skip to content

Commit 76fcd70

Browse files
authored
Fix PHPcs baseline test failure when there are no files to check (#115)
* fix: baseline coding standard fail when there are no files * feat: also scan graphql/css/less/js files with phpcs
1 parent dcd92b1 commit 76fcd70

File tree

1 file changed

+69
-38
lines changed

1 file changed

+69
-38
lines changed

coding-standard-baseline/action.yml

Lines changed: 69 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -48,74 +48,105 @@ inputs:
4848
runs:
4949
using: composite
5050
steps:
51-
- name: Checkout Project
52-
uses: actions/checkout@v3
51+
- name: Checkout head
52+
uses: actions/checkout@v4
5353
with:
54-
fetch-depth: 0
54+
ref: ${{github.event.pull_request.head.ref}}
55+
repository: ${{github.event.pull_request.head.repo.full_name}}
5556

56-
- name: Set PHP Version
57+
- name: Get all changed files
58+
uses: tj-actions/changed-files@v39
59+
id: changed-files-phpcs
60+
with:
61+
files_yaml: |
62+
app:
63+
- '**/*.php'
64+
- '**/*.phtml'
65+
- '**/*.graphqls'
66+
- '**/*.less'
67+
- '**/*.css'
68+
- '**/*.html'
69+
- '**/*.xml'
70+
- '**/*.js'
71+
72+
- name: Inform if no files have changed
73+
if: steps.changed-files-phpcs.outputs.app_any_changed == 'false'
74+
shell: bash
75+
run: |
76+
echo "No files relevant to PHPCS have changed. Skipping PHPCS run."
77+
echo "List all the files that have changed: ${{ steps.changed-files-phpcs.outputs.app_all_changed_files }}"
78+
79+
- name: Inform if relevant files have changed
80+
if: steps.changed-files-phpcs.outputs.app_any_changed == 'true'
81+
shell: bash
82+
run: |
83+
echo "One or more files relevant to PHPCS have changed."
84+
echo "List all the files that have changed: ${{ steps.changed-files-phpcs.outputs.app_all_changed_files }}"
85+
86+
- name: Setup PHP
87+
if: steps.changed-files-phpcs.outputs.app_any_changed == 'true'
5788
uses: shivammathur/setup-php@v2
5889
with:
5990
php-version: ${{ inputs.php_version }}
6091
tools: composer:v${{ inputs.composer_version }}
6192
coverage: none
6293

6394
- name: Install Coding Standard && Codesniffer baseline
95+
if: steps.changed-files-phpcs.outputs.app_any_changed == 'true'
6496
shell: bash
6597
run: |
6698
git config --global advice.detachedHead false
6799
composer require "magento/magento-coding-standard: ${{ github.event.inputs.version || '*' }}" -W
68100
composer config --no-plugins allow-plugins.digitalrevolution/php-codesniffer-baseline true
69101
composer require --dev "digitalrevolution/php-codesniffer-baseline: ${{ github.event.inputs.baseline_version || '*' }}"
70102
71-
- name: Register Coding Standard
103+
- name: Register coding standards
104+
if: steps.changed-files-phpcs.outputs.app_any_changed == 'true'
72105
shell: bash
73-
run: vendor/bin/phpcs --config-set installed_paths ${{ github.workspace }}/vendor/magento/magento-coding-standard,${{ github.workspace }}/vendor/phpcompatibility/php-compatibility
106+
run: |
107+
${{ github.workspace }}/vendor/bin/phpcs --config-set installed_paths \
108+
${{ github.workspace }}/vendor/magento/magento-coding-standard,${{ github.workspace }}/vendor/phpcompatibility/php-compatibility
74109
75-
- name: Get all changed files
76-
uses: tj-actions/changed-files@v37
110+
- name: Checkout base to generate baseline
111+
if: steps.changed-files-phpcs.outputs.app_any_changed == 'true'
112+
uses: actions/checkout@v4
77113
with:
78-
write_output_files: true
79-
output_dir: ${{ github.workspace }}
80-
separator: " "
114+
ref: ${{github.event.pull_request.base.ref}}
115+
repository: ${{github.event.pull_request.base.repo.full_name}}
116+
path: base
81117

82-
- name: Verify the contents of the modified_files.txt file
118+
- name: Create phpcs.baseline.xml from base
83119
shell: bash
120+
working-directory: base
121+
if: steps.changed-files-phpcs.outputs.app_any_changed == 'true'
84122
run: |
85-
sed "s/ /\n/g" ${{ github.workspace }}/modified_files.txt > ${{ github.workspace }}/newline_file.txt
86-
grep -iE "\.php|\.phtml|\.html|\.xml" ${{ github.workspace }}/newline_file.txt > ${{ github.workspace }}/phpcs_files.txt || /bin/true
123+
php ${{ github.workspace }}/vendor/bin/phpcs --standard=Magento2 \
124+
$([ -n "${{ inputs.severity }}" ] && echo "--severity=${{ inputs.severity }}") \
125+
$([ -n "${{ inputs.warning_severity }}" ] && echo "--warning-severity=${{ inputs.warning_severity }}") \
126+
$([ -n "${{ inputs.error_severity }}" ] && echo "--error-severity=${{ inputs.error_severity }}") \
127+
--report=\\DR\\CodeSnifferBaseline\\Reports\\Baseline --report-file=phpcs.baseline.xml \
128+
${{ steps.changed-files-phpcs.outputs.app_all_changed_files }} || /bin/true
87129
88-
- name: Checkout - Before Merge
130+
- name: Copy baseline to head
131+
if: steps.changed-files-phpcs.outputs.app_any_changed == 'true'
89132
shell: bash
90-
if: test -s ${{ github.workspace }}/phpcs_files.txt
91133
run: |
92-
if ${{ github.event_name == 'pull_request' }}; then
93-
git checkout ${{ github.event.pull_request.base.ref }}
94-
else
95-
git checkout ${{ github.event.before }}
96-
fi
134+
cp ${{ github.workspace }}/base/phpcs.baseline.xml ${{ github.workspace }}/phpcs.baseline.xml
97135
98-
- name: Filter php files and execute phpcs - Before Merge
136+
# Since we ran phpcs in the base folder, the files in phpcs.baseline.xml contain the base folder in the path.
137+
# We need to remove /base/ so that the phpcs can locate the correct files.
138+
- name: Remove base dir from phpcs baseline
139+
if: steps.changed-files-phpcs.outputs.app_any_changed == 'true'
99140
shell: bash
100-
if: test -s ${{ github.workspace }}/phpcs_files.txt
101141
run: |
102-
php vendor/bin/phpcs --standard=Magento2 \
103-
$([ -n "${{ inputs.severity }}" ] && echo "--severity=${{ inputs.severity }}") \
104-
$([ -n "${{ inputs.warning_severity }}" ] && echo "--warning-severity=${{ inputs.warning_severity }}") \
105-
$([ -n "${{ inputs.error_severity }}" ] && echo "--error-severity=${{ inputs.error_severity }}") \
106-
--report=\\DR\\CodeSnifferBaseline\\Reports\\Baseline --report-file=${{ github.workspace }}/phpcs.baseline.xml --file-list=${{ github.workspace }}/phpcs_files.txt || /bin/true
142+
sed -i "s|/base/|/|" ${{ github.workspace }}/phpcs.baseline.xml
107143
108-
- name: Checkout after Merge and execute phpcs
144+
- name: Execute phpcs on head for changed files
109145
shell: bash
110-
if: test -s ${{ github.workspace }}/phpcs_files.txt
111-
run: |
112-
if ${{ github.event_name == 'pull_request' }}; then
113-
git checkout ${{ github.event.pull_request.head.ref }}
114-
else
115-
git checkout ${{ github.event.after }}
116-
fi
117-
php vendor/bin/phpcs --standard=Magento2 \
146+
if: steps.changed-files-phpcs.outputs.app_any_changed == 'true'
147+
run: |
148+
php ${{ github.workspace }}/vendor/bin/phpcs --standard=Magento2 \
118149
$([ -n "${{ inputs.severity }}" ] && echo "--severity=${{ inputs.severity }}") \
119150
$([ -n "${{ inputs.warning_severity }}" ] && echo "--warning-severity=${{ inputs.warning_severity }}") \
120151
$([ -n "${{ inputs.error_severity }}" ] && echo "--error-severity=${{ inputs.error_severity }}") \
121-
--file-list=${{ github.workspace }}/phpcs_files.txt
152+
${{ steps.changed-files-phpcs.outputs.app_all_changed_files }}

0 commit comments

Comments
 (0)