Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
bump: patch
---

### Fixed

- The merge gate's force-push read no longer fails on a pull request that
changes a lane source and has any timeline item that is not a force push.
GitHub's `totalCount` on a `timelineItems` connection counts every item,
whatever `itemTypes` names, so the read now treats the listing as whole only
when its last page answers `hasNextPage: false`, and raises on a page whose
pagination answer is missing or malformed, or whose force-push node is not an
object carrying `beforeCommit`.
Original file line number Diff line number Diff line change
Expand Up @@ -3422,7 +3422,7 @@ def _main_reviews(
_FORCE_PUSHES_QUERY = (
"query($o:String!,$r:String!,$n:Int!,$c:String){repository(owner:$o,name:$r){"
"pullRequest(number:$n){timelineItems(first:100,after:$c,itemTypes:[HEAD_REF_FORCE_PUSHED_EVENT])"
"{totalCount pageInfo{hasNextPage endCursor}"
"{pageInfo{hasNextPage endCursor}"
"nodes{... on HeadRefForcePushedEvent{beforeCommit{oid}}}}}}}"
)
# A pull request rebased more often than this is past what the gate reads.
Expand All @@ -3433,11 +3433,12 @@ def _force_pushed_heads(repo: str, number: int) -> list[str]:
"""The heads force pushes replaced, which the commits list no longer
names but whose recorded counts still hold, read a page of 100 at a time
up to _FORCE_PUSH_PAGES. Any doubt raises, so the evidence is unbound
rather than missing a count."""
rather than missing a count. The listing is whole when its last page says
`hasNextPage: false`: the connection's totalCount counts every timeline
item, not only the itemTypes asked for, so no count checks it."""
owner, name = repo.split("/", 1)
nodes: list[Any] = []
cursor = None
total = None
for _ in range(_FORCE_PUSH_PAGES):
args = [
"api",
Expand All @@ -3461,20 +3462,20 @@ def _force_pushed_heads(repo: str, number: int) -> list[str]:
info = json_object(items.get("pageInfo"))
if not is_json_array(page):
raise RuntimeError(f"#{number} force pushes: unread")
total = items.get("totalCount")
nodes.extend(page)
if info.get("hasNextPage") is not True:
more = info.get("hasNextPage")
if more is False:
break
cursor = info.get("endCursor")
if not isinstance(cursor, str) or not cursor:
raise RuntimeError(f"#{number} force pushes: unread")
if more is not True or not isinstance(cursor, str) or not cursor:
raise RuntimeError(f"#{number} force pushes: unread or incomplete")
else:
raise RuntimeError(f"#{number} force pushes: past {_FORCE_PUSH_PAGES} pages")
if total != len(nodes):
raise RuntimeError(f"#{number} force pushes: unread or incomplete")
heads = []
for node in nodes:
oid = json_object(json_object(node).get("beforeCommit")).get("oid")
if not isinstance(node, dict) or "beforeCommit" not in node:
raise RuntimeError(f"#{number} force pushes: unread")
oid = json_object(node["beforeCommit"]).get("oid")
# GitHub leaves beforeCommit null when the old commit is gone.
if oid is not None:
heads.append(str(oid))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2853,24 +2853,16 @@ class ForcePushedHeadsTests(unittest.TestCase):
hold, read across pages; a listing that is not whole raises."""

@staticmethod
def page(total: int, oids: list[Any], cursor: str | None = None) -> dict:
def answer(items: dict) -> dict:
return {"data": {"repository": {"pullRequest": {"timelineItems": items}}}}

@classmethod
def page(cls, oids: list[Any], cursor: str | None = None) -> dict:
nodes = [
{"beforeCommit": None if oid is None else {"oid": oid}} for oid in oids
]
info = {"hasNextPage": cursor is not None, "endCursor": cursor}
return {
"data": {
"repository": {
"pullRequest": {
"timelineItems": {
"totalCount": total,
"pageInfo": info,
"nodes": nodes,
}
}
}
}
}
return cls.answer({"pageInfo": info, "nodes": nodes})

def heads(self, *pages: dict) -> tuple[list[str], mock.MagicMock]:
with mock.patch.object(
Expand All @@ -2879,29 +2871,107 @@ def heads(self, *pages: dict) -> tuple[list[str], mock.MagicMock]:
return merge._force_pushed_heads("owner/repo", 5), read

def test_names_each_replaced_head(self) -> None:
heads, _ = self.heads(self.page(3, ["a" * 40, None, "b" * 40]))
heads, _ = self.heads(self.page(["a" * 40, None, "b" * 40]))
self.assertEqual(heads, ["a" * 40, "b" * 40])

def test_reads_every_page_by_its_cursor(self) -> None:
heads, read = self.heads(
self.page(101, ["a" * 40] * 100, cursor="next"),
self.page(101, ["b" * 40]),
self.page(["a" * 40] * 100, cursor="next"),
self.page(["b" * 40]),
)
self.assertEqual(heads, ["a" * 40] * 100 + ["b" * 40])
self.assertNotIn("c=next", read.call_args_list[0].args[0])
self.assertIn("c=next", read.call_args_list[1].args[0])

def test_a_count_the_pages_do_not_reach_raises(self) -> None:
with self.assertRaises(RuntimeError):
self.heads(self.page(101, ["a" * 40]))
# Recorded GitHub answers to the gate's query with totalCount,
# filteredCount and pageCount added: totalCount counts every timeline
# item, whatever itemTypes names. Nine items, none a force push:
NO_FORCE_PUSH = {
"totalCount": 9,
"filteredCount": 0,
"pageCount": 0,
"pageInfo": {"hasNextPage": False, "endCursor": None},
"nodes": [],
}
# Nine items, one a force push:
ONE_FORCE_PUSH = {
"totalCount": 9,
"filteredCount": 1,
"pageCount": 1,
"pageInfo": {
"hasNextPage": False,
"endCursor": "Y3Vyc29yOnYyOpPPAAABodNjVagBqzM1OTk2MjA1Njc1",
},
"nodes": [
{
"__typename": "HeadRefForcePushedEvent",
"beforeCommit": {"oid": "aa6d0971946598e912086838a6af2b21785cf3ec"},
}
],
}

def test_other_timeline_items_and_no_force_push_read_as_none(self) -> None:
heads, _ = self.heads(self.answer(self.NO_FORCE_PUSH))
self.assertEqual(
heads, [], "pull request 15846's recorded answer: no head replaced"
)

def test_a_force_push_among_other_items_names_its_head(self) -> None:
heads, _ = self.heads(self.answer(self.ONE_FORCE_PUSH))
self.assertEqual(
heads,
["aa6d0971946598e912086838a6af2b21785cf3ec"],
"pull request 15860's recorded answer: one force push replaced aa6d0971",
)

def test_a_page_that_is_not_an_array_raises(self) -> None:
for nodes in (None, {}, "nodes"):
info = {"hasNextPage": False, "endCursor": None}
with (
self.subTest(nodes=nodes),
self.assertRaises(RuntimeError, msg="a page of nodes not read"),
):
self.heads(self.answer({"pageInfo": info, "nodes": nodes}))

def test_a_malformed_node_raises(self) -> None:
for node in (None, {}, "node"):
with (
self.subTest(node=node),
self.assertRaises(RuntimeError, msg="a force push not read"),
):
self.heads(
self.answer(
{
"pageInfo": {"hasNextPage": False, "endCursor": None},
"nodes": [node],
}
)
)

def test_a_force_push_whose_old_commit_is_gone_names_no_head(self) -> None:
heads, _ = self.heads(self.page([None]))
self.assertEqual(heads, [], "beforeCommit null: no head to name")

def test_a_page_with_no_pagination_answer_raises(self) -> None:
# A totalCount equal to the nodes proves nothing: only a pagination
# answer says the listing is whole.
for info in (None, {}, {"hasNextPage": None, "endCursor": None}):
items = {"totalCount": 1, "nodes": [{"beforeCommit": {"oid": "a" * 40}}]}
if info is not None:
items["pageInfo"] = info
with (
self.subTest(pageInfo=info),
self.assertRaises(RuntimeError, msg="whether a next page exists"),
):
self.heads(self.answer(items))

def test_a_next_page_with_no_cursor_raises(self) -> None:
with self.assertRaises(RuntimeError):
self.heads(self.page(200, ["a" * 40], cursor=""))
self.heads(self.page(["a" * 40], cursor=""))

def test_a_listing_past_the_page_cap_raises(self) -> None:
pages = [
self.page(9999, ["a" * 40], cursor=f"c{i}")
self.page(["a" * 40], cursor=f"c{i}")
for i in range(merge._FORCE_PUSH_PAGES)
]
with self.assertRaises(RuntimeError):
Expand Down
Loading