diff --git a/jira/client.py b/jira/client.py index 468fad4b5..286e3075c 100644 --- a/jira/client.py +++ b/jira/client.py @@ -4516,13 +4516,29 @@ def sprints( self.AGILE_BASE_URL, ) - def sprints_by_name(self, id, extended=False): + def sprints_by_name( + self, id: Union[str, int], extended: bool = False, state: str = None + ) -> Dict[str, Dict[str, Any]]: + """Get a dictionary of sprint Resources where the name of the sprint is the key. + + Args: + board_id (int): the board to get sprints from + extended (bool): Deprecated. + state (str): Filters results to sprints in specified states. Valid values: `future`, `active`, `closed`. + You can define multiple states separated by commas + + Returns: + Dict[str, Dict[str, Any]]: dictionary of sprints with the sprint name as key + """ sprints = {} - for s in self.sprints(id, extended=extended): + for s in self.sprints(id, extended=extended, state=state): if s.name not in sprints: sprints[s.name] = s.raw else: - raise Exception + raise JIRAError( + f"There are multiple sprints defined with the name {s.name} on board id {id},\n" + f"returning a dict with sprint names as a key, assumes unique names for each sprint" + ) return sprints def update_sprint(self, id, name=None, startDate=None, endDate=None, state=None): diff --git a/tests/resources/test_sprint.py b/tests/resources/test_sprint.py index 3f5e534e1..959ebda6d 100644 --- a/tests/resources/test_sprint.py +++ b/tests/resources/test_sprint.py @@ -2,6 +2,9 @@ from functools import lru_cache from typing import Iterator, Tuple +import pytest as pytest + +from jira.exceptions import JIRAError from jira.resources import Board, Filter, Sprint from tests.conftest import JiraTestCase, rndstr @@ -14,9 +17,9 @@ def setUp(self): self.issue_3 = self.test_manager.project_b_issue3 uniq = rndstr() - self.board_name = "board-" + uniq - self.sprint_name = "sprint-" + uniq - self.filter_name = "filter-" + uniq + self.board_name = f"board-{uniq}" + self.sprint_name = f"sprint-{uniq}" + self.filter_name = f"filter-{uniq}" self.board, self.filter = self._create_board_and_filter() @@ -97,3 +100,11 @@ def test_move_issue_to_backlog(self): # THEN: There is no longer the sprint assigned updated_issue_1 = self.jira.issue(self.issue_1) assert updated_issue_1.get_field(self._sprint_customfield()) is None + + def test_two_sprints_with_the_same_name_raise_a_jira_error_when_sprints_by_name_is_called( + self, + ): + with self._create_sprint(): + with self._create_sprint(): + with pytest.raises(JIRAError): + self.jira.sprints_by_name(self.board.id)