Skip to content

Commit 3bd07dc

Browse files
author
Bryan Sieber
committed
add helper to bugzilla client for duplicated call
1 parent 5cf9a2e commit 3bd07dc

File tree

5 files changed

+23
-19
lines changed

5 files changed

+23
-19
lines changed

src/jbi/router.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from src.jbi import configuration
1515
from src.jbi.bugzilla import BugzillaBug, BugzillaWebhookRequest
1616
from src.jbi.errors import IgnoreInvalidRequestError
17-
from src.jbi.services import get_bugzilla
17+
from src.jbi.services import getbug_as_bugzilla_object
1818

1919
templates = Jinja2Templates(directory="src/templates")
2020

@@ -46,10 +46,8 @@ def execute_action(request: BugzillaWebhookRequest, action_map, settings):
4646
if is_private_bug:
4747
raise IgnoreInvalidRequestError("private bugs are not valid")
4848

49-
bugzilla_client = get_bugzilla()
50-
current_bug_info = bugzilla_client.getbug(request.bug.id)
51-
request.bug = BugzillaBug.parse_obj(current_bug_info.__dict__)
52-
current_action = extract_current_action(request.bug, action_map)
49+
request.bug = getbug_as_bugzilla_object(request=request)
50+
current_action = extract_current_action(request.bug, action_map) # type: ignore
5351
if not current_action:
5452
raise IgnoreInvalidRequestError(
5553
"whiteboard tag not found in configured actions"

src/jbi/services.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from atlassian import Jira # type: ignore
66

77
from src.app import environment
8+
from src.jbi.bugzilla import BugzillaBug, BugzillaWebhookRequest
89

910
settings = environment.get_settings()
1011
services_logger = logging.getLogger("src.jbi.services")
@@ -26,6 +27,12 @@ def get_bugzilla():
2627
)
2728

2829

30+
def getbug_as_bugzilla_object(request: BugzillaWebhookRequest) -> BugzillaBug:
31+
"""Helper method to get up to date bug data from Request.bug.id in BugzillaBug format"""
32+
current_bug_info = get_bugzilla().getbug(request.bug.id) # type: ignore
33+
return BugzillaBug.parse_obj(current_bug_info.__dict__)
34+
35+
2936
def bugzilla_check_health():
3037
"""Check health for Bugzilla Service"""
3138
bugzilla = get_bugzilla()

src/jbi/whiteboard_actions/default.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
"""
88

99
from src.app.environment import get_settings
10-
from src.jbi.bugzilla import BugzillaBug, BugzillaWebhookRequest
10+
from src.jbi.bugzilla import BugzillaWebhookRequest
1111
from src.jbi.errors import ActionError
12-
from src.jbi.services import get_bugzilla, get_jira
12+
from src.jbi.services import get_bugzilla, get_jira, getbug_as_bugzilla_object
1313

1414

1515
def init(whiteboard_tag, jira_project_key, **kwargs):
@@ -102,8 +102,7 @@ def create_and_link_issue(self, payload):
102102
raise ActionError(f"response contains error: {jira_response_create}")
103103

104104
jira_key_in_response = jira_response_create.get("key")
105-
current_bug_info = self.bugzilla_client.getbug(payload.bug.id) # type: ignore
106-
bug_obj = BugzillaBug.parse_obj(current_bug_info.__dict__)
105+
bug_obj = getbug_as_bugzilla_object(request=payload)
107106
jira_key_in_bugzilla = bug_obj.extract_from_see_also()
108107
_duplicate_creation_event = (
109108
jira_key_in_bugzilla is not None

tests/unit/jbi/test_router.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,9 @@ def test_request_is_ignored_because_no_action(
6666
caplog, webhook_request_example: BugzillaWebhookRequest
6767
):
6868
with mock.patch("src.jbi.whiteboard_actions.default.get_jira") as mocked_jira:
69-
with mock.patch("src.jbi.router.get_bugzilla") as mocked_bz:
70-
bugzilla_client = MagicMock()
71-
bugzilla_client.getbug.return_value = webhook_request_example.bug
72-
mocked_bz.return_value = bugzilla_client
69+
with mock.patch("src.jbi.router.getbug_as_bugzilla_object") as mocked_bz_func:
70+
mocked_bz_func = MagicMock()
71+
mocked_bz_func.return_value = webhook_request_example.bug
7372
with TestClient(app) as anon_client:
7473
# https://fastapi.tiangolo.com/advanced/testing-events/
7574
invalid_webhook_request_example = webhook_request_example

tests/unit/jbi/whiteboard_actions/test_default.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,12 @@ def test_default_returns_callable_with_data(webhook_request_example):
2626

2727
with mock.patch("src.jbi.whiteboard_actions.default.get_jira") as mocked_jira:
2828
with mock.patch("src.jbi.whiteboard_actions.default.get_bugzilla") as mocked_bz:
29-
bugzilla_client = MagicMock()
30-
bugzilla_client.getbug.return_value = webhook_request_example.bug
31-
mocked_bz.return_value = bugzilla_client
32-
callable_object = default.init(whiteboard_tag="", jira_project_key="")
33-
assert callable_object
34-
value = callable_object(payload=webhook_request_example)
29+
with mock.patch(
30+
"src.jbi.whiteboard_actions.default.getbug_as_bugzilla_object"
31+
) as mocked_bz_func:
32+
mocked_bz_func.return_value = webhook_request_example.bug
33+
callable_object = default.init(whiteboard_tag="", jira_project_key="")
34+
assert callable_object
35+
value = callable_object(payload=webhook_request_example)
3536

3637
assert value["status"] == "create"

0 commit comments

Comments
 (0)