Skip to content
This repository was archived by the owner on Mar 18, 2019. It is now read-only.

Commit 22db6a0

Browse files
committed
Add deployer phpcs inspections action
Signed-off-by: Riddhesh Sanghvi <riddheshsanghvi96@gmail.com>
1 parent eb95cf4 commit 22db6a0

7 files changed

Lines changed: 330 additions & 2 deletions

File tree

Dockerfile

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
# alpine:latest at 2019-01-04T21:27:39IST
2+
FROM alpine@sha256:46e71df1e5191ab8b8034c5189e325258ec44ea739bba1e5645cff83c9048ff1
3+
4+
LABEL "com.github.actions.icon"="search"
5+
LABEL "com.github.actions.name"="PHPCodeSniffer"
6+
LABEL "com.github.actions.description"="Check phpcs rules in the diff getting deployed"
7+
8+
# Environments
9+
ENV TIMEZONE Asia/Kolkata
10+
ENV PHP_MEMORY_LIMIT 512M
11+
ENV MAX_UPLOAD 50M
12+
ENV PHP_MAX_FILE_UPLOAD 200
13+
ENV PHP_MAX_POST 100M
14+
ENV PHP_INI_DIR /etc/php7/php.ini
15+
ENV HOME /root
16+
ENV PATH "/composer/vendor/bin:~/.local/bin:$PATH"
17+
ENV COMPOSER_ALLOW_SUPERUSER 1
18+
ENV COMPOSER_HOME /composer
19+
20+
# PHP-CLI installation inspired from https://github.com/bravist/php-cli-alpine-aliyun-app/blob/master/Dockerfile
21+
# https://github.com/matriphe/docker-alpine-php/blob/master/7.0/FPM/Dockerfile
22+
23+
RUN apk update && \
24+
apk upgrade && \
25+
apk add \
26+
bash \
27+
jq \
28+
curl \
29+
git \
30+
tzdata \
31+
rsync \
32+
mysql-client \
33+
subversion \
34+
unzip \
35+
wget \
36+
zip \
37+
openssh-client \
38+
php7 \
39+
php7-dev \
40+
php7-apcu \
41+
php7-bcmath \
42+
php7-bz2 \
43+
php7-xmlwriter \
44+
php7-ctype \
45+
php7-curl \
46+
php7-exif \
47+
php7-iconv \
48+
php7-intl \
49+
php7-json \
50+
php7-mbstring\
51+
php7-opcache \
52+
php7-openssl \
53+
php7-pcntl \
54+
php7-pdo \
55+
php7-mysqlnd \
56+
php7-mysqli \
57+
php7-pdo_mysql \
58+
php7-pdo_pgsql \
59+
php7-phar \
60+
php7-posix \
61+
php7-session \
62+
php7-xml \
63+
php7-simplexml \
64+
php7-mcrypt \
65+
php7-xsl \
66+
php7-zip \
67+
php7-zlib \
68+
php7-dom \
69+
php7-redis\
70+
php7-tokenizer \
71+
php7-gd \
72+
php7-fileinfo \
73+
php7-zmq \
74+
php7-memcached \
75+
php7-xmlreader \
76+
python \
77+
py2-pip && \
78+
cp /usr/share/zoneinfo/${TIMEZONE} /etc/localtime && \
79+
echo "${TIMEZONE}" > /etc/timezone && \
80+
apk del tzdata && \
81+
pip install shyaml && \
82+
rm -rf /var/cache/apk/*
83+
84+
# https://github.com/docker-library/php/issues/240
85+
# https://gist.github.com/guillemcanal/be3db96d3caa315b4e2b8259cab7d07e
86+
# https://forum.alpinelinux.org/forum/installation/php-iconv-issue
87+
# https://github.com/gliderlabs/docker-alpine/issues/157#issuecomment-200860301
88+
89+
RUN apk add --no-cache --repository http://dl-cdn.alpinelinux.org/alpine/edge/testing gnu-libiconv
90+
ENV LD_PRELOAD /usr/lib/preloadable_libiconv.so php
91+
92+
RUN rm -rf /var/cache/apk/*
93+
94+
# Set environments
95+
RUN sed -i "s|;*date.timezone =.*|date.timezone = ${TIMEZONE}|i" "$PHP_INI_DIR" && \
96+
sed -i "s|;*memory_limit =.*|memory_limit = ${PHP_MEMORY_LIMIT}|i" "$PHP_INI_DIR" && \
97+
sed -i "s|;*upload_max_filesize =.*|upload_max_filesize = ${MAX_UPLOAD}|i" "$PHP_INI_DIR" && \
98+
sed -i "s|;*max_file_uploads =.*|max_file_uploads = ${PHP_MAX_FILE_UPLOAD}|i" "$PHP_INI_DIR" && \
99+
sed -i "s|;*post_max_size =.*|post_max_size = ${PHP_MAX_POST}|i" "$PHP_INI_DIR" && \
100+
sed -i "s|;*cgi.fix_pathinfo=.*|cgi.fix_pathinfo= 0|i" "$PHP_INI_DIR"
101+
102+
# Update php config
103+
RUN mkdir -p "/etc/php7/conf.d" && \
104+
echo "memory_limit=-1" > "/etc/php7/conf.d/memory-limit.ini" && \
105+
echo "date.timezone=${TIMEZONE}" > "/etc/php7/conf.d/date_timezone.ini"
106+
107+
# Setup composer
108+
RUN mkdir -p /composer && \
109+
curl -sS https://getcomposer.org/installer | \
110+
php -- --install-dir=/usr/bin/ --filename=composer
111+
112+
COPY *.php /
113+
COPY *.sh /
114+
RUN chmod +x /*.sh
115+
116+
ENTRYPOINT ["/entrypoint.sh"]

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2019 rtCamp
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,39 @@
1-
# action-phpcs-inspecion
2-
GitHub Action for PHPCS inspection.
1+
# PHPCS Inspections - GitHub Action
2+
3+
A [GitHub Action](https://github.com/features/actions) that can be used to run PHPCS inspections on the diff of patch being commited.
4+
5+
## Installation
6+
7+
To use this GitHub Action, you must have access to GitHub Actions. GitHub Actions are currently only available in public beta (you must [apply for access](https://github.com/features/actions)).
8+
9+
1. Create a `.github/main.workflow` in your GitHub repo.
10+
2. Add the following code to the `main.workflow` file and commit it to the repo's `master` branch.
11+
12+
```bash
13+
workflow "PHPCS Inspections" {
14+
resolves = ["Run phpcs inspection"]
15+
on = "push"
16+
}
17+
18+
action "Run phpcs inspection" {
19+
uses = "rtCamp/rtCamp/action-phpcs-inspection@master"
20+
env = {
21+
DIFF_BASE="master"
22+
}
23+
}
24+
25+
```
26+
27+
## Environment Variables
28+
29+
```shell
30+
# To compare the committed changes between master and the current branch. Default: origin/master.
31+
DIFF_BASE=master
32+
33+
# Head commit from which diff will be taken. Default: `git rev-parse HEAD`
34+
DIFF_HEAD=0fbe5466e1ec4eecb4bf0c7453ee4fa045ef3ebf
35+
```
36+
37+
## License
38+
39+
[MIT](LICENSE) © 2019 rtCamp

entrypoint.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/usr/bin/env bash
2+
3+
# custom path for files to override default files
4+
custom_path="$GITHUB_WORKSPACE/.github/inspections/codesniffer"
5+
main_script="/phpcs.sh"
6+
7+
if [[ -d "$custom_path" ]]; then
8+
rsync -av "$custom_path/" /
9+
chmod +x /*.sh
10+
fi
11+
12+
bash "$main_script"

filter-report-for-patch-ranges.php

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
# Take unix/emacs reports as STDIN, such as:
3+
# foo/js/bar.js:503:55: Missing radix parameter.
4+
# And takes a single argument pointing to the file containing the output
5+
# of parse-diff-ranges.php, such as:
6+
# path/to/file.ext:123-456
7+
# And then echos the line from STDIN if the path (here, foo/js/bar.hs) exists
8+
# in the latter, and the line number (here 503) is among the ranges represented.
9+
# If there are any matches, this script will return an exit code 1.
10+
# If there are no matches, the script will return exit code 0.
11+
12+
if ( empty( $argv[1] ) ) {
13+
echo 'Missing argument for file containing output of parse-diff-ranges.php.';
14+
exit( 2 );
15+
}
16+
if ( ! file_exists( $argv[1] ) ) {
17+
echo 'Argument for file containing output of parse-diff-ranges.php does not exist.';
18+
exit( 2 );
19+
}
20+
21+
$matched_patch_count = 0;
22+
23+
$parsed_diff_ranges = array();
24+
foreach ( explode( "\n", trim( file_get_contents( $argv[1] ) ) ) as $line ) {
25+
if ( preg_match( '/^(?P<file_path>.+):(?P<start_line>\d+)-(?P<end_line>\d+)$/', $line, $matches ) ) {
26+
$file_path = realpath( $matches['file_path'] );
27+
if ( ! array_key_exists( $file_path, $parsed_diff_ranges ) ) {
28+
$parsed_diff_ranges[ $file_path ] = array();
29+
}
30+
$parsed_diff_ranges[ $file_path ][] = array(
31+
'start_line' => intval( $matches['start_line'] ),
32+
'end_line' => intval( $matches['end_line'] ),
33+
);
34+
}
35+
}
36+
37+
while ( $line = fgets( STDIN ) ) {
38+
$matched = (
39+
preg_match( '#^(?P<file_path>.+):(?P<line_number>\d+):\d+:.+$$#', $line, $matches )
40+
||
41+
preg_match( '/^(?P<file_path>.+): line (?P<line_number>\d+),/', $line, $matches )
42+
);
43+
if ( ! $matched ) {
44+
continue;
45+
}
46+
$file_path = realpath( $matches['file_path'] );
47+
if ( ! array_key_exists( $file_path, $parsed_diff_ranges ) ) {
48+
continue;
49+
}
50+
$line_number = intval( $matches['line_number'] );
51+
$matched = false;
52+
foreach ( $parsed_diff_ranges[ $file_path ] as $range ) {
53+
if ( $line_number >= $range['start_line'] && $line_number <= $range['end_line'] ) {
54+
$matched = true;
55+
break;
56+
}
57+
}
58+
if ( ! $matched ) {
59+
continue;
60+
}
61+
62+
$matched_patch_count += 1;
63+
echo $line;
64+
}
65+
66+
if ( $matched_patch_count > 0 ) {
67+
exit( 1 );
68+
} else {
69+
exit( 0 );
70+
}

parse-diff-ranges.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
# Take input from `git diff --no-prefix --unified=0`
3+
# Outputs path/to/file.ext:123-456
4+
# Where 123 is the start line in a diff and 456 is the end line.
5+
# A line appears for each patch occuring in a file.
6+
7+
$current_file = null;
8+
9+
while ( $line = fgets( STDIN ) ) {
10+
if ( preg_match( '#^\+\+\+ (?P<file_path>.+)#', $line, $matches ) ) {
11+
$current_file = $matches['file_path'];
12+
$file_ranges[ $current_file ] = array();
13+
continue;
14+
}
15+
if ( empty( $current_file ) ) {
16+
continue;
17+
}
18+
if ( preg_match( '#^@@ -(\d+)(?:,(\d+))? \+(?P<line_number>\d+)(?:,(?P<line_count>\d+))? @@#', $line, $matches ) ) {
19+
if ( empty( $matches['line_count'] ) ) {
20+
$matches['line_count'] = 0;
21+
}
22+
$start_line = intval( $matches['line_number'] );
23+
$end_line = intval( $matches['line_number'] ) + intval( $matches['line_count'] );
24+
echo "$current_file:$start_line-$end_line\n";
25+
}
26+
}

phpcs.sh

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#!/usr/bin/env bash
2+
3+
function remove_diff_range {
4+
sed 's/:[0-9][0-9]*-[0-9][0-9]*$//' | sort | uniq
5+
}
6+
7+
function lint_php_files {
8+
PROJECT_ROOT="$(git rev-parse --show-toplevel)"
9+
TEMP_DIRECTORY=$(mktemp -d 2>/dev/null || mktemp -d -t 'dev-lib')
10+
11+
DIFF_BASE="origin/${DIFF_BASE:-master}"
12+
DIFF_HEAD=${DIFF_HEAD:-$(git rev-parse HEAD)}
13+
DIFF_ARGS="$DIFF_BASE...$DIFF_HEAD"
14+
15+
git diff --diff-filter=AM --no-prefix --unified=0 "$DIFF_ARGS" | \
16+
php /parse-diff-ranges.php | \
17+
{ grep -E '\.php(:|$)' || true; } > "$TEMP_DIRECTORY/paths-scope-php"
18+
19+
# Setup phpcs WordPress config
20+
composer global require dealerdirect/phpcodesniffer-composer-installer \
21+
wp-coding-standards/wpcs 1.2.1 \
22+
automattic/vipwpcs \
23+
phpcompatibility/php-compatibility \
24+
phpcompatibility/phpcompatibility-wp:* --update-no-dev
25+
26+
phpcs -i
27+
if ! [[ $(cat "$TEMP_DIRECTORY/paths-scope-php") ]]; then
28+
echo "No files to process"
29+
return 0
30+
fi
31+
if ! cat "$TEMP_DIRECTORY/paths-scope-php" | remove_diff_range | xargs phpcs -s --report-emacs="$TEMP_DIRECTORY/phpcs-report" --standard="phpcs.xml"; then
32+
if [ ! -s "$TEMP_DIRECTORY/phpcs-report" ]; then
33+
return 1
34+
else
35+
cat "$TEMP_DIRECTORY/phpcs-report" | php /filter-report-for-patch-ranges.php "$TEMP_DIRECTORY/paths-scope-php" | cut -c$( expr ${#PROJECT_ROOT} + 2 )-
36+
phpcs_status="${PIPESTATUS[1]}"
37+
if [[ $phpcs_status != 0 ]]; then
38+
return $phpcs_status
39+
fi
40+
fi
41+
fi
42+
rm -rf $TEMP_DIRECTORY
43+
44+
}
45+
46+
lint_php_files

0 commit comments

Comments
 (0)