From 68de3d18ab58f142703eee4b656ef1808171657d Mon Sep 17 00:00:00 2001 From: Jameer Pathan <21165044+jameerpathan111@users.noreply.github.com> Date: Mon, 17 Jun 2024 14:25:26 +0200 Subject: [PATCH] Add support for overriding jira issues to comment on. --- pytest_plugins/jira_comments.py | 33 ++++++++++++++++---------- pytest_plugins/metadata_markers.py | 11 +-------- robottelo/utils/__init__.py | 11 +++++++++ robottelo/utils/issue_handlers/jira.py | 2 +- 4 files changed, 34 insertions(+), 23 deletions(-) diff --git a/pytest_plugins/jira_comments.py b/pytest_plugins/jira_comments.py index 9e8d56a6843..ccc07670b56 100644 --- a/pytest_plugins/jira_comments.py +++ b/pytest_plugins/jira_comments.py @@ -5,19 +5,23 @@ from robottelo.config import settings from robottelo.logging import logger +from robottelo.utils import parse_comma_separated_list from robottelo.utils.issue_handlers.jira import add_comment_on_jira def pytest_addoption(parser): """Add --jira-comments option to report test results on the Jira issue.""" help_comment = ( - 'Report/Comment test results on Jira issues. ' - 'Test results marked with "Verifies" or "BlockedBy" doc fields will be commented on the corresponding Jira issues. ' + 'Report/Comment test results on Jira issues.\n' + 'Results for tests marked with "Verifies" or "BlockedBy" doc fields will be commented on the corresponding Jira issues. ' + 'This behaviour can be overriden by providing a comma separated list of jira issue ids.\n' 'Note: To prevent accidental use, users must set ENABLE_COMMENT to true in the jira.yaml configuration file.' ) parser.addoption( '--jira-comments', - action='store_true', + type=parse_comma_separated_list, + nargs='?', + const=True, default=False, help=help_comment, ) @@ -29,16 +33,15 @@ def pytest_configure(config): pytest.jira_comments = config.getoption('jira_comments') -def update_issue_to_tests_map(item, marker, test_result): +def update_issue_to_tests_map(item, issues, test_result): """If the test has Verifies or BlockedBy doc field, an issue to tests mapping will be added/updated in config.issue_to_tests_map for each test run with outcome of the test. """ - if marker: - for issue in marker.args[0]: - item.config.issue_to_tests_map[issue].append( - {'nodeid': item.nodeid, 'outcome': test_result} - ) + for issue in issues: + item.config.issue_to_tests_map[issue].append( + {'nodeid': item.nodeid, 'outcome': test_result} + ) @pytest.hookimpl(trylast=True, hookwrapper=True) @@ -48,10 +51,16 @@ def pytest_runtest_makereport(item, call): verifies_marker = item.get_closest_marker('verifies_issues') blocked_by_marker = item.get_closest_marker('blocked_by') enable_jira_comments = item.config.getoption('jira_comments') + verifies_issues = verifies_marker.args[0] if verifies_marker else [] + blocked_by_issues = blocked_by_marker.args[0] if blocked_by_marker else [] + # Override jira issues to report/comment on. + if isinstance(enable_jira_comments, list): + verifies_issues = enable_jira_comments + blocked_by_issues = [] if ( settings.jira.enable_comment and enable_jira_comments - and (verifies_marker or blocked_by_marker) + and (verifies_issues or blocked_by_issues) ): report = outcome.get_result() if report.when == 'teardown': @@ -67,9 +76,9 @@ def pytest_runtest_makereport(item, call): if not hasattr(item.config, 'issue_to_tests_map'): item.config.issue_to_tests_map = defaultdict(list) # Update issue_to_tests_map for Verifies testimony marker - update_issue_to_tests_map(item, verifies_marker, test_result) + update_issue_to_tests_map(item, verifies_issues, test_result) # Update issue_to_tests_map for BlockedBy testimony marker - update_issue_to_tests_map(item, blocked_by_marker, test_result) + update_issue_to_tests_map(item, blocked_by_issues, test_result) def pytest_sessionfinish(session, exitstatus): diff --git a/pytest_plugins/metadata_markers.py b/pytest_plugins/metadata_markers.py index d61105d9924..e141d45a1ad 100644 --- a/pytest_plugins/metadata_markers.py +++ b/pytest_plugins/metadata_markers.py @@ -7,6 +7,7 @@ from robottelo.config import settings from robottelo.hosts import get_sat_rhel_version from robottelo.logging import collection_logger as logger +from robottelo.utils import parse_comma_separated_list from robottelo.utils.issue_handlers.jira import are_any_jira_open FMT_XUNIT_TIME = '%Y-%m-%dT%H:%M:%S' @@ -15,16 +16,6 @@ deselected = [] -def parse_comma_separated_list(option_value): - if isinstance(option_value, str): - if option_value.lower() == 'true': - return True - if option_value.lower() == 'false': - return False - return [item.strip() for item in option_value.split(',')] - return None - - def pytest_addoption(parser): """Add CLI options related to Testimony token based mark collection""" parser.addoption( diff --git a/robottelo/utils/__init__.py b/robottelo/utils/__init__.py index 0cf4020ca5a..6f83afdbfbd 100644 --- a/robottelo/utils/__init__.py +++ b/robottelo/utils/__init__.py @@ -55,3 +55,14 @@ def slugify_component(string, keep_hyphens=True): if not keep_hyphens: string = string.replace('-', '_') return re.sub("[^-_a-zA-Z0-9]", "", string.lower()) + + +def parse_comma_separated_list(option_value): + """Mainly used for parsing pytest option values.""" + if isinstance(option_value, str): + if option_value.lower() == 'true': + return True + if option_value.lower() == 'false': + return False + return [item.strip() for item in option_value.split(',')] + return None diff --git a/robottelo/utils/issue_handlers/jira.py b/robottelo/utils/issue_handlers/jira.py index 0a03a538952..eed7e27cf1a 100644 --- a/robottelo/utils/issue_handlers/jira.py +++ b/robottelo/utils/issue_handlers/jira.py @@ -292,7 +292,7 @@ def add_comment_on_jira( [list of dicts] -- [{'id':..., 'status':..., 'resolution': ...}] """ # Raise a warning if any of the following option is not set. Note: It's a xor condition. - if settings.jira.enable_comment != pytest.jira_comments: + if settings.jira.enable_comment != bool(pytest.jira_comments): logger.warning( 'Jira comments are currently disabled for this run. ' 'To enable it, please set "enable_comment" to "true" in "config/jira.yaml '