Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions jbi/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,11 +224,12 @@ def is_assigned(self) -> bool:
"""Return `true` if the bug is assigned to a user."""
return self.assigned_to != "[email protected]"

def extract_from_see_also(self):
def extract_from_see_also(self, project_key):
"""Extract Jira Issue Key from see_also if jira url present"""
if not self.see_also or len(self.see_also) == 0:
return None

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

return None
return candidates[0] if candidates else None

def lookup_action(self, actions: Actions) -> Action:
"""
Expand Down
4 changes: 3 additions & 1 deletion jbi/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,9 @@ def execute_action(
) from err
runner_context = runner_context.update(action=action)

linked_issue_key: Optional[str] = bug.extract_from_see_also()
linked_issue_key: Optional[str] = bug.extract_from_see_also(
project_key=action.jira_project_key
)

action_context = ActionContext(
action=action,
Expand Down
4 changes: 3 additions & 1 deletion jbi/services/jira.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,9 @@ def delete_jira_issue_if_duplicate(
"""Rollback the Jira issue creation if there is already a linked Jira issue
on the Bugzilla ticket"""
issue_key = context.jira.issue
jira_key_in_bugzilla = latest_bug.extract_from_see_also()
jira_key_in_bugzilla = latest_bug.extract_from_see_also(
project_key=context.jira.project
)
_duplicate_creation_event = (
jira_key_in_bugzilla is not None and issue_key != jira_key_in_bugzilla
)
Expand Down
14 changes: 13 additions & 1 deletion tests/unit/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,23 @@ def test_override_step_configuration_for_single_action_type():
(["http://mozilla.jira.com/ticket/123"], "123"),
(["http://atlassian.com/ticket/123"], "123"),
(["http://mozilla.jira.com/123", "http://mozilla.jira.com/456"], "123"),
(
["http://mozilla.jira.com/FOO-123", "http://mozilla.jira.com/BAR-456"],
"FOO-123",
),
(
["http://mozilla.jira.com/FOO-123", "http://mozilla.jira.com/JBI456"],
"FOO-123",
),
(
["http://mozilla.jira.com/FOO-123", "http://mozilla.jira.com/JBI-456"],
"JBI-456",
),
],
)
def test_extract_see_also(see_also, expected, bug_factory):
bug = bug_factory(see_also=see_also)
assert bug.extract_from_see_also() == expected
assert bug.extract_from_see_also("JBI") == expected


@pytest.mark.parametrize(
Expand Down
4 changes: 3 additions & 1 deletion tests/unit/test_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,9 @@ def test_runner_ignores_request_if_jira_is_linked_but_without_whiteboard(
mocked_bugzilla.get_bug.return_value = webhook_comment_example.bug
webhook_comment_example.bug.whiteboard = "[not-matching-local-config]"

assert webhook_comment_example.bug.extract_from_see_also() is not None
assert (
webhook_comment_example.bug.extract_from_see_also(project_key="foo") is not None
)

with pytest.raises(IgnoreInvalidRequestError) as exc_info:
execute_action(
Expand Down
6 changes: 4 additions & 2 deletions tests/unit/test_steps.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,9 @@ def test_modified_public(

callable_object(context=context_update_example)

assert context_update_example.bug.extract_from_see_also(), "see_also is not empty"
assert context_update_example.bug.extract_from_see_also(
project_key=context_update_example.jira.project
), "see_also is not empty"

mocked_jira.update_issue_field.assert_called_once_with(
key="JBI-234",
Expand Down Expand Up @@ -213,7 +215,7 @@ def test_comment_for_modified_assignee_and_status(
operation=Operation.UPDATE,
bug=bug,
event=event,
jira=jira_context_factory(issue=bug.extract_from_see_also()),
jira=jira_context_factory(issue=bug.extract_from_see_also(project_key="JBI")),
)

callable_object = Executor(
Expand Down