Skip to content

Commit b302351

Browse files
committed
Auto create Markdown based hook docs with pre-commit
Using a local hook and a Python script we can auto create a Markdown page with pre-commit hook docs.
1 parent 39126a4 commit b302351

File tree

4 files changed

+198
-0
lines changed

4 files changed

+198
-0
lines changed
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
#!/usr/bin/env python3
2+
3+
# Licensed to the Apache Software Foundation (ASF) under one
4+
# or more contributor license agreements. See the NOTICE file
5+
# distributed with this work for additional information
6+
# regarding copyright ownership. The ASF licenses this file
7+
# to you under the Apache License, Version 2.0 (the
8+
# "License"); you may not use this file except in compliance
9+
# with the License. You may obtain a copy of the License at
10+
#
11+
# http://www.apache.org/licenses/LICENSE-2.0
12+
#
13+
# Unless required by applicable law or agreed to in writing,
14+
# software distributed under the License is distributed on an
15+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16+
# KIND, either express or implied. See the License for the
17+
# specific language governing permissions and limitations
18+
# under the License.
19+
20+
import yaml
21+
22+
23+
def generate_pre_commit_table(yaml_path):
24+
"""
25+
Generates a Markdown table from a pre-commit-config.yaml file.
26+
"""
27+
try:
28+
with open(yaml_path) as f:
29+
config = yaml.safe_load(f)
30+
except FileNotFoundError:
31+
return f"Error: The file '{yaml_path}' was not found."
32+
except yaml.YAMLError as e:
33+
return f"Error parsing YAML file: {e}"
34+
35+
table_header = "| Hook ID | Language | Description | Version |\n"
36+
table_separator = "|---|---|---|---|\n"
37+
table_rows = []
38+
39+
for repo in config.get("repos", []):
40+
version = repo.get("rev", "N/A")
41+
url = repo.get("repo", "N/A")
42+
for hook in repo.get("hooks", []):
43+
hook_id = hook.get("id", "N/A")
44+
language = hook.get("language", "N/A")
45+
description = hook.get("description", "N/A")
46+
if description == "N/A":
47+
description = hook.get("name", "N/A")
48+
# args = ", ".join(hook.get("args", [])) if hook.get("args") else "N/A"
49+
50+
if url not in ["local", "meta"]:
51+
entry = f"[{hook_id}]({url})"
52+
else:
53+
entry = f"{hook_id}"
54+
55+
table_rows.append(f"| {entry} | {language} | {description} | {version} |\n")
56+
57+
return table_header + table_separator + "".join(table_rows)
58+
59+
60+
def create_markdown_file(target_file_path, content_to_append):
61+
"""
62+
Creates a Markdown file with the content.
63+
"""
64+
try:
65+
with open(target_file_path, "w+") as f:
66+
f.seek(0)
67+
f.write(
68+
"""<!--
69+
Licensed to the Apache Software Foundation (ASF) under one
70+
or more contributor license agreements. See the NOTICE file
71+
distributed with this work for additional information
72+
regarding copyright ownership. The ASF licenses this file
73+
to you under the Apache License, Version 2.0 (the
74+
"License"); you may not use this file except in compliance
75+
with the License. You may obtain a copy of the License at
76+
77+
http://www.apache.org/licenses/LICENSE-2.0
78+
79+
Unless required by applicable law or agreed to in writing,
80+
software distributed under the License is distributed on an
81+
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
82+
KIND, either express or implied. See the License for the
83+
specific language governing permissions and limitations
84+
under the License.
85+
-->
86+
87+
# pre-commit hook documentation
88+
89+
"""
90+
)
91+
f.write(content_to_append)
92+
return f"File content successfully created at '{target_file_path}'."
93+
except OSError as e:
94+
return f"Error creating file: {e}"
95+
96+
97+
if __name__ == "__main__":
98+
99+
pre_commit_yaml_path = (
100+
".pre-commit-config.yaml" # Assuming this file is in the same directory
101+
)
102+
output_markdown_path = "PRE_COMMIT_HOOK_DOCS.md"
103+
104+
# Generate the Markdown table
105+
markdown_table = generate_pre_commit_table(pre_commit_yaml_path)
106+
107+
# Add the table to the target Markdown file
108+
if "Error" not in markdown_table:
109+
result = create_markdown_file(output_markdown_path, markdown_table)
110+
print(result)
111+
else:
112+
print(markdown_table)

