72 lines
3.3 KiB
Python
72 lines
3.3 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
|
|
|
|
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."""
|
|
children = http("POST", api(sandbox, "search/jql"), authorization, {"jql": f"parent = {key}", "fields": ["summary"], "maxResults": 100})
|
|
found = children.get("issues") if isinstance(children, dict) else None
|
|
# The first failure is the one to report; the epic is deleted all the same.
|
|
problem = children if isinstance(children, Failure) else None if isinstance(found, list) else 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] if isinstance(found, list) else [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
|