Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 26 additions & 5 deletions scripts/commands/rule_validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
codelens rule-validate scripts/rules/python_security.yaml
codelens rule-validate --strict scripts/rules/*.yaml
codelens rule-validate --json scripts/rules/python_security.yaml
codelens rule-validate scripts/rules/ # validate every rule file in a directory
"""

from __future__ import annotations
Expand Down Expand Up @@ -46,7 +47,7 @@
parser.add_argument(
"rule_path",
nargs="+",
help="Path(s) to rule YAML file(s) to validate (one or more)",
help="Path(s) to rule YAML file(s) to validate, or directory(ies) of rule files",
)
parser.add_argument(
"--strict",
Expand Down Expand Up @@ -135,7 +136,7 @@
return json.dumps(payload, indent=2)


def execute(args, workspace):

Check failure on line 139 in scripts/commands/rule_validate.py

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Refactor this function to reduce its Cognitive Complexity from 25 to the 15 allowed.

See more on https://sonarcloud.io/project/issues?id=Wolfvin_CodeLens&issues=AZ8Xw3su22uNeXUduRVQ&open=AZ8Xw3su22uNeXUduRVQ&pullRequest=103
"""Execute the rule-validate command.

Returns a dict (so the result flows through the standard CodeLens
Expand All @@ -152,16 +153,36 @@
``output`` string (human or JSON).
"""
# Expand and deduplicate paths. ``args.rule_path`` is a list (nargs="+").
# Each entry may be either a single rule file OR a directory of rule files
# (issue #97): when a directory is given, enumerate the ``*.yaml``/``*.yml``
# rule files inside it rather than trying to ``open()`` the directory
# itself — ``read_text()`` on a directory raises ``IsADirectoryError`` on
# Linux/macOS and ``PermissionError`` on Windows.
paths: List[Path] = []
seen: set = set()
for raw in args.rule_path:
# Expand ``~`` and resolve to absolute. We don't follow symlinks
# here — a missing file is reported as a validation error below.
p = Path(os.path.expanduser(raw)).resolve()
if p in seen:
continue
seen.add(p)
paths.append(p)

if p.is_dir():
# Directory → enumerate rule files inside it (recursive, matching
# rule-test's behavior). Skip ``.test.yaml``/``.test.yml`` (test
# fixtures with a different schema) and hidden/dotfiles.
for entry in sorted(p.rglob("*.y*ml")):
if entry.name.endswith((".test.yaml", ".test.yml")):
continue
if entry.name.startswith("."):
continue
if entry in seen:
continue
seen.add(entry)
paths.append(entry)
else:
if p in seen:
continue
seen.add(p)
paths.append(p)

# Validate each path. Missing files produce a single-error result
# rather than crashing — the validator's ``_parse_yaml`` already
Expand Down
Loading