Skip to content

Commit 1c3b58a

Browse files
authored
Pick Jira link that matches project in config (#637)
Pick Jira link that matches config project
1 parent 8e5cfed commit 1c3b58a

File tree

6 files changed

+34
-9
lines changed

6 files changed

+34
-9
lines changed

jbi/models.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -224,11 +224,12 @@ def is_assigned(self) -> bool:
224224
"""Return `true` if the bug is assigned to a user."""
225225
return self.assigned_to != "[email protected]"
226226

227-
def extract_from_see_also(self):
227+
def extract_from_see_also(self, project_key):
228228
"""Extract Jira Issue Key from see_also if jira url present"""
229229
if not self.see_also or len(self.see_also) == 0:
230230
return None
231231

232+
candidates = []
232233
for url in self.see_also: # pylint: disable=not-an-iterable
233234
try:
234235
parsed_url: ParseResult = urlparse(url=url)
@@ -249,9 +250,13 @@ def extract_from_see_also(self):
249250
if any(part in JIRA_HOSTNAMES for part in host_parts):
250251
parsed_jira_key = parsed_url.path.rstrip("/").split("/")[-1]
251252
if parsed_jira_key: # URL ending with /
252-
return parsed_jira_key
253+
# Issue keys are like `{project_key}-{number}`
254+
if parsed_jira_key.startswith(f"{project_key}-"):
255+
return parsed_jira_key
256+
# If not obvious, then keep this link as candidate.
257+
candidates.append(parsed_jira_key)
253258

254-
return None
259+
return candidates[0] if candidates else None
255260

256261
def lookup_action(self, actions: Actions) -> Action:
257262
"""

jbi/runner.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,9 @@ def execute_action(
184184
) from err
185185
runner_context = runner_context.update(action=action)
186186

187-
linked_issue_key: Optional[str] = bug.extract_from_see_also()
187+
linked_issue_key: Optional[str] = bug.extract_from_see_also(
188+
project_key=action.jira_project_key
189+
)
188190

189191
action_context = ActionContext(
190192
action=action,

jbi/services/jira.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,9 @@ def delete_jira_issue_if_duplicate(
341341
"""Rollback the Jira issue creation if there is already a linked Jira issue
342342
on the Bugzilla ticket"""
343343
issue_key = context.jira.issue
344-
jira_key_in_bugzilla = latest_bug.extract_from_see_also()
344+
jira_key_in_bugzilla = latest_bug.extract_from_see_also(
345+
project_key=context.jira.project
346+
)
345347
_duplicate_creation_event = (
346348
jira_key_in_bugzilla is not None and issue_key != jira_key_in_bugzilla
347349
)

tests/unit/test_models.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,23 @@ def test_override_step_configuration_for_single_action_type():
7272
(["http://mozilla.jira.com/ticket/123"], "123"),
7373
(["http://atlassian.com/ticket/123"], "123"),
7474
(["http://mozilla.jira.com/123", "http://mozilla.jira.com/456"], "123"),
75+
(
76+
["http://mozilla.jira.com/FOO-123", "http://mozilla.jira.com/BAR-456"],
77+
"FOO-123",
78+
),
79+
(
80+
["http://mozilla.jira.com/FOO-123", "http://mozilla.jira.com/JBI456"],
81+
"FOO-123",
82+
),
83+
(
84+
["http://mozilla.jira.com/FOO-123", "http://mozilla.jira.com/JBI-456"],
85+
"JBI-456",
86+
),
7587
],
7688
)
7789
def test_extract_see_also(see_also, expected, bug_factory):
7890
bug = bug_factory(see_also=see_also)
79-
assert bug.extract_from_see_also() == expected
91+
assert bug.extract_from_see_also("JBI") == expected
8092

8193

8294
@pytest.mark.parametrize(

tests/unit/test_runner.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,9 @@ def test_runner_ignores_request_if_jira_is_linked_but_without_whiteboard(
283283
mocked_bugzilla.get_bug.return_value = webhook_comment_example.bug
284284
webhook_comment_example.bug.whiteboard = "[not-matching-local-config]"
285285

286-
assert webhook_comment_example.bug.extract_from_see_also() is not None
286+
assert (
287+
webhook_comment_example.bug.extract_from_see_also(project_key="foo") is not None
288+
)
287289

288290
with pytest.raises(IgnoreInvalidRequestError) as exc_info:
289291
execute_action(

tests/unit/test_steps.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,9 @@ def test_modified_public(
179179

180180
callable_object(context=context_update_example)
181181

182-
assert context_update_example.bug.extract_from_see_also(), "see_also is not empty"
182+
assert context_update_example.bug.extract_from_see_also(
183+
project_key=context_update_example.jira.project
184+
), "see_also is not empty"
183185

184186
mocked_jira.update_issue_field.assert_called_once_with(
185187
key="JBI-234",
@@ -213,7 +215,7 @@ def test_comment_for_modified_assignee_and_status(
213215
operation=Operation.UPDATE,
214216
bug=bug,
215217
event=event,
216-
jira=jira_context_factory(issue=bug.extract_from_see_also()),
218+
jira=jira_context_factory(issue=bug.extract_from_see_also(project_key="JBI")),
217219
)
218220

219221
callable_object = Executor(

0 commit comments

Comments
 (0)