Skip invalid ownerless JSON rows (#1540)

This commit is contained in:
red person
2026-06-03 08:06:57 +03:00
committed by GitHub
parent ee8c049f9e
commit d8f5c04340
2 changed files with 31 additions and 5 deletions

View File

@@ -13,6 +13,18 @@ import json
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
def claim_json_entries(entries, owner):
count = 0
for entry in entries:
if not isinstance(entry, dict):
continue
if not entry.get("owner"):
entry["owner"] = owner
count += 1
return count
def main(): def main():
if len(sys.argv) < 2: if len(sys.argv) < 2:
print("Usage: python scripts/claim_ownerless.py <username>") print("Usage: python scripts/claim_ownerless.py <username>")
@@ -31,11 +43,7 @@ def main():
continue continue
with open(path, "r", encoding="utf-8") as f: with open(path, "r", encoding="utf-8") as f:
entries = json.load(f) entries = json.load(f)
count = 0 count = claim_json_entries(entries, owner)
for e in entries:
if not e.get("owner"):
e["owner"] = owner
count += 1
if count: if count:
with open(path, "w", encoding="utf-8") as f: with open(path, "w", encoding="utf-8") as f:
json.dump(entries, f, ensure_ascii=False, indent=2) json.dump(entries, f, ensure_ascii=False, indent=2)

View File

@@ -0,0 +1,18 @@
from scripts.claim_ownerless import claim_json_entries
def test_claim_json_entries_skips_invalid_rows():
rows = [
{"id": "a"},
"bad-row",
None,
{"id": "b", "owner": "already"},
]
assert claim_json_entries(rows, "admin") == 1
assert rows == [
{"id": "a", "owner": "admin"},
"bad-row",
None,
{"id": "b", "owner": "already"},
]