Answer principles round 1: 1 🔴, 7 🟡 (0 misread); every effect is handed in as an argument, no test patches a global, and the traces principle scopes tools that run on their user's machine
CI / tests (pull_request) Successful in 3m43s
CI / tests (pull_request) Successful in 3m43s
This commit is contained in:
+38
-15
@@ -16,7 +16,8 @@ DONE = "done"
|
||||
# What a failed create ends with: a request Jira never answered, or answered brokenly, may still have filed the issue.
|
||||
PERHAPS_FILED = "unless Jira refused the request, the issue may exist: look under {under} before filing it again"
|
||||
# What a failed move ends with, for the same reason.
|
||||
PERHAPS_MOVED = "unless Jira refused the request, the issue may have moved: look at {key} before finishing the item again"
|
||||
SHAPE = "is not in the shape Jira Cloud's REST API version 3 documents"
|
||||
PERHAPS_MOVED ="unless Jira refused the request, the issue may have moved: look at {key} before finishing the item again"
|
||||
|
||||
|
||||
@dataclasses.dataclass(frozen=True)
|
||||
@@ -44,17 +45,21 @@ def entry_of(raw: object, property_: str) -> Entry | Failure:
|
||||
key = raw.get("key") if isinstance(raw, dict) else None
|
||||
if not isinstance(raw, dict) or not isinstance(key, str) or not ISSUE_KEY.fullmatch(key):
|
||||
return Failure("the search answered an issue without an issue key")
|
||||
# The answer's nested fields are read as Jira documents them; one missing or of another type ends the read here, as a Failure, so no later step meets it.
|
||||
try:
|
||||
fields = raw["fields"]
|
||||
return Entry(key, fields, blocked_by_links(fields), raw.get("properties", {}).get(property_, {}))
|
||||
except (AttributeError, KeyError, TypeError) as failure:
|
||||
return Failure(unshaped(key, failure))
|
||||
# Each nested field is checked as Jira documents it; one missing or of another type ends the read here, as a Failure, so no later step meets it.
|
||||
fields, properties = raw.get("fields"), raw.get("properties", {})
|
||||
if not isinstance(fields, dict):
|
||||
return Failure(f"{key} {SHAPE}: its fields are no object")
|
||||
if not isinstance(properties, dict):
|
||||
return Failure(f"{key} {SHAPE}: its properties are no object")
|
||||
links = blocked_by_links(fields)
|
||||
if isinstance(links, Failure):
|
||||
return Failure(f"{key} {SHAPE}: {links.reason}")
|
||||
return Entry(key, fields, links, properties.get(property_, {}))
|
||||
|
||||
|
||||
def unshaped(key: str, failure: Exception) -> str:
|
||||
"""Why the issue named key cannot be read, for a field that is missing or of another type than Jira documents."""
|
||||
return f"{key} is not in the shape Jira Cloud's REST API version 3 documents: {failure!r}"
|
||||
return f"{key} {SHAPE}: {failure!r}"
|
||||
|
||||
|
||||
def offer_of(raw: object) -> tuple[str, str, str, str] | None:
|
||||
@@ -115,11 +120,11 @@ def rank_refusal(site: str, key: str, before: str, authorization: str, http: Htt
|
||||
return "; ".join(str(error) for entry in refused for error in entry.get("errors") or [f"Jira answered {entry.get('status')}"]) or None
|
||||
|
||||
|
||||
def searched(site: str, what: str, jql: str, asked: dict[str, object], authorization: str, http: Http) -> list[object] | Failure:
|
||||
"""Every issue a JQL search on site finds, page by page. The dict asked holds the request's `fields` and `properties` lists. The text what names the issues sought, in the Failure of a search that doesn't end."""
|
||||
def searched(site: str, what: str, jql: str, asked: dict[str, object], authorization: str, http: Http, max_pages: int = MAX_PAGES) -> list[object] | Failure:
|
||||
"""Every issue a JQL search on site finds, page by page, up to max_pages pages. The dict asked holds the request's `fields` and `properties` lists. The text what names the issues sought, in the Failure of a search that doesn't end."""
|
||||
body: dict[str, object] = {"jql": jql, "maxResults": PAGE} | asked
|
||||
found: list[object] = []
|
||||
for _ in range(MAX_PAGES):
|
||||
for _ in range(max_pages):
|
||||
page = http("POST", f"{site}/rest/api/3/search/jql", authorization, body)
|
||||
if isinstance(page, Failure):
|
||||
return page
|
||||
@@ -129,9 +134,27 @@ def searched(site: str, what: str, jql: str, asked: dict[str, object], authoriza
|
||||
if not page.get("nextPageToken"):
|
||||
return found
|
||||
body = body | {"nextPageToken": page["nextPageToken"]}
|
||||
return Failure(f"the search for {what} did not end within {MAX_PAGES} pages")
|
||||
return Failure(f"the search for {what} did not end within {max_pages} pages")
|
||||
|
||||
|
||||
def blocked_by_links(fields: dict[str, typing.Any]) -> tuple[Link, ...]:
|
||||
"""The "is blocked by" links among an issue's `issuelinks` field. A field not in the shape Jira documents raises a KeyError or TypeError, which the caller turns into a Failure."""
|
||||
return tuple(Link(link["inwardIssue"]["key"], link["id"]) for link in fields["issuelinks"] if "inwardIssue" in link and link["type"]["inward"] == "is blocked by")
|
||||
def blocked_by_links(fields: dict[str, typing.Any]) -> tuple[Link, ...] | Failure:
|
||||
"""The "is blocked by" links among an issue's `issuelinks` field, or how that field is not in the shape Jira documents."""
|
||||
listed = fields.get("issuelinks")
|
||||
if not isinstance(listed, list):
|
||||
return Failure("its issuelinks are no list")
|
||||
found = []
|
||||
for link in listed:
|
||||
if not isinstance(link, dict):
|
||||
return Failure("its issuelinks hold a link that is no object")
|
||||
if "inwardIssue" not in link:
|
||||
continue
|
||||
kind = link.get("type")
|
||||
if not isinstance(kind, dict) or not isinstance(kind.get("inward"), str):
|
||||
return Failure("its issuelinks hold a link whose type names no inward text")
|
||||
if kind["inward"] != "is blocked by":
|
||||
continue
|
||||
blocker, id_ = link["inwardIssue"], link.get("id")
|
||||
if not isinstance(blocker, dict) or not isinstance(blocker.get("key"), str) or not isinstance(id_, str):
|
||||
return Failure("its issuelinks hold a link whose id or whose linked issue's key is no text")
|
||||
found.append(Link(blocker["key"], id_))
|
||||
return tuple(found)
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
import pathlib
|
||||
import sys
|
||||
import unittest
|
||||
|
||||
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1]))
|
||||
|
||||
from atlassian.jira import Entry, entry_of, Link
|
||||
from failure import Failure
|
||||
from atlassian import jira
|
||||
|
||||
BLOCKED = {"id": "9", "type": {"inward": "is blocked by"}, "inwardIssue": {"key": "ABC-2"}}
|
||||
SHAPE = "ABC-1 is not in the shape Jira Cloud's REST API version 3 documents: "
|
||||
|
||||
|
||||
def raw(*links: object, **more: object) -> dict[str, object]:
|
||||
return {"key": "ABC-1", "fields": {"issuelinks": list(links)}} | more
|
||||
|
||||
|
||||
class TheHeadOfAnEntry(unittest.TestCase):
|
||||
def test_holds_the_key_the_fields_the_blocked_by_links_and_the_property_asked_for(self):
|
||||
outward = {"id": "3", "type": {"inward": "is blocked by"}, "outwardIssue": {"key": "ABC-3"}}
|
||||
relates = {"id": "4", "type": {"inward": "relates to"}, "inwardIssue": {"key": "ABC-4"}}
|
||||
found = entry_of(raw(BLOCKED, outward, relates, properties={"plan": {"L": 2}}), "plan")
|
||||
self.assertEqual(found, Entry("ABC-1", {"issuelinks": [BLOCKED, outward, relates]}, (Link("ABC-2", "9"),), {"L": 2}))
|
||||
self.assertEqual(entry_of(raw(), "plan"), Entry("ABC-1", {"issuelinks": []}, (), {}))
|
||||
|
||||
def test_an_entry_without_an_issue_key_is_a_failure(self):
|
||||
for wrong in ([], {}, {"key": 7}, {"key": "abc"}):
|
||||
with self.subTest(wrong=wrong):
|
||||
self.assertEqual(entry_of(wrong, "plan"), Failure("the search answered an issue without an issue key"))
|
||||
|
||||
def test_an_entry_in_another_shape_is_a_failure_naming_the_issue_and_the_field(self):
|
||||
cases = {
|
||||
"its fields are no object": {"key": "ABC-1"},
|
||||
"its properties are no object": raw(properties=[]),
|
||||
"its issuelinks are no list": {"key": "ABC-1", "fields": {}},
|
||||
"its issuelinks hold a link that is no object": raw("link"),
|
||||
"its issuelinks hold a link whose type names no inward text": raw(BLOCKED | {"type": None}),
|
||||
"its issuelinks hold a link whose id or whose linked issue's key is no text": raw(BLOCKED | {"inwardIssue": {"key": 7}}),
|
||||
}
|
||||
for how, wrong in cases.items():
|
||||
with self.subTest(how=how):
|
||||
self.assertEqual(entry_of(wrong, "plan"), Failure(SHAPE + how))
|
||||
self.assertEqual(entry_of(raw(BLOCKED | {"id": 9}), "plan"), Failure(SHAPE + "its issuelinks hold a link whose id or whose linked issue's key is no text"))
|
||||
self.assertEqual(entry_of(raw(BLOCKED | {"inwardIssue": None}), "plan"), Failure(SHAPE + "its issuelinks hold a link whose id or whose linked issue's key is no text"))
|
||||
|
||||
def test_a_fault_in_reading_the_links_is_not_reported_as_jiras(self):
|
||||
class Faulty(dict):
|
||||
def get(self, key, default=None):
|
||||
raise TypeError("ours")
|
||||
|
||||
with self.assertRaisesRegex(TypeError, "ours"):
|
||||
entry_of({"key": "ABC-1", "fields": Faulty()}, "plan")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user