diff --git a/jbi/bugzilla/models.py b/jbi/bugzilla/models.py index e47fefa9..5c06a791 100644 --- a/jbi/bugzilla/models.py +++ b/jbi/bugzilla/models.py @@ -78,6 +78,36 @@ class WebhookComment(BaseModel, frozen=True): creation_time: Optional[SmartAwareDatetime] = None +class AttachmentFlag(BaseModel, frozen=True): + """Flag object associated with a Bugzilla attachment.""" + + id: int + name: str + type_id: int + creation_date: SmartAwareDatetime + modification_date: SmartAwareDatetime + status: str + setter: str + requestee: Optional[str] = None + + +class WebhookAttachment(BaseModel, frozen=True): + """Attachment object associated with a Bugzilla bug + when the webhook event is of type "attachment". + See https://bugzilla.mozilla.org/page.cgi?id=webhooks.html + """ + + id: int + content_type: str + creation_time: SmartAwareDatetime + description: str + file_name: str + flags: list[AttachmentFlag] + is_obsolete: bool + is_patch: bool + is_private: bool + + class Bug(BaseModel, frozen=True): """Bugzilla Bug Object""" @@ -99,6 +129,7 @@ class Bug(BaseModel, frozen=True): creator: Optional[str] = None assigned_to: Optional[str] = None comment: Optional[WebhookComment] = None + attachment: Optional[WebhookAttachment] = None # Custom field Firefox for story points cf_fx_points: Optional[str] = None diff --git a/jbi/jira/service.py b/jbi/jira/service.py index b5bb9513..ef50dfc0 100644 --- a/jbi/jira/service.py +++ b/jbi/jira/service.py @@ -123,7 +123,16 @@ def add_jira_comment(self, context: ActionContext): ) _, verb = routing_key.rsplit(".", 1) past_verb = {"modify": "modified"}.get(verb, f"{verb}d") - formatted_comment = f"*{commenter}* {past_verb} an attachment" + formatted_comment = f"*{commenter}* {past_verb} an attachment:" + + # When event target is an attachment, the webhook bug object has attachment info. + if context.bug.attachment: + att = context.bug.attachment + formatted_comment += f"\n*Description*: {att.description}" + formatted_comment += ( + f"\n*Filename*: {att.file_name} ({att.content_type})" + ) + else: comment = context.bug.comment assert comment # See jbi.steps.create_comment() diff --git a/tests/conftest.py b/tests/conftest.py index a0d71bae..a56d744e 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -171,6 +171,7 @@ def context_attachment_example(action_context_factory) -> ActionContext: return action_context_factory( operation=Operation.ATTACHMENT, bug__see_also=["https://mozilla.atlassian.net/browse/JBI-234"], + bug__with_attachment=True, event__target="attachment", event__routed_key="attachment.create", event__user__login="phab-bot@bmo.tld", diff --git a/tests/fixtures/factories.py b/tests/fixtures/factories.py index 86a52227..b1758599 100644 --- a/tests/fixtures/factories.py +++ b/tests/fixtures/factories.py @@ -69,12 +69,30 @@ class Meta: creation_time = None +class WebhookAttachmentFactory(PydanticFactory): + class Meta: + model = bugzilla_models.WebhookAttachment + + id = 1 + creation_time = datetime.now() + description = "Bug 1337 - Stop war r?peace" + file_name = "phabricator-D1234-url.txt" + content_type = "text/x-phabricator-request" + flags = [] + is_obsolete = False + is_patch = False + is_private = False + + class BugFactory(PydanticFactory): class Meta: model = bugzilla_models.Bug class Params: with_comment = factory.Trait(comment=factory.SubFactory(WebhookCommentFactory)) + with_attachment = factory.Trait( + attachment=factory.SubFactory(WebhookAttachmentFactory) + ) assigned_to = "nobody@mozilla.org" comment = None @@ -93,6 +111,7 @@ class Params: summary = "JBI Test" type = "defect" whiteboard = "[devtest]" + attachment = None class WebhookUserFactory(PydanticFactory): diff --git a/tests/unit/test_steps.py b/tests/unit/test_steps.py index c669964d..4f286304 100644 --- a/tests/unit/test_steps.py +++ b/tests/unit/test_steps.py @@ -245,7 +245,7 @@ def test_added_attachment( mocked_jira.issue_add_comment.assert_called_once_with( issue_key="JBI-234", - comment="*phab-bot@bmo.tld* created an attachment", + comment="*phab-bot@bmo.tld* created an attachment:\n*Description*: Bug 1337 - Stop war r?peace\n*Filename*: phabricator-D1234-url.txt (text/x-phabricator-request)", )