-
-
Notifications
You must be signed in to change notification settings - Fork 5
488 lines (444 loc) · 19.5 KB
/
autofix.yaml
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
---
name: Autofix
"on":
workflow_call:
inputs:
gitignore-location:
description: 'File path of the .gitignore to update, relative to the root of the repository.'
default: './.gitignore'
required: false
type: string
gitignore-extra-categories:
description: 'List of additional categories to add to .gitignore file.'
required: false
type: string
gitignore-extra-content:
description: 'Additional content to append at the end of the generated .gitignore file.'
required: false
type: string
push:
branches:
- main
# Defaults sets in workflow_call.inputs or workflow_dispatch.inputs are not propagated to other events.
# We have to manually manage them: https://github.com/orgs/community/discussions/39357#discussioncomment-7500641
env:
gitignore-location: >
${{ inputs.gitignore-location == null && './.gitignore' || inputs.gitignore-location }}
concurrency:
# Group workflow jobs so new commits cancels in-progress execution triggered by previous commits.
# Source: https://mail.python.org/archives/list/pypa-committers@python.org/thread/PCBCQMJF64JGRBOX7E2EE4YLKHT4DI55/
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.sha }}
cancel-in-progress: true
jobs:
project-metadata:
name: Project metadata
runs-on: ubuntu-24.04
outputs:
python_files: ${{ steps.project-metadata.outputs.python_files }}
doc_files: ${{ steps.project-metadata.outputs.doc_files }}
is_python_project: ${{ steps.project-metadata.outputs.is_python_project }}
blacken_docs_params: ${{ steps.project-metadata.outputs.blacken_docs_params }}
ruff_py_version: ${{ steps.project-metadata.outputs.ruff_py_version }}
steps:
- uses: actions/checkout@v4.2.2
with:
# Checkout pull request HEAD commit to ignore actions/checkout's merge commit. Fallback to push SHA.
ref: ${{ github.event.pull_request.head.sha || github.sha }}
# We're going to browse all new commits.
fetch-depth: 0
- name: Hack setup-python cache
id: setup_python_hack
# XXX Create an empty pyproject.toml if this file (or any *requirements.txt) can't be found in repository.
# This hack fix an issue with setup-python, which ends up with this error in non-Python repos:
#
# Run actions/setup-python@v5.3.0
# with:
# python-version: 3.12
# cache: pip
# cache-dependency-path: |
# **/pyproject.toml
# requirements/*.txt
# Installed versions
# Successfully set up CPython (3.12.1)
# Error: No file in /home/runner/work/awesome-iam/awesome-iam matched to
# [requirements/*.txt or **/pyproject.toml], make sure you have checked out the target repository
#
# This has been reported at: https://github.com/actions/setup-python/issues/807
# In the future this might be addressed by: https://github.com/actions/setup-python/pull/762
# or https://github.com/actions/setup-python/issues/751
if: hashFiles('**/pyproject.toml', '*requirements.txt', 'requirements/*.txt') == ''
run: |
touch ./pyproject.toml
echo "tmp_deps_file=true" >> "$GITHUB_OUTPUT"
- uses: actions/setup-python@v5.3.0
with:
python-version: "3.12"
cache: "pip"
cache-dependency-path: |
**/pyproject.toml
*requirements.txt
requirements/*.txt
- name: Remove setup-python hack
if: steps.setup_python_hack.outputs.tmp_deps_file
run: |
rm ./pyproject.toml
- name: Install uv
run: |
python -m pip install -r https://raw.githubusercontent.com/kdeldycke/workflows/main/requirements/uv.txt
- name: Install gha-utils
run: >
uv tool install --with-requirements
https://raw.githubusercontent.com/kdeldycke/workflows/main/requirements/gha-utils.txt gha-utils
- name: Project metadata
id: project-metadata
env:
GITHUB_CONTEXT: ${{ toJSON(github) }}
run: |
gha-utils --verbosity DEBUG metadata --overwrite "$GITHUB_OUTPUT"
format-python:
name: Format Python
needs:
- project-metadata
if: needs.project-metadata.outputs.python_files || needs.project-metadata.outputs.doc_files
runs-on: ubuntu-24.04
steps:
- uses: actions/checkout@v4.2.2
- name: Hack setup-python cache
id: setup_python_hack
# XXX Create an empty pyproject.toml if this file (or any *requirements.txt) can't be found in repository.
# This hack fix an issue with setup-python, which ends up with this error in non-Python repos:
#
# Run actions/setup-python@v5.3.0
# with:
# python-version: 3.12
# cache: pip
# cache-dependency-path: |
# **/pyproject.toml
# requirements/*.txt
# Installed versions
# Successfully set up CPython (3.12.1)
# Error: No file in /home/runner/work/awesome-iam/awesome-iam matched to
# [requirements/*.txt or **/pyproject.toml], make sure you have checked out the target repository
#
# This has been reported at: https://github.com/actions/setup-python/issues/807
# In the future this might be addressed by: https://github.com/actions/setup-python/pull/762
# or https://github.com/actions/setup-python/issues/751
if: hashFiles('**/pyproject.toml', '*requirements.txt', 'requirements/*.txt') == ''
run: |
touch ./pyproject.toml
echo "tmp_deps_file=true" >> "$GITHUB_OUTPUT"
- uses: actions/setup-python@v5.3.0
with:
python-version: "3.12"
cache: "pip"
cache-dependency-path: |
**/pyproject.toml
*requirements.txt
requirements/*.txt
- name: Install uv
run: |
python -m pip install -r https://raw.githubusercontent.com/kdeldycke/workflows/main/requirements/uv.txt
- name: Install Ruff, blacken-docs and autopep8
run: |
uv tool install --with-requirements \
https://raw.githubusercontent.com/kdeldycke/workflows/main/requirements/ruff.txt ruff
uv tool install --with-requirements \
https://raw.githubusercontent.com/kdeldycke/workflows/main/requirements/blacken-docs.txt blacken-docs
uv tool install --with-requirements \
https://raw.githubusercontent.com/kdeldycke/workflows/main/requirements/autopep8.txt autopep8
- name: Run autopep8
if: needs.project-metadata.outputs.python_files
# Ruff is not wrapping comments: https://github.com/astral-sh/ruff/issues/7414
# We use autopep8 to only wrap long-line comments:
# - E501 is "Try to make lines fit within --max-line-length characters."
# - --aggressive is requires to force autopep8 to consider comments.
# Explicit list of files is provided, as autopep8 is not able to handle find files in ".github" subdirectory.
run: >
autopep8 --recursive --in-place --max-line-length 88 --select E501 --aggressive
${{ needs.project-metadata.outputs.python_files }}
- name: Optional Ruff config
if: needs.project-metadata.outputs.ruff_py_version
run: |
cat > ./ruff.toml <<EOF
target-version = "${{ needs.project-metadata.outputs.ruff_py_version }}"
EOF
- name: Generate Ruff config
run: |
tee -a ./ruff.toml <<-EOF
# Extend the pyproject.toml file of the project.
extend = "./pyproject.toml"
# Enable preview mode; checks will include unstable rules and fixes.
# See: https://astral.sh/blog/ruff-v0.1.0#introducing-preview-mode
preview = true
fix = true
# Include fixes that may not retain the original intent of the code.
# See: https://docs.astral.sh/ruff/linter/#fix-safety
unsafe-fixes = true
# Enumerate all fixed violations.
show-fixes = true
output-format = "github"
[lint]
# D400 - First line should end with a period.
# Allows docstrings to end up with any punctuation, not just a period.
# See: https://github.com/astral-sh/ruff/issues/1858#issuecomment-1382640623
# ERA001 - Found commented-out code.
# Do not remove commented code, as it might be used for documentation.
# See: https://docs.astral.sh/ruff/rules/#eradicate-era
ignore = ["D400", "ERA001"]
[format]
# Enable reformatting of code snippets in docstrings.
# https://docs.astral.sh/ruff/formatter/#docstring-formatting
docstring-code-format = true
EOF
- name: Print Ruff config
run: |
cat ./ruff.toml
- name: Run Ruff check
run: |
ruff check --config ./ruff.toml
- name: Run Ruff format
# XXX: Ruff is planning to support linting and formatting in one unified command at one point.
# See: https://github.com/astral-sh/ruff/issues/8232
run: |
ruff format --config ./ruff.toml
- name: Remove temporary Ruff config
run: |
rm ./ruff.toml
- name: Remove setup-python hack
if: steps.setup_python_hack.outputs.tmp_deps_file
run: |
rm ./pyproject.toml
- name: Run blacken-docs
# Ignore failing command: blacken-docs returns 1 if it finds a file that needs to be reformatted:
# https://github.com/adamchainz/blacken-docs/blob/79ef671/blacken_docs.py#L207-L211
# TODO: replace blacken-docs by ruff. See: https://github.com/astral-sh/ruff/issues/8237
# https://github.com/astral-sh/ruff/issues/3792
run: >
blacken-docs
--line-length 88
${{ needs.project-metadata.outputs.blacken_docs_params }}
${{ needs.project-metadata.outputs.doc_files }}
|| true
- uses: peter-evans/create-pull-request@v7.0.5
with:
assignees: ${{ github.actor }}
commit-message: "[autofix] Format Python"
title: "[autofix] Format Python"
body: >
<details><summary><code>Workflow metadata</code></summary>
> [Auto-generated on run `#${{ github.run_id }}`](${{ github.event.repository.html_url }}/actions/runs/${{
github.run_id }}) by `${{ github.job }}` job from [`autofix.yaml`](${{ github.event.repository.html_url
}}/blob/${{ github.sha }}/.github/workflows/autofix.yaml) workflow.
</details>
labels: "🤖 ci"
branch: format-python
sync-uv-lock:
# XXX Dependabot does not support uv.lock files yet, so this job is doing it.
# See: https://github.com/astral-sh/uv/issues/2512
name: Sync uv.lock
needs:
- project-metadata
if: fromJSON(needs.project-metadata.outputs.is_python_project)
runs-on: ubuntu-24.04
steps:
- uses: actions/checkout@v4.2.2
- uses: actions/setup-python@v5.3.0
with:
python-version: "3.12"
cache: "pip"
cache-dependency-path: |
**/pyproject.toml
*requirements.txt
requirements/*.txt
- name: Install uv
run: |
python -m pip install -r https://raw.githubusercontent.com/kdeldycke/workflows/main/requirements/uv.txt
- name: Sync uv.lock
run: |
uv --no-progress sync --upgrade
- uses: peter-evans/create-pull-request@v7.0.5
with:
assignees: ${{ github.actor }}
commit-message: "[autofix] Sync uv.lock"
title: "[autofix] Sync `uv.lock`"
body: >
<details><summary><code>Workflow metadata</code></summary>
> [Auto-generated on run `#${{ github.run_id }}`](${{ github.event.repository.html_url }}/actions/runs/${{
github.run_id }}) by `${{ github.job }}` job from [`docs.yaml`](${{ github.event.repository.html_url
}}/blob/${{ github.sha }}/.github/workflows/docs.yaml) workflow.
</details>
labels: "📦 dependencies"
branch: sync-uv-lock
format-markdown:
name: Format Markdown
runs-on: ubuntu-24.04
steps:
- uses: actions/checkout@v4.2.2
- name: Hack setup-python cache
id: setup_python_hack
# XXX Create an empty pyproject.toml if this file (or any *requirements.txt) can't be found in repository.
# This hack fix an issue with setup-python, which ends up with this error in non-Python repos:
#
# Run actions/setup-python@v5.3.0
# with:
# python-version: 3.12
# cache: pip
# cache-dependency-path: |
# **/pyproject.toml
# requirements/*.txt
# Installed versions
# Successfully set up CPython (3.12.1)
# Error: No file in /home/runner/work/awesome-iam/awesome-iam matched to
# [requirements/*.txt or **/pyproject.toml], make sure you have checked out the target repository
#
# This has been reported at: https://github.com/actions/setup-python/issues/807
# In the future this might be addressed by: https://github.com/actions/setup-python/pull/762
# or https://github.com/actions/setup-python/issues/751
if: hashFiles('**/pyproject.toml', '*requirements.txt', 'requirements/*.txt') == ''
run: |
touch ./pyproject.toml
echo "tmp_deps_file=true" >> "$GITHUB_OUTPUT"
- uses: actions/setup-python@v5.3.0
with:
python-version: "3.12"
cache: "pip"
cache-dependency-path: |
**/pyproject.toml
*requirements.txt
requirements/*.txt
- name: Remove setup-python hack
if: steps.setup_python_hack.outputs.tmp_deps_file
run: |
rm ./pyproject.toml
- name: Install uv
run: |
python -m pip install -r https://raw.githubusercontent.com/kdeldycke/workflows/main/requirements/uv.txt
- name: Install mdformat
run: >
uv tool install --with-requirements
https://raw.githubusercontent.com/kdeldycke/workflows/main/requirements/mdformat.txt mdformat
- name: Install shfmt
run: |
sudo apt install --yes shfmt
- name: mdformat --version
run: |
mdformat --version
- name: Auto-format Markdown
run: |
find ./ -iname "*.md" -exec mdformat "{}" \;
- name: Markdown fixes for Awesome Lists
if: startsWith(github.event.repository.name, 'awesome-')
# Remove forbidden TOC entries
# See: https://github.com/sindresorhus/awesome-lint/blob/v0.18.0/rules/toc.js#L15-L18
# Also remove the title of the section containing the TOC (i.e. "Contents") to fix the following error:
# ✖ 26:1 ToC item "Contents" does not match corresponding heading "Meta" remark-lint:awesome-toc
#
# TODO: contribute these fixes to mdformat-toc as configurable options.
run: >
find ./ -type f \( -name 'readme.md' -or -name 'readme.*.md' \) -print
-exec gawk -i inplace '!/^- \[(Contents|Contributing|Footnotes)\]\(#.+\)$/{print}' "{}" \;
- uses: peter-evans/create-pull-request@v7.0.5
with:
assignees: ${{ github.actor }}
commit-message: "[autofix] Format Markdown"
title: "[autofix] Format Markdown"
body: >
<details><summary><code>Workflow metadata</code></summary>
> [Auto-generated on run `#${{ github.run_id }}`](${{ github.event.repository.html_url }}/actions/runs/${{
github.run_id }}) by `${{ github.job }}` job from [`autofix.yaml`](${{ github.event.repository.html_url
}}/blob/${{ github.sha }}/.github/workflows/autofix.yaml) workflow.
</details>
labels: "📚 documentation"
branch: format-markdown
format-json:
name: Format JSON
runs-on: ubuntu-24.04
steps:
- uses: actions/checkout@v4.2.2
- name: Install jsonlint
run: |
sudo npm install --global jsonlint
- name: Lint
run: |
find ./ -type f -name '*.json' -print -exec jsonlint --in-place "{}" \;
- uses: peter-evans/create-pull-request@v7.0.5
with:
assignees: ${{ github.actor }}
commit-message: "[autofix] Format JSON"
title: "[autofix] Format JSON"
body: >
<details><summary><code>Workflow metadata</code></summary>
> [Auto-generated on run `#${{ github.run_id }}`](${{ github.event.repository.html_url }}/actions/runs/${{
github.run_id }}) by `${{ github.job }}` job from [`autofix.yaml`](${{ github.event.repository.html_url
}}/blob/${{ github.sha }}/.github/workflows/autofix.yaml) workflow.
</details>
labels: "🤖 ci"
branch: format-json
check-gitignore:
name: Does .gitignore exist?
runs-on: ubuntu-24.04
outputs:
exists: ${{ steps.detection.outputs.exists }}
steps:
- uses: actions/checkout@v4.2.2
- id: detection
# Bare-called reused workflow are not fed with defaults, so force it here.
run: |
echo "exists=$( [[ -f '${{ env.gitignore-location }}' ]] && echo 'true' )" >> "$GITHUB_OUTPUT"
- name: Detection results
run: |
echo "Does .gitignore exist at root? ${{ steps.detection.outputs.exists && true || false }}"
update-gitignore:
name: Update .gitignore
needs:
- check-gitignore
# Only update gitignore if a file is found at the root of repository.
if: needs.check-gitignore.outputs.exists
runs-on: ubuntu-24.04
steps:
- uses: actions/checkout@v4.2.2
- name: Install git-extras
run: |
sudo apt update
sudo apt install --yes git-extras
- name: Fetch category definitions
# Update the list manually so the first call below will not introduce these extra log messages:
# -----Initial gitignore.io list----
# -----Save to /home/runner/.gi_list-----
run: |
git ignore-io --update-list
- name: Generate .gitignore
run: >
git ignore-io ${{ inputs.gitignore-extra-categories }}
certificates
emacs
git
gpg
linux
macos
nohup
python
ssh
vim
virtualenv
visualstudiocode
windows > ${{ env.gitignore-location }}
- name: Append extra content to .gitignore
if: inputs.gitignore-extra-content
run: |
tee -a ${{ env.gitignore-location }} <<-EOF
${{ inputs.gitignore-extra-content }}
EOF
- uses: peter-evans/create-pull-request@v7.0.5
with:
assignees: ${{ github.actor }}
commit-message: "[autofix] Update .gitignore"
title: "[autofix] Update `.gitignore`"
body: >
<details><summary><code>Workflow metadata</code></summary>
> [Auto-generated on run `#${{ github.run_id }}`](${{ github.event.repository.html_url }}/actions/runs/${{
github.run_id }}) by `${{ github.job }}` job from [`autofix.yaml`](${{ github.event.repository.html_url
}}/blob/${{ github.sha }}/.github/workflows/autofix.yaml) workflow.
</details>
labels: "🤖 ci"
branch: update-gitignore