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
25 changes: 19 additions & 6 deletions infra/fleet-paperwork/fleet_paperwork.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@
DEFAULT_SOPS = "~/secrets/pm-stack.sops.yaml"
DEFAULT_LEDGER = "~/.local/state/fleet-paperwork/ledger.tsv"
APP_SLUG_HINT = "developing.today (App ID read from the sops file at runtime)"
# Pagination contract (#6622 rule 2): every GitHub page loop carries a
# page bound whose breach is an error naming the listing. 100 pages of
# 100 is far past any installation or repository count this App has,
# so crossing it means the listing is not terminating.
GITHUB_LIST_MAX_PAGES = 100


def die(msg: str) -> "SystemExit":
Expand Down Expand Up @@ -260,8 +265,7 @@ def installations(self) -> list[dict]:
# empty page, mirroring assert_repo_in_scope below; the
# endpoint returns a top-level array with no total_count.
collected: list[dict] = []
page = 1
while True:
for page in range(1, GITHUB_LIST_MAX_PAGES + 1):
_, data = http(
"GET",
f"/app/installations?per_page=100&page={page}",
Expand All @@ -277,7 +281,11 @@ def installations(self) -> list[dict]:
collected.extend(data)
if len(data) < 100:
break
page += 1
else:
die(
"/app/installations listing exceeded the page bound "
f"({GITHUB_LIST_MAX_PAGES} pages of 100) without a short page (#9150)"
)
self._installations = collected
return self._installations

Expand Down Expand Up @@ -309,8 +317,8 @@ def token_for(self, owner: str) -> str:
def assert_repo_in_scope(self, repo: str) -> None:
owner = repo.split("/")[0]
token = self.token_for(owner)
page, seen = 1, []
while True:
seen: list[str] = []
for page in range(1, GITHUB_LIST_MAX_PAGES + 1):
_, data = http(
"GET", f"/installation/repositories?per_page=100&page={page}", token
)
Expand All @@ -335,7 +343,12 @@ def assert_repo_in_scope(self, repo: str) -> None:
)
if len(seen) >= total or not repos:
break
page += 1
else:
die(
f"/installation/repositories listing for {owner} exceeded the page "
f"bound ({GITHUB_LIST_MAX_PAGES} pages of 100) before reaching its "
"total_count (#9150)"
)
if repo not in seen:
die(
f"repo {repo} is not in the app installation scope for {owner} "
Expand Down
72 changes: 72 additions & 0 deletions infra/fleet-paperwork/tests/test_fleet_paperwork.py
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,78 @@ def fake_http(method, path, token, payload=None, **kw):
self.assertEqual(hit["number"], 5)


class TestPageBounds(unittest.TestCase):
"""#9150 (#6622 rule 2): both App page loops stop at a page bound and
fail naming the listing instead of following pages forever."""

def client(self):
c = fp.AppClient.__new__(fp.AppClient)
c.jwt = "jwt"
c._tokens = {}
c._installations = None
return c

def test_installations_stops_at_short_page(self):
def fake_http(method, path, token, body=None, expect=dict):
page = int(path.rsplit("page=", 1)[1])
return 200, [
{"id": page * 1000 + i} for i in range(100 if page == 1 else 3)
]

with unittest.mock.patch.object(fp, "http", fake_http):
self.assertEqual(len(self.client().installations()), 103)

def test_installations_never_short_fails_at_bound(self):
calls = []

def fake_http(method, path, token, body=None, expect=dict):
calls.append(path)
return 200, [{"id": i} for i in range(100)]

with (
unittest.mock.patch.object(fp, "http", fake_http),
unittest.mock.patch("sys.stderr"),
):
with self.assertRaises(SystemExit):
self.client().installations()
self.assertEqual(len(calls), fp.GITHUB_LIST_MAX_PAGES)

def test_repositories_never_reaching_total_fails_at_bound(self):
calls = []

def fake_http(method, path, token, body=None, expect=dict):
calls.append(path)
page = int(path.rsplit("page=", 1)[1])
return 200, {
"total_count": 10**9,
"repositories": [{"full_name": f"o/r{page}-{i}"} for i in range(100)],
}

c = self.client()
c.token_for = lambda owner: "tok"
with (
unittest.mock.patch.object(fp, "http", fake_http),
unittest.mock.patch("sys.stderr"),
):
with self.assertRaises(SystemExit):
c.assert_repo_in_scope("o/missing")
self.assertEqual(len(calls), fp.GITHUB_LIST_MAX_PAGES)

def test_repositories_found_on_second_page(self):
def fake_http(method, path, token, body=None, expect=dict):
page = int(path.rsplit("page=", 1)[1])
names = [f"o/r{i}" for i in range(100)] if page == 1 else ["o/target"]
return 200, {
"total_count": 101,
"repositories": [{"full_name": n} for n in names],
}

c = self.client()
c.token_for = lambda owner: "tok"
with unittest.mock.patch.object(fp, "http", fake_http):
self.assertIsNone(c.assert_repo_in_scope("o/target"))


if __name__ == "__main__":
unittest.main()

Expand Down