-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaction.yml
184 lines (163 loc) · 7.94 KB
/
action.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
---
name: "PHP Code Beautifier and Fixer"
description: Sniff the PHP code and beautify it if necessary.
author:
name: "Stanislav Rejthar"
email: "rejthar@stanislavrejthar.com"
url: "https://github.com/WorkOfStan"
branding:
icon: "check-circle"
color: "blue"
inputs:
commit-changes:
description: "If true, commit changes to the current branch"
type: boolean
default: false
commit-message:
description: "Commit message for the changes"
type: string
default: "PHP Code Beautifier fixes applied automatically"
# https://github.com/PHPCSStandards/PHP_CodeSniffer/wiki/Advanced-Usage#specifying-valid-file-extensions
# By default, PHP_CodeSniffer will check any file it finds with a .inc, .php, .js or .css extension, although not all standards will actually check all these file types.
# Empty value means no checking at all.
# The default is `php` as js and css are handled by prettier.
# Btw: JS/CSS support will be removed from PHP_CodeSniffer in PHPCS 4.x. Source: https://phpcsutils.com/
extensions:
description: "Comma delimited list of extensions to be sniffed"
required: false
type: string
default: "php" # "php,inc,lib"
ignore:
description: "Ignore files based on a comma-separated list of patterns matching files and/or directories."
required: false
type: string
default: "vendor/"
php-version:
description: "PHP version to be used for running phpcs and phpcbf"
required: false
type: string
default: "8.2"
standard:
description: "The name of, or the path to, the coding standard to use. Can be a comma-separated list specifying multiple standards."
required: false
type: string
default: "" # if left empty, .github/linters/phpcs.xml or https://github.com/super-linter/super-linter/blob/main/TEMPLATES/phpcs.xml will be used
stop-on-manual-fix:
description: "If true, the execution will stop when manual fixes are necessary."
type: boolean
default: false
outputs:
branch-name:
description: "The branch where changes were committed"
runs:
using: "composite"
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
ref: ${{ github.ref }} # Fetch latest changes (even by previous job)
fetch-depth: 0 # Fetch all history for all branches and tags. Otherwise github.event.pull_request.base.ref SHA isn't found.
# Fetch latest changes (even by previous job)
- name: Fetch latest changes
run: git pull origin ${{ github.ref }}
shell: bash
- name: Set up PHP
uses: shivammathur/setup-php@v2
with:
php-version: "${{ inputs.php-version }}"
tools: composer:v2
# https://github.com/actions/cache
# A repository can have up to 10 GB of caches. Once the 10 GB limit is reached, older caches will be evicted based on when the cache was last accessed.
# Caches that are not accessed within the last week will also be evicted.
- name: Cache composer dependencies
uses: actions/cache@v4
id: vendor-cache
with:
# path to Checkout working directory is /home/runner/work/repo-name/repo-name , so just add /vendor/
path: ${{ github.workspace }}/vendor/
# Use composer.json for key, if composer.lock is not committed.
key: phpcs-fix-${{ runner.os }}-PHP${{ inputs.php-version }}-vendor-${{ hashFiles('**/composer.json') }}
- name: "Use the latest PHPCS version"
if: ${{ steps.vendor-cache.outputs.cache-hit != 'true' }}
run: composer require --dev squizlabs/php_codesniffer --prefer-dist --no-progress
shell: bash
- name: Run PHP Code Sniffer
id: phpcs
run: |
# Determine which coding standard to use.
# Priority:
# 1. Use inputs.standard if provided (non-empty).
# 2. If not, use the repository's own phpcs.xml if available.
# 3. Otherwise, fall back to the action's default template. Which is a copy of https://github.com/super-linter/super-linter/blob/main/TEMPLATES/phpcs.xml
if [ -n "${{ inputs.standard }}" ]; then
use_standard="${{ inputs.standard }}"
elif [ -f .github/linters/phpcs.xml ]; then
use_standard=".github/linters/phpcs.xml"
else
use_standard="$GITHUB_ACTION_PATH/.github/linters/super-linter-templates-phpcs.xml"
fi
echo "USE_STANDARD=$use_standard" >> "$GITHUB_ENV" # for subsequent steps
echo "phpcs standard=$use_standard"
vendor/bin/phpcs --extensions="${{ inputs.extensions }}" . --standard="$use_standard" --ignore="${{ inputs.ignore }}" || HAS_ISSUES=true
# because of `||`, the previous command always exit 0
# Default the variable if no issues were found
: "${HAS_ISSUES:=false}"
echo "HAS_ISSUES=$HAS_ISSUES" >> "$GITHUB_ENV"
shell: bash
- name: Run PHP Code Beautifier if needed
if: env.HAS_ISSUES == 'true'
run: |
echo "phpcbf standard=${{ env.USE_STANDARD }}"
PHPCBF_EXIT_CODE=0
MORE_ISSUES=false
vendor/bin/phpcbf --extensions="${{ inputs.extensions }}" . --standard="${{ env.USE_STANDARD }}" --ignore="${{ inputs.ignore }}" || PHPCBF_EXIT_CODE=$?
if [ ${PHPCBF_EXIT_CODE} -ne 0 ]; then
echo "::notice title=PHPCBF wasn't idle::Non-zero code exit of phpcbf = ${PHPCBF_EXIT_CODE} (phpcbf wasn't idle)"
vendor/bin/phpcs --extensions="${{ inputs.extensions }}" . --standard="${{ env.USE_STANDARD }}" --ignore="${{ inputs.ignore }}" || MORE_ISSUES=true
# because of `||`, the previous command always exit 0
if [ "${MORE_ISSUES}" = "true" ]; then
echo "::warning title=Some PHPCS issues remained.::Some phpcs issues remained. Manual fix necessary."
fi
fi
# restore composer.json before staging the changes
git checkout composer.json
# Check for fixable changes
if [ -z "$(git status --porcelain)" ]; then
echo "::warning title=No fixable errors were found by PHPCBF::No fixable errors were found by phpcbf: Manual fix necessary. Compare to the PHP Code Sniffer section above."
if [ "${{ inputs.stop-on-manual-fix }}" = "true" ]; then
# Indicates an error
exit 1
else
# Exiting gracefully
exit 0
fi
# The rest of the script must still be within the same step to really stop the execution
fi
git add .
# Determine branch name based on commit-changes input
if ${{ inputs.commit-changes }}; then
BRANCH_NAME=$(git branch --show-current)
NOTICE_MESSAGE="A PHPCBF commit was successfully added to the current branch: $BRANCH_NAME"
else
# name includes timestamp and short_hash
BRANCH_NAME="phpcbf/fix-$(date +'%y%m%d%H%M%S')-$(git rev-parse --short HEAD)"
git checkout -b "$BRANCH_NAME"
echo "::warning title=New branch created::New branch created: Consider pull request for a new branch: $BRANCH_NAME (or delete it)"
NOTICE_MESSAGE="A PHPCBF commit was successfully added to the new branch: $BRANCH_NAME"
fi
# Configure Git user
git config user.name "github-actions[bot]"
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
# Commit and push changes with custom message
git commit -m "${{ inputs.commit-message }} on $(date +'%Y-%m-%d %H:%M:%S') UTC"
git push origin "$BRANCH_NAME"
echo "::notice title=PHPCBF commit added::${NOTICE_MESSAGE}"
COMMIT_URL=${{ github.server_url }}/${{ github.repository }}/commit/$(git rev-parse HEAD)
echo "::notice title=View the PHPCBF commit::${COMMIT_URL}"
# Set branch name as output
echo "branch-name=$BRANCH_NAME" >> $GITHUB_OUTPUT
## Stop with an error if some manual fixes are required on top of automatic fixes
if [ "${{ inputs.stop-on-manual-fix }}" = "true" ] && [ "${MORE_ISSUES}" = "true" ]; then
exit 1
fi
shell: bash