watch_github.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. #!/usr/bin/env python3
  2. """Watch GitHub activity — issues, pulls, releases, or commits — with dedup.
  3. Usage (via cron with --no-agent):
  4. hermes cron create hermes-issues \\
  5. --schedule "*/5 * * * *" --no-agent \\
  6. --script "$HERMES_HOME/skills/devops/watchers/scripts/watch_github.py" \\
  7. --script-args "--name hermes-issues --repo NousResearch/hermes-agent --scope issues"
  8. Set GITHUB_TOKEN (or GH_TOKEN) in the Hermes .env file
  9. (``${HERMES_HOME:-~/.hermes}/.env``) to avoid the 60 req/hr
  10. anonymous rate limit.
  11. Scopes: issues | pulls | releases | commits. Or pass --search QUERY to
  12. use the /search/issues endpoint instead of /repos/:owner/:repo/:scope.
  13. """
  14. from __future__ import annotations
  15. import argparse
  16. import json
  17. import os
  18. import re
  19. import sys
  20. import urllib.error
  21. import urllib.parse
  22. import urllib.request
  23. from pathlib import Path
  24. sys.path.insert(0, str(Path(__file__).parent))
  25. from _watermark import Watermark, format_items_as_markdown # type: ignore
  26. VALID_SCOPES = ("issues", "pulls", "releases", "commits")
  27. def _flatten_commit(item):
  28. """Commit objects nest title/author/date under 'commit' — flatten for rendering."""
  29. commit = item.get("commit") or {}
  30. msg = (commit.get("message") or "").strip().splitlines()
  31. title = msg[0] if msg else ""
  32. body = "\n".join(msg[1:]).strip() if len(msg) > 1 else ""
  33. author = (item.get("author") or {}).get("login") or (commit.get("author") or {}).get("name", "")
  34. date = (commit.get("author") or {}).get("date", "")
  35. return {
  36. "id": item.get("sha", ""),
  37. "title": f"{title} ({author})" if author else title,
  38. "url": item.get("html_url"),
  39. "body": body,
  40. "created_at": date,
  41. }
  42. def _flatten_issue_or_release(item):
  43. return {
  44. "id": str(item.get("id", "")),
  45. "title": item.get("title") or item.get("name") or "",
  46. "url": item.get("html_url") or item.get("url"),
  47. "body": (item.get("body") or "").strip(),
  48. "state": item.get("state"),
  49. "author": (item.get("user") or {}).get("login")
  50. or (item.get("author") or {}).get("login"),
  51. "created_at": item.get("created_at"),
  52. }
  53. def main() -> int:
  54. p = argparse.ArgumentParser(description="Watch GitHub issues / pulls / releases / commits.")
  55. p.add_argument("--name", required=True, help="Watcher name (used for state file)")
  56. p.add_argument("--repo", default="",
  57. help="owner/name of the repo (one of --repo or --search is required)")
  58. p.add_argument("--scope", default="issues", choices=VALID_SCOPES,
  59. help="What to poll (default: issues)")
  60. p.add_argument("--search", default="",
  61. help="GitHub issues search query (alternative to --repo/--scope)")
  62. p.add_argument("--per-page", type=int, default=30,
  63. help="Results per page (default: 30, max: 100)")
  64. p.add_argument("--max", type=int, default=20,
  65. help="Max new items to emit per tick (default: 20)")
  66. p.add_argument("--with-body", action="store_true",
  67. help="Include issue/commit body as a snippet under each item")
  68. p.add_argument("--timeout", type=float, default=30.0,
  69. help="HTTP timeout in seconds (default: 30)")
  70. args = p.parse_args()
  71. if not args.repo and not args.search:
  72. print("watch_github: one of --repo or --search is required", file=sys.stderr)
  73. return 2
  74. if args.repo and not re.fullmatch(r"[A-Za-z0-9._-]+/[A-Za-z0-9._-]+", args.repo):
  75. print(f"watch_github: --repo must be owner/name (got {args.repo!r})", file=sys.stderr)
  76. return 2
  77. # URL + flattening strategy.
  78. if args.search:
  79. url = (
  80. "https://api.github.com/search/issues"
  81. f"?q={urllib.parse.quote(args.search)}&per_page={args.per_page}"
  82. )
  83. flatten = _flatten_issue_or_release
  84. items_path = "items"
  85. elif args.scope == "commits":
  86. url = f"https://api.github.com/repos/{args.repo}/commits?per_page={args.per_page}"
  87. flatten = _flatten_commit
  88. items_path = ""
  89. else:
  90. url = (
  91. f"https://api.github.com/repos/{args.repo}/{args.scope}"
  92. f"?per_page={args.per_page}&state=all"
  93. )
  94. flatten = _flatten_issue_or_release
  95. items_path = ""
  96. headers = {
  97. "Accept": "application/vnd.github+json",
  98. "User-Agent": "Hermes-Watcher/1.0",
  99. }
  100. token = os.environ.get("GITHUB_TOKEN") or os.environ.get("GH_TOKEN")
  101. if token:
  102. headers["Authorization"] = f"Bearer {token}"
  103. req = urllib.request.Request(url)
  104. for k, v in headers.items():
  105. req.add_header(k, v)
  106. try:
  107. with urllib.request.urlopen(req, timeout=args.timeout) as resp:
  108. raw = resp.read()
  109. except urllib.error.HTTPError as e:
  110. print(f"watch_github: HTTP {e.code} from {url}", file=sys.stderr)
  111. return 2
  112. except (urllib.error.URLError, TimeoutError, OSError) as e:
  113. print(f"watch_github: network error: {e}", file=sys.stderr)
  114. return 2
  115. try:
  116. data = json.loads(raw.decode("utf-8"))
  117. except (UnicodeDecodeError, json.JSONDecodeError) as e:
  118. print(f"watch_github: response is not valid JSON: {e}", file=sys.stderr)
  119. return 2
  120. # Drill into items_path if needed (search endpoint returns {"items":[...]}).
  121. if items_path:
  122. data = data.get(items_path) if isinstance(data, dict) else None
  123. if not isinstance(data, list):
  124. print(f"watch_github: expected a list of items; got {type(data).__name__}",
  125. file=sys.stderr)
  126. return 2
  127. items = [flatten(i) for i in data if isinstance(i, dict)]
  128. # Drop any items that flattened without an ID (defensive).
  129. items = [i for i in items if i.get("id")]
  130. wm = Watermark.load(args.name)
  131. new_items = wm.filter_new(items, id_key="id")
  132. wm.save()
  133. if args.max > 0:
  134. new_items = new_items[: args.max]
  135. body_key = "body" if args.with_body else None
  136. output = format_items_as_markdown(new_items, body_key=body_key)
  137. if output:
  138. sys.stdout.write(output)
  139. return 0
  140. if __name__ == "__main__":
  141. sys.exit(main())