Skip to content

Commit 6be6859

Browse files
authored
Ref #984: Show more details about attachment when created (#1167)
Parse attachment info when context.event.target == "attachment"
1 parent 7b9cc7a commit 6be6859

File tree

5 files changed

+62
-2
lines changed

5 files changed

+62
-2
lines changed

jbi/bugzilla/models.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,36 @@ class WebhookComment(BaseModel, frozen=True):
7878
creation_time: Optional[SmartAwareDatetime] = None
7979

8080

81+
class AttachmentFlag(BaseModel, frozen=True):
82+
"""Flag object associated with a Bugzilla attachment."""
83+
84+
id: int
85+
name: str
86+
type_id: int
87+
creation_date: SmartAwareDatetime
88+
modification_date: SmartAwareDatetime
89+
status: str
90+
setter: str
91+
requestee: Optional[str] = None
92+
93+
94+
class WebhookAttachment(BaseModel, frozen=True):
95+
"""Attachment object associated with a Bugzilla bug
96+
when the webhook event is of type "attachment".
97+
See https://bugzilla.mozilla.org/page.cgi?id=webhooks.html
98+
"""
99+
100+
id: int
101+
content_type: str
102+
creation_time: SmartAwareDatetime
103+
description: str
104+
file_name: str
105+
flags: list[AttachmentFlag]
106+
is_obsolete: bool
107+
is_patch: bool
108+
is_private: bool
109+
110+
81111
class Bug(BaseModel, frozen=True):
82112
"""Bugzilla Bug Object"""
83113

@@ -99,6 +129,7 @@ class Bug(BaseModel, frozen=True):
99129
creator: Optional[str] = None
100130
assigned_to: Optional[str] = None
101131
comment: Optional[WebhookComment] = None
132+
attachment: Optional[WebhookAttachment] = None
102133
# Custom field Firefox for story points
103134
cf_fx_points: Optional[str] = None
104135

jbi/jira/service.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,16 @@ def add_jira_comment(self, context: ActionContext):
123123
)
124124
_, verb = routing_key.rsplit(".", 1)
125125
past_verb = {"modify": "modified"}.get(verb, f"{verb}d")
126-
formatted_comment = f"*{commenter}* {past_verb} an attachment"
126+
formatted_comment = f"*{commenter}* {past_verb} an attachment:"
127+
128+
# When event target is an attachment, the webhook bug object has attachment info.
129+
if context.bug.attachment:
130+
att = context.bug.attachment
131+
formatted_comment += f"\n*Description*: {att.description}"
132+
formatted_comment += (
133+
f"\n*Filename*: {att.file_name} ({att.content_type})"
134+
)
135+
127136
else:
128137
comment = context.bug.comment
129138
assert comment # See jbi.steps.create_comment()

tests/conftest.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ def context_attachment_example(action_context_factory) -> ActionContext:
171171
return action_context_factory(
172172
operation=Operation.ATTACHMENT,
173173
bug__see_also=["https://mozilla.atlassian.net/browse/JBI-234"],
174+
bug__with_attachment=True,
174175
event__target="attachment",
175176
event__routed_key="attachment.create",
176177
event__user__login="[email protected]",

tests/fixtures/factories.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,30 @@ class Meta:
6969
creation_time = None
7070

7171

72+
class WebhookAttachmentFactory(PydanticFactory):
73+
class Meta:
74+
model = bugzilla_models.WebhookAttachment
75+
76+
id = 1
77+
creation_time = datetime.now()
78+
description = "Bug 1337 - Stop war r?peace"
79+
file_name = "phabricator-D1234-url.txt"
80+
content_type = "text/x-phabricator-request"
81+
flags = []
82+
is_obsolete = False
83+
is_patch = False
84+
is_private = False
85+
86+
7287
class BugFactory(PydanticFactory):
7388
class Meta:
7489
model = bugzilla_models.Bug
7590

7691
class Params:
7792
with_comment = factory.Trait(comment=factory.SubFactory(WebhookCommentFactory))
93+
with_attachment = factory.Trait(
94+
attachment=factory.SubFactory(WebhookAttachmentFactory)
95+
)
7896

7997
assigned_to = "[email protected]"
8098
comment = None
@@ -93,6 +111,7 @@ class Params:
93111
summary = "JBI Test"
94112
type = "defect"
95113
whiteboard = "[devtest]"
114+
attachment = None
96115

97116

98117
class WebhookUserFactory(PydanticFactory):

tests/unit/test_steps.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ def test_added_attachment(
245245

246246
mocked_jira.issue_add_comment.assert_called_once_with(
247247
issue_key="JBI-234",
248-
comment="*[email protected]* created an attachment",
248+
comment="*[email protected]* created an attachment:\n*Description*: Bug 1337 - Stop war r?peace\n*Filename*: phabricator-D1234-url.txt (text/x-phabricator-request)",
249249
)
250250

251251

0 commit comments

Comments
 (0)