Skip to content

Commit

Permalink
Add support for overriding jira issues to comment on.
Browse files Browse the repository at this point in the history
  • Loading branch information
jameerpathan111 committed Jun 19, 2024
1 parent 8752ca9 commit 68de3d1
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 23 deletions.
33 changes: 21 additions & 12 deletions pytest_plugins/jira_comments.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand All @@ -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)
Expand All @@ -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':
Expand All @@ -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):
Expand Down
11 changes: 1 addition & 10 deletions pytest_plugins/metadata_markers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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(
Expand Down
11 changes: 11 additions & 0 deletions robottelo/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion robottelo/utils/issue_handlers/jira.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 '
Expand Down

0 comments on commit 68de3d1

Please sign in to comment.