Convert every description of a plan read in one adf-codec container run, and take a heavy slot around the build of the codec's image
CI / tests (pull_request) Successful in 2m57s
CI / tests (pull_request) Successful in 2m57s
This commit is contained in:
+13
-20
@@ -4,13 +4,12 @@ An item is an open child issue. Its ID is the issue's number, so every child mus
|
||||
|
||||
The store only reads so far: every write is a Failure."""
|
||||
import base64
|
||||
import concurrent.futures
|
||||
import dataclasses
|
||||
import pathlib
|
||||
import re
|
||||
import typing
|
||||
|
||||
from adf_codec.convert import adf_to_markdown
|
||||
from adf_codec.convert import adfs_to_markdown, Refused
|
||||
from plan_store.markdown_edits import bold_cell
|
||||
from project_config.settings import Epic
|
||||
from release_plan.todo_table import EXEMPTIONS, is_exempt, Item, MOVES, parse_moves, parse_scores, SHAPE
|
||||
@@ -30,13 +29,11 @@ TIMEOUT = 30
|
||||
PAGE = 100
|
||||
# The pages one read asks for at most, so an answer that never names a last page ends the read.
|
||||
MAX_PAGES = 100
|
||||
# The adf-codec containers run at once, each converting one description: one heavy slot's CPUs.
|
||||
CONVERSIONS = 4
|
||||
READ_ONLY = "a plan in Jira is only read so far: file, edit and finish its issues in Jira"
|
||||
|
||||
# Sends a request with the Authorization header given, the body as JSON where there is one, and returns the JSON answer, or a Failure saying why there is none.
|
||||
Http = typing.Callable[[str, str, str, object], object]
|
||||
ToMarkdown = typing.Callable[[dict[str, typing.Any]], str | Failure]
|
||||
ToMarkdown = typing.Callable[[list[dict[str, typing.Any]]], list[str] | Refused | Failure]
|
||||
|
||||
|
||||
def http_json(method: str, url: str, authorization: str, body: object) -> object:
|
||||
@@ -75,7 +72,7 @@ class JiraPlan:
|
||||
epic: Epic
|
||||
env_file: pathlib.Path
|
||||
http: Http = http_json
|
||||
to_markdown: ToMarkdown = adf_to_markdown
|
||||
to_markdown: ToMarkdown = adfs_to_markdown
|
||||
|
||||
@property
|
||||
def label(self) -> str:
|
||||
@@ -164,8 +161,8 @@ def plan_of(issues: list[Issue], epic: Epic, to_markdown: ToMarkdown) -> Plan |
|
||||
items = [item(issue, project, ids) for issue in issues]
|
||||
if failed := next((found for found in items if isinstance(found, Failure)), None):
|
||||
return failed
|
||||
# The descriptions convert last, since each costs a container run, which a read that fails doesn't need.
|
||||
texts = details(issues, to_markdown)
|
||||
# The descriptions convert last, since that costs a container run, which a read that fails doesn't need.
|
||||
texts = details(issues, epic, to_markdown)
|
||||
if isinstance(texts, Failure):
|
||||
return texts
|
||||
return Plan([dataclasses.replace(found, detail=texts.get(found.id, "")) for found in items if isinstance(found, Item)])
|
||||
@@ -175,19 +172,15 @@ def number(key: str) -> int:
|
||||
return int(key.rpartition("-")[2])
|
||||
|
||||
|
||||
def details(issues: list[Issue], to_markdown: ToMarkdown) -> dict[int, str] | Failure:
|
||||
"""Each described issue's description as markdown, under the issue's number. The first converts alone, since that run builds the codec's image where the machine has none; the rest convert CONVERSIONS at a time."""
|
||||
def details(issues: list[Issue], epic: Epic, to_markdown: ToMarkdown) -> dict[int, str] | Failure:
|
||||
"""Each described issue's description as markdown, under the issue's number, all converted at once."""
|
||||
described = [(issue, issue.description) for issue in issues if issue.description is not None]
|
||||
texts: list[str | Failure] = [to_markdown(description) for _, description in described[:1]]
|
||||
if not any(isinstance(text, Failure) for text in texts):
|
||||
with concurrent.futures.ThreadPoolExecutor(CONVERSIONS) as pool:
|
||||
texts += pool.map(to_markdown, [description for _, description in described[1:]])
|
||||
found = {}
|
||||
for (issue, _), text in zip(described, texts):
|
||||
if isinstance(text, Failure):
|
||||
return Failure(f"{issue.key}'s description: {text.reason}")
|
||||
found[issue.number] = text.strip()
|
||||
return found
|
||||
texts = to_markdown([description for _, description in described])
|
||||
if isinstance(texts, Refused):
|
||||
return Failure(f"{described[texts.index][0].key}'s description: {texts.failure.reason}")
|
||||
if isinstance(texts, Failure):
|
||||
return Failure(f"cannot convert the descriptions of {epic.key}'s children to markdown: {texts.reason}")
|
||||
return {issue.number: text.strip() for (issue, _), text in zip(described, texts)}
|
||||
|
||||
|
||||
def item(issue: Issue, project: str, ids: set[int]) -> Item | Failure:
|
||||
|
||||
@@ -2,8 +2,6 @@ import base64
|
||||
import pathlib
|
||||
import sys
|
||||
import tempfile
|
||||
import threading
|
||||
import time
|
||||
import unittest
|
||||
import unittest.mock
|
||||
|
||||
@@ -13,6 +11,7 @@ from project_config.settings import Epic
|
||||
from failure import Failure
|
||||
from plan_store import jira_epic
|
||||
from plan_store.stores import Plan
|
||||
from adf_codec.convert import Refused
|
||||
from release_plan.release import release_of
|
||||
|
||||
EPIC = Epic("https://acme.atlassian.net", "ABC-12")
|
||||
@@ -48,7 +47,7 @@ class JiraPlan(unittest.TestCase):
|
||||
self.calls: list[tuple[str, str, str, object]] = []
|
||||
self.epic: object = {"key": "ABC-12"}
|
||||
self.epic_calls: list[tuple[str, str, str, object]] = []
|
||||
self.converted: list[object] = []
|
||||
self.converted: list[list[dict[str, object]]] = []
|
||||
|
||||
def http(self, method: str, url: str, authorization: str, body: object) -> object:
|
||||
"""Stands in for Jira: answers self.epic to the request for the epic, and the next of self.pages to each search."""
|
||||
@@ -58,9 +57,9 @@ class JiraPlan(unittest.TestCase):
|
||||
self.calls.append((method, url, authorization, body))
|
||||
return self.pages[len(self.calls) - 1]
|
||||
|
||||
def to_markdown(self, document: dict[str, object]) -> str | Failure:
|
||||
self.converted.append(document)
|
||||
return text_of(document)
|
||||
def to_markdown(self, documents: list[dict[str, object]]) -> list[str] | Refused | Failure:
|
||||
self.converted.append(documents)
|
||||
return [text_of(document) for document in documents]
|
||||
|
||||
def store(self) -> jira_epic.JiraPlan:
|
||||
return jira_epic.JiraPlan(EPIC, self.env, self.http, self.to_markdown)
|
||||
@@ -167,22 +166,6 @@ class JiraPlan(unittest.TestCase):
|
||||
with unittest.mock.patch.object(jira_epic, "MAX_PAGES", 2):
|
||||
self.assertEqual(self.store().read(), Failure("the search for ABC-12's children did not end within 2 pages"))
|
||||
|
||||
def test_converts_at_most_four_descriptions_at_once(self):
|
||||
lock, running, most = threading.Lock(), [0], [0]
|
||||
|
||||
def slow(document: dict[str, object]) -> str:
|
||||
with lock:
|
||||
running[0] += 1
|
||||
most[0] = max(most[0], running[0])
|
||||
time.sleep(0.02)
|
||||
with lock:
|
||||
running[0] -= 1
|
||||
return "text"
|
||||
|
||||
self.pages = [{"issues": [issue(f"ABC-{number}", description=adf("Why.")) for number in range(1, 41)]}]
|
||||
self.assertIsInstance(jira_epic.JiraPlan(EPIC, self.env, self.http, slow).read(), Plan)
|
||||
self.assertLessEqual(most[0], 4)
|
||||
|
||||
def test_an_issue_in_another_shape_fails_the_read_naming_it(self):
|
||||
good = issue("ABC-1")
|
||||
for wrong in ({"key": "ABC-1"}, issue("ABC-1", summary=None), issue("ABC-1", description="text"), good | {"fields": good["fields"] | {"fixVersions": [{"name": 7}]}}, good | {"fields": good["fields"] | {"issuelinks": [{"type": {"inward": "is blocked by"}, "inwardIssue": {"key": 7}}]}}, good | {"fields": good["fields"] | {"issuelinks": [{"type": {"inward": "is blocked by"}, "inwardIssue": {"key": "ABC-x"}}]}}, good | {"properties": None}): # type: ignore[arg-type, operator]
|
||||
@@ -191,22 +174,20 @@ class JiraPlan(unittest.TestCase):
|
||||
self.assertIsInstance(failure, Failure)
|
||||
self.assertTrue(failure.reason.startswith("ABC-1 is not in the shape Jira Cloud's REST API version 3 documents: "), failure) # type: ignore[union-attr]
|
||||
|
||||
def test_converts_each_description_once_and_none_for_an_issue_without_one(self):
|
||||
def test_converts_every_description_in_one_run_and_none_for_an_issue_without_one(self):
|
||||
found = self.items(issue("ABC-1", description=adf("One.")), issue("ABC-2"), issue("ABC-3", description=adf("Three.")), issue("ABC-4", description=adf("Four.")))
|
||||
self.assertEqual([item.detail for item in found], ["One.", "", "Three.", "Four."])
|
||||
self.assertEqual(len(self.converted), 3)
|
||||
|
||||
def test_a_first_description_the_codec_refuses_converts_no_other(self):
|
||||
calls: list[object] = []
|
||||
store = jira_epic.JiraPlan(EPIC, self.env, self.http, lambda document: calls.append(document) or Failure("no docker")) # type: ignore[func-returns-value]
|
||||
self.pages = [{"issues": [issue("ABC-1", description=adf("One.")), issue("ABC-2", description=adf("Two."))]}]
|
||||
self.assertEqual(store.read(), Failure("ABC-1's description: no docker"))
|
||||
self.assertEqual(len(calls), 1)
|
||||
self.assertEqual(self.converted, [[adf("One."), adf("Three."), adf("Four.")]])
|
||||
|
||||
def test_a_description_the_codec_refuses_fails_the_read_naming_the_issue(self):
|
||||
store = jira_epic.JiraPlan(EPIC, self.env, self.http, lambda document: Failure("adf-codec refused"))
|
||||
store = jira_epic.JiraPlan(EPIC, self.env, self.http, lambda documents: Refused(1, Failure("adf-codec refused")))
|
||||
self.pages = [{"issues": [issue("ABC-33", description=adf("x")), issue("ABC-34"), issue("ABC-35", description=adf("y"))]}]
|
||||
self.assertEqual(store.read(), Failure("ABC-35's description: adf-codec refused"))
|
||||
|
||||
def test_a_codec_that_cannot_run_fails_the_read(self):
|
||||
store = jira_epic.JiraPlan(EPIC, self.env, self.http, lambda documents: Failure("docker is not installed"))
|
||||
self.pages = [{"issues": [issue("ABC-34", description=adf("x"))]}]
|
||||
self.assertEqual(store.read(), Failure("ABC-34's description: adf-codec refused"))
|
||||
self.assertEqual(store.read(), Failure("cannot convert the descriptions of ABC-12's children to markdown: docker is not installed"))
|
||||
|
||||
def test_an_env_file_without_the_credentials_fails_before_any_request(self):
|
||||
self.env.write_text("ATLASSIAN_EMAIL=me@example.com\n")
|
||||
|
||||
Reference in New Issue
Block a user