Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to use use sub-rule matches on skip or warn lists #2251

Merged
merged 4 commits into from
Jul 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .ansible-lint
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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