Hashar has uploaded a new change for review. ( https://gerrit.wikimedia.org/r/358554 )
Change subject: PHP CodeSniffer on CI should only lint HEAD ...................................................................... PHP CodeSniffer on CI should only lint HEAD The Jenkins job mediawiki-core-phpcs-trusty runs PHP CodeSniffer against every single files. That is one of the slowest jobs we have. Lets be smarter and only run against files changed in the patchset proposed. That dramatically speeds it up. phpcs supports a bootstrap file to finely tweak its state after all arguments and configuration have been processed. The bootstrap occurs just before the lint actually starts. Thus, we can shell out to git just like the slave script git-changed-in-head, grab the files, filter out files that do not match phpcs.xml extension: <arg name="extensions" value="php,php5,inc,sample"/> Pass --bootstrap in phpcs.xml Add a bootstrap script utils/bootstrap-ci.php. Early returns unless we are on CLI and JENKINS_URL env variable is set. Get list of files added/modified/changed in HEAD based on integration/jenkins.git script bin/git-changed-in-head for which I am the author and licensed under GPL2.0+ Trigger a full pass whenever phpcs.xml (rules definitions) is altered. For composer.json, only run a full pass when mediawiki/mediawiki-codesniffer version has changed. Filter out any files that do not match phpcs extensions and use what is left to override PHPCS built in list of files. Update example in README.md Bug: T158974 Change-Id: I2f492f889594135e37950fcb8e40da0c4d088430 --- M README.md A utils/bootstrap-ci.php 2 files changed, 75 insertions(+), 0 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/tools/codesniffer refs/changes/54/358554/1 diff --git a/README.md b/README.md index f737508..757a42c 100644 --- a/README.md +++ b/README.md @@ -32,6 +32,7 @@ <ruleset> <rule ref="vendor/mediawiki/mediawiki-codesniffer/MediaWiki"/> <file>.</file> + <arg name="bootstrap" value="vendor/mediawiki/mediawiki-codesniffer/utils/bootstrap-ci.php"/> <arg name="extensions" value="php,php5,inc"/> <arg name="encoding" value="utf8"/> <exclude-pattern>vendor</exclude-pattern> diff --git a/utils/bootstrap-ci.php b/utils/bootstrap-ci.php new file mode 100644 index 0000000..d540c58 --- /dev/null +++ b/utils/bootstrap-ci.php @@ -0,0 +1,74 @@ +<?php +/** + * Change PHP CodeSniffer to only lint files changed in HEAD. + * + * Copyright © 2017 Antoine Musso <has...@free.fr> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * http://www.gnu.org/copyleft/gpl.html + * + * @file + * @author Antoine Musso + */ + +# Only filter when running from cli and using Jenkins +if ( ! ( PHP_SAPI === 'cli' && getenv( 'JENKINS_URL' ) !== false ) ) { + return; +} + +# From integration/jenkins.git bin/git-changed-in-head +$_head_files = []; +exec( + 'git show HEAD --name-only --diff-filter=ACM -m --first-parent --format=format:', + $_head_files, $_return +); +if ( $_return !== 0 ) { + unset( $_head_files ); + unset( $_return ); + return; +} + +# Changes to phpcs.xml affect all files +if ( in_array( 'phpcs.xml', $_head_files ) ) { + return; +} +# composer.json might affect mediawiki/mediawiki-codesniffer version +if ( in_array( 'composer.json', $_head_files ) ) { + exec( 'git show HEAD^:composer.json', $_prev_composer, $_return ); + if ( $_return !== 0 ) { + return; + } + exec( 'git show HEAD:composer.json', $_cur_composer, $_return ); + if ( $_return !== 0 ) { + return; + } + $_prev_composer = json_decode( join( '', $_prev_composer ), true ); + $_cur_composer = json_decode( join( '', $_cur_composer ), true ); + if ( $_prev_composer['require-dev']['mediawiki/mediawiki-codesniffer'] + !== $_cur_composer['require-dev']['mediawiki/mediawiki-codesniffer'] ) { + return; + } +} + +# Only keep files that matches phpcs.xml extensions. +$values['files'] = array_filter( $_head_files, function ( $file ) use ( $values ) { + $pinfo = pathinfo( $file ); + return in_array( + strtolower( $pinfo['extension'] ), $values['extensions'] ); +} ); +if ( empty( $values['files'] ) ) { + echo "No files to process. Skipping run\n"; + exit( 0 ); +} -- To view, visit https://gerrit.wikimedia.org/r/358554 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I2f492f889594135e37950fcb8e40da0c4d088430 Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/tools/codesniffer Gerrit-Branch: master Gerrit-Owner: Hashar <has...@free.fr> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits