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
3 changes: 2 additions & 1 deletion infra/fleet-paperwork/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ through a personal user token has two failure modes, both observed live on
and `dezren39/fleet`.
- **A filing is verified, or it is a failure.** Every create / comment / close
is followed by a readback GET, and the result is compared (title, body,
state). A missing, empty, or mismatched readback exits nonzero.
state) — plus the labels that were sent, for create. A missing, empty, or
mismatched readback exits nonzero.
- Creates are **deduped by exact title** (search API + byte-exact title
comparison) and **paced** (default 3 s minimum between writes, enforced
across invocations via the ledger), so re-running a batch is safe and
Expand Down
45 changes: 44 additions & 1 deletion infra/fleet-paperwork/fleet_paperwork.py
Original file line number Diff line number Diff line change
Expand Up @@ -463,10 +463,53 @@ def cmd_create(args) -> int:
f"create in {args.repo} returned no issue number (response keys: "
f"{sorted(created) if isinstance(created, dict) else type(created)})"
)
# Verify by readback — an unverified create counts as a failure.
# Verify by readback — an unverified create counts as a failure. The
# README promises the comparison covers (title, body, state) and the
# labels that were sent (#6901): a create whose body was dropped or
# mangled, or whose labels were not applied, must not report success.
_, got = http("GET", f"/repos/{args.repo}/issues/{number}", token)
if got.get("title") != title or got.get("number") != number:
die(f"readback mismatch for {args.repo}#{number}: title={got.get('title')!r}")
readback_body = got.get("body")
if readback_body != body:
sent_chars = len(body) if isinstance(body, str) else "n/a"
readback_chars = (
len(readback_body) if isinstance(readback_body, str) else "n/a"
)
die(
f"readback mismatch for {args.repo}#{number}: body sent "
f"{sent_chars} chars, read back {readback_chars} chars "
f"(body={_shape_name(readback_body)})"
)
if got.get("state") != "open":
die(
f"readback mismatch for {args.repo}#{number}: state={got.get('state')!r}, "
f"expected 'open' for a freshly created issue"
)
if args.label:
readback_labels = got.get("labels", [])
if not isinstance(readback_labels, list):
die(
f"readback mismatch for {args.repo}#{number}: labels have invalid "
f"shape (expected array, got {_shape_name(readback_labels)})"
)
have = set()
for label in readback_labels:
name = label if isinstance(label, str) else (
label.get("name") if isinstance(label, dict) else None
)
if not isinstance(name, str):
die(
f"readback mismatch for {args.repo}#{number}: label entry has "
f"no string name (got {_shape_name(label)})"
)
have.add(name)
missing = sorted(set(args.label) - have)
if missing:
die(
f"readback mismatch for {args.repo}#{number}: labels not applied: "
f"{missing} (read back {sorted(have)})"
)
ledger_append("create", args.repo, str(number), title)
print(f"created+verified: {args.repo}#{number} {created.get('html_url')}")
return 0
Expand Down
101 changes: 101 additions & 0 deletions infra/fleet-paperwork/tests/test_fleet_paperwork.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,9 @@ class _FakeClient:
def token_for(self, owner):
return "test-token"

def assert_repo_in_scope(self, repo):
return None


class TestFindByExactTitle(unittest.TestCase):
"""The real find_by_exact_title against stubbed Search-API payloads,
Expand Down Expand Up @@ -168,6 +171,104 @@ def test_exact_issue_past_first_page_is_found(self):
self.assertEqual(len(paths), 2)


class TestCreateReadback(unittest.TestCase):
"""#6901: the create readback must compare body, state and requested
labels against what was sent — not just title and number — so a create
whose body was dropped or whose labels were not applied fails loudly."""

TITLE = "Preserved branch: queue/x — decision needed"
BODY = "## Finding\n\nbody that the filer sent\n"
REPO = "developing-today/code"

def run_create(self, readback_overrides, labels=None):
"""Run cmd_create against stubbed HTTP. Returns (exit_code, paths)."""
paths = []
created = {"number": 42, "html_url": "https://example.test/42"}
readback = {
"number": 42,
"title": self.TITLE,
"body": self.BODY,
"state": "open",
"labels": [{"name": n} for n in (labels or [])],
}
readback.update(readback_overrides)

def fake_http(method, path, token, body=None, expect=dict):
paths.append((method, path))
if method == "POST":
return 200, created
return 200, readback

args = [
"create",
"--repo",
self.REPO,
"--title",
self.TITLE,
"--body",
self.BODY,
"--no-dedupe",
"--pace",
"0",
]
for label in labels or []:
args += ["--label", label]

with unittest.mock.patch.object(fp, "AppClient", lambda _s: _FakeClient()), \
unittest.mock.patch.object(
fp, "find_by_exact_title", lambda *_a, **_k: None
), \
unittest.mock.patch.object(fp, "http", fake_http), \
unittest.mock.patch.object(fp, "ledger_append"):
try:
code = fp.main(args)
except SystemExit as exc:
code = exc.code
return code, paths

def test_matching_readback_passes(self):
code, paths = self.run_create({})
self.assertEqual(code, 0)
self.assertEqual([m for m, _ in paths], ["POST", "GET"])

def test_dropped_body_fails(self):
code, _ = self.run_create({"body": ""})
self.assertEqual(code, 1)

def test_mangled_body_fails(self):
code, _ = self.run_create({"body": self.BODY.strip()})
self.assertEqual(code, 1)

def test_non_string_body_readback_fails_cleanly(self):
code, _ = self.run_create({"body": 42})
self.assertEqual(code, 1)

def test_closed_state_fails(self):
code, _ = self.run_create({"state": "closed"})
self.assertEqual(code, 1)

def test_missing_labels_fail(self):
"""Labels were requested but the readback shows none applied."""
code, _ = self.run_create({"labels": []}, labels=["kind/bug"])
self.assertEqual(code, 1)

def test_malformed_label_entry_fails_cleanly(self):
code, _ = self.run_create({"labels": [{"name": None}]}, labels=["kind/bug"])
self.assertEqual(code, 1)

def test_applied_labels_pass(self):
code, _ = self.run_create({}, labels=["kind/bug", "effort/xs"])
self.assertEqual(code, 0)

def test_extra_labels_are_tolerated(self):
"""GitHub may add labels on create; only the requested ones matter."""
code, _ = self.run_create(
{"labels": [{"name": "kind/bug"}, {"name": "auto-added"}]},
labels=["kind/bug"],
)
self.assertEqual(code, 0)


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
Expand Down
Loading