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

Allow rules to request original raw task data #1834

Merged
merged 5 commits into from
Jan 29, 2022

Conversation

cognifloyd
Copy link
Contributor

@cognifloyd cognifloyd commented Jan 26, 2022

This allows a rule to opt-in to receiving a "raw" copy of the task (as it is before normalization) via task["__raw_task__"].
To use it, rules set needs_raw_task = True at the class level.

For one of the rules I'm developing, I need to inspect the raw (not normalized via ansible) task to identify the target issue.
This rule flags tasks that use action shorthand. normalize_task(), however, parses the shorthand and puts it in task['action'], so there's no good way to detect the action shorthand with the current matchtask() interface. There are a variety of awkward workarounds like parsing the playbooks myself, but I would really like to take advantage of all the work ansible-lint already does to find the relevant tasks and skip them as needed.
https://github.com/cognifloyd/ansible-lint/blob/fmt/src/ansiblelint/rules/TaskNoActionShorthand.py

For more discussion about this no-action-shorthand rule, see: #1609

The TaskNoActionShorthand rule will be extracted from #1805.

This is an alternative implementation to #1833 which was extracted from #1805. Only one of the PRs should be merged.

If `needs_raw_task` (a class level attribute) is `True`, then
the original task (before normalization) will be made available under
`task["__raw_task__"]`.
@cognifloyd cognifloyd requested a review from a team as a code owner January 26, 2022 03:44
@cognifloyd cognifloyd requested review from relrod, ganeshrn and priyamsahoo and removed request for a team January 26, 2022 03:44
@cognifloyd cognifloyd changed the title Add AnsibleLintRule.needs_raw_task optional property New Feature: Add AnsibleLintRule.needs_raw_task optional property Jan 26, 2022
@cognifloyd cognifloyd changed the title New Feature: Add AnsibleLintRule.needs_raw_task optional property New Feature: Add AnsibleLintRule.needs_raw_task optional property Jan 26, 2022
@cognifloyd cognifloyd changed the title New Feature: Add AnsibleLintRule.needs_raw_task optional property Add AnsibleLintRule.needs_raw_task optional property Jan 26, 2022
@cognifloyd cognifloyd changed the title Add AnsibleLintRule.needs_raw_task optional property Allow rules to use the raw (not normalized) task in matchtask() via task["__raw_task__"] (rules must opt-in to get it) Jan 26, 2022
@ssbarnea ssbarnea changed the title Allow rules to use the raw (not normalized) task in matchtask() via task["__raw_task__"] (rules must opt-in to get it) Allow rules to request original raw task data Jan 27, 2022
@ssbarnea
Copy link
Member

I like this approach more than the alternative but you need to also add a simple test for it, one that verifies that a rule that requests this does receive it. This will also address the problem of decreasing the code coverage.

I think we already have a custom rule inside tests that you may reuse for testing this, or you can add a new one.

We have not had prematch()/postmatch() in a long time.
Now test_rule_matching and test_rule_postmatching are
effectively duplicates of each other. So drop the
postmatching rule test.
Also adjust test counts now that I added another rule under test/rules.
@cognifloyd
Copy link
Contributor Author

K. I added a test Rule that uses this and adjusted the counts for RulesCollection since that uses the new rule now too. codecov is happier now.

@cognifloyd
Copy link
Contributor Author

The test errors are failures to upload codecov reports.

@ssbarnea
Copy link
Member

I seen it and did a recheck. If it happens again let's make the uploading optional. Even if we fail to upload half of the files we will likely see any coverage drop do no problems.

@cognifloyd
Copy link
Contributor Author

Tests are green :)

@ssbarnea ssbarnea merged commit ae80b1f into ansible:main Jan 29, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

None yet

2 participants