.pre-commit-config.yaml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,26 @@ repos:
2424
- repo: meta
2525
hooks:
2626
- id: identity
27+
description: a simple hook which prints all arguments passed to it, useful for debugging.
2728
- id: check-hooks-apply
29+
description: check that all the hooks apply to the repository
2830
- repo: https://github.com/thlorenz/doctoc.git
2931
rev: v2.2.0
3032
hooks:
3133
- id: doctoc
3234
name: Add TOC for Markdown files
35+
description: automatically keeps your table of contents up to date
3336
files: ^CONTRIBUTING\.md$|^INSTALL\.md$|^README\.md$
37+
- repo: local
38+
hooks:
39+
- id: create-pre-commit-docs
40+
name: create pre-commit docs
41+
description: creates a Markdown file with information on the pre-commit hooks
42+
entry: .github/workflows/scripts/create_pre_commit_docs.py
43+
language: python
44+
additional_dependencies:
45+
- pyyaml
46+
require_serial: true
3447
- repo: https://github.com/oxipng/oxipng
3548
rev: v9.1.5
3649
hooks:
@@ -49,11 +62,13 @@ repos:
4962
hooks:
5063
- id: chmod
5164
name: set file permissions
65+
description: manual hook to be run by macOS or Linux users for a full repository clean up
5266
args: ['644']
5367
files: \.md$
5468
stages: [manual]
5569
- id: insert-license
5670
name: add license for all Markdown files
71+
description: automatically adds a licence header to all Markdown files that don't have a license header
5772
files: \.md$
5873
args:
5974
- --comment-style
@@ -74,6 +89,7 @@ repos:
7489
- --fuzzy-match-generates-todo
7590
- id: insert-license
7691
name: add license for all SQL files
92+
description: automatically adds a licence header to all SQL files that don't have a license header
7793
files: \.sql$
7894
args:
7995
- --comment-style
@@ -96,18 +112,27 @@ repos:
96112
hooks:
97113
#- id: check-added-large-files
98114
- id: check-case-conflict
115+
description: check for case conflicts in file names
99116
#- id: check-executables-have-shebangs
100117
- id: check-illegal-windows-names
118+
description: check for Windows-illegal file names
101119
- id: check-merge-conflict
120+
description: check for merge conflict markers
102121
- id: check-shebang-scripts-are-executable
122+
description: check that scripts with shebangs are executable
103123
files: \.sh$
104124
- id: check-symlinks
125+
description: checks for symlinks which do not point to anything.
105126
- id: check-vcs-permalinks
127+
description: ensures that links to vcs websites are permalinks
106128
#- id: check-yaml
107129
- id: destroyed-symlinks
130+
description: detects symlinks which are changed to regular files with a content of a path which that symlink was pointing to
108131
- id: detect-aws-credentials
132+
description: checks for the existence of AWS secrets that you have set up with the AWS CLI
109133
args: [--allow-missing-credentials]
110134
- id: detect-private-key
135+
description: checks for the existence of private keys
111136
exclude: >
112137
(?x)
113138
^scripts/vm/systemvm/id_rsa\.cloud$|
@@ -124,14 +149,20 @@ repos:
124149
^systemvm/agent/certs/realhostip\.key$|
125150
^test/integration/smoke/test_ssl_offloading\.py$
126151
- id: end-of-file-fixer
152+
description: makes sure files end in a newline and only a newline
127153
exclude: \.vhd$|\.svg$
128154
- id: file-contents-sorter
155+
description: sort the lines in specified files (defaults to alphabetical)
129156
args: [--unique]
130157
files: ^\.github/linters/codespell\.txt$
131158
- id: fix-byte-order-marker
159+
description: removes UTF-8 byte order marker
132160
- id: forbid-submodules
161+
description: forbids any submodules in the repository
133162
- id: mixed-line-ending
163+
description: replaces or checks mixed line ending
134164
- id: trailing-whitespace
165+
description: trims trailing whitespace
135166
files: \.(bat|cfg|cs|css|gitignore|header|in|install|java|md|properties|py|rb|rc|sh|sql|te|template|txt|ucls|vue|xml|xsl|yaml|yml)$|^cloud-cli/bindir/cloud-tool$|^debian/changelog$
136167
args: [--markdown-linebreak-ext=md]
137168
exclude: ^services/console-proxy/rdpconsole/src/test/doc/freerdp-debug-log\.txt$
@@ -147,6 +178,7 @@ repos:
147178
rev: 7.0.0
148179
hooks:
149180
- id: flake8
181+
description: flake8 is a Python tool that glues together pycodestyle, pyflakes, mccabe, and third-party plugins to check the style and quality of some python code
150182
args: [--config, .github/linters/.flake8]
151183
- repo: https://github.com/igorshubovych/markdownlint-cli
152184
rev: v0.45.0

