Skip to content

Commit 3c2c5dd

Browse files
Return 4XX response if no bug is provided (fixes #418) (#222)
Co-authored-by: Mathieu Leplatre <[email protected]>
1 parent 3e78b46 commit 3c2c5dd

File tree

4 files changed

+14
-25
lines changed

4 files changed

+14
-25
lines changed

jbi/models.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ class Config:
326326
webhook_id: int
327327
webhook_name: str
328328
event: BugzillaWebhookEvent
329-
bug: Optional[BugzillaBug]
329+
bug: BugzillaBug
330330

331331
def map_as_jira_comment(self):
332332
"""Extract comment from Webhook Event"""
@@ -388,8 +388,6 @@ def getbug_as_bugzilla_object(self) -> BugzillaBug:
388388
@functools.cached_property
389389
def bugzilla_object(self) -> BugzillaBug:
390390
"""Returns the bugzilla bug object, querying the API as needed for private bugs"""
391-
if not self.bug:
392-
raise ValueError("missing bug reference")
393391
if not self.bug.is_private:
394392
return self.bug
395393
return self.getbug_as_bugzilla_object()

jbi/runner.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,15 @@ def execute_action(
2727
The value returned by the action call is returned.
2828
"""
2929
log_context = RunnerLogContext(
30-
bug={
31-
"id": request.bug.id if request.bug else None,
32-
},
33-
request=request,
30+
bug=request.bug.dict(),
31+
request=request.dict(),
3432
operation=Operation.HANDLE,
3533
)
3634
try:
3735
logger.debug(
3836
"Handling incoming request",
3937
extra=log_context.dict(),
4038
)
41-
if not request.bug:
42-
raise IgnoreInvalidRequestError("no bug data received")
4339

4440
try:
4541
bug_obj: BugzillaBug = request.bugzilla_object

tests/unit/test_router.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,17 @@ def test_webhook_is_200_if_action_raises_IgnoreInvalidRequestError(
111111
)
112112

113113

114+
def test_webhook_is_422_if_bug_information_missing(webhook_create_example):
115+
webhook_create_example.bug = None
116+
117+
with TestClient(app) as anon_client:
118+
response = anon_client.post(
119+
"/bugzilla_webhook", data=webhook_create_example.json()
120+
)
121+
assert response.status_code == 422
122+
assert response.json()["detail"][0]["msg"] == "none is not an allowed value"
123+
124+
114125
def test_read_version(anon_client):
115126
"""__version__ returns the contents of version.json."""
116127
here = os.path.dirname(__file__)

tests/unit/test_runner.py

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -56,22 +56,6 @@ def test_private_request_is_allowed(
5656
assert payload.bug.id == 654321
5757

5858

59-
def test_request_is_ignored_because_no_bug(
60-
webhook_create_example: BugzillaWebhookRequest,
61-
actions_example: Actions,
62-
settings: Settings,
63-
):
64-
webhook_create_example.bug = None
65-
66-
with pytest.raises(IgnoreInvalidRequestError) as exc_info:
67-
execute_action(
68-
request=webhook_create_example,
69-
actions=actions_example,
70-
settings=settings,
71-
)
72-
assert str(exc_info.value) == "no bug data received"
73-
74-
7559
def test_request_is_ignored_because_no_action(
7660
webhook_create_example: BugzillaWebhookRequest,
7761
actions_example: Actions,

0 commit comments

Comments
 (0)