Files
claude-config/bin/do_epic/e2e/sandbox.py
T

76 lines
3.4 KiB
Python

"""The sandbox epic the e2e command makes on the real Jira site and deletes when it ends."""
import contextlib
import typing
from atlassian.client import basic_auth, Http, jira_json, ToAdf
from failure import Failure
from adf_codec.convert import markdown_to_adf
from do_epic.e2e.settings import Sandbox
from atlassian.jira import searched
EPIC_TYPE = "Epic"
# How a team-managed project's workflow moves an issue to done, as do-epic's `jira-finish-transition` and `jira-finish-status` name it.
FINISH_TRANSITION = "Done"
FINISH_STATUS = "Done"
DESCRIPTION = """**Language:** English
## Goals
1. **Achieve:** Prove do-epic's commands against a real Jira site.
## Audience
The person who runs do-epic's e2e command.
"""
class Leftover(Exception):
"""The sandbox epic or one of its issues could not be deleted, so some of them may still be on the site."""
def api(sandbox: Sandbox, path: str) -> str:
return f"{sandbox.jira_url}/rest/api/3/{path}"
@contextlib.contextmanager
def epic_made(sandbox: Sandbox, summary: str, http: Http = jira_json, to_adf: ToAdf = markdown_to_adf) -> typing.Iterator[str | Failure]:
"""Makes an epic in the sandbox project and yields its key; a Failure where it cannot be made. Deletes the epic with its issues and sub-tasks on the way out, however the block ends."""
authorization = basic_auth(sandbox.env_file)
if isinstance(authorization, Failure):
yield authorization
return
description = to_adf(DESCRIPTION)
if isinstance(description, Failure):
yield Failure(f"cannot convert the sandbox epic's description: {description.reason}")
return
fields = {"project": {"key": sandbox.project}, "issuetype": {"name": EPIC_TYPE}, "summary": summary, "description": description}
made = http("POST", api(sandbox, "issue"), authorization, {"fields": fields})
if isinstance(made, Failure):
yield Failure(f"cannot make the sandbox epic: {made.reason}")
return
key = made.get("key") if isinstance(made, dict) else None
if not isinstance(key, str):
yield Failure(f"Jira answered the epic's creation with {made!r}, which holds no key")
return
try:
yield key
finally:
deleted = deleted_with_children(sandbox, key, authorization, http)
if isinstance(deleted, Failure):
raise Leftover(f"the sandbox epic {key} and some of its issues may be left in {sandbox.project}: look for them and delete them by hand ({deleted.reason})")
def deleted_with_children(sandbox: Sandbox, key: str, authorization: str, http: Http) -> None | Failure:
"""Deletes the epic's child issues, with their sub-tasks, and then the epic: Jira deletes an epic's sub-tasks along with it, but not its child issues."""
found = searched(sandbox.jira_url, f"the issues of {key}", f"parent = {key}", {"fields": ["summary"]}, authorization, http)
# The epic is deleted all the same; the first failure is the one to report.
problem = found if isinstance(found, Failure) else None
entries = [] if isinstance(found, Failure) else found
children = [item["key"] for item in entries if isinstance(item, dict) and isinstance(item.get("key"), str)]
if len(children) < len(entries):
problem = Failure(f"the search for the issues of {key} answered an entry without a key, so that issue is not deleted")
for each in [*children, key]:
deleted = http("DELETE", api(sandbox, f"issue/{each}?deleteSubtasks=true"), authorization, None)
problem = problem or (deleted if isinstance(deleted, Failure) else None)
return problem