Skip to content

Commit

Permalink
Add comment based on issue status
Browse files Browse the repository at this point in the history
  • Loading branch information
jameerpathan111 committed May 30, 2024
1 parent 5ca6124 commit 5078ca7
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 27 deletions.
2 changes: 2 additions & 0 deletions conf/jira.yaml.template
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
9 changes: 0 additions & 9 deletions conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import pytest

option = None

pytest_plugins = [
# Plugins
'pytest_plugins.auto_vault',
Expand Down Expand Up @@ -88,10 +86,3 @@ def pytest_runtest_makereport(item, call):
# be "setup", "call", "teardown"

setattr(item, "report_" + report.when, report)


@pytest.hookimpl(trylast=True)
def pytest_configure(config):
"""Make cmdline arguments available."""
global option
option = config.option
7 changes: 4 additions & 3 deletions pytest_plugins/jira_comments.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions robottelo/config/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
37 changes: 22 additions & 15 deletions robottelo/utils/issue_handlers/jira.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -293,23 +292,31 @@ 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"'
'To enable it, please set "enable_comment" to "true" in "config/jira.yaml '
'and provide --jira-comment pytest option."'
)
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

0 comments on commit 5078ca7

Please sign in to comment.