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
8 changes: 5 additions & 3 deletions .agents/skills/api-review/scripts/parse_pgy_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@
from pathlib import Path

# A row looks like:
# {ID: 15, Method: "POST", Path: "/access/external-exchange",
# {Product: "Platform", Method: "POST", Path: "/access/external-exchange",
# Name: "access:read:externalExchange", NameCN: "外部认证交换",
# Description: "...", Auth: "none", IsDangerous: false, IsAudit: false,
# Qps: 100, Provider: "pgy", Domain: "http://127.0.0.1:11482"},
# Older rows can start with `ID: <number>,`; current name-keyed rows do not.
#
# Rows can be prefixed with `//` (commented-out). The convention:
# - Commented rows = existing production APIs already persisted in the DB.
Expand All @@ -38,7 +39,7 @@
# The file is a cumulative ledger; nothing deletes a row once registered.

ROW_RE = re.compile(
r"""^\s*(?P<commented>//\s*)?\{ID:\s*(?P<id>\d+)\s*,\s*(?P<fields>.*)\},?\s*$""",
r"""^\s*(?P<commented>//\s*)?\{(?:ID:\s*(?P<id>\d+)\s*,\s*|(?=\s*Product:))(?P<fields>.*)\},?\s*$""",
re.VERBOSE,
)

Expand Down Expand Up @@ -76,10 +77,11 @@ def parse_file(path: Path) -> list[dict]:
continue

row: dict = {
"id": int(m.group("id")),
"commented": m.group("commented") is not None,
"line": lineno,
}
if m.group("id") is not None:
row["id"] = int(m.group("id"))

for fm in FIELD_RE.finditer(m.group("fields")):
key = fm.group("key")
Expand Down
63 changes: 63 additions & 0 deletions tests/test_parse_pgy_registry.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import importlib.util
import tempfile
import unittest
from pathlib import Path


PARSER_PATH = Path(__file__).parents[1] / ".agents/skills/api-review/scripts/parse_pgy_registry.py"
SPEC = importlib.util.spec_from_file_location("parse_pgy_registry", PARSER_PATH)
PARSER = importlib.util.module_from_spec(SPEC)
assert SPEC.loader is not None
SPEC.loader.exec_module(PARSER)


class ParseRegistryTest(unittest.TestCase):
def test_parses_name_keyed_registry_row_without_numeric_id(self):
row = (
'{Product: "AI SRE", Provider: "safari", Name: "skill:read:list", '
'NameCN: "技能:列表", Method: "POST", Path: "/safari/skill/list", '
'Auth: "all", IsDangerous: false, IsAudit: false},\n'
)
with tempfile.NamedTemporaryFile(mode="w", encoding="utf-8", delete=False) as fixture:
fixture.write(row)
fixture_path = Path(fixture.name)
try:
rows = PARSER.parse_file(fixture_path)
finally:
fixture_path.unlink()

self.assertEqual(len(rows), 1)
self.assertEqual(rows[0]["name"], "skill:read:list")
self.assertEqual(rows[0]["auth"], "all")
self.assertEqual(rows[0]["provider"], "safari")

def test_ignores_non_registry_struct_without_numeric_id(self):
row = '{Method: "POST", Path: "/test", Auth: "all", Provider: "pgy"},\n'
with tempfile.NamedTemporaryFile(mode="w", encoding="utf-8", delete=False) as fixture:
fixture.write(row)
fixture_path = Path(fixture.name)
try:
rows = PARSER.parse_file(fixture_path)
finally:
fixture_path.unlink()

self.assertEqual(rows, [])

def test_preserves_legacy_registry_row_with_numeric_id(self):
row = (
'{ID: 15, Method: "POST", Path: "/team/list", Name: "team:read:list", '
'Auth: "all", Provider: "pgy"},\n'
)
with tempfile.NamedTemporaryFile(mode="w", encoding="utf-8", delete=False) as fixture:
fixture.write(row)
fixture_path = Path(fixture.name)
try:
rows = PARSER.parse_file(fixture_path)
finally:
fixture_path.unlink()

self.assertEqual(rows[0]["id"], 15)


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