Files
claude-config/bin/record_store/jira_description.py
T

88 lines
4.4 KiB
Python

"""A project's goals and audience kept as the description of one Jira Cloud epic, read and written over Jira's REST API with the credentials of the config's env-file, and converted by the adf-codec shim.
Jira keeps no version of a description, so the version a read gives is a digest of its markdown, and a write refuses where the description no longer has that digest. A write changes ## Goals or ## Audience only where the store was told the human approved that section in an interrupt (`approved`)."""
import dataclasses
import pathlib
from adf_codec.convert import adfs_to_markdown, markdown_to_adf, Refused
from atlassian.client import basic_auth, Http, jira_json, ToAdf, ToMarkdown
from record_store.held import digest_of, Held
from project_config.settings import Epic
from failure import Failure
from text_files.markdown_text import sections
# The sections of the description a write changes only after the human approves.
APPROVED_SECTIONS = ("Goals", "Audience")
@dataclasses.dataclass(frozen=True)
class EpicDescription:
"""The description of epic as markdown, read and written with the credentials env_file holds. approved names the APPROVED_SECTIONS whose change the human approved."""
epic: Epic
env_file: pathlib.Path
http: Http = jira_json
to_markdown: ToMarkdown = adfs_to_markdown
to_adf: ToAdf = markdown_to_adf
approved: frozenset[str] = frozenset()
@property
def label(self) -> str:
return self.epic.spelling
@property
def tracked(self) -> str | None:
return None
def read(self) -> str | Failure:
"""The description as markdown; empty where the epic has none."""
held = self.held()
return held if isinstance(held, Failure) else held.text
def held(self) -> Held | Failure:
"""The description as markdown, with its digest as the version a write over it names."""
authorization = basic_auth(self.env_file)
if isinstance(authorization, Failure):
return authorization
key = self.epic.key
answer = self.http("GET", self.url + "?fields=description", authorization, None)
if isinstance(answer, Failure):
return Failure(f"cannot read the epic {key}: {answer.reason}")
fields = answer.get("fields") if isinstance(answer, dict) else None
if not isinstance(fields, dict) or "description" not in fields or not isinstance(fields["description"], dict | None):
return Failure(f"the epic {key} is not in the shape Jira Cloud's REST API version 3 documents: its description field is missing, or neither a document nor null")
if fields["description"] is None:
return Held("", digest_of(""))
texts = self.to_markdown([fields["description"]])
if isinstance(texts, Refused):
return Failure(f"{key}'s description: {texts.failure.reason}")
if isinstance(texts, Failure):
return Failure(f"cannot convert {key}'s description to markdown: {texts.reason}")
return Held(texts[0], digest_of(texts[0]))
@property
def url(self) -> str:
return f"{self.epic.site}/rest/api/3/issue/{self.epic.key}"
def write(self, text: str, over: str | None) -> Held | Failure:
"""Replaces the description with text, over the version held() gave. A text that is the description as read sends nothing, since converting it back would drop each block's localId and Jira's spacing paragraphs. The check of over is a read just before the request: Jira would still take an edit made between the two."""
key = self.epic.key
current = self.held()
if isinstance(current, Failure):
return current
if over is not None and current.version != over:
return Failure(f"someone saved the description of {key} since the loop read it, so the loop didn't write it: never overwrite their edit, but ask the human what to do")
if text == current.text:
return current
if unapproved := [name for name in APPROVED_SECTIONS if name not in self.approved and sections(text).get(name) != sections(current.text).get(name)]:
return Failure(f"the write changes ## {unapproved[0]} of {key}'s description, which needs the human's approval in an interrupt first")
document = self.to_adf(text) if text else None
if isinstance(document, Failure):
return Failure(f"cannot convert the text for {key}'s description to ADF: {document.reason}")
authorization = basic_auth(self.env_file)
if isinstance(authorization, Failure):
return authorization
answer = self.http("PUT", self.url, authorization, {"fields": {"description": document}})
if isinstance(answer, Failure):
return Failure(f"cannot write the description of {key}: {answer.reason}")
return self.held()