mirror of
https://github.com/utmapp/utmapp-bot.git
synced 2026-12-01 14:02:03 -10:00
Every issue opened from now on (by anyone but the bot account) and every open pull request by someone outside the org (not a draft) is taken up without a call. New threads send the account no notification, so the issue list is read; nothing older than the switch-on is ever taken up. Issues (answer-utm-issue --new), in this order: - a template form left unfilled or filled with garbage: ignored completely; - opened blank with too little to triage: closed as not planned, with a comment asking for one of the templates (utmbot gh no-template); - a duplicate: closed through GitHub's own close-as-duplicate, which links the original on the issue itself (no label), with an invitation to reply; a possible relation is only noted, and triage goes on; - a feature request: "enhancement"; - a filled form that still gives nothing to try: the template's missing fields are asked for, "needs info", and 14 days, after which the poller closes it. Only where the bot's own marked comment and labelling are on the thread, and not when the author commented, the description was edited, or an org member wrote since; - cannot be triaged on this Mac: left alone, no comment; - the rest is triaged, with "not reproducible" when it does not reproduce; - a problem outside UTM's control (macOS, iOS, a jailbreak tool, the Virtualization framework): "external", and the issue stays open. Labels come off and the issue is analyzed again when the author replies; an issue the bot closed is reopened when the reply warrants it. Pull requests (review-utm-pr --new): a bug fix is reproduced, fixed independently by a subagent that never sees the pull request, compared, merged with what the other version lacks, reviewed and validated; the result is an approval, suggestions, or a branch on the bot's fork. Commits made for it carry the original commit's author in the Author field. Other pull requests are left alone. Strangers start these sessions, so the games-db rules for untrusted input apply and are enforced where they can be: `utmbot gh label|needs-info| duplicate|no-template|reopen|attachment` and the posting commands work only on the session's own thread; four labels; posted text is checked (nothing that identifies this Mac, no links outside the reference list, no secrets or home-folder names); attachments come only from GitHub's own hosts, quarantined, for the sandboxed readers; links are followed only in text an org member wrote; a pull request that changes anything run at build time is not built here unless a member asked, and a stranger's code runs only inside VMs. Requests go before unasked work: one unasked triage at a time, never in the last session slot, GHBOT_AUTO_PER_DAY (15) a day, GHBOT_AUTO=0 off. Co-Authored-By: Claude Fable 5.2 <[email protected]>
176 lines
5.2 KiB
Python
176 lines
5.2 KiB
Python
"""Settings and local state of the GitHub poller (this Mac only).
|
|
|
|
~/.utmapp-bot/github.env settings (KEY=VALUE; the environment wins)
|
|
~/.utmapp-bot/github/
|
|
state.json poll cursor, the account's login, membership cache
|
|
prs/<n>.json one pull request: who owns it, its session, the
|
|
activity already handled, its job
|
|
sessions/ the sessions' stream-json logs
|
|
signals/<session>.json what a session says about itself (blocked, later)
|
|
ghbot.log
|
|
"""
|
|
|
|
import fcntl
|
|
import json
|
|
import os
|
|
import shlex
|
|
import time
|
|
from contextlib import contextmanager
|
|
from pathlib import Path
|
|
|
|
DATA = Path(os.environ.get("UTMAPP_BOT_DATA", "~/.utmapp-bot")).expanduser()
|
|
CONFIG_FILE = DATA / "github.env"
|
|
D = Path(os.environ["GHBOT_STATE"]).expanduser() if os.environ.get("GHBOT_STATE") else DATA / "github"
|
|
STATE_FILE = D / "state.json"
|
|
PRS = D / "prs"
|
|
SIGNALS = D / "signals"
|
|
SESSIONS = D / "sessions"
|
|
LOG = D / "ghbot.log"
|
|
PAUSED = D / "PAUSED"
|
|
|
|
DEFAULTS = {"GHBOT_REPO": "utmapp/UTM", "GHBOT_ORG": "utmapp", "GHBOT_POLL": "120",
|
|
"GHBOT_REVIEWS_PER_DAY": "0", "GHBOT_RELEASE_DAYS": "7", "GHBOT_MAX_SESSIONS": "2",
|
|
# triage every new issue and bug-fix PR without being asked (0: only when called);
|
|
# at most this many such sessions a day; days before a "needs info" issue is closed
|
|
"GHBOT_AUTO": "1", "GHBOT_AUTO_PER_DAY": "15", "GHBOT_NEEDS_INFO_DAYS": "14"}
|
|
|
|
|
|
def config():
|
|
vals = dict(DEFAULTS)
|
|
if CONFIG_FILE.exists():
|
|
for line in CONFIG_FILE.read_text().splitlines():
|
|
line = line.strip()
|
|
if not line or line.startswith("#") or "=" not in line:
|
|
continue
|
|
k, v = line.split("=", 1)
|
|
k = k.strip().removeprefix("export ").strip()
|
|
try:
|
|
parts = shlex.split(v)
|
|
except ValueError:
|
|
parts = [v.strip()]
|
|
vals[k] = parts[0] if parts else ""
|
|
for k, v in os.environ.items():
|
|
if k.startswith("GHBOT_") and k != "GHBOT_STATE" or k in vals:
|
|
vals[k] = v
|
|
return vals
|
|
|
|
|
|
def write_config(updates):
|
|
lines, seen = [], set()
|
|
if CONFIG_FILE.exists():
|
|
for line in CONFIG_FILE.read_text().splitlines():
|
|
k = line.split("=", 1)[0].strip().removeprefix("export ").strip()
|
|
if k in updates:
|
|
seen.add(k)
|
|
if updates[k] is None:
|
|
continue
|
|
line = f"{k}={shlex.quote(str(updates[k]))}"
|
|
lines.append(line)
|
|
else:
|
|
lines.append("# GitHub poller settings for the utmapp-bot plugin (written by github-setup)")
|
|
for k, v in updates.items():
|
|
if k not in seen and v is not None:
|
|
lines.append(f"{k}={shlex.quote(str(v))}")
|
|
DATA.mkdir(parents=True, exist_ok=True)
|
|
CONFIG_FILE.write_text("\n".join(lines) + "\n")
|
|
os.chmod(CONFIG_FILE, 0o600)
|
|
|
|
|
|
def private(p):
|
|
p.mkdir(parents=True, exist_ok=True)
|
|
os.chmod(p, 0o700)
|
|
return p
|
|
|
|
|
|
def utc(t=None):
|
|
return time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime(time.time() if t is None else t))
|
|
|
|
|
|
def log(msg, echo=True):
|
|
private(D)
|
|
if LOG.exists() and LOG.stat().st_size > 4 << 20:
|
|
LOG.replace(LOG.with_suffix(".log.1"))
|
|
with open(LOG, "a") as f:
|
|
f.write(f"{time.strftime('%Y-%m-%d %H:%M:%S')} {msg}\n")
|
|
if echo:
|
|
import sys
|
|
print(msg, file=sys.stderr, flush=True)
|
|
|
|
|
|
def _write(path, data):
|
|
private(path.parent)
|
|
tmp = path.with_suffix(path.suffix + f".tmp{os.getpid()}")
|
|
tmp.write_text(json.dumps(data, indent=2, ensure_ascii=False) + "\n")
|
|
os.chmod(tmp, 0o600)
|
|
tmp.replace(path)
|
|
|
|
|
|
def _read(path, default):
|
|
try:
|
|
return json.loads(path.read_text())
|
|
except (OSError, ValueError):
|
|
return default
|
|
|
|
|
|
@contextmanager
|
|
def locked(name="state"):
|
|
"""One writer at a time: the poller and the commands sessions run (a
|
|
session's `gh comment` records what it posted in the PR's file)."""
|
|
private(D)
|
|
with open(D / f".{name}.lock", "w") as f:
|
|
fcntl.flock(f, fcntl.LOCK_EX)
|
|
yield
|
|
|
|
|
|
def load():
|
|
return _read(STATE_FILE, {})
|
|
|
|
|
|
def save(st):
|
|
_write(STATE_FILE, st)
|
|
|
|
|
|
def pr_path(n):
|
|
return PRS / f"{int(n)}.json"
|
|
|
|
|
|
def pr_load(n):
|
|
return _read(pr_path(n), None)
|
|
|
|
|
|
def pr_save(rec):
|
|
rec["updated"] = utc()
|
|
_write(pr_path(rec["pr"]), rec)
|
|
|
|
|
|
def pr_all():
|
|
out = []
|
|
if PRS.exists():
|
|
for p in sorted(PRS.glob("*.json"), key=lambda p: int(p.stem) if p.stem.isdigit() else 0):
|
|
r = _read(p, None)
|
|
if r:
|
|
out.append(r)
|
|
return out
|
|
|
|
|
|
def signal(session_id, state, **fields):
|
|
rec = {"session_id": session_id, "state": state, "time": utc(), **fields}
|
|
_write(SIGNALS / f"{session_id}.json", rec)
|
|
return rec
|
|
|
|
|
|
def read_signal(session_id):
|
|
return _read(SIGNALS / f"{session_id}.json", None)
|
|
|
|
|
|
def clear_signal(session_id):
|
|
(SIGNALS / f"{session_id}.json").unlink(missing_ok=True)
|
|
|
|
|
|
def use(d):
|
|
"""Point the state at directory d (tests: two Macs in one process)."""
|
|
global D, STATE_FILE, PRS, SIGNALS, SESSIONS, LOG, PAUSED
|
|
D = Path(d)
|
|
STATE_FILE, PRS, SIGNALS = D / "state.json", D / "prs", D / "signals"
|
|
SESSIONS, LOG, PAUSED = D / "sessions", D / "ghbot.log", D / "PAUSED"
|