Skip to content

Commit 7bc42d5

Browse files
committed
[CI] pre-commit: auto create Markdown hook docs with Python
1 parent 867c93d commit 7bc42d5

File tree

5 files changed

+157
-0
lines changed

5 files changed

+157
-0
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#!/usr/bin/env python3
2+
3+
import yaml
4+
5+
6+
def generate_pre_commit_table(yaml_path):
7+
"""
8+
Generates a Markdown table from a pre-commit-config.yaml file.
9+
"""
10+
try:
11+
with open(yaml_path) as f:
12+
config = yaml.safe_load(f)
13+
except FileNotFoundError:
14+
return f"Error: The file '{yaml_path}' was not found."
15+
except yaml.YAMLError as e:
16+
return f"Error parsing YAML file: {e}"
17+
18+
table_header = "| Hook ID | Language | Name | Description | Version |\n"
19+
table_separator = "|---|---|---|---|---|\n"
20+
table_rows = []
21+
22+
for repo in config.get("repos", []):
23+
version = repo.get("rev", "N/A")
24+
url = repo.get("repo", "N/A")
25+
for hook in repo.get("hooks", []):
26+
hook_id = hook.get("id", "N/A")
27+
name = hook.get("name", "N/A")
28+
description = hook.get("description", "N/A")
29+
language = hook.get("language", "N/A")
30+
# args = ", ".join(hook.get("args", [])) if hook.get("args") else "N/A"
31+
if url not in ["local", "meta"]:
32+
entry = f"[{hook_id}]({url})"
33+
else:
34+
entry = f"{hook_id}"
35+
36+
table_rows.append(f"| {entry} | {language} | {name} | {description} | {version} |\n")
37+
38+
return table_header + table_separator + "".join(table_rows)
39+
40+
41+
def create_markdown_file(target_file_path, content_to_append):
42+
"""
43+
Creates a Markdown file with the content.
44+
"""
45+
try:
46+
with open(target_file_path, "w+") as f:
47+
f.seek(0)
48+
f.write("# pre-commit hook documentation\n\n")
49+
f.write(content_to_append)
50+
return f"File content successfully created at '{target_file_path}'."
51+
except OSError as e:
52+
return f"Error creating file: {e}"
53+
54+
55+
if __name__ == "__main__":
56+
57+
pre_commit_yaml_path = (
58+
".pre-commit-config.yaml" # Assuming this file is in the same directory
59+
)
60+
output_markdown_path = "doc/guides/pre-commit-hooks.md"
61+
62+
# Generate the Markdown table
63+
markdown_table = generate_pre_commit_table(pre_commit_yaml_path)
64+
65+
# Add the table to the target Markdown file
66+
if "Error" not in markdown_table:
67+
result = create_markdown_file(output_markdown_path, markdown_table)
68+
print(result)
69+
else:
70+
print(markdown_table)

