diff --git a/bin/do_epic/backend/jira_start.py b/bin/do_epic/backend/jira_start.py index 4a3cd930..adc37465 100644 --- a/bin/do_epic/backend/jira_start.py +++ b/bin/do_epic/backend/jira_start.py @@ -1,4 +1,4 @@ -"""How a Jira issue shows its team that its work is under way: assigned, moved by the team's start transition and in a board's active sprint. JiraEpic.start_issue calls `started` for an issue it found in its epic.""" +"""How a Jira issue shows its team that its work is under way: assigned, moved by the team's start transition and in a board's active sprint.""" import dataclasses import typing @@ -6,7 +6,7 @@ from failure import Failure from atlassian.client import Http from atlassian.jira import offers_of, unshaped -# Three readers, seated as those who write and read do-epic's config, picked both spellings from four candidates each, 3-0. `jira-start-transition` pairs with `jira-finish-transition`. `jira-board` reads as the board do-epic takes its issues from. +# Three readers, seated as those who write and read do-epic's config, picked both spellings from four candidates each, 3-0. `jira-start-transition` pairs with `jira-finish-transition`. `jira-board`, a candidate they turned down, reads as the board do-epic takes its issues from. START_TRANSITION = "jira-start-transition" SPRINT_BOARD = "jira-sprint-board" # The key of the category Jira gives each status at which an issue's work hasn't started. @@ -22,22 +22,25 @@ class Start: @dataclasses.dataclass(frozen=True) class Held: - """An issue as Jira holds it before it starts. holder is its assignee's account ID and display name; None where nobody is assigned.""" + """What Jira answers about an issue before it starts. holder is its assignee's account ID and display name; None where nobody is assigned.""" holder: tuple[str, str] | None status: str category: str def started(site: str, key: str, sprint_key: str, start: Start, authorization: str, http: Http) -> str | None | Failure: - """Starts the issue named key, where sprint_key is the issue a sprint takes for it: the issue itself, or the parent of a sub-task. The answer is the display name of someone else who holds the issue, else None; a Failure names the first step Jira did not take.""" + """Starts the issue named key, where sprint_key is the issue a sprint takes for it: the issue itself, or the parent of a sub-task. The answer is the display name of someone else who holds the issue, else None. A Failure names each step Jira did not take, and that someone else who holds the issue.""" held = held_of(site, key, authorization, http) if isinstance(held, Failure): return held other = assigned(site, key, held, authorization, http) # A status in any later category, such as Code Review, is past the start transition, so the issue keeps it. - moved = move_refusal(site, key, held.status, start.transition, authorization, http) if start.transition and held.category == NEW else None + moved = start_refusal(site, key, held.status, start.transition, authorization, http) if start.transition and held.category == NEW else None joined = sprint_refusal(site, sprint_key, start.sprint_board, authorization, http) if start.sprint_board is not None else None - return next((step for step in (other, moved, joined) if isinstance(step, Failure)), other) + refused = [step.reason for step in (other, moved, joined) if isinstance(step, Failure)] + if not refused: + return other + return Failure("; ".join(refused) + (f"; {key} stays assigned to {other}" if isinstance(other, str) else "")) def held_of(site: str, key: str, authorization: str, http: Http) -> Held | Failure: @@ -65,7 +68,7 @@ def assigned(site: str, key: str, held: Held, authorization: str, http: Http) -> return Failure(f"cannot assign {key}: {put.reason}") if isinstance(put, Failure) else None -def move_refusal(site: str, key: str, status: str, transition: str, authorization: str, http: Http) -> Failure | None: +def start_refusal(site: str, key: str, status: str, transition: str, authorization: str, http: Http) -> Failure | None: """Moves the issue named key, which has the status named status, by the transition named transition. Why Jira did not take the move; None where it did.""" offered = http("GET", f"{site}/rest/api/3/issue/{key}/transitions", authorization, None) if isinstance(offered, Failure): @@ -76,13 +79,13 @@ def move_refusal(site: str, key: str, status: str, transition: str, authorizatio named = [id_ for id_, name, _, _ in offers if name == transition] if not named: others = " and ".join(f"{name!r} to {to!r}" for _, name, to, _ in offers) - return Failure(f"{key} has no transition {transition!r} from its status {status!r}, " + (f"only {others}: name in `{START_TRANSITION}` one that its statuses before work starts have" if offers else "nor any other the account may make")) + return Failure(f"{key} has no transition {transition!r} from its status {status!r}, " + (f"only {others}: set `{START_TRANSITION}` to a transition the issue has before its work starts" if offers else "nor any other the account may make")) moved = http("POST", f"{site}/rest/api/3/issue/{key}/transitions", authorization, {"transition": {"id": named[0]}}) return Failure(f"cannot move {key} by {transition!r}: {moved.reason}") if isinstance(moved, Failure) else None def sprint_refusal(site: str, key: str, board: int, authorization: str, http: Http) -> Failure | None: - """Adds the issue named key to the one active sprint of the board with the ID board. Why it joined none; None where it joined.""" + """Adds the issue named key to the one active sprint of the board with the ID board. Why it didn't join one; None where it joined.""" answered = http("GET", f"{site}/rest/agile/1.0/board/{board}/sprint?state=active", authorization, None) if isinstance(answered, Failure): return Failure(f"cannot read the active sprint of board {board}, which `{SPRINT_BOARD}` names: {answered.reason}") @@ -91,6 +94,6 @@ def sprint_refusal(site: str, key: str, board: int, authorization: str, http: Ht if not isinstance(values, list) or len(sprints) != len(values): return Failure(f"Jira answered board {board}'s sprints in a shape other than the one it documents") if len(sprints) != 1: - return Failure(f"board {board}, which `{SPRINT_BOARD}` names, has {f"{len(sprints)} active sprints" if sprints else "no active sprint"}, so {key} joins none") + return Failure(f"board {board}, which `{SPRINT_BOARD}` names, has {f"{len(sprints)} active sprints" if sprints else "no active sprint"}, so {key} doesn't join a sprint") added = http("POST", f"{site}/rest/agile/1.0/sprint/{sprints[0]}/issue", authorization, {"issues": [key]}) return Failure(f"cannot add {key} to the active sprint of board {board}: {added.reason}") if isinstance(added, Failure) else None diff --git a/bin/do_epic/backend/test_jira.py b/bin/do_epic/backend/test_jira.py index 5b967199..6ea47926 100644 --- a/bin/do_epic/backend/test_jira.py +++ b/bin/do_epic/backend/test_jira.py @@ -338,7 +338,7 @@ class Jira(Case): self.assertEqual((self.jira.issues[0]["fields"]["assignee"], self.jira.issues[0]["fields"]["status"]), (ME, TO_DO)) # type: ignore[index] self.assertEqual(self.jira.calls[-2:], [("GET", MYSELF), ("PUT", f"{ISSUES}/{key}/assignee")]) - def test_a_start_names_the_first_step_jira_did_not_take_and_makes_the_others(self) -> None: + def test_a_start_names_each_step_jira_did_not_take_and_makes_the_others(self) -> None: store = self.starting() key = store.file("ABC-12", "Count the runs.", "", ()) assert isinstance(key, str) @@ -352,16 +352,16 @@ class Jira(Case): ({("PUT", assignee): Failure("Jira answered 403")}, f"cannot assign {key}: Jira answered 403"), ({("GET", transitions): Failure("Jira answered 503")}, f"cannot read {key}'s transitions: Jira answered 503"), ({("GET", transitions): {"transitions": [{"id": "21"}]}}, f"Jira answered {key}'s transitions in a shape other than the one it documents"), - ({("GET", transitions): {"transitions": [{"id": "31", "name": "Mark as done", "to": DONE}]}}, f"{key} has no transition 'Start' from its status 'To Do', only 'Mark as done' to 'Done': name in `jira-start-transition` one that its statuses before work starts have"), - ({("GET", transitions): {"transitions": [{"id": "31", "name": "Mark as done", "to": DONE}, {"id": "41", "name": "Wait", "to": TO_DO}]}}, f"{key} has no transition 'Start' from its status 'To Do', only 'Mark as done' to 'Done' and 'Wait' to 'To Do': name in `jira-start-transition` one that its statuses before work starts have"), + ({("GET", transitions): {"transitions": [{"id": "31", "name": "Mark as done", "to": DONE}]}}, f"{key} has no transition 'Start' from its status 'To Do', only 'Mark as done' to 'Done': set `jira-start-transition` to a transition the issue has before its work starts"), + ({("GET", transitions): {"transitions": [{"id": "31", "name": "Mark as done", "to": DONE}, {"id": "41", "name": "Wait", "to": TO_DO}]}}, f"{key} has no transition 'Start' from its status 'To Do', only 'Mark as done' to 'Done' and 'Wait' to 'To Do': set `jira-start-transition` to a transition the issue has before its work starts"), ({("GET", transitions): {"transitions": []}}, f"{key} has no transition 'Start' from its status 'To Do', nor any other the account may make"), ({("POST", transitions): Failure("Jira answered 400")}, f"cannot move {key} by 'Start': Jira answered 400"), ({("GET", ACTIVE_SPRINTS): Failure("Jira answered 400")}, f"cannot read the active sprint of board 6, which `jira-sprint-board` names: Jira answered 400"), ({("GET", ACTIVE_SPRINTS): {"values": [{"name": "Sprint 9"}]}}, "Jira answered board 6's sprints in a shape other than the one it documents"), ({("GET", ACTIVE_SPRINTS): [{"id": 77}]}, "Jira answered board 6's sprints in a shape other than the one it documents"), ({("GET", ACTIVE_SPRINTS): {"values": "Sprint 9"}}, "Jira answered board 6's sprints in a shape other than the one it documents"), - ({("GET", ACTIVE_SPRINTS): {"values": []}}, f"board 6, which `jira-sprint-board` names, has no active sprint, so {key} joins none"), - ({("GET", ACTIVE_SPRINTS): {"values": [{"id": 77}, {"id": 78}]}}, f"board 6, which `jira-sprint-board` names, has 2 active sprints, so {key} joins none"), + ({("GET", ACTIVE_SPRINTS): {"values": []}}, f"board 6, which `jira-sprint-board` names, has no active sprint, so {key} doesn't join a sprint"), + ({("GET", ACTIVE_SPRINTS): {"values": [{"id": 77}, {"id": 78}]}}, f"board 6, which `jira-sprint-board` names, has 2 active sprints, so {key} doesn't join a sprint"), ({("POST", SPRINT_ISSUES): Failure("Jira answered 403")}, f"cannot add {key} to the active sprint of board 6: Jira answered 403"), ): with self.subTest(reason=reason): @@ -373,7 +373,19 @@ class Jira(Case): store.start_issue(key) self.assertEqual((self.jira.issues[0]["fields"]["status"], self.jira.sprinted), (IN_PROGRESS, [[key]])) # type: ignore[index] - def test_a_start_whose_issue_or_parent_jira_cannot_read_or_that_the_epic_does_not_hold_says_which(self) -> None: + def test_a_start_that_fails_twice_on_an_issue_someone_holds_names_both_steps_and_the_holder(self) -> None: + store = self.starting() + key = store.file("ABC-12", "Count the runs.", "", ()) + self.jira.change(key, assignee={"accountId": "ada", "displayName": "Ada Berg"}) + self.jira.answers = {("POST", f"{ISSUES}/{key}/transitions"): Failure("Jira answered 400"), ("POST", SPRINT_ISSUES): Failure("Jira answered 403")} + self.assertEqual(store.start_issue(key), Failure(f"cannot move {key} by 'Start': Jira answered 400; cannot add {key} to the active sprint of board 6: Jira answered 403; {key} stays assigned to Ada Berg")) + self.jira.answers = {("GET", MYSELF): Failure("Jira answered 503"), ("POST", SPRINT_ISSUES): Failure("Jira answered 403")} + self.assertEqual(store.start_issue(key), Failure(f"cannot read which account do-epic works as, to assign {key}: Jira answered 503; cannot add {key} to the active sprint of board 6: Jira answered 403")) + self.jira.change(key, assignee=ME) + self.jira.answers = {("POST", SPRINT_ISSUES): Failure("Jira answered 403")} + self.assertEqual(store.start_issue(key), Failure(f"cannot add {key} to the active sprint of board 6: Jira answered 403")) + + def test_a_start_says_which_issue_jira_cannot_read_and_refuses_one_outside_the_epic(self) -> None: store = self.starting() child = store.file("ABC-12", "Count the runs.", "", ()) task = store.file(child, "Count them in the api.", "", ())