diff --git a/infra/fleet-paperwork/fleet_paperwork.py b/infra/fleet-paperwork/fleet_paperwork.py index c0feaa731a..540c2246fd 100755 --- a/infra/fleet-paperwork/fleet_paperwork.py +++ b/infra/fleet-paperwork/fleet_paperwork.py @@ -281,13 +281,28 @@ def ledger_append(action: str, repo: str, number: str, detail: str) -> None: def find_by_exact_title(client: AppClient, repo: str, title: str) -> dict | None: """Search-API lookup, filtered to byte-exact title equality (the search - itself matches title+body text, so the filter is what makes it a dedupe).""" + itself matches title+body text, so the filter is what makes it a dedupe). + + Issues only, on two layers (#6697): the query carries `type:issue` AND + the filter skips items with a `pull_request` key, because the search + index contains PRs and a same-titled PR is not an already-filed issue — + treating it as one made `create` exit 0 having filed nothing. The scan + pages through the matches (the Search API serves at most 1000 results) + so an exact-title issue past the first page is still found.""" token = client.token_for(repo.split("/")[0]) - q = urllib.parse.quote(f'repo:{repo} "{title}"') - _, data = http("GET", f"/search/issues?q={q}&per_page=20", token) - for item in data.get("items", []): - if item.get("title") == title: - return item + q = urllib.parse.quote(f'repo:{repo} type:issue "{title}"') + for page in range(1, 11): + _, data = http( + "GET", f"/search/issues?q={q}&per_page=100&page={page}", token + ) + items = data.get("items", []) + for item in items: + if "pull_request" in item: + continue + if item.get("title") == title: + return item + if len(items) < 100: + return None return None diff --git a/infra/fleet-paperwork/tests/test_fleet_paperwork.py b/infra/fleet-paperwork/tests/test_fleet_paperwork.py index c48873053e..330e0729a8 100644 --- a/infra/fleet-paperwork/tests/test_fleet_paperwork.py +++ b/infra/fleet-paperwork/tests/test_fleet_paperwork.py @@ -8,6 +8,7 @@ import tempfile import time import unittest +import unittest.mock from pathlib import Path sys.path.insert(0, str(Path(__file__).resolve().parent.parent)) @@ -111,6 +112,62 @@ def test_exact_match_only(self): self.assertEqual([h["number"] for h in hits], [3]) +class _FakeClient: + def token_for(self, owner): + return "test-token" + + +class TestFindByExactTitle(unittest.TestCase): + """The real find_by_exact_title against stubbed Search-API payloads, + shaped as GitHub returns them (PRs carry a `pull_request` key).""" + + TITLE = "id: exact title shared with a PR" + + def run_lookup(self, pages): + """pages: {page_number: [items]}. Returns (hit, requested_paths).""" + paths = [] + + def fake_http(method, path, token, payload=None): + paths.append(path) + page = int(path.rsplit("page=", 1)[1]) + return 200, {"items": pages.get(page, [])} + + with unittest.mock.patch.object(fp, "http", fake_http): + hit = fp.find_by_exact_title( + _FakeClient(), "developing-today/code", self.TITLE + ) + return hit, paths + + def test_pr_with_same_title_is_not_a_hit(self): + pr = { + "title": self.TITLE, + "number": 4242, + "pull_request": {"url": "https://api.github.com/x/pulls/4242"}, + } + hit, paths = self.run_lookup({1: [pr]}) + self.assertIsNone(hit) + # The query itself also scopes to issues (belt and braces). + self.assertIn("type%3Aissue", paths[0]) + + def test_exact_title_issue_is_a_hit(self): + issue = {"title": self.TITLE, "number": 7} + hit, _ = self.run_lookup({1: [{"title": "other", "number": 1}, issue]}) + self.assertEqual(hit["number"], 7) + + def test_pr_before_exact_issue_on_same_page_is_skipped(self): + pr = {"title": self.TITLE, "number": 4242, "pull_request": {}} + issue = {"title": self.TITLE, "number": 8} + hit, _ = self.run_lookup({1: [pr, issue]}) + self.assertEqual(hit["number"], 8) + + def test_exact_issue_past_first_page_is_found(self): + page1 = [{"title": f"{self.TITLE} (mention {i})", "number": i} for i in range(100)] + issue = {"title": self.TITLE, "number": 99} + hit, paths = self.run_lookup({1: page1, 2: [issue]}) + self.assertEqual(hit["number"], 99) + self.assertEqual(len(paths), 2) + + class TestGenericCommandArgs(unittest.TestCase): """The generic commands must not be able to act by accident: `api` requires an explicit --method for every call (no default to hide a