Skip to content

Commit

Permalink
Simultaneous regex filtering at decriptor and linter levels
Browse files Browse the repository at this point in the history
Fixes #2668
  • Loading branch information
nvuillam committed May 19, 2023
1 parent 1092b81 commit 2e13cee
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 22 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ Note: Can be used with `oxsecurity/megalinter@beta` in your GitHub Action mega-l
- Run stale workflow only on schedule, by @echoix in [#2641](https://github.com/oxsecurity/megalinter/pull/2641)
- Add explicit permissions to stale workflow, by @echoix in [#2641](https://github.com/oxsecurity/megalinter/pull/2641)
- Allow MEGALINTER_CONFIG to contain a full path to a MegaLinter config file
- Simultaneous regex filtering at decriptor and linter levels

- Documentation
- Apply many updates after the use of [Vale](https://vale.sh/) on MegaLinter own sources and docs
Expand Down
26 changes: 15 additions & 11 deletions megalinter/Linter.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,8 @@ def __init__(self, params=None, linter_config=None):
self.ignore_file_label = None
self.ignore_file_error = None
self.filter_regex_include = None
self.filter_regex_exclude = None
self.filter_regex_exclude_descriptor = None
self.filter_regex_exclude_linter = None
self.post_linter_status = (
params["post_linter_status"]
if "post_linter_status" in params
Expand Down Expand Up @@ -687,17 +688,16 @@ def load_config_vars(self, params):
== "true"
):
self.disable_errors = True
# Exclude regex: try first NAME + _FILTER_REGEX_EXCLUDE, then LANGUAGE + _FILTER_REGEX_EXCLUDE
# Exclude regex: descriptor level
if config.exists(self.request_id, self.descriptor_id + "_FILTER_REGEX_EXCLUDE"):
self.filter_regex_exclude_descriptor = config.get(
self.request_id, self.descriptor_id + "_FILTER_REGEX_EXCLUDE"
)
# Exclude regex: linter level
if config.exists(self.request_id, self.name + "_FILTER_REGEX_EXCLUDE"):
self.filter_regex_exclude = config.get(
self.filter_regex_exclude_linter = config.get(
self.request_id, self.name + "_FILTER_REGEX_EXCLUDE"
)
elif config.exists(
self.request_id, self.descriptor_id + "_FILTER_REGEX_EXCLUDE"
):
self.filter_regex_exclude = config.get(
self.request_id, self.descriptor_id + "_FILTER_REGEX_EXCLUDE"
)
# Override default docker image version
if config.exists(self.request_id, self.name + "_DOCKER_IMAGE_VERSION"):
self.cli_docker_image_version = config.get(
Expand Down Expand Up @@ -832,7 +832,8 @@ def log_file_filters(self):
log_object = {
"name": self.name,
"filter_regex_include": self.filter_regex_include,
"filter_regex_exclude": self.filter_regex_exclude,
"filter_regex_exclude_descriptor": self.filter_regex_exclude_descriptor,
"filter_regex_exclude_linter": self.filter_regex_exclude_linter,
"files_sub_directory": self.files_sub_directory,
"lint_all_files": self.lint_all_files,
"lint_all_other_linters_files": self.lint_all_other_linters_files,
Expand All @@ -851,7 +852,10 @@ def collect_files(self, all_files):
self.files = utils.filter_files(
all_files=all_files,
filter_regex_include=self.filter_regex_include,
filter_regex_exclude=self.filter_regex_exclude,
filter_regex_exclude=[
self.filter_regex_exclude_descriptor,
self.filter_regex_exclude_linter,
],
file_names_regex=self.file_names_regex,
file_extensions=self.file_extensions,
ignored_files=[],
Expand Down
2 changes: 1 addition & 1 deletion megalinter/MegaLinter.py
Original file line number Diff line number Diff line change
Expand Up @@ -703,7 +703,7 @@ def collect_files(self):
filtered_files = utils.filter_files(
all_files=all_files,
filter_regex_include=self.filter_regex_include,
filter_regex_exclude=self.filter_regex_exclude,
filter_regex_exclude=[self.filter_regex_exclude],
file_names_regex=self.file_names_regex,
file_extensions=self.file_extensions,
ignored_files=ignored_files,
Expand Down
4 changes: 2 additions & 2 deletions megalinter/tests/test_megalinter/filters_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def test_filter_files_with_ignored_files(self):
filtered_files = utils.filter_files(
all_files=all_files,
filter_regex_include=None,
filter_regex_exclude=None,
filter_regex_exclude=[None],
file_names_regex=[],
file_extensions=["", ".md", ".ext"],
ignored_files=ignored_files,
Expand All @@ -95,7 +95,7 @@ def test_filter_files_with_file_extensions(self):
filtered_files = utils.filter_files(
all_files=all_files,
filter_regex_include=None,
filter_regex_exclude=None,
filter_regex_exclude=[],
file_names_regex=[],
file_extensions=file_extensions,
ignored_files=[],
Expand Down
24 changes: 16 additions & 8 deletions megalinter/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def get_excluded_directories(request_id):
def filter_files(
all_files: Sequence[str],
filter_regex_include: Optional[str],
filter_regex_exclude: Optional[str],
filter_regex_exclude: Sequence[str],
file_names_regex: Sequence[str],
file_extensions: Any,
ignored_files: Optional[Sequence[str]],
Expand All @@ -108,9 +108,12 @@ def filter_files(
filter_regex_include_object = (
re.compile(filter_regex_include) if filter_regex_include else None
)
filter_regex_exclude_object = (
re.compile(filter_regex_exclude) if filter_regex_exclude else None
)
filter_regex_exclude_objects = []
for filter_regex_exclude_item in filter_regex_exclude:
filter_regex_exclude_object = (
re.compile(filter_regex_exclude_item) if filter_regex_exclude_item else None
)
filter_regex_exclude_objects += [filter_regex_exclude_object]
file_names_regex_object = re.compile("|".join(file_names_regex))
filtered_files = []
file_contains_regex_object = (
Expand Down Expand Up @@ -152,10 +155,15 @@ def filter_files(
file_with_workspace
):
continue
# Skip according to FILTER_REGEX_EXCLUDE
if filter_regex_exclude_object and filter_regex_exclude_object.search(
file_with_workspace
):
# Skip according to FILTER_REGEX_EXCLUDE list
excluded_by_regex = False
for filter_regex_exclude_object in filter_regex_exclude_objects:
if filter_regex_exclude_object and filter_regex_exclude_object.search(
file_with_workspace
):
excluded_by_regex = True
break
if excluded_by_regex is True:
continue

# Skip according to file extension (only if lint_all_other_linter_files is false or file_extensions is defined)
Expand Down

0 comments on commit 2e13cee

Please sign in to comment.