Skip to content

Commit 413cb79

Browse files
x0rwPLeVasseur
andauthored
[Sphinx Extension] Add check for required guideline fields (#45)
* Add check for required guideline fields * Remove debugging output * Apply suggestions from code review Co-authored-by: Pete LeVasseur <[email protected]> * test: intentionally break build to verify CI failure this commit removes tags, decidability, scope fields from types-and-traits.rst * Revert temporary build failure for validation test --------- Co-authored-by: Pete LeVasseur <[email protected]>
1 parent 1fbab6f commit 413cb79

File tree

3 files changed

+54
-1
lines changed

3 files changed

+54
-1
lines changed

exts/coding_guidelines/__init__.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from . import write_guidelines_ids
66
from . import std_role
77
from . import fls_linking
8+
from . import guidelines_checks
89

910
from sphinx_needs.api import add_dynamic_function
1011
from sphinx.errors import SphinxError
@@ -15,7 +16,6 @@
1516
# Get the Sphinx logger
1617
logger = logging.getLogger('sphinx')
1718

18-
1919
class CodingGuidelinesDomain(Domain):
2020
name = "coding-guidelines"
2121
label = "Rust Standard Library"
@@ -52,9 +52,19 @@ def setup(app):
5252
app.add_config_value(name='enable_spec_lock_consistency',
5353
default=True,
5454
rebuild='env')
55+
app.add_config_value(
56+
name='required_guideline_fields',
57+
default=['release', 'fls', 'decidability', 'scope'],
58+
rebuild='env',
59+
types=[list],
60+
)
61+
62+
app.connect('env-check-consistency', guidelines_checks.validate_required_fields)
5563

5664
app.connect('env-check-consistency', fls_checks.check_fls)
65+
5766
app.connect('build-finished', write_guidelines_ids.build_finished)
67+
5868
app.connect('build-finished', fls_linking.build_finished)
5969

6070
return {
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# SPDX-License-Identifier: MIT OR Apache-2.0
2+
# SPDX-FileCopyrightText: The Coding Guidelines Subcommittee Contributors
3+
4+
from sphinx.errors import SphinxError
5+
from sphinx_needs.data import SphinxNeedsData
6+
import logging
7+
8+
logger = logging.getLogger('sphinx')
9+
10+
class IntegrityCheckError(SphinxError):
11+
category = "Integrity Check Error"
12+
13+
def validate_required_fields(app, env):
14+
"""
15+
Validate the required fields defined in conf.py
16+
"""
17+
logger.debug("Validating required fields")
18+
data = SphinxNeedsData(env)
19+
needs = data.get_needs_view()
20+
21+
required_fields = app.config.required_guideline_fields # Access the configured values
22+
23+
for key, value in needs.items():
24+
if value.get('type') == 'guideline':
25+
missing_fields = []
26+
for field in required_fields:
27+
if value.get(field) in (None, '', []):
28+
missing_fields.append(field)
29+
30+
if missing_fields:
31+
error_message = (
32+
f"Guideline '{value.get('title')}' (ID: {value.get('id')}) "
33+
f"in {value.get('docname')}:{value.get('lineno')} is missing the following required fields: "
34+
f"{', '.join(missing_fields)}"
35+
)
36+
logger.error(error_message)
37+
app.builder.statuscode = 1
38+
raise IntegrityCheckError(error_message)
39+
logger.info("No missing required field")

src/conf.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,10 @@
127127
# Enable needs export
128128
needs_extra_options = ["category", "recommendation", "fls", "decidability", "scope", "release"]
129129

130+
131+
# Required guideline fields
132+
required_guideline_fields = ['category', 'release', 'fls', 'decidability', 'scope', 'tags'] # Id is automatically generated
133+
130134
# -- Options for HTML output -------------------------------------------------
131135

132136
# Configure the theme

0 commit comments

Comments
 (0)