Files
claude-config/bin/do_epic/backend/jira_status.py
T

61 lines
3.8 KiB
Python

"""Moves a Jira issue to a named status, or through a named transition, for a team whose own automation doesn't see the move's cause, such as a pull request into an epic's working branch."""
import typing
from failure import Failure
from atlassian.client import Http
from atlassian.jira import offers_of, unshaped
def status_of(site: str, key: str, authorization: str, http: Http) -> str | Failure:
"""The name of the status the issue named key has now, read from the issue itself, since a search may lag behind a move."""
answered: typing.Any = http("GET", f"{site}/rest/api/3/issue/{key}?fields=status", authorization, None)
if isinstance(answered, Failure):
return Failure(f"cannot read {key} to move it: {answered.reason}")
try:
return str(answered["fields"]["status"]["name"])
except (KeyError, TypeError) as failure:
return Failure(unshaped(key, failure))
def moved_to(site: str, key: str, status: str, authorization: str, http: Http) -> Failure | None:
"""Moves the issue named key to the status named status, in upper or lower case, by the first transition to that status the issue's workflow offers. An issue already there stays as it is. Why Jira did not take the move; None where the issue has the status."""
now = status_of(site, key, authorization, http)
if isinstance(now, Failure):
return now
if now.casefold() == status.casefold():
return None
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
leading = [id_ for id_, _, to, _ in offers if to.casefold() == status.casefold()]
if not leading:
others = " and ".join(f"{name!r} to {to!r}" for _, name, to, _ in offers)
return Failure(f"{key} has no transition to the status {status!r} from its status {now!r}, " + (f"only {others}" if offers else "nor any other the account may make"))
moved = http("POST", f"{site}/rest/api/3/issue/{key}/transitions", authorization, {"transition": {"id": leading[0]}})
return Failure(f"cannot move {key} to {status!r}: {moved.reason}") if isinstance(moved, Failure) else None
def moved_by(site: str, key: str, transition: str, to: tuple[str, ...], authorization: str, http: Http) -> str | Failure:
"""Moves the issue named key by the transition named transition, where that transition leads to one of the statuses to. Names match in upper or lower case. An issue already at one of those statuses stays as it is. The name of the status the issue has afterwards; why the move was not made."""
accepted = {name.casefold() for name in to}
now = status_of(site, key, authorization, http)
if isinstance(now, Failure) or now.casefold() in accepted:
return now
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_, to) for id_, name, to, _ in offers if name.casefold() == transition.casefold()]
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 {now!r}, " + (f"only {others}" if offers else "nor any other the account may make"))
if named and named[0][1].casefold() not in accepted:
return Failure(f"the transition {transition!r} leads {key} to the status {named[0][1]!r}, not to {' or '.join(repr(name) for name in to)}")
moved = http("POST", f"{site}/rest/api/3/issue/{key}/transitions", authorization, {"transition": {"id": named[0][0]}})
return Failure(f"cannot move {key} by {transition!r}: {moved.reason}") if isinstance(moved, Failure) else named[0][1]