Leave a Jira description or a Confluence page as it is where a save hands back the text a read showed
CI / tests (pull_request) Successful in 3m1s
CI / tests (pull_request) Successful in 3m1s
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
|
||||
A write never overwrites a teammate's edit: it sends the version after the one held() read, and Confluence refuses it where someone saved the page since. The human then decides (lilleman, 2026-11-12)."""
|
||||
import dataclasses
|
||||
import hashlib
|
||||
import json
|
||||
import pathlib
|
||||
import typing
|
||||
@@ -20,9 +21,10 @@ 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 a read found them or a write left them. A write needs both, so a Held's version holds both, as JSON."""
|
||||
"""The page's version number and title as a read found them or a write left them, and the digest of the markdown it found or sent. A write needs all three, so a Held's version holds them, as JSON."""
|
||||
version: int
|
||||
title: str
|
||||
digest: str | None = None
|
||||
|
||||
@property
|
||||
def token(self) -> str:
|
||||
@@ -64,7 +66,7 @@ class ConfluencePage:
|
||||
if not isinstance(body, str):
|
||||
return Failure(f"{self.label} is {SHAPE}: it holds no body in atlas_doc_format")
|
||||
text = self.converted(body)
|
||||
return text if isinstance(text, Failure) else Held(text, seen.token)
|
||||
return text if isinstance(text, Failure) else Held(text, Seen(seen.version, seen.title, digest_of(text)).token)
|
||||
|
||||
def converted(self, held: str) -> str | Failure:
|
||||
"""The markdown of the body held, the JSON of an ADF document; empty where held is."""
|
||||
@@ -86,6 +88,9 @@ class ConfluencePage:
|
||||
seen = self.seen_of(over)
|
||||
if isinstance(seen, Failure):
|
||||
return seen
|
||||
# The markdown a read shows drops the localIds and spacing paragraphs Confluence holds, so writing the text a read found back would lose them: leave the page alone.
|
||||
if over is not None and seen.digest == digest_of(text):
|
||||
return Held(text, over)
|
||||
document = self.to_adf(text)
|
||||
if isinstance(document, Failure):
|
||||
return Failure(f"cannot convert the text for {self.label} to ADF: {document.reason}")
|
||||
@@ -98,7 +103,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 Held(text, Seen(seen.version + 1, seen.title).token)
|
||||
return Held(text, Seen(seen.version + 1, seen.title, digest_of(text)).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."""
|
||||
@@ -124,6 +129,10 @@ class ConfluencePage:
|
||||
return fetched if isinstance(fetched, Failure) else fetched[0]
|
||||
try:
|
||||
held: typing.Any = json.loads(over)
|
||||
return Seen(int(held["version"]), str(held["title"]))
|
||||
return Seen(int(held["version"]), str(held["title"]), held.get("digest"))
|
||||
except (ValueError, KeyError, TypeError) as failure:
|
||||
return Failure(f"cannot read the version of {self.label} to write over, {over!r}: {failure}")
|
||||
|
||||
|
||||
def digest_of(text: str) -> str:
|
||||
return hashlib.sha256(text.encode("utf-8")).hexdigest()
|
||||
|
||||
@@ -7,7 +7,7 @@ import unittest
|
||||
|
||||
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1]))
|
||||
|
||||
from record_store.confluence_page import ConfluencePage
|
||||
from record_store.confluence_page import ConfluencePage, digest_of, Seen
|
||||
from rest_client.json_request import ErrorAnswer
|
||||
from failure import Failure
|
||||
from record_store.held import Held
|
||||
@@ -80,9 +80,25 @@ class ConfluencePageTest(ConfluenceCase):
|
||||
store = self.new_store()
|
||||
held = store.held()
|
||||
assert isinstance(held, Held), held
|
||||
self.assertEqual(store.write("- a decision\n", held.version), Held("- a decision\n", '{"version": 8, "title": "Decisions"}'))
|
||||
self.assertEqual(store.write("- a decision\n", held.version), Held("- a decision\n", Seen(8, "Decisions", digest_of("- a decision\n")).token))
|
||||
self.assertEqual(self.confluence.calls[-1], ("PUT", URL, AUTHORIZATION, {"id": PAGE.page_id, "status": "current", "title": "Decisions", "body": {"representation": "atlas_doc_format", "value": json.dumps(to_adf("- a decision\n"))}, "version": {"number": 8}}))
|
||||
|
||||
def test_saving_the_text_a_read_found_leaves_the_page_as_confluence_holds_it(self) -> None:
|
||||
self.confluence.teammate_saves("- a decision\n")
|
||||
value = self.confluence.value
|
||||
held = self.new_store().held()
|
||||
assert isinstance(held, Held), held
|
||||
self.assertEqual(self.new_store().write(held.text, held.version), held)
|
||||
self.assertEqual([call[0] for call in self.confluence.calls], ["GET"])
|
||||
self.assertEqual((self.confluence.version, self.confluence.value), (2, value))
|
||||
|
||||
def test_saving_an_edited_text_writes_the_page(self) -> None:
|
||||
self.confluence.teammate_saves("- a decision\n")
|
||||
held = self.new_store().held()
|
||||
assert isinstance(held, Held), held
|
||||
self.assertIsInstance(self.new_store().write("- another decision\n", held.version), Held)
|
||||
self.assertEqual(self.new_store().read(), "- another decision\n")
|
||||
|
||||
def test_refuses_to_overwrite_a_teammates_save_since_the_version_it_is_handed_and_says_to_ask_the_human(self) -> None:
|
||||
held = self.new_store().held()
|
||||
assert isinstance(held, Held), held
|
||||
|
||||
Reference in New Issue
Block a user