.pre-commit-config.yaml

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,14 @@ repos:
1919
description: check hooks apply to the repository
2020
- repo: local
2121
hooks:
22+
- id: create-pre-commit-docs
23+
name: create pre-commit docs
24+
description: creates a Markdown file with information on the pre-commit hooks
25+
entry: .github/workflows/scripts/create_pre_commit_docs.py
26+
language: python
27+
additional_dependencies:
28+
- pyyaml
29+
require_serial: true
2230
- id: prettier
2331
name: run prettier
2432
description: format files with prettier
@@ -43,29 +51,67 @@ repos:
4351
rev: v5.0.0
4452
hooks:
4553
- id: check-added-large-files
54+
name: check added large files
55+
description: Prevent giant files from being committed
4656
- id: check-case-conflict
57+
name: check case conflict
58+
description: Check for files with names that would conflict on a case-insensitive filesystem like MacOS HFS+ or Windows FAT
4759
- id: check-executables-have-shebangs
60+
name: check executables have shebangs
61+
description: Checks that non-binary executables have a proper shebang
4862
exclude: ^test/t/lang\.rb$
4963
- id: check-illegal-windows-names
64+
name: check-illegal-windows-names
65+
description: Check for files that cannot be created on Windows
5066
- id: pretty-format-json
67+
name: pretty format json
68+
description: Checks that all your JSON files are pretty
5169
args: [--autofix, --no-sort-keys]
5270
- id: check-json
71+
name: check json
72+
description: Attempts to load all json files to verify syntax
5373
- id: check-merge-conflict
74+
name: check merge conflict
75+
description: Check for files that contain merge conflict strings
5476
- id: check-shebang-scripts-are-executable
77+
name: check shebang scripts are executable
78+
description: Checks that scripts with shebangs are executable
5579
- id: check-vcs-permalinks
80+
name: check vcs permalinks
81+
description: Ensures that links to vcs websites are permalinks
5682
- id: check-yaml
83+
name: check yaml
84+
description: Attempts to load all yaml files to verify syntax
5785
- id: destroyed-symlinks
86+
name: destroyed symlinks
87+
description: Check for debugger imports and py37+ breakpoint() calls in python source
5888
- id: detect-aws-credentials
89+
name: detect aws credentials
90+
description: Checks for the existence of AWS secrets that you have set up with the AWS CLI
5991
args: [--allow-missing-credentials]
6092
- id: detect-private-key
93+
name: detect private key
94+
description: Checks for the existence of private keys
6195
- id: end-of-file-fixer
96+
name: end of file fixer
97+
description: Makes sure files end in a newline and only a newline
6298
- id: file-contents-sorter
99+
name: file contents sorter
100+
description: Sort the lines in specified files (defaults to alphabetical)
63101
args: [--unique]
64102
files: ^\.github/linters/codespell\.txt$
65103
- id: fix-byte-order-marker
104+
name: fix byte order marker
105+
description: Removes UTF-8 byte order marker
66106
- id: forbid-submodules
107+
name: forbid submodules
108+
description: Prevent addition of new git submodules
67109
- id: mixed-line-ending
110+
name: mixed line ending
111+
description: Replaces or checks mixed line ending
68112
- id: trailing-whitespace
113+
name: trailing whitespace
114+
description: Trims trailing whitespace
69115
- repo: https://github.com/Lucas-C/pre-commit-hooks
70116
rev: v1.5.5
71117
hooks:

.prettierignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Ignore artifacts:
22
build
33
coverage
4+
doc/guides/pre-commit-hooks.md
45
doc/internal/opcode.md
56
tools/lrama

