Skip to content

Commit 0260876

Browse files
authored
Add automatic README command help update via GitHub Actions (#2574)
* feat: add GitHub Actions workflow to update README command help * fix: skip README checks in pre-commit configuration
1 parent 42d6852 commit 0260876

5 files changed

Lines changed: 65 additions & 11 deletions

File tree

.github/workflows/readme.yaml

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
name: Update README
2+
3+
on:
4+
push:
5+
branches: [main]
6+
paths:
7+
- 'src/datamodel_code_generator/__main__.py'
8+
pull_request:
9+
branches: [main]
10+
paths:
11+
- 'src/datamodel_code_generator/__main__.py'
12+
13+
permissions:
14+
contents: write
15+
16+
jobs:
17+
update-readme:
18+
if: github.event_name == 'push' || !github.event.pull_request.head.repo.fork
19+
runs-on: ubuntu-latest
20+
steps:
21+
- uses: actions/checkout@v4
22+
with:
23+
fetch-depth: 0
24+
ref: ${{ github.head_ref || github.ref }}
25+
token: ${{ secrets.GITHUB_TOKEN }}
26+
- name: Install the latest version of uv
27+
uses: astral-sh/setup-uv@v5
28+
- name: Install tox
29+
run: uv tool install --python-preference only-managed --python 3.13 tox --with tox-uv
30+
- name: Setup environment
31+
run: tox run -vv --notest --skip-missing-interpreters false -e readme
32+
env:
33+
UV_PYTHON_PREFERENCE: "only-managed"
34+
- name: Update README
35+
run: .tox/readme/bin/python scripts/update_command_help_on_markdown.py
36+
- name: Commit and push if changed
37+
run: |
38+
git config user.name "github-actions[bot]"
39+
git config user.email "github-actions[bot]@users.noreply.github.com"
40+
git add README.md docs/index.md
41+
git diff --staged --quiet || git commit -m "docs: update command help in README
42+
43+
🤖 Generated by GitHub Actions"
44+
git push

.github/workflows/test.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,6 @@ jobs:
127127
- dev
128128
- docs
129129
- pkg_meta
130-
- readme
131130
steps:
132131
- uses: actions/checkout@v4
133132
with:

.pre-commit-config.yaml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
ci:
2+
skip: [readme]
3+
14
repos:
25
- repo: https://github.com/python-jsonschema/check-jsonschema
36
rev: 0.35.0
@@ -29,3 +32,11 @@ repos:
2932
additional_dependencies:
3033
- tomli
3134
exclude: "^tests/|^CODE_OF_CONDUCT.md"
35+
- repo: local
36+
hooks:
37+
- id: readme
38+
name: Update README command help
39+
entry: bash -c 'test -x .tox/fix/bin/python || tox run -e fix --notest -qq; .tox/fix/bin/python scripts/update_command_help_on_markdown.py'
40+
language: system
41+
files: ^(src/datamodel_code_generator/__main__\.py|README\.md|docs/index\.md)$
42+
pass_filenames: false

scripts/update_command_help_on_markdown.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,21 +64,22 @@ def main() -> Exit:
6464
"""Update or validate command help in target markdown files."""
6565
help_text = get_help()
6666
arg_parser.add_argument(
67-
"--validate",
67+
"--check",
6868
action="store_true",
69-
help="Validate the file content is up to date",
69+
help="Check if the file content is up to date without modifying",
7070
)
7171
args = arg_parser.parse_args()
72-
validate: bool = args.validate
72+
check: bool = args.check
7373

7474
for file_path in TARGET_MARKDOWN_FILES:
7575
with file_path.open("r") as f:
7676
markdown_text = f.read()
7777
new_markdown_text = inject_help(markdown_text, help_text)
78-
if validate and new_markdown_text != markdown_text:
79-
return Exit.ERROR
80-
with file_path.open("w") as f:
81-
f.write(new_markdown_text)
78+
if new_markdown_text != markdown_text:
79+
if check:
80+
return Exit.ERROR
81+
with file_path.open("w") as f:
82+
f.write(new_markdown_text)
8283
return Exit.OK
8384

8485

tox.ini

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,14 @@ dependency_groups =
5656

5757
[testenv:fix]
5858
description = format the code base to adhere to our styles, and complain about what we cannot do automatically
59-
skip_install = true
6059
commands =
6160
pre-commit run --all-files --show-diff-on-failure
6261
dependency_groups = fix
6362

6463
[testenv:readme]
65-
description = Update help in readme
64+
description = Check help in readme is up to date
6665
commands =
67-
python scripts/update_command_help_on_markdown.py {posargs:--validate}
66+
python scripts/update_command_help_on_markdown.py {posargs:--check}
6867
dependency_groups =
6968
no_default_groups = true
7069

0 commit comments

Comments
 (0)