Files
coucou/scripts/community-mochis.py
LouisandClaude a01d252f3c The community's Mochis: shared teams in the repository, a gallery in the app (#1302)
- community/mochis/: teams of Mochis as .mochis.json files with an index (name, description in
  English and French, author, tags); three to start: Maintainer team, Night shift, Solo builder.
  A README says how to share one.
- scripts/community-mochis.py checks every file (format, 1 to 12 Mochis, no secret, no local
  path, no repository, nothing autonomous) and keeps the index's counts and faces in step; its own
  CI workflow runs it when community/mochis/ changes.
- Mochis › Team › From the community: the index is read from GitHub only on a click, and Import
  opens the import sheet on that file. The Mochis arrive off and asking first.
- 6 new strings in 16 languages.


Claude-Session: https://claude.ai/code/session_01LJm3SRL9k3gBPaVtfPhwcT

Co-authored-by: Claude <noreply@anthropic.com>
2026-12-03 22:03:42 +00:00

105 lines
5.2 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env python3
"""The community's Mochis (community/mochis/): checks every .mochis.json file and keeps index.json in step.
python3 scripts/community-mochis.py checks (CI): fails on a bad file or a stale index
python3 scripts/community-mochis.py --write fills index.json's computed fields (count, faces, repo)
index.json lists each file once, with what a person writes by hand (name, description in English and
optionally French, author, tags) and what this script computes from the file (mochis, faces, needsRepo).
A file is data: its instructions are read by a Mochi, so nothing that looks like a secret, no local
path, nothing autonomous (Coucou imports them off and asking first anyway)."""
import json, re, sys
from pathlib import Path
ROOT = Path(__file__).resolve().parent.parent / "community" / "mochis"
INDEX = ROOT / "index.json"
KINDS = {"brief", "repo", "watch", "mail"}
ONS = {"schedule", "manual", "issue-label", "ci-failed", "pr-opened", "issue-opened", "watch", "after", "pr-queue"}
SECRET = re.compile(r"(sk-[A-Za-z0-9_-]{16,}|gh[pousr]_[A-Za-z0-9]{20,}|xox[abpr]-|AKIA[0-9A-Z]{16}|-----BEGIN [A-Z ]*PRIVATE KEY|bot[0-9]{6,}:[A-Za-z0-9_-]{30,})")
LOCAL = re.compile(r"(/Users/[^/\s]+|/home/[^/\s]+|C:\\\\Users\\\\)")
SLUG = re.compile(r"^[a-z0-9][a-z0-9-]{1,40}\.mochis\.json$")
def check_file(path: Path) -> tuple[list[str], dict]:
errors = []
try:
data = json.loads(path.read_text(encoding="utf-8"))
except Exception as e: # noqa: BLE001
return [f"{path.name}: not JSON ({e})"], {}
if not SLUG.match(path.name):
errors.append(f"{path.name}: name it like my-team.mochis.json (lowercase, dashes)")
if data.get("format") != "coucou-mochis" or data.get("version") != 1:
errors.append(f"{path.name}: format must be coucou-mochis, version 1 (export it from Coucou)")
mochis = data.get("mochis")
if not isinstance(mochis, list) or not 1 <= len(mochis) <= 12:
errors.append(f"{path.name}: 1 to 12 Mochis")
mochis = []
raw = path.read_text(encoding="utf-8")
if SECRET.search(raw):
errors.append(f"{path.name}: something looks like a secret (a key or a token): remove it")
if LOCAL.search(raw):
errors.append(f"{path.name}: a path on someone's computer: remove it")
for m in mochis:
who = f"{path.name} › {m.get('name', '?')}"
if not isinstance(m.get("name"), str) or not 1 <= len(m["name"]) <= 60:
errors.append(f"{who}: a name of 1 to 60 characters")
if m.get("kind") not in KINDS:
errors.append(f"{who}: kind must be one of {sorted(KINDS)}")
if m.get("on", "schedule") not in ONS:
errors.append(f"{who}: unknown trigger {m.get('on')}")
ins = m.get("instructions")
if not isinstance(ins, str) or not 20 <= len(ins) <= 8000:
errors.append(f"{who}: instructions of 20 to 8000 characters")
if not re.match(r"^#[0-9A-Fa-f]{6}$", str(m.get("color", ""))):
errors.append(f"{who}: color like #22D3EE")
if "repo" in m:
errors.append(f"{who}: no repository: the person who imports it picks theirs")
if m.get("level") == "autonome":
errors.append(f"{who}: not autonomous: shared Mochis ask first")
if m.get("kind") == "watch" and not m.get("urls"):
errors.append(f"{who}: a Prod Watch needs at least one address")
facts = {
"mochis": len(mochis),
"faces": [{"color": m.get("color", "#22D3EE"), "outfit": m.get("outfit", "none")} for m in mochis[:6]],
"needsRepo": any(m.get("kind") == "repo" for m in mochis),
}
return errors, facts
def main() -> int:
write = "--write" in sys.argv
files = sorted(p for p in ROOT.glob("*.json") if p.name != "index.json")
index = json.loads(INDEX.read_text(encoding="utf-8")) if INDEX.exists() else {"teams": []}
errors = []
listed = {e.get("file") for e in index["teams"]}
for p in files:
if p.name not in listed:
errors.append(f"{p.name}: add it to community/mochis/index.json (name, description, author)")
for e in index["teams"]:
f = ROOT / str(e.get("file", ""))
if not f.exists():
errors.append(f"index.json: {e.get('file')} doesn't exist")
continue
errs, facts = check_file(f)
errors += errs
for k in ("name", "author"):
if not isinstance(e.get(k), str) or not e[k].strip():
errors.append(f"index.json › {e.get('file')}: {k} is missing")
if not isinstance(e.get("description"), dict) or not str(e["description"].get("en", "")).strip():
errors.append(f"index.json › {e.get('file')}: description.en is missing")
if write:
e.update(facts)
elif any(e.get(k) != v for k, v in facts.items()):
errors.append(f"index.json › {e.get('file')}: out of date, run python3 scripts/community-mochis.py --write")
if write:
INDEX.write_text(json.dumps(index, ensure_ascii=False, indent=2) + "\n", encoding="utf-8")
if errors:
print("\n".join(errors))
return 1
print(f"PASS: {len(index['teams'])} community teams")
return 0
if __name__ == "__main__":
sys.exit(main())