Skip to content

Commit fafc484

Browse files
authored
Retrieve comments from the API if they are private (fixes #154) (#163)
1 parent fc3d938 commit fafc484

File tree

4 files changed

+99
-0
lines changed

4 files changed

+99
-0
lines changed

src/jbi/bugzilla.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,15 @@ def map_as_jira_comment(self):
227227
comment: BugzillaWebhookComment = self.bug.comment
228228
commenter: BugzillaWebhookUser = self.event.user
229229
comment_body: str = comment.body
230+
231+
if comment.is_private:
232+
bug_comments = get_bugzilla().get_comments([self.bug.id])
233+
comment_list = bug_comments["bugs"][str(self.bug.id)]["comments"]
234+
matching_comments = [c for c in comment_list if c["id"] == comment.id]
235+
if len(matching_comments) != 1:
236+
return None
237+
comment_body = matching_comments[0]["text"]
238+
230239
body = f"*({commenter.login})* commented: \n{{quote}}{comment_body}{{quote}}"
231240
return body
232241

src/jbi/whiteboard_actions/default.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ def comment_create_or_noop(self, payload: BugzillaWebhookRequest):
6969
)
7070
return {"status": "noop"}
7171

72+
comment = payload.map_as_jira_comment()
73+
if comment is None:
74+
return {"status": "noop"}
75+
7276
jira_response = self.jira_client.issue_add_comment(
7377
issue_key=linked_issue_key,
7478
comment=payload.map_as_jira_comment(),

tests/unit/conftest.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,59 @@ def webhook_comment_example(webhook_create_example) -> BugzillaWebhookRequest:
100100
return webhook_comment_example
101101

102102

103+
@pytest.fixture
104+
def webhook_private_comment_example(
105+
webhook_create_example, mocked_bugzilla
106+
) -> BugzillaWebhookRequest:
107+
webhook_comment_example: BugzillaWebhookRequest = webhook_create_example
108+
webhook_comment_example.event.target = "comment"
109+
webhook_comment_example.event.user.login = "[email protected]" # type: ignore
110+
assert webhook_comment_example.bug
111+
webhook_comment_example.bug.comment = BugzillaWebhookComment.parse_obj(
112+
{"id": 344, "number": 2, "is_private": True}
113+
)
114+
webhook_comment_example.bug.see_also = [
115+
"https://mozilla.atlassian.net/browse/JBI-234"
116+
]
117+
118+
# Call to Bugzilla returns the actual bug comments.
119+
mocked_bugzilla().get_comments.return_value = {
120+
"bugs": {
121+
str(webhook_comment_example.bug.id): {
122+
"comments": [
123+
{
124+
"id": 343,
125+
"text": "not this one",
126+
"bug_id": webhook_comment_example.bug.id,
127+
"count": 1,
128+
"is_private": True,
129+
"creator": "[email protected]",
130+
},
131+
{
132+
"id": 344,
133+
"text": "hello",
134+
"bug_id": webhook_comment_example.bug.id,
135+
"count": 2,
136+
"is_private": True,
137+
"creator": "[email protected]",
138+
},
139+
{
140+
"id": 345,
141+
"text": "or this one",
142+
"bug_id": webhook_comment_example.bug.id,
143+
"count": 3,
144+
"is_private": True,
145+
"creator": "[email protected]",
146+
},
147+
]
148+
}
149+
},
150+
"comments": {},
151+
}
152+
153+
return webhook_comment_example
154+
155+
103156
@pytest.fixture
104157
def webhook_create_private_example(
105158
webhook_create_example, mocked_bugzilla

tests/unit/jbi/whiteboard_actions/test_default.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,39 @@ def test_added_comment(webhook_comment_example: BugzillaWebhookRequest, mocked_j
112112
assert value["status"] == "comment"
113113

114114

115+
def test_added_private_comment(
116+
webhook_private_comment_example: BugzillaWebhookRequest, mocked_jira
117+
):
118+
119+
callable_object = default.init(jira_project_key="")
120+
121+
value = callable_object(payload=webhook_private_comment_example)
122+
123+
mocked_jira().issue_add_comment.assert_called_once_with(
124+
issue_key="JBI-234",
125+
comment="*([email protected])* commented: \n{quote}hello{quote}",
126+
)
127+
assert value["status"] == "comment"
128+
129+
130+
def test_added_missing_private_comment(
131+
webhook_private_comment_example: BugzillaWebhookRequest,
132+
mocked_jira,
133+
mocked_bugzilla,
134+
):
135+
136+
callable_object = default.init(jira_project_key="")
137+
mocked_bugzilla().get_comments.return_value = {
138+
"bugs": {str(webhook_private_comment_example.bug.id): {"comments": []}},
139+
"comments": {},
140+
}
141+
142+
value = callable_object(payload=webhook_private_comment_example)
143+
144+
mocked_jira().issue_add_comment.assert_not_called()
145+
assert value["status"] == "noop"
146+
147+
115148
def test_added_comment_without_linked_issue(
116149
webhook_comment_example: BugzillaWebhookRequest, mocked_jira
117150
):

0 commit comments

Comments
 (0)