doc/guides/pre-commit-hooks.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# pre-commit hook documentation
2+
3+
| Hook ID | Language | Name | Description | Version |
4+
|---|---|---|---|---|
5+
| identity | N/A | run identity | check your identity | N/A |
6+
| check-hooks-apply | N/A | run check-hooks-apply | check hooks apply to the repository | N/A |
7+
| create-pre-commit-docs | python | create pre-commit docs | creates a Markdown file with information on the pre-commit hooks | N/A |
8+
| prettier | node | run prettier | format files with prettier | N/A |
9+
| [gitleaks](https://github.com/gitleaks/gitleaks) | N/A | run gitleaks | detect hardcoded secrets with gitleaks | v8.27.2 |
10+
| [oxipng](https://github.com/shssoichiro/oxipng) | N/A | run oxipng | use lossless compression to optimize PNG files | v9.1.5 |
11+
| [check-added-large-files](https://github.com/pre-commit/pre-commit-hooks) | N/A | check added large files | Prevent giant files from being committed | v5.0.0 |
12+
| [check-case-conflict](https://github.com/pre-commit/pre-commit-hooks) | N/A | check case conflict | Check for files with names that would conflict on a case-insensitive filesystem like MacOS HFS+ or Windows FAT | v5.0.0 |
13+
| [check-executables-have-shebangs](https://github.com/pre-commit/pre-commit-hooks) | N/A | check executables have shebangs | Checks that non-binary executables have a proper shebang | v5.0.0 |
14+
| [check-illegal-windows-names](https://github.com/pre-commit/pre-commit-hooks) | N/A | check-illegal-windows-names | Check for files that cannot be created on Windows | v5.0.0 |
15+
| [pretty-format-json](https://github.com/pre-commit/pre-commit-hooks) | N/A | pretty format json | Checks that all your JSON files are pretty | v5.0.0 |
16+
| [check-json](https://github.com/pre-commit/pre-commit-hooks) | N/A | check json | Attempts to load all json files to verify syntax | v5.0.0 |
17+
| [check-merge-conflict](https://github.com/pre-commit/pre-commit-hooks) | N/A | check merge conflict | Check for files that contain merge conflict strings | v5.0.0 |
18+
| [check-shebang-scripts-are-executable](https://github.com/pre-commit/pre-commit-hooks) | N/A | check shebang scripts are executable | Checks that scripts with shebangs are executable | v5.0.0 |
19+
| [check-vcs-permalinks](https://github.com/pre-commit/pre-commit-hooks) | N/A | check vcs permalinks | Ensures that links to vcs websites are permalinks | v5.0.0 |
20+
| [check-yaml](https://github.com/pre-commit/pre-commit-hooks) | N/A | check yaml | Attempts to load all yaml files to verify syntax | v5.0.0 |
21+
| [destroyed-symlinks](https://github.com/pre-commit/pre-commit-hooks) | N/A | destroyed symlinks | Check for debugger imports and py37+ breakpoint() calls in python source | v5.0.0 |
22+
| [detect-aws-credentials](https://github.com/pre-commit/pre-commit-hooks) | N/A | detect aws credentials | Checks for the existence of AWS secrets that you have set up with the AWS CLI | v5.0.0 |
23+
| [detect-private-key](https://github.com/pre-commit/pre-commit-hooks) | N/A | detect private key | Checks for the existence of private keys | v5.0.0 |
24+
| [end-of-file-fixer](https://github.com/pre-commit/pre-commit-hooks) | N/A | end of file fixer | Makes sure files end in a newline and only a newline | v5.0.0 |
25+
| [file-contents-sorter](https://github.com/pre-commit/pre-commit-hooks) | N/A | file contents sorter | Sort the lines in specified files (defaults to alphabetical) | v5.0.0 |
26+
| [fix-byte-order-marker](https://github.com/pre-commit/pre-commit-hooks) | N/A | fix byte order marker | Removes UTF-8 byte order marker | v5.0.0 |
27+
| [forbid-submodules](https://github.com/pre-commit/pre-commit-hooks) | N/A | forbid submodules | Prevent addition of new git submodules | v5.0.0 |
28+
| [mixed-line-ending](https://github.com/pre-commit/pre-commit-hooks) | N/A | mixed line ending | Replaces or checks mixed line ending | v5.0.0 |
29+
| [trailing-whitespace](https://github.com/pre-commit/pre-commit-hooks) | N/A | trailing whitespace | Trims trailing whitespace | v5.0.0 |
30+
| [forbid-tabs](https://github.com/Lucas-C/pre-commit-hooks) | N/A | run no-tabs checker | check the codebase for tabs | v1.5.5 |
31+
| [remove-tabs](https://github.com/Lucas-C/pre-commit-hooks) | N/A | run tabs remover | find and convert tabs to spaces | v1.5.5 |
32+
| [actionlint](https://github.com/rhysd/actionlint) | N/A | run actionlint | lint GitHub Actions workflow files | v1.7.7 |
33+
| [codespell](https://github.com/codespell-project/codespell) | N/A | run codespell | check spelling with codespell | v2.4.1 |
34+
| [markdownlint](https://github.com/igorshubovych/markdownlint-cli) | N/A | run markdownlint | checks the style of Markdown files | v0.45.0 |
35+
| [markdown-link-check](https://github.com/tcort/markdown-link-check) | N/A | run markdown-link-check | checks hyperlinks in Markdown files | v3.13.7 |
36+
| [rubocop](https://github.com/rubocop/rubocop) | N/A | run rubocop | RuboCop is a Ruby code style checker (linter) and formatter based on the community-driven Ruby Style Guide | v1.78.0 |
37+
| [shellcheck](https://github.com/shellcheck-py/shellcheck-py) | N/A | run shellcheck | check shell scripts with a static analysis tool | v0.10.0.1 |
38+
| [yamllint](https://github.com/adrienverge/yamllint) | N/A | run yamllint | check YAML files with yamllint | v1.37.1 |

requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pre-commit
2+
pyyaml

0 commit comments

Comments
 (0)