Skip to content

Commit

Permalink
Add ability to use use sub-rule matches on skip or warn lists (#2251)
Browse files Browse the repository at this point in the history
* Add ability to use use sub-rule matches on skip or warn lists

You can now add entries like `yaml[document-start]` to the skip_list
or warn_list, instead of adding the entire rule, allowing you to have
more granular filtering.

Closes: #2248
Closes: #1794

* Update src/ansiblelint/rules/yaml.md

Co-authored-by: Jacob Floyd <cognifloyd@gmail.com>

* Update .ansible-lint

Co-authored-by: Jacob Floyd <cognifloyd@gmail.com>

* Update src/ansiblelint/app.py

Co-authored-by: Jacob Floyd <cognifloyd@gmail.com>

Co-authored-by: Jacob Floyd <cognifloyd@gmail.com>
  • Loading branch information
ssbarnea and cognifloyd committed Jul 27, 2022
1 parent 0b744d8 commit 1a9c031
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
1 change: 1 addition & 0 deletions .ansible-lint
Expand Up @@ -60,6 +60,7 @@ warn_list:
- git-latest
- experimental # experimental is included in the implicit list
# - role-name
# - yaml[document-start] # you can also use sub-rule matches

# Some rules can transform files to fix (or make it easier to fix) identified
# errors. `ansible-lint --write` will reformat YAML files and run these transforms.
Expand Down
12 changes: 10 additions & 2 deletions src/ansiblelint/app.py
Expand Up @@ -104,7 +104,12 @@ def count_results(self, matches: List[MatchError]) -> SummarizedResults:
fixed_failures = 0
fixed_warnings = 0
for match in matches:
if {match.rule.id, *match.rule.tags}.isdisjoint(self.options.warn_list):
# tag can include a sub-rule id: `yaml[document-start]`
# rule.id is the generic rule id: `yaml`
# *rule.tags is the list of the rule's tags (categories): `style`
if {match.tag, match.rule.id, *match.rule.tags}.isdisjoint(
self.options.warn_list
):
if match.fixed:
fixed_failures += 1
else:
Expand All @@ -129,7 +134,10 @@ def _get_matched_skippable_rules(
) -> "Dict[str, BaseRule]":
"""Extract the list of matched rules, if skippable, from the list of matches."""
matches_unignored = [match for match in matches if not match.ignored]
matched_rules = {match.rule.id: match.rule for match in matches_unignored}
# match.tag is more specialized than match.rule.id
matched_rules = {
match.tag or match.rule.id: match.rule for match in matches_unignored
}
# remove unskippable rules from the list
for rule_id in list(matched_rules.keys()):
if "unskippable" in matched_rules[rule_id].tags:
Expand Down
10 changes: 7 additions & 3 deletions src/ansiblelint/rules/yaml.md
Expand Up @@ -7,9 +7,13 @@ ansible-lint, not from yamllint.

You can fully disable all yamllint violations by adding `yaml` to the `skip_list`.

Specific tag identifiers that are printed at the end of rule name,
like `yaml[trailing-spaces]` or `yaml[indentation]` can also be be skipped, allowing
you to have a more fine control.
Specific tag identifiers that are printed at the end of the rule name,
like `yaml[trailing-spaces]` or `yaml[indentation]` can also be skipped, allowing
you to have more control.

Keep in mind that `ansible-lint` does not take into consideration the warning level
of yamllint; we treat all yamllint matches as errors. So, if you want to treat
some of these as warnings, add them to `warn_list`.

### Problematic code

Expand Down

0 comments on commit 1a9c031

Please sign in to comment.