-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgithub_mira_trigger.py
More file actions
370 lines (320 loc) · 13.4 KB
/
Copy pathgithub_mira_trigger.py
File metadata and controls
370 lines (320 loc) · 13.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
"""GitHub → Mira trigger bridge.
Receives GitHub webhook events at ``/api/github/mira/webhook``, validates the
``X-Hub-Signature-256`` HMAC, and forwards cutover-relevant events to:
1. The Mira Telegram group (via :func:`mira_notify.send_mira_notification`).
2. Any configured Mira partner webhook URL (via
:func:`tools.partner_webhooks.dispatch_partner_event`) with an HMAC-SHA256
signature in the ``X-THO-Signature`` header.
3. The Firestore ``activities/`` collection for auditability.
Partner-facing status/config endpoint lives at ``/api/v1/github/mira/status``
and uses the standard partner API key auth.
"""
from __future__ import annotations
import hashlib
import hmac
import json
import os
import uuid
from datetime import UTC, datetime
from typing import Any
from fastapi import APIRouter, HTTPException, Request
from pydantic import BaseModel
import mira_notify
from database.firestore_client import get_database
from structured_logging import logger as struct_logger
from tools.partner_webhooks import dispatch_partner_event
_GITHUB_WEBHOOK_SECRET = (os.environ.get("GITHUB_WEBHOOK_SECRET") or "").strip()
webhook_router = APIRouter(prefix="/api/github/mira", tags=["github-mira"])
status_router = APIRouter(prefix="/api/v1/github/mira", tags=["github-mira"])
_NOTIFY_EVENT_TYPES = {
"pull_request",
"pull_request_review",
"issues",
"workflow_run",
"workflow_job",
"deployment_status",
"release",
"push",
}
_IGNORED_ACTIONS = {
"assigned",
"unassigned",
"labeled",
"unlabeled",
"milestoned",
"demilestoned",
}
CUTOVER_PR_NUMBER = 156
class GitHubMiraStatus(BaseModel):
configured: bool
webhook_secret_set: bool
mira_group_configured: bool
partner_webhook_url_configured: bool
cutover_pr_number: int
def _verify_github_signature(body: bytes, signature: str, secret: str) -> bool:
"""Verify a GitHub ``X-Hub-Signature-256`` HMAC-SHA256 signature."""
if not secret or not signature:
return False
if not signature.startswith("sha256="):
return False
expected = hmac.new(secret.encode("utf-8"), body, hashlib.sha256).hexdigest()
return hmac.compare_digest(f"sha256={expected}", signature)
def _repo_basics(payload: dict[str, Any]) -> tuple[str, str | None]:
"""Return (full_name, html_url) for the repository in a payload."""
repo = payload.get("repository") or {}
return repo.get("full_name", "unknown/repo"), repo.get("html_url")
def _format_github_event(event_type: str, payload: dict[str, Any]) -> tuple[str, str | None]:
"""Return a Telegram-friendly (message, action_url) tuple for an event."""
repo_name, repo_url = _repo_basics(payload)
if event_type == "pull_request":
pr = payload.get("pull_request") or {}
number = pr.get("number")
title = pr.get("title", "Untitled PR")
action = payload.get("action", "updated")
url = pr.get("html_url")
if number == CUTOVER_PR_NUMBER:
message = f"🚀 *CUTOVER PR #{number}* `{action}`\n_{title}_"
else:
message = f"🔀 *{repo_name}* PR #{number} `{action}`\n_{title}_"
return message, url
if event_type == "issues":
issue = payload.get("issue") or {}
number = issue.get("number")
title = issue.get("title", "Untitled issue")
action = payload.get("action", "updated")
url = issue.get("html_url")
return f"🐛 *{repo_name}* issue #{number} `{action}`\n_{title}_", url
if event_type == "workflow_run":
run = payload.get("workflow_run") or {}
name = run.get("name", "Workflow")
status = run.get("status", "unknown")
conclusion = run.get("conclusion")
branch = run.get("head_branch", "unknown")
url = run.get("html_url")
level_icon = "🟢" if conclusion == "success" else "🔴" if conclusion == "failure" else "⚙️"
message = f"{level_icon} *{repo_name}* workflow `{name}` on `{branch}`\nstatus: {status}"
if conclusion:
message += f"\nconclusion: {conclusion}"
return message, url
if event_type == "workflow_job":
job = payload.get("workflow_job") or {}
name = job.get("name", "Job")
status = job.get("status", "unknown")
conclusion = job.get("conclusion")
url = job.get("html_url")
level_icon = "🟢" if conclusion == "success" else "🔴" if conclusion == "failure" else "⚙️"
message = f"{level_icon} *{repo_name}* job `{name}`\nstatus: {status}"
if conclusion:
message += f"\nconclusion: {conclusion}"
return message, url
if event_type == "deployment_status":
deployment = payload.get("deployment") or {}
status = payload.get("deployment_status") or {}
state = status.get("state", "unknown")
env = deployment.get("environment", "unknown")
url = status.get("target_url") or deployment.get("url")
level_icon = "🟢" if state == "success" else "🔴" if state in {"failure", "error"} else "⚙️"
return f"{level_icon} *{repo_name}* deploy to `{env}`: {state}", url
if event_type == "release":
release = payload.get("release") or {}
tag = release.get("tag_name", "unknown")
name = release.get("name", tag)
action = payload.get("action", "published")
url = release.get("html_url")
return f"🏷️ *{repo_name}* release `{tag}` {action}\n_{name}_", url
if event_type == "push":
ref = payload.get("ref", "unknown")
commits = payload.get("commits") or []
pusher = (payload.get("pusher") or {}).get("name", "unknown")
message = f"⬆️ *{repo_name}* push to `{ref}` by {pusher}\n{len(commits)} commit(s)"
return message, repo_url
return f"📡 *{repo_name}* `{event_type}` event received", repo_url
def _is_notable(event_type: str, payload: dict[str, Any]) -> bool:
"""Return True when an event should produce a Mira notification."""
if event_type not in _NOTIFY_EVENT_TYPES:
return False
action = payload.get("action", "")
if action in _IGNORED_ACTIONS:
return False
if event_type == "push":
return payload.get("ref") == "refs/heads/main"
return True
def _build_partner_payload(event_type: str, payload: dict[str, Any]) -> dict[str, Any]:
"""Build a PII-safe payload for the HMAC-signed outbound partner webhook."""
repo_name, repo_url = _repo_basics(payload)
base: dict[str, Any] = {
"github_event_type": event_type,
"repository": repo_name,
"repository_url": repo_url,
"action": payload.get("action"),
"sender": (payload.get("sender") or {}).get("login"),
}
if event_type == "pull_request":
pr = payload.get("pull_request") or {}
base.update(
{
"pr_number": pr.get("number"),
"pr_title": pr.get("title"),
"pr_url": pr.get("html_url"),
"pr_state": pr.get("state"),
"pr_merged": pr.get("merged"),
"base_branch": (pr.get("base") or {}).get("ref"),
"head_branch": (pr.get("head") or {}).get("ref"),
"is_cutover_pr": pr.get("number") == CUTOVER_PR_NUMBER,
}
)
elif event_type == "issues":
issue = payload.get("issue") or {}
base.update(
{
"issue_number": issue.get("number"),
"issue_title": issue.get("title"),
"issue_url": issue.get("html_url"),
"issue_state": issue.get("state"),
}
)
elif event_type == "workflow_run":
run = payload.get("workflow_run") or {}
base.update(
{
"workflow_name": run.get("name"),
"workflow_url": run.get("html_url"),
"workflow_status": run.get("status"),
"workflow_conclusion": run.get("conclusion"),
"head_branch": run.get("head_branch"),
}
)
elif event_type == "workflow_job":
job = payload.get("workflow_job") or {}
base.update(
{
"job_name": job.get("name"),
"job_url": job.get("html_url"),
"job_status": job.get("status"),
"job_conclusion": job.get("conclusion"),
"workflow_name": job.get("workflow_name"),
}
)
elif event_type == "deployment_status":
deployment = payload.get("deployment") or {}
status = payload.get("deployment_status") or {}
base.update(
{
"deployment_environment": deployment.get("environment"),
"deployment_state": status.get("state"),
"deployment_url": status.get("target_url") or deployment.get("url"),
}
)
elif event_type == "release":
release = payload.get("release") or {}
base.update(
{
"release_tag": release.get("tag_name"),
"release_name": release.get("name"),
"release_url": release.get("html_url"),
"release_action": payload.get("action"),
}
)
elif event_type == "push":
base.update(
{
"ref": payload.get("ref"),
"commits_count": len(payload.get("commits") or []),
"head_commit_message": (payload.get("head_commit") or {}).get("message"),
"pusher": (payload.get("pusher") or {}).get("name"),
}
)
return base
def _log_activity(
event_type: str,
payload: dict[str, Any],
telegram_ok: bool,
partner_ids: list[str],
error: str | None = None,
) -> None:
"""Persist the trigger outcome to Firestore ``activities/`` for audit."""
try:
db = get_database()
repo_name, _ = _repo_basics(payload)
activity = {
"id": str(uuid.uuid4()),
"activity_type": f"github_mira_trigger.{event_type}",
"description": f"GitHub → Mira trigger: {event_type} from {repo_name}",
"actor": "system:github-mira-trigger",
"metadata": {
"github_event_type": event_type,
"repository": repo_name,
"action": payload.get("action"),
"telegram_ok": telegram_ok,
"partner_ids": partner_ids,
"error": error,
},
"created_at": datetime.now(UTC).isoformat(),
}
db.db.collection("activities").document(activity["id"]).set(activity)
except Exception as e:
struct_logger.warning("github_mira_trigger activity log failed", error=str(e))
@webhook_router.post("/webhook")
async def github_mira_webhook(request: Request) -> dict:
"""Receive a GitHub webhook and forward notable events to Mira."""
if not _GITHUB_WEBHOOK_SECRET:
return {"status": "ignored", "reason": "github_webhook_secret_not_configured"}
body = await request.body()
signature = request.headers.get("X-Hub-Signature-256", "")
if not _verify_github_signature(body, signature, _GITHUB_WEBHOOK_SECRET):
raise HTTPException(status_code=401, detail="Invalid webhook signature")
event_type = request.headers.get("X-GitHub-Event", "")
try:
payload = json.loads(body)
except json.JSONDecodeError as e:
raise HTTPException(status_code=400, detail="Invalid JSON payload") from e
if not _is_notable(event_type, payload):
return {"status": "ignored", "event_type": event_type, "reason": "not_notable"}
message, action_url = _format_github_event(event_type, payload)
telegram_result: dict[str, Any] = {"ok": False, "error": "send_failed"}
try:
notify_payload = mira_notify.NotifyPayload(
message=message,
level="info",
source="github",
action_url=action_url,
)
telegram_result = mira_notify.send_mira_notification(notify_payload)
except Exception as e:
telegram_result = {"ok": False, "error": "send_failed"}
struct_logger.warning("github_mira_trigger telegram send failed", error=str(e))
partner_ids: list[str] = []
try:
partner_payload = _build_partner_payload(event_type, payload)
partner_ids = dispatch_partner_event(
f"github.{event_type}",
partner_payload,
db=get_database(),
)
except Exception as e:
struct_logger.warning("github_mira_trigger partner dispatch failed", error=str(e))
_log_activity(
event_type=event_type,
payload=payload,
telegram_ok=telegram_result.get("ok", False),
partner_ids=partner_ids,
error=telegram_result.get("error"),
)
return {
"status": "ok",
"event_type": event_type,
"telegram_ok": telegram_result.get("ok", False),
"partner_ids": partner_ids,
}
@status_router.get("/status")
async def github_mira_status() -> GitHubMiraStatus:
"""Return the trigger configuration status. Protected by partner API key."""
mira_group = os.environ.get("MIRA_GROUP_ID") or os.environ.get("KIMI_RELAY_CHAT_ID")
partner_url = (os.environ.get("PARTNER_WEBHOOK_URL_MIRA") or "").strip()
return GitHubMiraStatus(
configured=bool(_GITHUB_WEBHOOK_SECRET),
webhook_secret_set=bool(_GITHUB_WEBHOOK_SECRET),
mira_group_configured=bool(mira_group and mira_group.strip()),
partner_webhook_url_configured=bool(partner_url),
cutover_pr_number=CUTOVER_PR_NUMBER,
)