-
Notifications
You must be signed in to change notification settings - Fork 2
563 lines (496 loc) · 17.6 KB
/
__call-common-lint.yml
File metadata and controls
563 lines (496 loc) · 17.6 KB
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
---
# This workflow will lint the code in the repository using various tools. Most linting tools in LizardByte
# should be included in this workflow; however, there are cases where that is not true, such as with eslint.
name: common lint (called)
permissions: {}
on:
pull_request:
workflow_call:
inputs:
actionlint_config:
required: false
type: string
check_trailing_space_ignore_pattern:
required: false
type: string
default: ""
runner:
required: false
type: string
default: '["ubuntu-latest"]'
jobs:
lint:
name: Common Lint
permissions:
contents: read
pull-requests: read
runs-on: ${{ (inputs && inputs.runner && fromJson(inputs.runner)) || 'ubuntu-latest' }}
env:
CLANG_FORMAT_VERSION: 20
steps:
- name: Checkout
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
- name: Download problem matchers
shell: bash
working-directory: .github
run: |
mkdir -p matchers
cd matchers
if [ "${{ github.repository }}" = "LizardByte/.github" ]; then
# use the version from the same ref
ref="${{ github.ref }}"
else
ref="master"
fi
gh_base_url="https://raw.githubusercontent.com/LizardByte/.github"
gh_path="${gh_base_url}/${ref}/.github/matchers"
declare -A files=(
[actionlint]="https://raw.githubusercontent.com/rhysd/actionlint/main/.github/actionlint-matcher.json"
[clang-format]="${gh_path}/clang-format.json"
[cmake-lint]="${gh_path}/cmake-lint.json"
[gcc]="${gh_path}/gcc.json"
[hadolint]="${gh_path}/hadolint.json"
[shellcheck-gcc]="${gh_path}/shellcheck-gcc.json"
[yamllint]="${gh_path}/yamllint.json"
)
for name in "${!files[@]}"; do
if [ ! -f "${name}.json" ]; then
url="${files[$name]}"
echo "Downloading ${name}.json from ${url}"
curl \
-fsSL \
--retry 3 \
"$url" \
-o "${name}.json"
else
echo "Skipping download of ${name}.json, already exists"
continue
fi
done
- name: Set up Python
uses: actions/setup-python@a309ff8b426b58ec0e2a45f0f869d46889d02405 # v6.2.0
with:
python-version: '3.14'
- name: Install Python dependencies
shell: bash
run: |
# shellcheck disable=SC2102 # this is triggered by the [toolchain] extra
python -m pip install --upgrade \
"clang-format==${CLANG_FORMAT_VERSION}.*" \
pip \
setuptools \
wheel \
cmakelang \
flake8 \
flake8-github-annotations \
nb-clean \
nbqa[toolchain] \
yamllint
- name: Install actionlint
id: get_actionlint
shell: bash
env:
ACTIONLINT_CONFIG: ${{ inputs.actionlint_config }}
run: |
bash <(curl https://raw.githubusercontent.com/rhysd/actionlint/main/scripts/download-actionlint.bash)
if [ -n "${ACTIONLINT_CONFIG}" ]; then
mkdir -p .github
printf "%s" "${ACTIONLINT_CONFIG}" > .github/actionlint.yml
elif [ ! -f ".github/actionlint.yml" ]; then
url="https://raw.githubusercontent.com/LizardByte/.github/master/.github/actionlint.yml"
echo "Downloading ${url} with curl"
curl \
-fsS \
--retry 3 \
-o ".github/actionlint.yml" \
${url}
fi
- name: Replace shell
# for actionlint
shell: bash
run: |
# Replace in workflow files
find .github/workflows -type f -iname "*.yml" | while read -r file; do
sed -i -e 's/msys2 {0}/bash/g' -e 's/freebsd {0}/sh/g' "${file}"
done
# Replace in all action.yml files anywhere
find . -type f -iname "action.yml" | while read -r file; do
sed -i -e 's/msys2 {0}/bash/g' -e 's/freebsd {0}/sh/g' "${file}"
done
- name: actionlint
shell: bash
if: always()
run: |
echo "::add-matcher::.github/matchers/actionlint.json"
set +e
error=0
${{ steps.get_actionlint.outputs.executable }} -color
error=$?
set -e
echo "::remove-matcher owner=actionlint::"
exit ${error}
- name: check-trailing-spaces
if: always()
uses: LizardByte/actions/actions/trailing_spaces@4125866b7b655a6fe038b0e22a43a4c5d259af79 # v2026.417.35446
with:
ignore_patterns: ${{ inputs.check_trailing_space_ignore_pattern }}
- name: C++ - find files
id: cpp_files
if: always()
shell: bash
run: |
# find files
found_files=$(find . -type f \
-iname "*.c" -o \
-iname "*.cpp" -o \
-iname "*.h" -o \
-iname "*.hpp" -o \
-iname "*.m" -o \
-iname "*.mm" \
)
ignore_files=$(find . -type f -iname ".clang-format-ignore")
# Loop through each C++ file
for file in $found_files; do
for ignore_file in $ignore_files; do
ignore_directory=$(dirname "$ignore_file")
# if directory of ignore_file is beginning of file
if [[ "$file" == "$ignore_directory"* ]]; then
echo "ignoring file: ${file}"
found_files="${found_files//${file}/}"
break 1
fi
done
done
# remove empty lines
found_files=$(echo "$found_files" | sed '/^\s*$/d')
echo "found cpp files: ${found_files}"
# shellcheck disable=SC2086 # do not quote to keep this as a single line
echo found_files=${found_files} >> "${GITHUB_OUTPUT}"
- name: C++ - Update Clang format config
if: always() && steps.cpp_files.outputs.found_files
shell: bash
run: |
echo "LineEnding: LF" >> .clang-format
- name: C++ - Clang format (diff)
id: clang_format_diff
if: always() && steps.cpp_files.outputs.found_files
uses: DoozyX/clang-format-lint-action@bcb4eb2cb0d707ee4f3e5cc3b456eb075f12cf73 # v0.20
with:
source: ${{ steps.cpp_files.outputs.found_files }}
clangFormatVersion: '${{ env.CLANG_FORMAT_VERSION }}'
extensions: 'c,cpp,h,hpp,m,mm'
style: file
inplace: false
- name: C++ - Clang format (simple)
if: always() && steps.clang_format_diff.outcome == 'failure'
shell: bash
run: |
echo "::add-matcher::.github/matchers/clang-format.json"
set +e
error=0
clang-format \
--dry-run \
--style=file \
--Werror \
${{ steps.cpp_files.outputs.found_files }}
error=$?
set -e
echo "::remove-matcher owner=clang-format::"
exit ${error}
- name: CMake - find files
id: cmake_files
if: always()
shell: bash
run: |
# find files
found_files=$(find . -type f -iname "CMakeLists.txt" -o -iname "*.cmake")
ignore_files=$(find . -type f -iname ".cmake-lint-ignore")
# Loop through each C++ file
for file in $found_files; do
for ignore_file in $ignore_files; do
ignore_directory=$(dirname "$ignore_file")
# if directory of ignore_file is beginning of file
if [[ "$file" == "$ignore_directory"* ]]; then
echo "ignoring file: ${file}"
found_files="${found_files//${file}/}"
break 1
fi
done
done
# remove empty lines
found_files=$(echo "$found_files" | sed '/^\s*$/d')
echo "found cmake files: ${found_files}"
# shellcheck disable=SC2086 # do not quote to keep this as a single line
echo found_files=${found_files} >> "${GITHUB_OUTPUT}"
- name: CMake - cmake-lint
if: always() && steps.cmake_files.outputs.found_files
shell: bash
run: |
echo "::add-matcher::.github/matchers/cmake-lint.json"
set +e
error=0
cmake-lint --line-width 120 --tab-size 4 ${{ steps.cmake_files.outputs.found_files }}
error=$?
set -e
echo "::remove-matcher owner=cmake-lint::"
exit ${error}
- name: Docker - find files
id: docker_files
if: always()
shell: bash
run: |
found_files=$(find . -type f -iname "Dockerfile" -o -iname "*.dockerfile")
echo "found_files: ${found_files}"
# shellcheck disable=SC2086 # do not quote to keep this as a single line
echo found_files=${found_files} >> "${GITHUB_OUTPUT}"
- name: Docker - hadolint
if: always() && steps.docker_files.outputs.found_files
shell: bash
run: |
docker pull hadolint/hadolint
# create hadolint config file
cat <<EOF > .hadolint.yaml
---
failure-threshold: style
format: tty
ignored:
- DL3008
- DL3013
- DL3016
- DL3018
- DL3028
- DL3059
no-color: true
no-fail: false
strict-labels: true
EOF
error=0
echo "::add-matcher::.github/matchers/hadolint.json"
set +e
for file in ${{ steps.docker_files.outputs.found_files }}; do
echo "file: ${file}"
output=$(docker run --rm -i \
-e "NO_COLOR=0" \
-e "HADOLINT_VERBOSE=0" \
-v "$(pwd)"/.hadolint.yaml:/.config/hadolint.yaml \
hadolint/hadolint < "${file}" 2>&1)
status=$?
if [ ${status} -ne 0 ]; then
error=1
echo "${output}"
fi
done
set -e
echo "::remove-matcher owner=hadolint::"
exit ${error}
- name: PowerShell - PSScriptAnalyzer
if: always()
shell: pwsh
run: |
# PSScriptAnalyzer is already installed on GitHub runners
if ($env:RUNNER_NAME -notlike 'GitHub Actions*') {
$repo = Get-PSRepository -Name PSGallery -ErrorAction SilentlyContinue
if (-not $repo) {
Register-PSRepository -Default -InstallationPolicy Trusted
} else {
Set-PSRepository -Name PSGallery -InstallationPolicy Trusted
}
Install-Module -Name PSScriptAnalyzer -Force
}
# To see a list of available rules, run the following command:
# Get-ScriptAnalyzerRule | Format-List
# Create PSScriptAnalyzer config file only if it doesn't exist
if (-not (Test-Path ./PSScriptAnalyzerSettings.psd1)) {
@'
@{
Severity=@(
'Error',
'Information',
'Warning'
)
ExcludeRules=@(
'PSAlignAssignmentStatement'
)
}
'@ | Out-File -FilePath ./PSScriptAnalyzerSettings.psd1
}
# Run PSScriptAnalyzer recursively on the whole repository
$results = Invoke-ScriptAnalyzer -Path "." -Recurse
Write-Host "::group::Analyzing PowerShell files"
if ($results) {
foreach ($result in $results) {
$file = $result.ScriptPath
$line = $result.Line
$title = "[$($result.Severity)] $($result.RuleName)"
$message = $result.Message
# https://docs.github.com/en/actions/reference/workflows-and-actions/workflow-commands#setting-an-error-message
Write-Output "::error file=$file,line=$line,title=$title::$message"
}
}
Write-Host "::endgroup::"
if ($results) {
$results | Format-Table -AutoSize
Write-Error "PSScriptAnalyzer found issues in PowerShell files"
exit 1
}
Write-Information "PSScriptAnalyzer found no issues in PowerShell files"
- name: Python - flake8
if: always()
shell: bash
run: |
echo "::group::problem matcher"
set +e
error=0
python -m flake8 \
--format github \
--verbose
error=$?
set -e
echo "::endgroup::"
# run flake8 again with human friendly output if there were errors
if [ $error -ne 0 ]; then
python -m flake8 \
--color=always \
--verbose
fi
- name: Python - nbqa flake8
if: always()
shell: bash
run: |
echo "::group::problem matcher"
set +e
error=0
python -m nbqa flake8 \
--format=github \
--verbose \
.
error=$?
set -e
echo "::endgroup::"
# run nbqa flake8 again with human friendly output if there were errors
if [ $error -ne 0 ]; then
python -m nbqa flake8 \
--color=always \
--verbose \
.
fi
- name: Python - nb-clean
if: always()
shell: bash
run: |
output=$(find . -name '*.ipynb' -exec nb-clean check {} \;)
# fail if there are any issues
if [ -n "$output" ]; then
echo "$output"
exit 1
fi
- name: Rust - find Cargo.toml
id: run_cargo
if: always()
shell: bash
run: |
# check if Cargo.toml exists
if [ -f "Cargo.toml" ]; then
echo "found_cargo=true" >> "${GITHUB_OUTPUT}"
else
echo "found_cargo=false" >> "${GITHUB_OUTPUT}"
fi
- name: Setup Rust
if: always() && steps.run_cargo.outputs.found_cargo == 'true'
uses: actions-rust-lang/setup-rust-toolchain@2b1f5e9b395427c92ee4e3331786ca3c37afe2d7 # v1.16.0
with:
components: 'rustfmt'
cache: false
matcher: false # disable the built-in problem matcher
toolchain: 'nightly'
- name: Rust - cargo fmt
if: always() && steps.run_cargo.outputs.found_cargo == 'true'
shell: bash
run: |
set +e
error=0
cargo +nightly fmt -- --check
error=$?
set -e
if [ $error -ne 0 ]; then
echo "::group::rustfmt github annotations"
# https://docs.github.com/en/actions/reference/workflows-and-actions/workflow-commands#setting-an-error-message
# each mismatch in mismatches has the following fields:
# - original_begin_line
# - original_end_line
# - expected_begin_line
# - expected_end_line
# - original
# - expected
# note: original and expected may have \n which does not render properly,
# so we will just tell the user to run `cargo +nightly fmt`
cargo +nightly fmt -- --emit=json | jq -r '
.[] as $file
| $file.mismatches[]?
| "::error "
+ "file=\($file.name),"
+ "line=\(.original_begin_line),"
+ "endLine=\(.original_end_line),"
+ "title=rustfmt mismatch"
+ "::Run `cargo +nightly fmt` to fix formatting issues"
'
echo "::endgroup::"
exit ${error}
fi
- name: shellcheck - find files
id: shellcheck_files
if: always()
shell: bash
run: |
found_files=$(find . -type f -iname "*.bash" -o -iname "*.sh")
echo "found_files: ${found_files}"
# shellcheck disable=SC2086 # do not quote to keep this as a single line
echo found_files=${found_files} >> "${GITHUB_OUTPUT}"
- name: shellcheck
if: always() && steps.shellcheck_files.outputs.found_files
shell: bash
run: |
echo "::add-matcher::.github/matchers/shellcheck-gcc.json"
set +e
error=0
shellcheck --format gcc ${{ steps.shellcheck_files.outputs.found_files }}
error=$?
set -e
echo "::remove-matcher owner=shellcheck-gcc::"
exit ${error}
- name: YAML - find files
id: yaml_files
if: always()
shell: bash
run: |
# space separated list of files
FILES=.clang-format
# empty placeholder
found_files=""
for FILE in ${FILES}; do
if [ -f "$FILE" ]
then
found_files="$found_files $FILE"
fi
done
echo "found_files=${found_files}" >> "${GITHUB_OUTPUT}"
- name: YAML - yamllint
id: yamllint
if: always()
shell: bash
run: |
if [ ! -f .yamllint.yml ]; then
curl -sS https://raw.githubusercontent.com/LizardByte/.github/master/.yamllint.yml -o .yamllint.yml
fi
echo "::add-matcher::.github/matchers/yamllint.json"
set +e
error=0
yamllint \
--config-file .yamllint.yml \
--format=standard \
--strict \
. ${{ steps.yaml_files.outputs.found_files }}
error=$?
set -e
echo "::remove-matcher owner=yamllint::"
exit ${error}