100 lines
6.5 KiB
Python
100 lines
6.5 KiB
Python
"""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
|
|
|
|
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`, 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.
|
|
NEW = "new"
|
|
|
|
|
|
@dataclasses.dataclass(frozen=True)
|
|
class Start:
|
|
"""What the machine's settings give a starting issue beside its assignee: transition names the workflow transition that moves it, and sprint_board is the ID of the board whose active sprint it joins. Each is None where the settings don't name it."""
|
|
transition: str | None = None
|
|
sprint_board: int | None = None
|
|
|
|
|
|
@dataclasses.dataclass(frozen=True)
|
|
class Held:
|
|
"""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 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 = 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
|
|
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:
|
|
answered: typing.Any = http("GET", f"{site}/rest/api/3/issue/{key}?fields=assignee,status", authorization, None)
|
|
if isinstance(answered, Failure):
|
|
return Failure(f"cannot read {key} to start it: {answered.reason}")
|
|
try:
|
|
assignee, status = answered["fields"]["assignee"], answered["fields"]["status"]
|
|
return Held(None if assignee is None else (str(assignee["accountId"]), str(assignee["displayName"])), str(status["name"]), str(status["statusCategory"]["key"]))
|
|
except (KeyError, TypeError) as failure:
|
|
return Failure(unshaped(key, failure))
|
|
|
|
|
|
def assigned(site: str, key: str, held: Held, authorization: str, http: Http) -> str | None | Failure:
|
|
"""Assigns the issue named key to the account authorization names, unless someone holds it. The display name of someone else who holds it, else None."""
|
|
me = http("GET", f"{site}/rest/api/3/myself", authorization, None)
|
|
if isinstance(me, Failure):
|
|
return Failure(f"cannot read which account do-epic works as, to assign {key}: {me.reason}")
|
|
account = me.get("accountId") if isinstance(me, dict) else None
|
|
if not isinstance(account, str):
|
|
return Failure(f"cannot assign {key}: Jira named no account ID for the account do-epic works as")
|
|
if held.holder is not None:
|
|
return None if held.holder[0] == account else held.holder[1]
|
|
put = http("PUT", f"{site}/rest/api/3/issue/{key}/assignee", authorization, {"accountId": account})
|
|
return Failure(f"cannot assign {key}: {put.reason}") if isinstance(put, Failure) else 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):
|
|
return Failure(f"cannot read {key}'s transitions: {offered.reason}")
|
|
offers = offers_of(offered, key)
|
|
if isinstance(offers, Failure):
|
|
return offers
|
|
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}: 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 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}")
|
|
values = answered.get("values") if isinstance(answered, dict) else None
|
|
sprints = [each["id"] for each in values if isinstance(each, dict) and isinstance(each.get("id"), int)] if isinstance(values, list) else []
|
|
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} 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
|