Answer principles round 2: 0 🔴, 2 🟡 (0 misread); workflow-bench hands its effects in as arguments, and no Python file of the repo patches a global
CI / tests (pull_request) Successful in 3m46s
CI / tests (pull_request) Successful in 3m46s
This commit is contained in:
+49
-15
@@ -72,23 +72,45 @@ class PullView:
|
||||
said: tuple[str, ...]
|
||||
|
||||
|
||||
def text(value: object) -> str:
|
||||
"""value, which a forge's answer must hold as a string. Raises TypeError where it is none."""
|
||||
if not isinstance(value, str):
|
||||
raise TypeError(f"{value!r} is not a string")
|
||||
# What `held` gives where the answer lacks the key asked for: no text, so the check of the field refuses it.
|
||||
MISSING = object()
|
||||
|
||||
|
||||
def held(value: object, *path: str) -> object:
|
||||
"""What a forge's answer holds along path, one key per level; MISSING where a level is no object or lacks its key."""
|
||||
for key in path:
|
||||
value = value.get(key, MISSING) if isinstance(value, dict) else MISSING
|
||||
return value
|
||||
|
||||
|
||||
def pull_view(printed: str) -> PullView | Failure:
|
||||
"""The pull request that gh printed, with each field checked here, so what decides on it meets no other shape."""
|
||||
unshaped = Failure(f"gh pr view printed no review decision: {printed.strip()[:200]}")
|
||||
try:
|
||||
pull = json.loads(printed)
|
||||
author = text(pull["author"]["login"])
|
||||
reviews = tuple(Review(text(review["author"]["login"]), text(review["state"]), text(review["submittedAt"] or ""), text(review["commit"]["oid"]) if review["state"] == CHANGES_REQUESTED else "") for review in pull["reviews"])
|
||||
commented = tuple(text(comment["createdAt"]) for comment in pull["comments"] if text(comment["author"]["login"]) == author)
|
||||
return PullView(bool(pull["isDraft"]), text(pull["reviewDecision"] or ""), reviews, tuple(review.submitted_at for review in reviews if review.author == author) + commented)
|
||||
except (ValueError, KeyError, TypeError):
|
||||
return Failure(f"gh pr view printed no review decision: {printed.strip()[:200]}")
|
||||
except ValueError:
|
||||
return unshaped
|
||||
author, draft, decision, listed, comments = held(pull, "author", "login"), held(pull, "isDraft"), held(pull, "reviewDecision") or "", held(pull, "reviews"), held(pull, "comments")
|
||||
if not isinstance(author, str) or draft is MISSING or not isinstance(decision, str) or not isinstance(listed, list) or not isinstance(comments, list):
|
||||
return unshaped
|
||||
reviews = []
|
||||
for review in listed:
|
||||
reviewer, state, submitted_at = held(review, "author", "login"), held(review, "state"), held(review, "submittedAt") or ""
|
||||
commit = held(review, "commit", "oid") if state == CHANGES_REQUESTED else ""
|
||||
if not isinstance(reviewer, str) or not isinstance(state, str) or not isinstance(submitted_at, str) or not isinstance(commit, str):
|
||||
return unshaped
|
||||
reviews.append(Review(reviewer, state, submitted_at, commit))
|
||||
commented = []
|
||||
for comment in comments:
|
||||
commenter, created_at = held(comment, "author", "login"), held(comment, "createdAt")
|
||||
if not isinstance(commenter, str):
|
||||
return unshaped
|
||||
if commenter != author:
|
||||
continue
|
||||
if not isinstance(created_at, str):
|
||||
return unshaped
|
||||
commented.append(created_at)
|
||||
return PullView(bool(draft), decision, tuple(reviews), tuple(review.submitted_at for review in reviews if review.author == author) + tuple(commented))
|
||||
|
||||
|
||||
def answered(pull: PullView, head: str) -> bool:
|
||||
@@ -135,12 +157,24 @@ class Thread:
|
||||
|
||||
def threads_read(printed: str) -> tuple[str, tuple[Thread, ...]] | Failure:
|
||||
"""Who opened the pull request and its review threads, as gh printed THREADS' answer, with each field checked here."""
|
||||
unshaped = Failure(f"gh api graphql printed no review threads: {printed.strip()[:200]}")
|
||||
try:
|
||||
pull = json.loads(printed)["data"]["repository"]["pullRequest"]
|
||||
threads = tuple(Thread(text(thread["path"]), bool(thread["isResolved"]), tuple(text(comment["author"]["login"]) for comment in thread["comments"]["nodes"])) for thread in pull["reviewThreads"]["nodes"])
|
||||
return text(pull["author"]["login"]), threads
|
||||
except (ValueError, KeyError, TypeError):
|
||||
return Failure(f"gh api graphql printed no review threads: {printed.strip()[:200]}")
|
||||
pull = held(json.loads(printed), "data", "repository", "pullRequest")
|
||||
except ValueError:
|
||||
return unshaped
|
||||
author, nodes = held(pull, "author", "login"), held(pull, "reviewThreads", "nodes")
|
||||
if not isinstance(author, str) or not isinstance(nodes, list):
|
||||
return unshaped
|
||||
threads = []
|
||||
for thread in nodes:
|
||||
path, resolved, comments = held(thread, "path"), held(thread, "isResolved"), held(thread, "comments", "nodes")
|
||||
if not isinstance(path, str) or resolved is MISSING or not isinstance(comments, list):
|
||||
return unshaped
|
||||
last = tuple(login for comment in comments if isinstance(login := held(comment, "author", "login"), str))
|
||||
if len(last) != len(comments):
|
||||
return unshaped
|
||||
threads.append(Thread(path, bool(resolved), last))
|
||||
return author, tuple(threads)
|
||||
|
||||
|
||||
def waits(author: str, threads: tuple[Thread, ...]) -> list[str]:
|
||||
|
||||
@@ -35,7 +35,7 @@ class ThePullRequestAsViewed(unittest.TestCase):
|
||||
self.assertEqual(reviews.pull_view(printed), PullView(False, CHANGES_REQUESTED, (Review("rev", CHANGES_REQUESTED, "2026-11-02T10:00:00Z", "abc"), Review("me", "PENDING", "", "")), ("", "2026-11-02T11:00:00Z")))
|
||||
|
||||
def test_an_answer_in_another_shape_is_a_failure_naming_what_gh_printed(self):
|
||||
unshaped = ("not json", "[]", json.dumps(PULL | {"reviews": None}), json.dumps(PULL | {"author": None}), json.dumps(PULL | {"reviews": [ASKED | {"commit": None}]}), json.dumps(PULL | {"reviews": [ASKED | {"submittedAt": 7}]}), json.dumps({key: value for key, value in PULL.items() if key != "isDraft"}))
|
||||
unshaped = ("not json", "[]", json.dumps(PULL | {"reviews": None}), json.dumps(PULL | {"author": None}), json.dumps(PULL | {"reviews": [ASKED | {"commit": None}]}), json.dumps(PULL | {"reviews": [ASKED | {"submittedAt": 7}]}), json.dumps(PULL | {"comments": [{"author": {"login": 7}, "createdAt": "2026-11-02T11:00:00Z"}]}), json.dumps(PULL | {"comments": [{"author": {"login": PULL["author"]["login"]}, "createdAt": None}]}), json.dumps(PULL | {"reviews": [{key: value for key, value in ASKED.items() if key != "submittedAt"}]}), json.dumps({key: value for key, value in PULL.items() if key != "isDraft"}))
|
||||
for printed in unshaped:
|
||||
with self.subTest(printed=printed):
|
||||
self.assertEqual(reviews.pull_view(printed), Failure(f"gh pr view printed no review decision: {printed[:200]}"))
|
||||
@@ -78,7 +78,7 @@ class TheThreadsThatWait(unittest.TestCase):
|
||||
self.assertEqual(reviews.waiting_threads("7", lambda args: printed), ["a.py", "b.py"])
|
||||
|
||||
def test_an_answer_in_another_shape_is_a_failure_naming_what_gh_printed(self):
|
||||
for printed in ("not json", "{}", json.dumps({"data": {"repository": {"pullRequest": {"author": {"login": "me"}, "reviewThreads": {"nodes": [{"isResolved": False}]}}}}})):
|
||||
for printed in ("not json", "{}", json.dumps({"data": {"repository": {"pullRequest": {"author": {"login": "me"}, "reviewThreads": {"nodes": [{"isResolved": False}]}}}}}), json.dumps({"data": {"repository": {"pullRequest": {"author": {"login": "me"}, "reviewThreads": {"nodes": [{"isResolved": False, "path": "a.py", "comments": {"nodes": [{"author": {"login": 7}}]}}]}}}}}), json.dumps({"data": {"repository": {"pullRequest": {"author": {"login": "me"}, "reviewThreads": {"nodes": [{"path": "a.py", "comments": {"nodes": []}}]}}}}})):
|
||||
with self.subTest(printed=printed):
|
||||
self.assertEqual(reviews.waiting_threads("7", lambda args: printed), Failure(f"gh api graphql printed no review threads: {printed[:200]}"))
|
||||
|
||||
|
||||
Reference in New Issue
Block a user