Skip to content

Commit 7f44c50

Browse files
authored
Merge pull request #61 from maykinmedia/feature/60-ci-check-envvar-help-texts
👷 [#60] Check if envvars have documentation or are excluded from docs
2 parents 5e2c7a4 + 2efd209 commit 7f44c50

File tree

3 files changed

+86
-2
lines changed

3 files changed

+86
-2
lines changed

.github/workflows/code_quality.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,14 @@ jobs:
3131
- run: tox
3232
env:
3333
TOXENV: ${{ matrix.toxenv }}
34+
35+
check-envvar-documentation:
36+
name: Check if environment variables are documented
37+
runs-on: ubuntu-latest
38+
steps:
39+
- uses: actions/checkout@v4
40+
- uses: actions/setup-python@v5
41+
with:
42+
python-version: '3.11'
43+
- name: Check environment variables
44+
run: python bin/check_envvar_usage.py

bin/check_envvar_usage.py

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
"""
2+
Script to check if open_api_framework has properly documented (or excluded) all environment
3+
variables that are loaded by the library itself
4+
"""
5+
6+
import ast
7+
import sys
8+
from pathlib import Path
9+
10+
11+
class ConfigChecker(ast.NodeVisitor):
12+
def __init__(self):
13+
self.issues = []
14+
15+
def visit_Call(self, node):
16+
if isinstance(node.func, ast.Name) and node.func.id == "config":
17+
has_help_text = False
18+
has_add_to_docs = False
19+
20+
# Check for 'help_text' or 'add_to_docs' in the arguments
21+
for keyword in node.keywords:
22+
if keyword.arg == "help_text" and isinstance(
23+
keyword.value, (ast.Constant, ast.JoinedStr)
24+
):
25+
has_help_text = True
26+
elif (
27+
keyword.arg == "add_to_docs"
28+
and isinstance(keyword.value, ast.Constant)
29+
and keyword.value.value is False
30+
):
31+
has_add_to_docs = True
32+
33+
# Record issue if neither is found
34+
if not (has_help_text or has_add_to_docs):
35+
self.issues.append((node.lineno, node.col_offset))
36+
37+
self.generic_visit(node)
38+
39+
40+
def check_config_usage(file_path):
41+
with file_path.open("r") as source:
42+
tree = ast.parse(source.read(), filename=str(file_path))
43+
44+
checker = ConfigChecker()
45+
checker.visit(tree)
46+
47+
return checker.issues
48+
49+
50+
def check_library(directory):
51+
issues = {}
52+
for file_path in directory.rglob("*.py"):
53+
issues_in_file = check_config_usage(file_path)
54+
if issues_in_file:
55+
issues[file_path] = issues_in_file
56+
57+
return issues
58+
59+
60+
# Example usage
61+
library_directory = Path("open_api_framework/")
62+
issues = check_library(library_directory)
63+
64+
if issues:
65+
for file_path, positions in issues.items():
66+
for lineno, col_offset in positions:
67+
print(
68+
f"Issue in {file_path} at line {lineno}, column {col_offset}: "
69+
"'config' call lacks 'help_text' or 'add_to_docs=False'"
70+
)
71+
sys.exit(1)
72+
else:
73+
print("All 'config' calls are correctly documented.")

open_api_framework/conf/base.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -849,7 +849,7 @@ def init_sentry(before_send: Callable | None = None):
849849
ELASTIC_APM_SERVER_URL = config(
850850
"ELASTIC_APM_SERVER_URL",
851851
None,
852-
"URL where Elastic APM is hosted",
852+
help_text="URL where Elastic APM is hosted",
853853
group="Elastic APM",
854854
)
855855
ELASTIC_APM = {
@@ -867,7 +867,7 @@ def init_sentry(before_send: Callable | None = None):
867867
"SECRET_TOKEN": config(
868868
"ELASTIC_APM_SECRET_TOKEN",
869869
"default",
870-
"Token used to communicate with Elastic APM",
870+
help_text="Token used to communicate with Elastic APM",
871871
group="Elastic APM",
872872
),
873873
"SERVER_URL": ELASTIC_APM_SERVER_URL,

0 commit comments

Comments
 (0)