Files
homelab/services/dev/gitea-pr-comment-filter.py
T

49 lines
1.3 KiB
Python

#!/usr/bin/env python3
"""Keep external comments on the agent's own Gitea pull requests."""
from __future__ import annotations
import json
import os
import sys
AGENT_USERNAME = os.environ.get("GITEA_AGENT_USERNAME", "luna")
def login(user: object) -> str:
if not isinstance(user, dict):
return ""
return str(user.get("login") or user.get("username") or "")
def main() -> int:
try:
payload = json.load(sys.stdin)
except (json.JSONDecodeError, OSError):
return 1
if not isinstance(payload, dict):
return 1
pull_request = payload.get("pull_request")
comment = payload.get("comment")
if not isinstance(pull_request, dict) or not isinstance(comment, dict):
# Fail closed: only PR comment payloads for the agent's own PRs should
# wake the route.
return 0
if login(pull_request.get("user")) != AGENT_USERNAME:
return 0
# Do not wake Hermes for its own reply, which would otherwise create a
# comment -> run -> comment loop.
if login(comment.get("user")) == AGENT_USERNAME:
return 0
json.dump(payload, sys.stdout, ensure_ascii=False, separators=(",", ":"))
sys.stdout.write("\n")
return 0
if __name__ == "__main__":
raise SystemExit(main())