|
1 | | -<p align="center"> |
2 | | - <a href="https://github.com/actions/typescript-action/actions"><img alt="typescript-action status" src="https://github.com/actions/typescript-action/workflows/build-test/badge.svg"></a> |
3 | | -</p> |
| 1 | +# GitHub Action to run PHP_CodeSniffer of files changed in current PR or even on changed lines |
4 | 2 |
|
5 | | -# Create a JavaScript Action using TypeScript |
| 3 | +This action runs [PHP_CodeSniffer](https://github.com/squizlabs/PHP_CodeSniffer) on files changed in a Pull Request, by default annotating only lines changed by the PR author. So, it's fast and great to work with legacy code: |
6 | 4 |
|
7 | | -Use this template to bootstrap the creation of a JavaScript action.:rocket: |
| 5 | + |
8 | 6 |
|
9 | | -This template includes compilication support, tests, a validation workflow, publishing, and versioning guidance. |
| 7 | +Contrary to some existing solutions this actions works faster than Docker based actions (as it runs from Node.JS directly in the same VM) and runs only on changed files (good |
| 8 | +for big projects with some non-conform files as well as to gradually introduce code style). |
10 | 9 |
|
11 | | -If you are new, there's also a simpler introduction. See the [Hello World JavaScript Action](https://github.com/actions/hello-world-javascript-action) |
| 10 | +## Usage |
12 | 11 |
|
13 | | -## Create an action from this template |
| 12 | +Add a file like this to `.github/workflows/phpcs.yml`: |
14 | 13 |
|
15 | | -Click the `Use this Template` and provide the new repo details for your action |
| 14 | +```yml |
| 15 | +name: "CI" |
16 | 16 |
|
17 | | -## Code in Master |
| 17 | +on: |
| 18 | + pull_request: |
| 19 | + paths: |
| 20 | + - "**.php" |
| 21 | + - "phpcs.xml" |
| 22 | + - ".github/workflows/phpcs.yml" |
18 | 23 |
|
19 | | -Install the dependencies |
20 | | -```bash |
21 | | -$ npm install |
22 | | -``` |
23 | | - |
24 | | -Build the typescript and package it for distribution |
25 | | -```bash |
26 | | -$ npm run build && npm run pack |
27 | | -``` |
28 | | - |
29 | | -Run the tests :heavy_check_mark: |
30 | | -```bash |
31 | | -$ npm test |
32 | | - |
33 | | - PASS ./index.test.js |
34 | | - ✓ throws invalid number (3ms) |
35 | | - ✓ wait 500 ms (504ms) |
36 | | - ✓ test runs (95ms) |
37 | | - |
38 | | -... |
39 | | -``` |
40 | | - |
41 | | -## Change action.yml |
42 | | - |
43 | | -The action.yml contains defines the inputs and output for your action. |
44 | | - |
45 | | -Update the action.yml with your name, description, inputs and outputs for your action. |
46 | | - |
47 | | -See the [documentation](https://help.github.com/en/articles/metadata-syntax-for-github-actions) |
48 | | - |
49 | | -## Change the Code |
50 | | - |
51 | | -Most toolkit and CI/CD operations involve async operations so the action is run in an async function. |
52 | | - |
53 | | -```javascript |
54 | | -import * as core from '@actions/core'; |
55 | | -... |
| 24 | +jobs: |
| 25 | + phpcs: |
| 26 | + runs-on: ubuntu-latest |
| 27 | + steps: |
| 28 | + - uses: actions/checkout@v2 |
| 29 | + with: |
| 30 | + fetch-depth: 0 # important! |
56 | 31 |
|
57 | | -async function run() { |
58 | | - try { |
59 | | - ... |
60 | | - } |
61 | | - catch (error) { |
62 | | - core.setFailed(error.message); |
63 | | - } |
64 | | -} |
| 32 | + # we may use whatever way to install phpcs, just specify the path on the next step |
| 33 | + # however, curl seems to be the fastest |
| 34 | + - name: Install PHP_CodeSniffer |
| 35 | + run: | |
| 36 | + curl -OL https://squizlabs.github.io/PHP_CodeSniffer/phpcs.phar |
| 37 | + php phpcs.phar --version |
65 | 38 |
|
66 | | -run() |
| 39 | + - uses: tinovyatkin/action-php-codesniffer@v1 |
| 40 | + with: |
| 41 | + files: "**.php" # you may customize glob as needed |
| 42 | + phpcs_path: php phpcs.phar |
| 43 | + standard: phpcs.xml |
67 | 44 | ``` |
68 | 45 |
|
69 | | -See the [toolkit documentation](https://github.com/actions/toolkit/blob/master/README.md#packages) for the various packages. |
70 | | - |
71 | | -## Publish to a distribution branch |
72 | | - |
73 | | -Actions are run from GitHub repos so we will checkin the packed dist folder. |
74 | | - |
75 | | -Then run [ncc](https://github.com/zeit/ncc) and push the results: |
76 | | -```bash |
77 | | -$ npm run pack |
78 | | -$ git add dist |
79 | | -$ git commit -a -m "prod dependencies" |
80 | | -$ git push origin releases/v1 |
81 | | -``` |
82 | | - |
83 | | -Your action is now published! :rocket: |
84 | | - |
85 | | -See the [versioning documentation](https://github.com/actions/toolkit/blob/master/docs/action-versioning.md) |
86 | | - |
87 | | -## Validate |
88 | | - |
89 | | -You can now validate the action by referencing `./` in a workflow in your repo (see [test.yml](.github/workflows/test.yml)]) |
90 | | - |
91 | | -```yaml |
92 | | -uses: ./ |
93 | | -with: |
94 | | - milliseconds: 1000 |
95 | | -``` |
96 | | -
|
97 | | -See the [actions tab](https://github.com/actions/javascript-action/actions) for runs of this action! :rocket: |
98 | | -
|
99 | | -## Usage: |
100 | | -
|
101 | | -After testing you can [create a v1 tag](https://github.com/actions/toolkit/blob/master/docs/action-versioning.md) to reference the stable and latest V1 action |
| 46 | +You also will need either to pick a build code style standard or create `phpcs.xml` file (like [this](https://github.com/woocommerce/woocommerce/blob/master/phpcs.xml) for example). |
| 47 | +You may find all available configuration options at [action.yml](action.yml) in this repository. |
| 48 | +Example of this action running over small change in big legacy codebase: https://github.com/tinovyatkin/woocommerce/pull/1/checks?check_run_id=642017335 |
0 commit comments