PRE_COMMIT_HOOK_DOCS.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<!--
2+
Licensed to the Apache Software Foundation (ASF) under one
3+
or more contributor license agreements. See the NOTICE file
4+
distributed with this work for additional information
5+
regarding copyright ownership. The ASF licenses this file
6+
to you under the Apache License, Version 2.0 (the
7+
"License"); you may not use this file except in compliance
8+
with the License. You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing,
13+
software distributed under the License is distributed on an
14+
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
KIND, either express or implied. See the License for the
16+
specific language governing permissions and limitations
17+
under the License.
18+
-->
19+
20+
# pre-commit hook documentation
21+
22+
| Hook ID | Language | Description | Version |
23+
|---|---|---|---|
24+
| identity | N/A | a simple hook which prints all arguments passed to it, useful for debugging. | N/A |
25+
| check-hooks-apply | N/A | check that all the hooks apply to the repository | N/A |
26+
| [doctoc](https://github.com/thlorenz/doctoc.git) | N/A | automatically keeps your table of contents up to date | v2.2.0 |
27+
| create-pre-commit-docs | python | creates a Markdown file with information on the pre-commit hooks | N/A |
28+
| [oxipng](https://github.com/oxipng/oxipng) | N/A | optimize PNG images with lossless compression | v9.1.5 |
29+
| [gitleaks](https://github.com/gitleaks/gitleaks) | N/A | detect hardcoded secrets | v8.27.2 |
30+
| [chmod](https://github.com/Lucas-C/pre-commit-hooks) | N/A | manual hook to be run by macOS or Linux users for a full repository clean up | v1.5.5 |
31+
| [insert-license](https://github.com/Lucas-C/pre-commit-hooks) | N/A | automatically adds a licence header to all Markdown files that don't have a license header | v1.5.5 |
32+
| [insert-license](https://github.com/Lucas-C/pre-commit-hooks) | N/A | automatically adds a licence header to all Shell files that don't have a license header | v1.5.5 |
33+
| [insert-license](https://github.com/Lucas-C/pre-commit-hooks) | N/A | automatically adds a licence header to all SQL files that don't have a license header | v1.5.5 |
34+
| [insert-license](https://github.com/Lucas-C/pre-commit-hooks) | N/A | automatically adds a licence header to all YAML files that don't have a license header | v1.5.5 |
35+
| [check-case-conflict](https://github.com/pre-commit/pre-commit-hooks) | N/A | check for case conflicts in file names | v6.0.0 |
36+
| [check-illegal-windows-names](https://github.com/pre-commit/pre-commit-hooks) | N/A | check for Windows-illegal file names | v6.0.0 |
37+
| [check-merge-conflict](https://github.com/pre-commit/pre-commit-hooks) | N/A | check for merge conflict markers | v6.0.0 |
38+
| [check-shebang-scripts-are-executable](https://github.com/pre-commit/pre-commit-hooks) | N/A | check that scripts with shebangs are executable | v6.0.0 |
39+
| [check-symlinks](https://github.com/pre-commit/pre-commit-hooks) | N/A | checks for symlinks which do not point to anything. | v6.0.0 |
40+
| [check-vcs-permalinks](https://github.com/pre-commit/pre-commit-hooks) | N/A | ensures that links to vcs websites are permalinks | v6.0.0 |
41+
| [destroyed-symlinks](https://github.com/pre-commit/pre-commit-hooks) | N/A | detects symlinks which are changed to regular files with a content of a path which that symlink was pointing to | v6.0.0 |
42+
| [detect-aws-credentials](https://github.com/pre-commit/pre-commit-hooks) | N/A | checks for the existence of AWS secrets that you have set up with the AWS CLI | v6.0.0 |
43+
| [detect-private-key](https://github.com/pre-commit/pre-commit-hooks) | N/A | checks for the existence of private keys | v6.0.0 |
44+
| [end-of-file-fixer](https://github.com/pre-commit/pre-commit-hooks) | N/A | makes sure files end in a newline and only a newline | v6.0.0 |
45+
| [file-contents-sorter](https://github.com/pre-commit/pre-commit-hooks) | N/A | sort the lines in specified files (defaults to alphabetical) | v6.0.0 |
46+
| [fix-byte-order-marker](https://github.com/pre-commit/pre-commit-hooks) | N/A | removes UTF-8 byte order marker | v6.0.0 |
47+
| [forbid-submodules](https://github.com/pre-commit/pre-commit-hooks) | N/A | forbids any submodules in the repository | v6.0.0 |
48+
| [mixed-line-ending](https://github.com/pre-commit/pre-commit-hooks) | N/A | replaces or checks mixed line ending | v6.0.0 |
49+
| [trailing-whitespace](https://github.com/pre-commit/pre-commit-hooks) | N/A | trims trailing whitespace | v6.0.0 |
50+
| [codespell](https://github.com/codespell-project/codespell) | N/A | Check spelling with codespell | v2.4.1 |
51+
| [flake8](https://github.com/pycqa/flake8) | N/A | flake8 is a Python tool that glues together pycodestyle, pyflakes, mccabe, and third-party plugins to check the style and quality of some python code | 7.0.0 |
52+
| [markdownlint](https://github.com/igorshubovych/markdownlint-cli) | N/A | check Markdown files with markdownlint | v0.45.0 |
53+
| [yamllint](https://github.com/adrienverge/yamllint) | N/A | check YAML files with yamllint | v1.37.1 |

requirements-dev.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@
1616
# under the License.
1717

1818
pre-commit
19+
pyyaml

0 commit comments

Comments
 (0)