Answer principles round 1: 2 🔴, 9 🟡 (0 misread); explicit record versions in place of the Confluence store's hidden state, the routing ruling filed as item 393 for 0.12.0, rule-hook catching heavy runs through uv, poetry, sudo and doas, one unreleased heading, one owner of the no-item message, typed Jira issues, a turn that gives its failure as a value, outcome tests for the Atlassian client, an Emptied reason type, an injected clock and git in touch-predictions, and paired record names
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
"""A record kept on one Confluence Cloud page, read and written as markdown over Confluence's REST API version 2, with the credentials of the config's env-file, and converted by the adf-codec shim.
|
||||
|
||||
A write never overwrites a teammate's edit: it sends the version it read, and Confluence refuses it where someone saved the page since. The human then decides (lilleman, 2026-11-12)."""
|
||||
A write never overwrites a teammate's edit: it sends the version it is handed, the one held() read, and Confluence refuses it where someone saved the page since. The human then decides (lilleman, 2026-11-12). The store keeps no version itself: its caller hands each write the version, as a Held's, and keeps the one the write leaves (lilleman, 2026-11-16)."""
|
||||
import dataclasses
|
||||
import json
|
||||
import pathlib
|
||||
@@ -10,8 +10,8 @@ from adf_codec.convert import adfs_to_markdown, markdown_to_adf, Refused
|
||||
from atlassian.client import basic_auth, confluence_json, Http, ToAdf, ToMarkdown
|
||||
from rest_client.json_request import ErrorAnswer
|
||||
from failure import Failure
|
||||
from record_store.held import Held
|
||||
from project_config.settings import Page
|
||||
from text_files.file_writes import replace_file
|
||||
|
||||
# What Confluence answers to a write whose version isn't the one after the version it holds.
|
||||
CONFLICT = 409
|
||||
@@ -20,23 +20,23 @@ SHAPE = "not in the shape Confluence Cloud's REST API version 2 documents"
|
||||
|
||||
@dataclasses.dataclass(frozen=True)
|
||||
class Seen:
|
||||
"""The page's version number and title as the store last read or wrote them. A write needs both."""
|
||||
"""The page's version number and title as a read found them or a write left them. A write needs both, so a Held's version holds both, as JSON."""
|
||||
version: int
|
||||
title: str
|
||||
|
||||
@property
|
||||
def token(self) -> str:
|
||||
return json.dumps(dataclasses.asdict(self))
|
||||
|
||||
|
||||
@dataclasses.dataclass
|
||||
class ConfluencePage:
|
||||
"""The text of page as markdown, read and written with the credentials env_file holds.
|
||||
|
||||
kept is a file that keeps the version the store read for a later process, such as `project-record save` after `project-record open`; None keeps it only in memory."""
|
||||
"""The text of page as markdown, read and written with the credentials env_file holds."""
|
||||
page: Page
|
||||
env_file: pathlib.Path
|
||||
kept: pathlib.Path | None = None
|
||||
http: Http = confluence_json
|
||||
to_markdown: ToMarkdown = adfs_to_markdown
|
||||
to_adf: ToAdf = markdown_to_adf
|
||||
seen: Seen | None = None
|
||||
|
||||
@property
|
||||
def label(self) -> str:
|
||||
@@ -52,18 +52,19 @@ class ConfluencePage:
|
||||
|
||||
def read(self) -> str | Failure:
|
||||
"""The page's text as markdown; empty where the page is."""
|
||||
held = self.held()
|
||||
return held if isinstance(held, Failure) else held.text
|
||||
|
||||
def held(self) -> Held | Failure:
|
||||
"""The page's text as markdown, with its version and title as the version a write over it names."""
|
||||
answer = self.fetched("?body-format=atlas_doc_format")
|
||||
if isinstance(answer, Failure):
|
||||
return answer
|
||||
seen, held = answer
|
||||
if not isinstance(held, str):
|
||||
seen, body = answer
|
||||
if not isinstance(body, str):
|
||||
return Failure(f"{self.label} is {SHAPE}: it holds no body in atlas_doc_format")
|
||||
text = self.converted(held)
|
||||
if isinstance(text, Failure):
|
||||
return text
|
||||
# Kept only once the text is in hand: a version kept for a read that failed would let a later write overwrite the save it saw.
|
||||
unkept = self.keep(seen)
|
||||
return unkept or text
|
||||
text = self.converted(body)
|
||||
return text if isinstance(text, Failure) else Held(text, seen.token)
|
||||
|
||||
def converted(self, held: str) -> str | Failure:
|
||||
"""The markdown of the body held, the JSON of an ADF document; empty where held is."""
|
||||
@@ -80,14 +81,9 @@ class ConfluencePage:
|
||||
return Failure(f"cannot convert {self.label} to markdown: {texts.reason}")
|
||||
return texts[0]
|
||||
|
||||
def write(self, text: str) -> Failure | None:
|
||||
"""Replaces the page's text with text, as the version after the one the store read. Where the store hasn't read the page, it replaces the page as it stands."""
|
||||
seen = self.seen or self.recalled()
|
||||
if seen is None:
|
||||
fetched = self.fetched("")
|
||||
if isinstance(fetched, Failure):
|
||||
return fetched
|
||||
seen = fetched[0]
|
||||
def write(self, text: str, over: str | None = None) -> Held | Failure:
|
||||
"""Replaces the page's text with text, as the version after over, the one held() gave. Where over is None, it replaces the page as it stands."""
|
||||
seen = self.seen_of(over)
|
||||
if isinstance(seen, Failure):
|
||||
return seen
|
||||
document = self.to_adf(text)
|
||||
@@ -102,7 +98,7 @@ class ConfluencePage:
|
||||
return Failure(f"someone saved {self.label} since the loop read version {seen.version} of it, so the loop didn't write it: never overwrite their edit, but ask the human what to do ({answer.reason})")
|
||||
if isinstance(answer, Failure):
|
||||
return Failure(f"cannot write {self.label}: {answer.reason}")
|
||||
return self.keep(Seen(seen.version + 1, seen.title))
|
||||
return Held(text, Seen(seen.version + 1, seen.title).token)
|
||||
|
||||
def fetched(self, query: str) -> tuple[Seen, object] | Failure:
|
||||
"""The page's version and title, and the value of its body in atlas_doc_format where query asks for it; None where it doesn't."""
|
||||
@@ -121,25 +117,13 @@ class ConfluencePage:
|
||||
adf = body.get("atlas_doc_format") if isinstance(body, dict) else None
|
||||
return Seen(number, title), adf.get("value") if isinstance(adf, dict) else None
|
||||
|
||||
def keep(self, seen: Seen) -> Failure | None:
|
||||
self.seen = seen
|
||||
if self.kept is None:
|
||||
return None
|
||||
def seen_of(self, over: str | None) -> Seen | Failure:
|
||||
"""The version and title a write sends: those over holds, else the page's as it stands."""
|
||||
if over is None:
|
||||
fetched = self.fetched("")
|
||||
return fetched if isinstance(fetched, Failure) else fetched[0]
|
||||
try:
|
||||
self.kept.parent.mkdir(parents=True, exist_ok=True)
|
||||
except OSError as failure:
|
||||
return Failure(f"cannot write {self.kept}: {failure.strerror}")
|
||||
unwritten = replace_file(self.kept, json.dumps(dataclasses.asdict(seen)).encode())
|
||||
return Failure(f"cannot write {self.kept}: {unwritten.reason}") if unwritten else None
|
||||
|
||||
def recalled(self) -> Seen | Failure | None:
|
||||
"""The version and title the file kept holds; None where kept is None or names no file."""
|
||||
if self.kept is None:
|
||||
return None
|
||||
try:
|
||||
held: typing.Any = json.loads(self.kept.read_text(encoding="utf-8"))
|
||||
held: typing.Any = json.loads(over)
|
||||
return Seen(int(held["version"]), str(held["title"]))
|
||||
except FileNotFoundError:
|
||||
return None
|
||||
except (OSError, ValueError, KeyError, TypeError) as failure:
|
||||
return Failure(f"cannot read the version of {self.label} that {self.kept} keeps: {failure}")
|
||||
except (ValueError, KeyError, TypeError) as failure:
|
||||
return Failure(f"cannot read the version of {self.label} to write over, {over!r}: {failure}")
|
||||
|
||||
Reference in New Issue
Block a user