├── action.yml
├── .editorconfig
├── .github
├── dependabot.yml
└── workflows
│ ├── release.yml
│ └── build.yml
├── entrypoint.sh
├── LICENSE
├── Dockerfile
├── README.md
└── main.sh
/action.yml:
--------------------------------------------------------------------------------
1 | name: 'PHPCS Code Review'
2 | description: 'This will run phpcs on PRs'
3 | author: 'rtCamp'
4 | runs:
5 | using: 'docker'
6 | image: 'docker://ghcr.io/rtcamp/action-phpcs-code-review:v3.1.1'
7 | branding:
8 | icon: 'check-circle'
9 | color: 'green'
10 |
--------------------------------------------------------------------------------
/.editorconfig:
--------------------------------------------------------------------------------
1 | # This file is for unifying the coding style for different editors and IDEs.
2 | # More information at http://editorconfig.org
3 |
4 | root = true
5 |
6 | [*]
7 | indent_style = space
8 | indent_size = 2
9 | end_of_line = lf
10 | charset = utf-8
11 | trim_trailing_whitespace = true
12 | insert_final_newline = true
13 |
--------------------------------------------------------------------------------
/.github/dependabot.yml:
--------------------------------------------------------------------------------
1 | version: 2
2 | updates:
3 | - package-ecosystem: github-actions
4 | directory: "/"
5 | schedule:
6 | interval: "weekly"
7 | reviewers:
8 | - mrrobot47
9 | - package-ecosystem: docker
10 | directory: "/"
11 | schedule:
12 | interval: "weekly"
13 | reviewers:
14 | - mrrobot47
15 |
--------------------------------------------------------------------------------
/.github/workflows/release.yml:
--------------------------------------------------------------------------------
1 | on:
2 | push:
3 | tags:
4 | - "v*"
5 | name: Release Creation
6 | jobs:
7 | release:
8 | name: Create a Draft Release on GitHub
9 | runs-on: ubuntu-latest
10 | steps:
11 | - name: Get Release Name
12 | id: get_release_name
13 | run: |
14 | echo "release_name=${GITHUB_REF_NAME/v/Version }" >> $GITHUB_ENV
15 | - name: Publish a Draft Release
16 | uses: softprops/action-gh-release@v2
17 | with:
18 | body: |
19 | ## What's Changed
20 |
21 | **Changelog:**
22 | draft: true # Creates a Draft Release
23 | name: ${{ steps.get_release_name.outputs.release_name }}
24 | generate_release_notes: true
25 | append_body: true
26 |
27 |
--------------------------------------------------------------------------------
/.github/workflows/build.yml:
--------------------------------------------------------------------------------
1 | on:
2 | workflow_dispatch:
3 | push:
4 | tags:
5 | - '*'
6 |
7 | permissions:
8 | contents: read
9 | packages: write
10 |
11 | jobs:
12 | build:
13 | runs-on: ubuntu-latest
14 | steps:
15 | - name: Checkout
16 | uses: actions/checkout@v4
17 | - name: install docker emulation
18 | run: docker run --rm --privileged tonistiigi/binfmt:latest --install amd64,arm64
19 | - name: Set up buildx
20 | uses: docker/setup-buildx-action@v3
21 | - name: Login to ghcr.io
22 | run: echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io -u ${{ github.actor }} --password-stdin
23 | - name: Build and push
24 | run: |
25 | docker buildx build --platform linux/amd64,linux/arm64 -t $(echo 'ghcr.io/${{ github.repository }}:${{ github.ref_name }}' | tr '[:upper:]' '[:lower:]') --push .
26 |
--------------------------------------------------------------------------------
/entrypoint.sh:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env bash
2 |
3 | message=$(cat << "EOF"
4 | PHPCS Code Review - GitHub Action by
5 | _ ____
6 | _ __| |_ / ___|__ _ _ __ ___ _ __
7 | | '__| __| | / _` | '_ ` _ \| '_ \
8 | | | | |_| |__| (_| | | | | | | |_) |
9 | |_| \__|\____\__,_|_| |_| |_| .__/
10 | |_|
11 | EOF
12 | )
13 |
14 | echo -e "\e[0;32m\n$message\n\e[0m"
15 |
16 | # If token are not set, exit with error.
17 | if [[ -z "$GH_BOT_TOKEN" ]] && [[ -z "$VAULT_TOKEN" ]]; then
18 | printf "[\e[0;31mERROR\e[0m] Secret \`GH_BOT_TOKEN\` or \`VAULT_TOKEN\` is missing. Please add it to this action for proper execution.\nRefer https://github.com/rtCamp/action-phpcs-code-review#github-token-creation for more information.\n"
19 | exit 1
20 | fi
21 |
22 | # custom path for files to override default files
23 | custom_path="$GITHUB_WORKSPACE/.github/inspections/vip-go-ci/"
24 | main_script="/usr/local/bin/main.sh"
25 |
26 | if [[ -d "$custom_path" ]]; then
27 | rsync -a "$custom_path" /usr/local/bin/
28 | fi
29 |
30 | bash "$main_script" "$@"
31 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2023 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.
22 |
--------------------------------------------------------------------------------
/Dockerfile:
--------------------------------------------------------------------------------
1 | FROM ubuntu:24.04
2 | LABEL "com.github.actions.icon"="check-circle"
3 | LABEL "com.github.actions.color"="green"
4 | LABEL "com.github.actions.name"="PHPCS Code Review"
5 | LABEL "com.github.actions.description"="Run automated code review using PHPCS on your pull requests."
6 | LABEL "org.opencontainers.image.source"="https://github.com/rtCamp/action-phpcs-code-review"
7 |
8 | ARG DEFAULT_PHP_VERSION=8.3
9 | ARG PHP_BINARIES_TO_PREINSTALL='7.4 8.0 8.1 8.2 8.3 8.4'
10 |
11 | ENV DOCKER_USER=rtbot
12 | ENV ACTION_WORKDIR=/home/$DOCKER_USER
13 | ENV DEBIAN_FRONTEND=noninteractive
14 |
15 | RUN useradd -m -s /bin/bash $DOCKER_USER \
16 | && mkdir -p $ACTION_WORKDIR \
17 | && chown -R $DOCKER_USER $ACTION_WORKDIR
18 |
19 | RUN set -ex \
20 | && savedAptMark="$(apt-mark showmanual)" \
21 | && apt-mark auto '.*' > /dev/null \
22 | && apt-get update && apt-get install -y --no-install-recommends git ca-certificates wget rsync gnupg jq software-properties-common unzip \
23 | && LC_ALL=C.UTF-8 add-apt-repository ppa:ondrej/php \
24 | && apt-get update \
25 | && for v in $PHP_BINARIES_TO_PREINSTALL; do \
26 | apt-get install -y --no-install-recommends \
27 | php"$v" \
28 | php"$v"-curl \
29 | php"$v"-tokenizer \
30 | php"$v"-simplexml \
31 | php"$v"-xmlwriter; \
32 | done \
33 | && update-alternatives --set php /usr/bin/php${DEFAULT_PHP_VERSION} \
34 | # cleanup
35 | && apt-get remove software-properties-common unzip -y \
36 | && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* \
37 | && { [ -z "$savedAptMark" ] || apt-mark manual $savedAptMark > /dev/null; } \
38 | && find /usr/local -type f -executable -exec ldd '{}' ';' \
39 | | awk '/=>/ { print $(NF-1) }' \
40 | | sort -u \
41 | | xargs -r dpkg-query --search \
42 | | cut -d: -f1 \
43 | | sort -u \
44 | | xargs -r apt-mark manual \
45 | && apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false \
46 | # smoke test
47 | && for v in $PHP_BINARIES_TO_PREINSTALL; do \
48 | php"$v" -v; \
49 | done \
50 | && php -v;
51 |
52 | COPY entrypoint.sh main.sh /usr/local/bin/
53 |
54 | RUN chmod +x /usr/local/bin/entrypoint.sh /usr/local/bin/main.sh
55 |
56 | USER $DOCKER_USER
57 |
58 | WORKDIR $ACTION_WORKDIR
59 |
60 | RUN wget https://raw.githubusercontent.com/Automattic/vip-go-ci/latest/tools-init.sh -O tools-init.sh \
61 | && bash tools-init.sh \
62 | && rm -f tools-init.sh
63 |
64 | ENTRYPOINT ["/usr/local/bin/entrypoint.sh"]
65 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | This action is a part of [GitHub Actions Library](https://github.com/rtCamp/github-actions-library/) created by [rtCamp](https://github.com/rtCamp/).
2 |
3 | # PHPCS Code Review - GitHub Action
4 |
5 | [](https://www.repostatus.org/#active)
6 |
7 |
8 | A [GitHub Action](https://github.com/features/actions) to perform automated [pull request review](https://help.github.com/en/articles/about-pull-request-reviews). It is based on https://github.com/Automattic/vip-go-ci/ but can be used for any WordPress or even PHP projects.
9 |
10 | The code review is performed using [PHPCS](https://github.com/squizlabs/PHP_CodeSniffer).
11 |
12 | Please note that, this action performs pull request review *only*. If you have an existing project, and you want entire project's code to be reviwed, you may need to do it manually.
13 |
14 | ## Usage
15 |
16 | 1. Create a `.github/workflows/phpcs.yml` in your GitHub repo, if one doesn't exist already.
17 | 2. Add the following code to the `phpcs.yml` file.
18 |
19 | ```yaml
20 | on: pull_request
21 |
22 | name: Inspections
23 | jobs:
24 | runPHPCSInspection:
25 | name: Run PHPCS inspection
26 | runs-on: ubuntu-latest
27 | steps:
28 | - uses: actions/checkout@v3
29 | with:
30 | ref: ${{ github.event.pull_request.head.sha }}
31 | - name: Run PHPCS inspection
32 | uses: rtCamp/action-phpcs-code-review@v3
33 | env:
34 | GH_BOT_TOKEN: ${{ secrets.GH_BOT_TOKEN }}
35 | SKIP_FOLDERS: "tests,.github"
36 | PHPCS_SNIFFS_EXCLUDE: "WordPress.Files.FileName"
37 | with:
38 | args: "WordPress,WordPress-Core,WordPress-Docs"
39 | ```
40 |
41 | 3. Define `GH_BOT_TOKEN` using [GitHub Action's Secret](https://developer.github.com/actions/creating-workflows/storing-secrets). See [GitHub Token Creation](#github-token-creation) section for more details.
42 |
43 | Now, next time you create a pull request or commit on an existing pull request, this action will run.
44 |
45 | By default, pull request will be reviwed using WordPress coding and documentation standards. You can change the default by passing different [PHPCS Coding Standard(s)](#phpcs-coding-standards) in line `args = ["WordPress-Core,WordPress-Docs"]`.
46 |
47 | 4. In case you want to skip PHPCS scanning in any pull request, add `[do-not-scan]` in the PR description. You can add it anywhere in the description and it will skip the action run for that pull request.
48 |
49 | 5. In case you want to skip linting all files on every pull request, set `PHP_LINT` to `false`.
50 |
51 | ## GitHub Token Creation
52 |
53 | You can create [GitHub Token from here](https://github.com/settings/tokens).
54 |
55 | It is necessary that you create this token from a [bot user account](https://stackoverflow.com/a/29177936/4108721). Please note that the bot account should have access to the repo in which action is being run, in case it is a private repo. It is compulsory to use a bot account because in GitHub it is forbidden to request changes on your own Pull Request by your own user.
56 | Additional benefit of using a bot account is that in a large team, if you use your human account token, you may get flooded with unncessary Github notifications.
57 |
58 | Permissions required for this token differ according to which type of repo this workflow has been setup for.
59 |
60 | Repo Type | Permissions Required | Screenshots
61 | ----------|-----------------------------------------------------|-------------------------------------------------------------
62 | Public | Under `Repo` section, only `public_repo` permission | [Screenshot Public Repo](https://user-images.githubusercontent.com/4115/54978322-01926100-4fc6-11e9-8da5-1e088fa52b34.png)
63 | Private | Complete `repo` and `write:discussion` permissions | [Screenshot Private Repo](https://user-images.githubusercontent.com/4115/54978180-86c94600-4fc5-11e9-846e-7d3fd1dfb7e0.png)
64 |
65 | ## Environment Variables
66 |
67 | Variable | Default | Possible Values | Purpose
68 | ---------------|---------|-----------------------------|----------------------------------------------------
69 | `SKIP_FOLDERS` | - | `tests`,`tests,.github` (Any other comma seprated top level directories in the repo) | If any specific folders should be ignored when scanning, then a comma seprated list of values should be added to this env variable.
70 | `PHPCS_SNIFFS_EXCLUDE` | - | `WordPress.Files.FileName` (Any other comma seprated list of valid sniffs) | Single sniff or comma seprated list of sniffs to be excluded from the phpcs scan.
71 | `PHP_LINT` | `true` | `true` or `false`, *case insensitive* (Any unknown value is the same as passing `true`) | If the default automatic linting of all PHP files should be deactivated, then this env variable should be set to `false`.
72 | `PHPCS_STANDARD_FILE_NAME` | - | phpcs ruleset file from project root dir. i.e phpcs.ruleset.xml | PHP_CodeSniffer ruleset filename. Default filename available: '.phpcs.xml', 'phpcs.xml', '.phpcs.xml.dist', 'phpcs.xml.dist'
73 | `PHPCS_FILE_PATH` | - | Custom phpcs execution file path from project. i.e Composer phpcs path. 'vendor/bin/phpcs' | This is useful in case of needed to use any custom coding standards apart from pre-defined in VIP/WP Coding Standards. [Wiki](https://github.com/rtCamp/action-phpcs-code-review/wiki/How-to%3F#use-custom-coding-standards)
74 | `SKIP_DRAFT_PRS` | false | `true`/`false` | Set it to true to skip PHPCS checks on draft PRs.
75 | `PHPCS_PHP_VERSION` | 8.1 | 7.4, 8.0, 8.1, or 8.2 | To use a different PHP interpreter than the system default.
76 |
77 | ## Modifying the bot’s behavior
78 |
79 | You can change the bot’s behavior by placing a configuration file named `.vipgoci_options` at the root of the relevant repository. This file must contain a valid JSON string for this to work; if the file is not parsable, it will be ignored. This file is where you can add code to turn off support messages as well as adjust PHPCS severity levels.
80 |
81 | i.e: You can update phpcs severity:
82 | ```json
83 | {
84 | "phpcs-severity": 5
85 | }
86 | ```
87 |
88 | Allowed options:
89 | - `"skip-execution"`
90 | - `"skip-draft-prs"`
91 | - `"results-comments-sort"`
92 | - `"review-comments-include-severity"`
93 | - `"phpcs"`
94 | - `"phpcs-severity"`
95 | - `"post-generic-pr-support-comments"`
96 | - `"phpcs-sniffs-include"`
97 | - `"phpcs-sniffs-exclude"`
98 | - `"hashes-api"`
99 | - `"svg-checks"`
100 | - `"autoapprove"`
101 | - `"autoapprove-php-nonfunctional-changes`
102 |
103 | For more details please check the documentation for [all options here](https://github.com/automattic/vip-go-ci#configuration-via-repository-config-file).
104 |
105 | ## Skipping PHPCS scanning for specific folders
106 |
107 | You can add files to the root of the repository indicating folders that should not be scanned. For PHPCS, the file should be named `.vipgoci_phpcs_skip_folders`. For PHP Linting the file should be named `.vipgoci_lint_skip_folders`. Please ensure both files are located in the root of the repository.
108 |
109 | This can be used as an alternate to `SKIP_FOLDERS` env variable.
110 |
111 | **Please note** that the folders exlcuded in the PHPCS xml file do not work in this action, you can check the reason [here](https://github.com/rtCamp/action-phpcs-code-review/issues/29#issuecomment-623933663). Instead you should add all the folders to be excluded in either `SKIP_FOLDERS` env or `.vipgoci_phpcs_skip_folders` file.
112 |
113 | List each folder to be skipped on individual lines within those files.
114 |
115 | i.e:
116 | ```
117 | foo
118 | tests/bar
119 | vendor
120 | node_modules
121 | ```
122 |
123 | For more details, please check the documentation [here](https://github.com/automattic/vip-go-ci#skipping-certain-folders).
124 |
125 | ## PHPCS Coding Standards
126 |
127 | Below is list of PHPCS sniffs available at runtime. You can pass more than one standard at a time by comma separated value.
128 |
129 | By default, `WordPress-Core,WordPress-Docs` value is passed.
130 |
131 | * MySource
132 | * PEAR
133 | * PHPCompatibility
134 | * PHPCompatibilityParagonieRandomCompat
135 | * PHPCompatibilityParagonieSodiumCompat
136 | * PHPCompatibilityWP
137 | * PSR1
138 | * PSR12
139 | * PSR2
140 | * Squiz
141 | * WordPress _(default)_
142 | * WordPress-Core _(default)_
143 | * WordPress-Docs _(default)_
144 | * WordPress-Extra
145 | * WordPress-VIP
146 | * WordPress-VIP-Go
147 | * WordPressVIPMinimum
148 | * Zend
149 |
150 | ### Custom Sniffs
151 |
152 | Default filename supported:
153 | - `.phpcs.xml`
154 | - `phpcs.xml`
155 | - `.phpcs.xml.dist`
156 | - `phpcs.xml.dist`
157 |
158 | If your git repo has a file named `phpcs.xml` in the root of the repository, then that will take precedence. In that case, value passed to args such as `args = ["WordPress,WordPress-Core,WordPress-Docs"]` will be ignored.
159 |
160 | If your git repo doesn't have `phpcs.xml` and you do not specify `args` in `main.workflow` PHPCS action, then this actions will fallback to default.
161 |
162 | If your git repo has phpcs ruleset file other than default filename list, use `PHPCS_STANDARD_FILE_NAME` environment var to provide filename.
163 |
164 | Here is a sample [phpcs.xml](https://github.com/rtCamp/github-actions-wordpress-skeleton/blob/master/phpcs.xml) you can use in case you want to use custom sniffs.
165 |
166 | ### Custom Coding Standards
167 |
168 | If you have custom coding standards from your git repository, you can use composer and use `phpcs` from execution from your repository phpcs file with the help of `PHPCS_FILE_PATH` environment variable. Please refer this [wiki page](https://github.com/rtCamp/action-phpcs-code-review/wiki/How-to%3F#use-custom-coding-standards) for more information.
169 |
170 | ## Screenshot
171 |
172 | **Automated Code Review in action**
173 |
174 |
175 |
176 |
177 | ## Limitations
178 |
179 | Please note...
180 |
181 | 1. This action runs only for PRs. It even runs on new commits pushed after a PR is created.
182 | 2. This action doesn't run on code in the repository added before this action.
183 | 3. This action doesn't run for code committed directly to a branch. We highly recommend that you disable direct commits to your main/master branch.
184 |
185 | ## License
186 |
187 | [MIT](LICENSE) © 2019 rtCamp
188 |
189 | ## Does this interest you?
190 |
191 |
192 |
--------------------------------------------------------------------------------
/main.sh:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env bash
2 |
3 | ## Logging functions
4 | # Arguments: Message.
5 | error_message() {
6 | echo -en "\033[31mERROR\033[0m: $1"
7 | }
8 |
9 | warning_message() {
10 | echo -en "\033[33mWARNING\033[0m: $1"
11 | }
12 |
13 | info_message() {
14 | echo -en "\033[32mINFO\033[0m: $1"
15 | }
16 |
17 | if [[ "$GITHUB_EVENT_NAME" != "pull_request" ]]; then
18 | echo $( warning_message "This action only runs on pull_request events." )
19 | echo $( info_message "Refer https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#pull_request for more information." )
20 | echo $( info_message "Exiting..." )
21 |
22 | exit 0
23 | fi
24 |
25 | if [[ $(cat "$GITHUB_EVENT_PATH" | jq -r .pull_request.body) == *"[do-not-scan]"* ]]; then
26 | echo $( info_message "[do-not-scan] found in PR description. Skipping PHPCS scan." )
27 |
28 | exit 0
29 | fi
30 |
31 | GITHUB_REPOSITORY_NAME=${GITHUB_REPOSITORY##*/}
32 | GITHUB_REPOSITORY_OWNER=${GITHUB_REPOSITORY%%/*}
33 | COMMIT_ID=$(cat $GITHUB_EVENT_PATH | jq -r '.pull_request.head.sha')
34 |
35 | echo $( info_message "COMMIT_ID: $COMMIT_ID" )
36 | echo $( info_message "GITHUB_REPOSITORY_NAME: $GITHUB_REPOSITORY_NAME" )
37 | echo $( info_message "GITHUB_REPOSITORY_OWNER: $GITHUB_REPOSITORY_OWNER" )
38 |
39 | if [[ -z "$GITHUB_REPOSITORY_NAME" ]] || [[ -z "$GITHUB_REPOSITORY_OWNER" ]] || [[ -z "$COMMIT_ID" ]]; then
40 | echo $( error_message "One or more of the following variables are not set: GITHUB_REPOSITORY_NAME, GITHUB_REPOSITORY_OWNER, COMMIT_ID" )
41 |
42 | exit 1
43 | fi
44 |
45 | if [[ -n "$VAULT_TOKEN" ]]; then
46 | GH_BOT_TOKEN=$(vault read -field=token secret/rtBot-token)
47 |
48 | echo "::warning ::Support for HashiCorp Vault will be discontinued in the future. Please use GitHub Action Secrets to store the secrets. Refer https://docs.github.com/en/actions/security-guides/encrypted-secrets#creating-encrypted-secrets-for-a-repository to know more about GitHub Action Secrets."
49 | fi
50 |
51 | # Remove trailing and leading whitespaces. At times copying token can give leading space.
52 | GH_BOT_TOKEN=${GH_BOT_TOKEN//[[:blank:]]/}
53 |
54 | if [[ -z "$GH_BOT_TOKEN" ]]; then
55 | echo $( error_message "GH_BOT_TOKEN is not set." )
56 |
57 | exit 1
58 | fi
59 |
60 | # VIP Go CI tools directory.
61 | VIP_GO_CI_TOOLS_DIR="$ACTION_WORKDIR/vip-go-ci-tools"
62 |
63 | # Setup GitHub workspace inside Docker container.
64 | DOCKER_GITHUB_WORKSPACE="$ACTION_WORKDIR/workspace"
65 |
66 | # Sync GitHub workspace to Docker GitHub workspace.
67 | rsync -a "$GITHUB_WORKSPACE/" "$DOCKER_GITHUB_WORKSPACE"
68 |
69 | echo $( info_message "DOCKER_GITHUB_WORKSPACE: $DOCKER_GITHUB_WORKSPACE" )
70 |
71 | if [[ ! -d "$VIP_GO_CI_TOOLS_DIR" ]] || [[ ! -d "$DOCKER_GITHUB_WORKSPACE" ]]; then
72 | echo $( error_message "One or more of the following directories are not present: VIP_GO_CI_TOOLS_DIR, DOCKER_GITHUB_WORKSPACE" )
73 |
74 | exit 1
75 | fi
76 |
77 | ################################################################################
78 | # Configure options for vip-go-ci #
79 | # #
80 | # Refer https://github.com/Automattic/vip-go-ci#readme for more information #
81 | ################################################################################
82 |
83 | ################################################################################
84 | # General Configuration #
85 | ################################################################################
86 |
87 | #######################################
88 | # Set the --lint and --phpcs
89 | # Default: true
90 | # Options: BOOLEAN
91 | #######################################
92 | CMD=( "--lint=false" "--phpcs=true" )
93 |
94 | #######################################
95 | # Set the --skip-draft-prs
96 | # Default: false
97 | # Options: BOOLEAN
98 | #######################################
99 | if [[ "$SKIP_DRAFT_PRS" == "true" ]]; then
100 | CMD+=( "--skip-draft-prs=true" )
101 | fi
102 |
103 | #######################################
104 | # Set the --local-git-repo
105 | # Options: STRING (Path to local git repo)
106 | #######################################
107 | CMD+=( "--local-git-repo=$DOCKER_GITHUB_WORKSPACE" )
108 |
109 | #######################################
110 | # Set the --name-to-use
111 | # Default: 'action-phpcs-code-review'
112 | # Options: STRING (Name to use for the bot)
113 | #######################################
114 | if [[ -n "$NAME_TO_USE" ]]; then
115 | CMD+=( "--name-to-use=$NAME_TO_USE" )
116 | else
117 | CMD+=( "--name-to-use=[action-phpcs-code-review](https://github.com/rtCamp/action-phpcs-code-review/)")
118 | fi
119 |
120 | ################################################################################
121 | # Environmental & repo configuration #
122 | ################################################################################
123 |
124 | #######################################
125 | # Set the --repo-options
126 | # Default: If .vipgoci_options file is present in the repo, then true.
127 | #######################################
128 | if [[ -f "$DOCKER_GITHUB_WORKSPACE/.vipgoci_options" ]]; then
129 | CMD+=( "--repo-options=true" )
130 | fi
131 |
132 | ################################################################################
133 | # GitHub configuration #
134 | ################################################################################
135 |
136 | #######################################
137 | # Set the --repo-owner
138 | # Default: $GITHUB_REPOSITORY_OWNER
139 | #######################################
140 | CMD+=( "--repo-owner=$GITHUB_REPOSITORY_OWNER" )
141 |
142 | #######################################
143 | # Set the --repo-name
144 | # Default: $GITHUB_REPOSITORY_NAME
145 | #######################################
146 | CMD+=( "--repo-name=$GITHUB_REPOSITORY_NAME" )
147 |
148 | #######################################
149 | # Set the --commit
150 | # Default: $GITHUB_SHA
151 | #######################################
152 | CMD+=( "--commit=$COMMIT_ID" )
153 |
154 | #######################################
155 | # Set the --token
156 | # Default: $GH_BOT_TOKEN
157 | # Options: STRING (GitHub token)
158 | #######################################
159 | CMD+=( "--token=$GH_BOT_TOKEN" )
160 |
161 | ################################################################################
162 | # PHPCS configuration #
163 | ################################################################################
164 |
165 | #######################################
166 | # Set the --phpcs-php-path
167 | # Default: PHP in $PATH
168 | # Options: FILE (Path to php executable)
169 | #######################################
170 | if [[ -n "$PHPCS_PHP_VERSION" ]]; then
171 | if [[ -z "$( command -v php$PHPCS_PHP_VERSION )" ]]; then
172 | echo $( warning_message "php$PHPCS_PHP_VERSION is not available. Using default php runtime...." )
173 |
174 | phpcs_php_path=$( command -v php )
175 | else
176 | phpcs_php_path=$( command -v php$PHPCS_PHP_VERSION )
177 | fi
178 |
179 | CMD+=( "--phpcs-php-path=$phpcs_php_path" )
180 | fi
181 |
182 | #######################################
183 | # Set the --phpcs-path
184 | # Default: $VIP_GO_CI_TOOLS_DIR/phpcs/bin/phpcs
185 | # Options: FILE (Path to phpcs executable)
186 | #######################################
187 | phpcs_path="$VIP_GO_CI_TOOLS_DIR/phpcs/bin/phpcs"
188 |
189 | if [[ -n "$PHPCS_FILE_PATH" ]]; then
190 | if [[ -f "$DOCKER_GITHUB_WORKSPACE/$PHPCS_FILE_PATH" ]]; then
191 | phpcs_path="$DOCKER_GITHUB_WORKSPACE/$PHPCS_FILE_PATH"
192 | else
193 | echo $( warning_message "$DOCKER_GITHUB_WORKSPACE/$PHPCS_FILE_PATH does not exist. Using default path...." )
194 | fi
195 | fi
196 |
197 | CMD+=( "--phpcs-path=$phpcs_path" )
198 |
199 | #######################################
200 | # Set the --phpcs-standard
201 | # Default: WordPress
202 | # Options: STRING (Comma separated list of standards to check against)
203 | #
204 | # 1. Either a comma separated list of standards to check against.
205 | # 2. Or a path to a custom ruleset.
206 | #######################################
207 | phpcs_standard=''
208 |
209 | defaultFiles=(
210 | '.phpcs.xml'
211 | 'phpcs.xml'
212 | '.phpcs.xml.dist'
213 | 'phpcs.xml.dist'
214 | )
215 |
216 | phpcsfilefound=1
217 |
218 | for phpcsfile in "${defaultFiles[@]}"; do
219 | if [[ -f "$DOCKER_GITHUB_WORKSPACE/$phpcsfile" ]]; then
220 | phpcs_standard="$DOCKER_GITHUB_WORKSPACE/$phpcsfile"
221 | phpcsfilefound=0
222 | fi
223 | done
224 |
225 | if [[ $phpcsfilefound -ne 0 ]]; then
226 | if [[ -n "$1" ]]; then
227 | phpcs_standard="$1"
228 | else
229 | phpcs_standard="WordPress"
230 | fi
231 | fi
232 |
233 | if [[ -n "$PHPCS_STANDARD_FILE_NAME" ]] && [[ -f "$DOCKER_GITHUB_WORKSPACE/$PHPCS_STANDARD_FILE_NAME" ]]; then
234 | phpcs_standard="$DOCKER_GITHUB_WORKSPACE/$PHPCS_STANDARD_FILE_NAME"
235 | fi;
236 |
237 | CMD+=( "--phpcs-standard=$phpcs_standard" )
238 |
239 | #######################################
240 | # Set the --phpcs-standards-to-ignore
241 | # Default: PHPCSUtils
242 | # Options:String (Comma separated list of standards to ignore)
243 | #######################################
244 | if [[ -n "$PHPCS_STANDARDS_TO_IGNORE" ]]; then
245 | CMD+=( "--phpcs-standards-to-ignore=$PHPCS_STANDARDS_TO_IGNORE" )
246 | else
247 | CMD+=( "--phpcs-standards-to-ignore=PHPCSUtils" )
248 | fi
249 |
250 | #######################################
251 | # Set the --phpcs-skip-scanning-via-labels-allowed
252 | # Default: true
253 | # Options: BOOLEAN
254 | #######################################
255 | CMD+=( "--phpcs-skip-scanning-via-labels-allowed=true" )
256 |
257 | #######################################
258 | # Set the --phpcs-skip-folders
259 | # Options: STRING (Comma separated list of folders to skip)
260 | #######################################
261 | if [[ -n "$SKIP_FOLDERS" ]]; then
262 | CMD+=( "--phpcs-skip-folders=$SKIP_FOLDERS" )
263 | fi
264 |
265 | #######################################
266 | # Set the --phpcs-sniffs-exclude
267 | # Default: ''
268 | # Options: STRING (Comma separated list of sniffs to exclude)
269 | #######################################
270 | if [[ -n "$PHPCS_SNIFFS_EXCLUDE" ]]; then
271 | CMD+=( "--phpcs-sniffs-exclude=$PHPCS_SNIFFS_EXCLUDE" )
272 | fi
273 |
274 | #######################################
275 | # Set the --phpcs-skip-folders-in-repo-options-file
276 | # Default: If .vipgoci_phpcs_skip_folders file exists in the repo, then true.
277 | #######################################
278 | if [[ -f "$DOCKER_GITHUB_WORKSPACE/.vipgoci_phpcs_skip_folders" ]]; then
279 | CMD+=( "--phpcs-skip-folders-in-repo-options-file=true" )
280 | fi
281 |
282 | ################################################################################
283 | # GitHub reviews & generic comments configuration #
284 | ################################################################################
285 |
286 | #######################################
287 | # Set the --report-no-issues-found
288 | # Default: false
289 | #######################################
290 | CMD+=( "--report-no-issues-found=false" )
291 |
292 | #######################################
293 | # Set the --informational-msg
294 | # Default: Powered by rtCamp's [GitHub Actions Library](https://github.com/rtCamp/github-actions-library/)
295 | # Options: STRING (Message to be included in the comment)
296 | #######################################
297 | if [[ -z "$INFORMATIONAL_MSG" ]]; then
298 | informational_msg="Powered by rtCamp's [GitHub Actions Library](https://github.com/rtCamp/github-actions-library)"
299 | else
300 | informational_msg="$INFORMATIONAL_MSG"
301 | fi
302 |
303 | CMD+=( "--informational-msg=$informational_msg" )
304 |
305 | #######################################
306 | # Set the --scan-details-msg-include
307 | # Default: false
308 | #######################################
309 | CMD+=( "--scan-details-msg-include=false" )
310 |
311 | #######################################
312 | # Set the --dismiss-stale-reviews
313 | # Default: true
314 | #######################################
315 | CMD+=( "--dismiss-stale-reviews=true" )
316 |
317 | ################################################################################
318 | # Start Code Review and set GH build status #
319 | ################################################################################
320 |
321 | echo $( info_message "Running PHPCS inspection..." )
322 | echo $( info_message "Command: $VIP_GO_CI_TOOLS_DIR/vip-go-ci/vip-go-ci.php ${CMD[*]}" )
323 |
324 | PHPCS_CMD=( php "$VIP_GO_CI_TOOLS_DIR/vip-go-ci/vip-go-ci.php" "${CMD[@]}" )
325 |
326 | if [[ "$ENABLE_STATUS_CHECKS" == "true" ]]; then
327 | php $VIP_GO_CI_TOOLS_DIR/vip-go-ci/github-commit-status.php --repo-owner="$GITHUB_REPOSITORY_OWNER" --repo-name="$GITHUB_REPOSITORY_NAME" --github-token="$GH_BOT_TOKEN" --github-commit="$COMMIT_ID" --build-context='PHPCS Code Review by rtCamp' --build-description="PR review in progress" --build-state="pending"
328 |
329 | "${PHPCS_CMD[@]}"
330 |
331 | export VIPGOCI_EXIT_CODE="$?"
332 |
333 | export BUILD_STATE="failure"
334 | export BUILD_DESCRIPTION="Unknown error"
335 |
336 | case "$VIPGOCI_EXIT_CODE" in
337 | "0")
338 | export BUILD_STATE="success"
339 | export BUILD_DESCRIPTION="No PHPCS errors found"
340 | ;;
341 | "230")
342 | export BUILD_DESCRIPTION="Pull request not found for commit"
343 | ;;
344 | "248")
345 | export BUILD_DESCRIPTION="Commit not latest in PR"
346 | ;;
347 | "249")
348 | export BUILD_DESCRIPTION="Inspection timed out, PR may be too large"
349 | ;;
350 | "250")
351 | export BUILD_DESCRIPTION="Found PHPCS errors"
352 | ;;
353 | "251")
354 | export BUILD_DESCRIPTION="Action is not configured properly"
355 | ;;
356 | "252")
357 | export BUILD_DESCRIPTION="GitHub communication error. Please retry"
358 | ;;
359 | "253")
360 | export BUILD_DESCRIPTION="Wrong options passed to action"
361 | ;;
362 | esac
363 |
364 |
365 | php $VIP_GO_CI_TOOLS_DIR/vip-go-ci/github-commit-status.php --repo-owner="$GITHUB_REPOSITORY_OWNER" --repo-name="$GITHUB_REPOSITORY_NAME" --github-token="$GH_BOT_TOKEN" --github-commit="$COMMIT_ID" --build-context='PHPCS Code Review by rtCamp' --build-description="$BUILD_DESCRIPTION" --build-state="$BUILD_STATE"
366 | else
367 | "${PHPCS_CMD[@]}"
368 | fi
369 |
--------------------------------------------------------------------------------