Merge pull request #189 from Tjitse-E/fix/changed-files-for-phpcs-baseline

Fix changed files action for phpcs baseline
This commit is contained in:
Simon Sprankel
2023-11-16 16:49:55 +01:00
committed by GitHub
2 changed files with 50 additions and 43 deletions
+14 -4
View File
@@ -1,15 +1,23 @@
# Magento 2 Coding Standard Action # Magento 2 Coding Standard Action
A Github Action that runs the Magento Coding Standard. This Github Action automates the enforcement of Magento Coding Standards. It ensures code consistency and quality by checking code against Magento's specific coding guidelines.
## Inputs ## Inputs
See the [action.yml](./action.yml) For detailed descriptions of each input, refer to [action.yml](./action.yml).
## Usage ## Why a Baseline?
Running PHP CodeSniffer (PHPCS) with a baseline is crucial for managing legacy code. It allows you to set a "starting point" for code quality, ignoring existing issues while ensuring no new issues are introduced. This approach is especially useful for large codebases where addressing all existing issues at once is not feasible. The baseline serves as a record of known issues, enabling teams to focus on maintaining and gradually improving code quality in new or modified code.
## Usage Example
The following example demonstrates how to set up the action in your workflow:
Check how this action is used in mage-os [here](https://github.com/mage-os/mageos-magento2/blob/2.4-develop/.github/workflows/coding-standard-baseline.yml).
```yml ```yml
name: Coding Standard baseline name: Coding Standard Baseline
on: on:
push: push:
@@ -25,6 +33,8 @@ jobs:
steps: steps:
- uses: mage-os/github-actions/coding-standard-baseline@main - uses: mage-os/github-actions/coding-standard-baseline@main
with: with:
head_repo: "mage-os/mageos-magento2"
head_ref: "main"
php_version: "8.2" php_version: "8.2"
composer_version: "2" composer_version: "2"
version: "*" version: "*"
+36 -39
View File
@@ -45,70 +45,67 @@ inputs:
default: "*" default: "*"
description: "The version of phpcs baseline to use (default: latest)." description: "The version of phpcs baseline to use (default: latest)."
head_repo:
type: string
required: true
description: "The repository full name of the head branch. E.g.: mage-os/mageos-magento2"
head_ref:
type: string
required: true
description: "The branch name of the head branch. E.g.: main"
runs: runs:
using: composite using: composite
steps: steps:
- name: Checkout head - name: Checkout head
uses: actions/checkout@v4 uses: actions/checkout@v4
with: with:
ref: ${{github.event.pull_request.head.ref}} ref: ${{ inputs.head_ref }}
repository: ${{github.event.pull_request.head.repo.full_name}} repository: ${{ inputs.head_repo }}
- name: Get all changed files - uses: dorny/paths-filter@v2
uses: tj-actions/changed-files@v39 name: Filter changed files
id: changed-files-phpcs id: filter
with: with:
files_yaml: | list-files: shell
app: filters: |
- '**/*.php' phpcs:
- '**/*.phtml' - added|modified: '**/**.{php,phtml,graphqls,less,css,html,xml,js}'
- '**/*.graphqls'
- '**/*.less'
- '**/*.css'
- '**/*.html'
- '**/*.xml'
- '**/*.js'
- name: Inform if no files have changed - name: Check changed files for PHPcs
if: steps.changed-files-phpcs.outputs.app_any_changed == 'false' if: steps.filter.outputs.phpcs == 'true'
shell: bash
run: |
echo "No files relevant to PHPCS have changed. Skipping PHPCS run."
echo "List all the files that have changed: ${{ steps.changed-files-phpcs.outputs.app_all_changed_files }}"
- name: Inform if relevant files have changed
if: steps.changed-files-phpcs.outputs.app_any_changed == 'true'
shell: bash shell: bash
run: | run: |
echo "One or more files relevant to PHPCS have changed." echo "One or more files relevant to PHPCS have changed."
echo "List all the files that have changed: ${{ steps.changed-files-phpcs.outputs.app_all_changed_files }}" echo "List all the files that have changed: ${{ steps.filter.outputs.phpcs_files }}"
- name: Setup PHP - name: Setup PHP
if: steps.changed-files-phpcs.outputs.app_any_changed == 'true' if: steps.filter.outputs.phpcs == 'true'
uses: shivammathur/setup-php@v2 uses: shivammathur/setup-php@v2
with: with:
php-version: ${{ inputs.php_version }} php-version: ${{ inputs.php_version }}
tools: composer:v${{ inputs.composer_version }} tools: composer:v${{ inputs.composer_version }}
coverage: none coverage: none
- name: Create composer project - name: Install coding standards
if: steps.changed-files-phpcs.outputs.app_any_changed == 'true' if: steps.filter.outputs.phpcs == 'true'
shell: bash shell: bash
run: | run: |
composer create-project --no-plugins --no-dev \ composer create-project --no-plugins --no-dev \
magento/magento-coding-standard \ magento/magento-coding-standard \
magento-coding-standard "${{ github.event.inputs.version || '*' }}" magento-coding-standard "${{ github.event.inputs.version || '*' }}"
- name: Register coding standards - name: Install phpcs baseline
if: steps.changed-files-phpcs.outputs.app_any_changed == 'true' if: steps.filter.outputs.phpcs == 'true'
working-directory: magento-coding-standard working-directory: magento-coding-standard
shell: bash shell: bash
run: | run: |
composer config --no-plugins allow-plugins.digitalrevolution/php-codesniffer-baseline true composer config --no-plugins allow-plugins.digitalrevolution/php-codesniffer-baseline true
composer require --dev "digitalrevolution/php-codesniffer-baseline: ${{ github.event.inputs.baseline_version || '*' }}" composer require --dev "digitalrevolution/php-codesniffer-baseline: ${{ github.event.inputs.baseline_version || '*' }}"
- name: Checkout base to generate baseline - name: Checkout base
if: steps.changed-files-phpcs.outputs.app_any_changed == 'true' if: steps.filter.outputs.phpcs == 'true'
uses: actions/checkout@v4 uses: actions/checkout@v4
with: with:
ref: ${{github.event.pull_request.base.ref}} ref: ${{github.event.pull_request.base.ref}}
@@ -118,17 +115,17 @@ runs:
- name: Create phpcs.baseline.xml from base - name: Create phpcs.baseline.xml from base
shell: bash shell: bash
working-directory: base working-directory: base
if: steps.changed-files-phpcs.outputs.app_any_changed == 'true' if: steps.filter.outputs.phpcs == 'true'
run: | run: |
php ${{ github.workspace }}/magento-coding-standard/vendor/bin/phpcs --standard=Magento2 \ php ${{ github.workspace }}/magento-coding-standard/vendor/bin/phpcs --standard=Magento2 \
$([ -n "${{ inputs.severity }}" ] && echo "--severity=${{ inputs.severity }}") \ $([ -n "${{ inputs.severity }}" ] && echo "--severity=${{ inputs.severity }}") \
$([ -n "${{ inputs.warning_severity }}" ] && echo "--warning-severity=${{ inputs.warning_severity }}") \ $([ -n "${{ inputs.warning_severity }}" ] && echo "--warning-severity=${{ inputs.warning_severity }}") \
$([ -n "${{ inputs.error_severity }}" ] && echo "--error-severity=${{ inputs.error_severity }}") \ $([ -n "${{ inputs.error_severity }}" ] && echo "--error-severity=${{ inputs.error_severity }}") \
--report=\\DR\\CodeSnifferBaseline\\Reports\\Baseline --report-file=phpcs.baseline.xml \ --report=\\DR\\CodeSnifferBaseline\\Reports\\Baseline --report-file=phpcs.baseline.xml \
${{ steps.changed-files-phpcs.outputs.app_all_changed_files }} || /bin/true ${{ steps.filter.outputs.phpcs_files }} || /bin/true
- name: Copy baseline to head - name: Copy baseline to head
if: steps.changed-files-phpcs.outputs.app_any_changed == 'true' if: steps.filter.outputs.phpcs == 'true'
shell: bash shell: bash
run: | run: |
cp ${{ github.workspace }}/base/phpcs.baseline.xml ${{ github.workspace }}/magento-coding-standard/phpcs.baseline.xml cp ${{ github.workspace }}/base/phpcs.baseline.xml ${{ github.workspace }}/magento-coding-standard/phpcs.baseline.xml
@@ -136,17 +133,17 @@ runs:
# Since we ran phpcs in the base folder, the files in phpcs.baseline.xml contain the base folder in the path. # Since we ran phpcs in the base folder, the files in phpcs.baseline.xml contain the base folder in the path.
# We need to remove /base/ so that the phpcs can locate the correct files. # We need to remove /base/ so that the phpcs can locate the correct files.
- name: Remove base dir from phpcs baseline - name: Remove base dir from phpcs baseline
if: steps.changed-files-phpcs.outputs.app_any_changed == 'true' if: steps.filter.outputs.phpcs == 'true'
shell: bash shell: bash
run: | run: |
sed -i "s|/base/|/|" ${{ github.workspace }}/magento-coding-standard/phpcs.baseline.xml sed -i "s|/base/|/|" ${{ github.workspace }}/magento-coding-standard/phpcs.baseline.xml
- name: Execute phpcs on head for changed files - name: Execute phpcs on head for changed files
shell: bash shell: bash
if: steps.changed-files-phpcs.outputs.app_any_changed == 'true' if: steps.filter.outputs.phpcs == 'true'
run: | run: |
php ${{ github.workspace }}/magento-coding-standard/vendor/bin/phpcs --standard=Magento2 \ php ${{ github.workspace }}/magento-coding-standard/vendor/bin/phpcs --standard=Magento2 \
$([ -n "${{ inputs.severity }}" ] && echo "--severity=${{ inputs.severity }}") \ $([ -n "${{ inputs.severity }}" ] && echo "--severity=${{ inputs.severity }}") \
$([ -n "${{ inputs.warning_severity }}" ] && echo "--warning-severity=${{ inputs.warning_severity }}") \ $([ -n "${{ inputs.warning_severity }}" ] && echo "--warning-severity=${{ inputs.warning_severity }}") \
$([ -n "${{ inputs.error_severity }}" ] && echo "--error-severity=${{ inputs.error_severity }}") \ $([ -n "${{ inputs.error_severity }}" ] && echo "--error-severity=${{ inputs.error_severity }}") \
${{ steps.changed-files-phpcs.outputs.app_all_changed_files }} ${{ steps.filter.outputs.phpcs_files }}