diff --git a/conf/jira.yaml.template b/conf/jira.yaml.template index 58bede08263..9c90993fe2d 100644 --- a/conf/jira.yaml.template +++ b/conf/jira.yaml.template @@ -6,3 +6,5 @@ JIRA: COMMENT_TYPE: group COMMENT_VISIBILITY: "Red Hat Employee" ENABLE_COMMENT: false + # Comment only if jira is in one of the following state + ISSUE_STATUS: ["Review", "Release Pending"] diff --git a/pytest_plugins/jira_comments.py b/pytest_plugins/jira_comments.py index c55f15199ef..9e8d56a6843 100644 --- a/pytest_plugins/jira_comments.py +++ b/pytest_plugins/jira_comments.py @@ -26,6 +26,7 @@ def pytest_addoption(parser): def pytest_configure(config): """Register jira_comments markers to avoid warnings.""" config.addinivalue_line('markers', 'jira_comments: Add test result comment on Jira issue.') + pytest.jira_comments = config.getoption('jira_comments') def update_issue_to_tests_map(item, marker, test_result): @@ -44,9 +45,9 @@ def update_issue_to_tests_map(item, marker, test_result): def pytest_runtest_makereport(item, call): """Create jira issue to test result mapping. Used for commenting result on Jira.""" outcome = yield - verifies_marker = item.get_closest_marker('verifies_issues', False) - blocked_by_marker = item.get_closest_marker('blocked_by', False) - enable_jira_comments = item.config.getoption('jira_comments', False) + 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') if ( settings.jira.enable_comment and enable_jira_comments diff --git a/robottelo/config/validators.py b/robottelo/config/validators.py index cef502fd724..8a726faa80a 100644 --- a/robottelo/config/validators.py +++ b/robottelo/config/validators.py @@ -198,6 +198,7 @@ Validator('jira.comment_type', default="group"), Validator('jira.comment_visibility', default="Red Hat Employee"), Validator('jira.enable_comment', default=False), + Validator('jira.issue_status', default=["Review", "Release Pending"]), ], ldap=[ Validator( diff --git a/robottelo/utils/issue_handlers/jira.py b/robottelo/utils/issue_handlers/jira.py index 7c05df9647c..6823e021cff 100644 --- a/robottelo/utils/issue_handlers/jira.py +++ b/robottelo/utils/issue_handlers/jira.py @@ -6,7 +6,6 @@ import requests from tenacity import retry, stop_after_attempt, wait_fixed -from conftest import option from robottelo.config import settings from robottelo.constants import ( JIRA_CLOSED_STATUSES, @@ -293,23 +292,30 @@ 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 != option.jira_comments: + if settings.jira.enable_comment != 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"' ) return None - logger.debug(f"Adding a new comment on {issue_id} Jira issue.") - response = requests.post( - f"{settings.jira.url}/rest/api/latest/issue/{issue_id}/comment", - json={ - "body": comment, - "visibility": { - "type": comment_type, - "value": comment_visibility, + data = try_from_cache(issue_id) + if data["status"] in settings.jira.issue_status: + logger.debug(f"Adding a new comment on {issue_id} Jira issue.") + response = requests.post( + f"{settings.jira.url}/rest/api/latest/issue/{issue_id}/comment", + json={ + "body": comment, + "visibility": { + "type": comment_type, + "value": comment_visibility, + }, }, - }, - headers={"Authorization": f"Bearer {settings.jira.api_key}"}, + headers={"Authorization": f"Bearer {settings.jira.api_key}"}, + ) + response.raise_for_status() + return response.json() + logger.warning( + f"Jira comments are currently disabled for this issue because it's in {data['status']} state. " + f"Please update issue_status in jira.conf to overide this behaviour." ) - response.raise_for_status() - return response.json() + return None