Merge remote-tracking branch 'origin/main' into item-381
CI / tests (pull_request) Successful in 4m34s
CI / tests (pull_request) Successful in 4m34s
# Conflicts: # docs/reading-cost.md
This commit is contained in:
@@ -3,8 +3,8 @@
|
||||
It runs @larvit/adf-codec, a Node package, in a throwaway container that holds node_shim/shim.mjs; the shim's header says what its exit status means.
|
||||
|
||||
adf-codec has two pairs of calls, and each function here names the one it runs:
|
||||
- The lossless pair, `*_lossless`, for a document that already exists in Jira or Confluence and is edited and saved back. It keeps each block's localId and the empty paragraphs Jira uses for spacing, so a document read and saved unedited comes back identical.
|
||||
- The portable pair, with no suffix, for new text written from scratch, and for markdown shown to a person. It drops the localId and the empty paragraphs, so saving a document that exists changes it.
|
||||
- The portable pair, with no suffix, which the loop's tools use. It drops each block's localId and the empty paragraphs Jira uses for spacing, so saving a document read by it changes the document; a tool that saves one unedited sends nothing instead (`docs/decisions.md` → `project config: the loop shows a Jira description and a Confluence page as adf-codec's portable markdown, …`).
|
||||
- The lossless pair, `*_lossless`, which keeps both, so a document read and saved unedited comes back identical. Its markdown wraps every block in `!adf:` marker lines, so no tool reads by it yet.
|
||||
"""
|
||||
import contextlib
|
||||
import dataclasses
|
||||
|
||||
@@ -185,6 +185,13 @@ class JiraPlan:
|
||||
description = self.to_adf(detail)
|
||||
return Failure(f"cannot convert the item's detail to a Jira description: {description.reason}") if isinstance(description, Failure) else description
|
||||
|
||||
def shows(self, issue: Issue, detail: str) -> bool:
|
||||
"""Whether detail is the issue's description as a read shows it. An edit leaves such a description as Jira holds it, since converting the markdown back would drop each block's localId and Jira's spacing paragraphs."""
|
||||
if issue.description is None:
|
||||
return not detail.strip()
|
||||
texts = self.to_markdown([issue.description])
|
||||
return isinstance(texts, list) and texts[0].strip() == detail.strip()
|
||||
|
||||
def edit(self, id_: int, fields: dict[str, str], detail: str | None, comment: str | None = None) -> Written | Failure:
|
||||
"""Sets the fields and the detail in one request, so Jira takes all of the edit or none, and then posts comment on the issue. `revision_of` says what an edit may set.
|
||||
|
||||
@@ -203,7 +210,7 @@ class JiraPlan:
|
||||
if isinstance(revision, Failure):
|
||||
return revision
|
||||
body = revision.body
|
||||
if detail is not None:
|
||||
if detail is not None and not self.shows(issue, detail):
|
||||
description = self.description_of(detail)
|
||||
if isinstance(description, Failure):
|
||||
return description
|
||||
@@ -217,7 +224,7 @@ class JiraPlan:
|
||||
return version
|
||||
body = body | {"fields": body.get("fields", {}) | {"fixVersions": [{"name": version}]}}
|
||||
url = f"{self.epic.site}/rest/api/3/issue/{key}"
|
||||
edited = self.http("PUT", url, authorization, body)
|
||||
edited = self.http("PUT", url, authorization, body) if body else None
|
||||
if isinstance(edited, Failure):
|
||||
return Failure(f"cannot edit {key}: {edited.reason}; {PERHAPS_EDITED.format(key=key)}")
|
||||
if said is None:
|
||||
|
||||
@@ -500,7 +500,7 @@ class Editing(unittest.TestCase):
|
||||
return adf(markdown)
|
||||
|
||||
def edit(self, id_: int, fields: dict[str, str], detail: str | None = None, comment: str | None = None):
|
||||
return jira_epic.JiraPlan(EPIC, self.env, self.http, text_of, self.to_adf, scoring=Stated()).edit(id_, fields, detail, comment)
|
||||
return jira_epic.JiraPlan(EPIC, self.env, self.http, lambda documents: [text_of(document) for document in documents], self.to_adf, scoring=Stated()).edit(id_, fields, detail, comment)
|
||||
|
||||
def writes(self) -> list[tuple[str, str, object]]:
|
||||
"""Each request that changes Jira, in order."""
|
||||
@@ -518,7 +518,21 @@ class Editing(unittest.TestCase):
|
||||
self.edit(34, {"E": "3"})
|
||||
self.assertEqual([body for _, _, body in self.writes()], [{"fields": {"summary": "Count every run."}}, {"properties": [{"key": "plan-item", "value": PROPERTY | {"E": 3}}]}])
|
||||
|
||||
def test_a_detail_unchanged_since_the_read_leaves_the_description_as_jira_holds_it(self):
|
||||
# Jira's own spacing paragraph and localIds, which a description converted back from markdown would lose.
|
||||
held = {"type": "doc", "version": 1, "content": [{"type": "paragraph", "attrs": {"localId": "p-1"}, "content": [{"type": "text", "text": "Why it counts."}]}, {"type": "paragraph", "attrs": {"localId": "p-2"}}]}
|
||||
self.open = [issue("ABC-34", scores=PROPERTY, description=held)]
|
||||
self.assertEqual(self.edit(34, {"L": "2"}, "Why it counts.\n"), Written(34))
|
||||
self.assertEqual(self.edit(34, {}, "Why it counts."), Written(34))
|
||||
self.assertEqual(self.writes(), [("PUT", f"{ISSUES}/ABC-34", {"properties": [{"key": "plan-item", "value": PROPERTY | {"L": 2}}]})])
|
||||
|
||||
def test_an_edited_detail_replaces_the_description(self):
|
||||
self.open = [issue("ABC-34", scores=PROPERTY, description=adf("Why it counts."))]
|
||||
self.edit(34, {}, "Why it counts twice.")
|
||||
self.assertEqual(self.writes(), [("PUT", f"{ISSUES}/ABC-34", {"fields": {"description": adf("Why it counts twice.")}})])
|
||||
|
||||
def test_an_empty_detail_removes_the_description(self):
|
||||
self.open = [issue("ABC-34", scores=PROPERTY, description=adf("Why it counts."))]
|
||||
self.edit(34, {}, " \n")
|
||||
self.assertEqual(self.writes(), [("PUT", f"{ISSUES}/ABC-34", {"fields": {"description": None}})])
|
||||
|
||||
|
||||
@@ -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 the version and title, and sends nothing where its text has that digest, so a Held's version holds all three, as JSON. The digest is None where the version came from a request that held no text."""
|
||||
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,14 @@ class ConfluencePage:
|
||||
seen = self.seen_of(over)
|
||||
if isinstance(seen, Failure):
|
||||
return seen
|
||||
# A read's markdown leaves out the localIds and spacing paragraphs Confluence holds, so a write of the same text back would drop them: send nothing. Without a write Confluence answers no 409, so read the version to refuse where a teammate saved since.
|
||||
if over is not None and seen.digest == digest_of(text):
|
||||
current = self.fetched("")
|
||||
if isinstance(current, Failure):
|
||||
return current
|
||||
if current[0].version != seen.version:
|
||||
return self.saved_since(seen.version, f"Confluence holds version {current[0].version}")
|
||||
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}")
|
||||
@@ -95,10 +105,14 @@ class ConfluencePage:
|
||||
body = {"id": self.page.page_id, "status": "current", "title": seen.title, "body": {"representation": "atlas_doc_format", "value": json.dumps(document)}, "version": {"number": seen.version + 1}}
|
||||
answer = self.http("PUT", self.url, authorization, body)
|
||||
if isinstance(answer, ErrorAnswer) and answer.status == CONFLICT:
|
||||
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})")
|
||||
return self.saved_since(seen.version, 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 saved_since(self, version: int, evidence: str) -> Failure:
|
||||
"""The refusal of a write over version, which a teammate's save replaced, as evidence shows."""
|
||||
return Failure(f"someone saved {self.label} since the loop read version {version} of it, so the loop didn't write it: never overwrite their edit, but ask the human what to do ({evidence})")
|
||||
|
||||
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 +138,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()
|
||||
|
||||
@@ -80,18 +80,50 @@ 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"}'))
|
||||
written = store.write("- a decision\n", held.version)
|
||||
assert isinstance(written, Held), written
|
||||
self.assertEqual(written.text, "- a decision\n")
|
||||
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.assertNotIn("PUT", [call[0] for call in self.confluence.calls])
|
||||
self.assertEqual(self.confluence.calls[-1], ("GET", URL, AUTHORIZATION, None))
|
||||
self.assertEqual((self.confluence.version, self.confluence.value), (2, value))
|
||||
|
||||
def test_refuses_to_save_the_text_a_read_found_where_a_teammate_saved_since(self) -> None:
|
||||
self.confluence.teammate_saves("- a decision\n")
|
||||
held = self.new_store().held()
|
||||
assert isinstance(held, Held), held
|
||||
self.confluence.teammate_saves("- their decision\n")
|
||||
written = self.new_store().write(held.text, held.version)
|
||||
self.assertEqual(written, Failure(f"someone saved {PAGE.spelling} since the loop read version 2 of it, so the loop didn't write it: never overwrite their edit, but ask the human what to do (Confluence holds version 3)"))
|
||||
self.assertNotIn("PUT", [call[0] for call in self.confluence.calls])
|
||||
self.assertEqual(self.new_store().read(), "- their decision\n")
|
||||
|
||||
def test_an_unedited_save_fails_where_the_pages_version_cannot_be_read(self) -> None:
|
||||
held = self.new_store().held()
|
||||
assert isinstance(held, Held), held
|
||||
store = ConfluencePage(PAGE, self.env, lambda *_: ErrorAnswer("GET /wiki/api/v2/pages/2293761 answered 404: Not Found", 404), to_markdown, to_adf)
|
||||
self.assertEqual(store.write(held.text, held.version), Failure(f"cannot read {PAGE.spelling}: GET /wiki/api/v2/pages/2293761 answered 404: Not Found"))
|
||||
|
||||
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
|
||||
self.confluence.teammate_saves("their edit\n")
|
||||
written = self.new_store().write("my edit\n", held.version)
|
||||
assert isinstance(written, Failure), written
|
||||
self.assertIn("someone saved", written.reason)
|
||||
self.assertIn("ask the human", written.reason)
|
||||
self.assertIn(PAGE.spelling, written.reason)
|
||||
self.assertEqual(written, Failure(f"someone saved {PAGE.spelling} since the loop read version 1 of it, so the loop didn't write it: never overwrite their edit, but ask the human what to do (PUT /wiki/api/v2/pages/{PAGE.page_id} answered 409: Version must be incremented on update. Current version is: 2)"))
|
||||
self.assertEqual(self.new_store().read(), "their edit\n")
|
||||
|
||||
def test_keeps_no_version_itself_so_a_read_never_moves_the_version_a_write_names(self) -> None:
|
||||
|
||||
Reference in New Issue
Block a user