Skip to content

Commit dc16dd4

Browse files
authored
improve explanation of exception when two sprints with the same name are found (#1405)
* adding test which should fail * raising a proper JiraError with explanation as to why it's being raised
1 parent 8465518 commit dc16dd4

File tree

2 files changed

+33
-6
lines changed

2 files changed

+33
-6
lines changed

jira/client.py

+19-3
Original file line numberDiff line numberDiff line change
@@ -4575,13 +4575,29 @@ def sprints(
45754575
self.AGILE_BASE_URL,
45764576
)
45774577

4578-
def sprints_by_name(self, id, extended=False):
4578+
def sprints_by_name(
4579+
self, id: Union[str, int], extended: bool = False, state: str = None
4580+
) -> Dict[str, Dict[str, Any]]:
4581+
"""Get a dictionary of sprint Resources where the name of the sprint is the key.
4582+
4583+
Args:
4584+
board_id (int): the board to get sprints from
4585+
extended (bool): Deprecated.
4586+
state (str): Filters results to sprints in specified states. Valid values: `future`, `active`, `closed`.
4587+
You can define multiple states separated by commas
4588+
4589+
Returns:
4590+
Dict[str, Dict[str, Any]]: dictionary of sprints with the sprint name as key
4591+
"""
45794592
sprints = {}
4580-
for s in self.sprints(id, extended=extended):
4593+
for s in self.sprints(id, extended=extended, state=state):
45814594
if s.name not in sprints:
45824595
sprints[s.name] = s.raw
45834596
else:
4584-
raise Exception
4597+
raise JIRAError(
4598+
f"There are multiple sprints defined with the name {s.name} on board id {id},\n"
4599+
f"returning a dict with sprint names as a key, assumes unique names for each sprint"
4600+
)
45854601
return sprints
45864602

45874603
def update_sprint(self, id, name=None, startDate=None, endDate=None, state=None):

tests/resources/test_sprint.py

+14-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
from functools import lru_cache
33
from typing import Iterator, Tuple
44

5+
import pytest as pytest
6+
7+
from jira.exceptions import JIRAError
58
from jira.resources import Board, Filter, Sprint
69
from tests.conftest import JiraTestCase, rndstr
710

@@ -14,9 +17,9 @@ def setUp(self):
1417
self.issue_3 = self.test_manager.project_b_issue3
1518

1619
uniq = rndstr()
17-
self.board_name = "board-" + uniq
18-
self.sprint_name = "sprint-" + uniq
19-
self.filter_name = "filter-" + uniq
20+
self.board_name = f"board-{uniq}"
21+
self.sprint_name = f"sprint-{uniq}"
22+
self.filter_name = f"filter-{uniq}"
2023

2124
self.board, self.filter = self._create_board_and_filter()
2225

@@ -97,3 +100,11 @@ def test_move_issue_to_backlog(self):
97100
# THEN: There is no longer the sprint assigned
98101
updated_issue_1 = self.jira.issue(self.issue_1)
99102
assert updated_issue_1.get_field(self._sprint_customfield()) is None
103+
104+
def test_two_sprints_with_the_same_name_raise_a_jira_error_when_sprints_by_name_is_called(
105+
self,
106+
):
107+
with self._create_sprint():
108+
with self._create_sprint():
109+
with pytest.raises(JIRAError):
110+
self.jira.sprints_by_name(self.board.id)

0 commit comments

Comments
 (0)