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

75 lines
3.1 KiB
Python

"""The sandbox epic the e2e command makes on the real Jira site and deletes when it ends."""
import contextlib
import typing
from adf_codec.convert import markdown_to_adf
from atlassian.client import basic_auth, Http, jira_json, ToAdf
from do_epic.e2e.settings import Sandbox
from failure import Failure
EPIC_TYPE = "Epic"
# How a team-managed project's workflow moves an issue to done, as do-epic's config names 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 could not be deleted, so it and its issues are still 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."""
children = http("POST", api(sandbox, "search/jql"), authorization, {"jql": f"parent = {key}", "fields": ["summary"], "maxResults": 100})
if isinstance(children, Failure):
return children
found = children.get("issues") if isinstance(children, dict) else None
if not isinstance(found, list):
return Failure(f"Jira answered the search for {key}'s issues with {children!r}, which holds no issues")
for each in [item.get("key") for item in found if isinstance(item, dict)] + [key]:
deleted = http("DELETE", api(sandbox, f"issue/{each}?deleteSubtasks=true"), authorization, None)
if isinstance(deleted, Failure):
return deleted
return None