robert-github-agent 0.1.0b1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- robert_agent/__init__.py +1 -0
- robert_agent/acceptance.py +394 -0
- robert_agent/acceptance_suite.py +287 -0
- robert_agent/audit_result.py +501 -0
- robert_agent/authorize.py +117 -0
- robert_agent/board.py +538 -0
- robert_agent/chat_status.py +338 -0
- robert_agent/cli/__init__.py +1 -0
- robert_agent/cli/exit_codes.py +5 -0
- robert_agent/cli/main.py +535 -0
- robert_agent/cli/output.py +20 -0
- robert_agent/common.py +6 -0
- robert_agent/controlled_e2e_acceptance.py +394 -0
- robert_agent/daemon.py +607 -0
- robert_agent/daemon_state.py +319 -0
- robert_agent/diagnostics.py +119 -0
- robert_agent/discover.py +775 -0
- robert_agent/dispatch.py +217 -0
- robert_agent/doctor.py +94 -0
- robert_agent/github_api.py +12 -0
- robert_agent/init_config.py +126 -0
- robert_agent/integrations/__init__.py +1 -0
- robert_agent/integrations/openclaw.py +285 -0
- robert_agent/live_discovery_acceptance.py +70 -0
- robert_agent/live_worker_acceptance.py +363 -0
- robert_agent/live_worktree_acceptance.py +344 -0
- robert_agent/loop_engine.py +285 -0
- robert_agent/memory_curator.py +105 -0
- robert_agent/migrate.py +157 -0
- robert_agent/notify.py +74 -0
- robert_agent/paths.py +16 -0
- robert_agent/production_write_canary.py +254 -0
- robert_agent/project_memory.py +503 -0
- robert_agent/publish.py +743 -0
- robert_agent/publish_dedupe_acceptance.py +270 -0
- robert_agent/redaction.py +41 -0
- robert_agent/render_prompt.py +544 -0
- robert_agent/resource_files.py +8 -0
- robert_agent/resources/__init__.py +1 -0
- robert_agent/resources/acceptance-matrix.md +120 -0
- robert_agent/resources/classifier.md +19 -0
- robert_agent/resources/command-matrix.md +38 -0
- robert_agent/resources/config.example.yml +20 -0
- robert_agent/resources/db/migrations/001_legacy_dd_to_robert.sql +14 -0
- robert_agent/resources/db/schema.sql +479 -0
- robert_agent/resources/failure-modes.md +36 -0
- robert_agent/resources/fixtures/actor-permission.json +1 -0
- robert_agent/resources/fixtures/discovery.json +1 -0
- robert_agent/resources/fixtures/redaction.json +1 -0
- robert_agent/resources/fixtures/review-followup.json +1 -0
- robert_agent/resources/fixtures/worker-results.json +1 -0
- robert_agent/resources/fixtures/workstreams.json +1 -0
- robert_agent/resources/github-events.md +77 -0
- robert_agent/resources/kanban-control.md +165 -0
- robert_agent/resources/redaction.md +29 -0
- robert_agent/resources/result-contract.md +56 -0
- robert_agent/resources/routes.yml +82 -0
- robert_agent/resources/schemas/config.schema.json +123 -0
- robert_agent/resources/schemas/event.schema.json +18 -0
- robert_agent/resources/schemas/result.schema.json +99 -0
- robert_agent/resources/schemas/route.schema.json +23 -0
- robert_agent/resources/schemas/snapshot.schema.json +17 -0
- robert_agent/resources/schemas/task.schema.json +15 -0
- robert_agent/resources/trust-model.md +16 -0
- robert_agent/resources/worker-prompt.md +121 -0
- robert_agent/resources/worker-protocol.md +47 -0
- robert_agent/resources/workflow.yml +50 -0
- robert_agent/resources/workstream-model.md +17 -0
- robert_agent/route.py +232 -0
- robert_agent/route_config.py +64 -0
- robert_agent/run_once.py +5375 -0
- robert_agent/runtime_knowledge.py +392 -0
- robert_agent/service.py +161 -0
- robert_agent/skills.py +28 -0
- robert_agent/status.py +1051 -0
- robert_agent/storage.py +376 -0
- robert_agent/summarize.py +100 -0
- robert_agent/supervise.py +72 -0
- robert_agent/usage.py +192 -0
- robert_agent/validate_config.py +616 -0
- robert_agent/wakeup.py +261 -0
- robert_agent/web.py +2893 -0
- robert_agent/web_assets/board.css +255 -0
- robert_agent/web_assets/board.html +165 -0
- robert_agent/web_assets/board.js +660 -0
- robert_agent/web_assets/github-shell.css +122 -0
- robert_agent/web_assets/index.html +103 -0
- robert_agent/web_assets/operations.css +168 -0
- robert_agent/web_assets/workbench.css +131 -0
- robert_agent/web_assets/workbench.js +581 -0
- robert_agent/work_items.py +1016 -0
- robert_agent/workbench.py +701 -0
- robert_agent/worker/__init__.py +1 -0
- robert_agent/worker/heartbeat.py +190 -0
- robert_agent/worker/result.py +321 -0
- robert_agent/worker/snapshot.py +98 -0
- robert_agent/worker_adapters/__init__.py +138 -0
- robert_agent/worker_adapters/cbc.py +68 -0
- robert_agent/worker_adapters/codex.py +56 -0
- robert_agent/worker_adapters/command.py +33 -0
- robert_agent/worker_adapters/tcodex.py +56 -0
- robert_agent/worker_heartbeat.py +6 -0
- robert_agent/worker_result.py +6 -0
- robert_agent/worker_snapshot.py +6 -0
- robert_agent/workstream.py +55 -0
- robert_agent/worktree.py +297 -0
- robert_github_agent-0.1.0b1.dist-info/METADATA +217 -0
- robert_github_agent-0.1.0b1.dist-info/RECORD +111 -0
- robert_github_agent-0.1.0b1.dist-info/WHEEL +4 -0
- robert_github_agent-0.1.0b1.dist-info/entry_points.txt +2 -0
- robert_github_agent-0.1.0b1.dist-info/licenses/LICENSE +201 -0
robert_agent/discover.py
ADDED
|
@@ -0,0 +1,775 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
import argparse
|
|
3
|
+
import json
|
|
4
|
+
import re
|
|
5
|
+
import subprocess
|
|
6
|
+
from pathlib import Path
|
|
7
|
+
|
|
8
|
+
from robert_agent.common import emit
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def source_key(repo_full_name, source_type, number):
|
|
12
|
+
marker = "!" if source_type == "pull_request" else "#"
|
|
13
|
+
return f"github:{repo_full_name}{marker}{number}"
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def event_fingerprint(event_type, event_id):
|
|
17
|
+
return f"{event_type}:{event_id}"
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def normalize_events(raw_events, repo_config):
|
|
21
|
+
repo = repo_config["full_name"]
|
|
22
|
+
github_account = repo_config["github_account"]
|
|
23
|
+
normalized = []
|
|
24
|
+
|
|
25
|
+
for raw in raw_events:
|
|
26
|
+
source_type = raw.get("source_type", "issue")
|
|
27
|
+
event_type = raw["event_type"]
|
|
28
|
+
number = int(raw["number"])
|
|
29
|
+
body = raw.get("body") or ""
|
|
30
|
+
event_id = raw.get("id") or raw.get("event_id")
|
|
31
|
+
normalized_event_fingerprint = raw.get("event_fingerprint")
|
|
32
|
+
if not event_id and not normalized_event_fingerprint:
|
|
33
|
+
raise ValueError("event id or event_fingerprint is required")
|
|
34
|
+
normalized_source_key = source_key(repo, source_type, number)
|
|
35
|
+
metadata = dict(raw.get("metadata") or {})
|
|
36
|
+
dd_workstream = metadata.get("dd_workstream") or parse_dd_workstream_metadata(body)
|
|
37
|
+
workstream_id = raw.get("workstream_id") or normalized_source_key
|
|
38
|
+
origin_workstream_id = raw.get("origin_workstream_id")
|
|
39
|
+
has_open_dd_pr = raw.get("has_open_dd_pr")
|
|
40
|
+
if source_type == "pull_request" and dd_workstream:
|
|
41
|
+
metadata["dd_workstream"] = dd_workstream
|
|
42
|
+
origin_workstream_id = (
|
|
43
|
+
dd_workstream.get("origin_workstream_id")
|
|
44
|
+
or dd_workstream.get("workstream_id")
|
|
45
|
+
or origin_workstream_id
|
|
46
|
+
)
|
|
47
|
+
workstream_id = normalized_source_key
|
|
48
|
+
has_open_dd_pr = True
|
|
49
|
+
normalized.append(
|
|
50
|
+
{
|
|
51
|
+
**raw,
|
|
52
|
+
"repo": repo,
|
|
53
|
+
"source_type": source_type,
|
|
54
|
+
"number": number,
|
|
55
|
+
"source_key": normalized_source_key,
|
|
56
|
+
"workstream_id": workstream_id,
|
|
57
|
+
"origin_workstream_id": origin_workstream_id,
|
|
58
|
+
"event_fingerprint": normalized_event_fingerprint
|
|
59
|
+
or event_fingerprint(event_type, event_id),
|
|
60
|
+
"mentions_dd": bool(raw.get("mentions_dd") or f"@{github_account}" in body),
|
|
61
|
+
"body": body,
|
|
62
|
+
"metadata": metadata,
|
|
63
|
+
"has_open_dd_pr": bool(has_open_dd_pr),
|
|
64
|
+
}
|
|
65
|
+
)
|
|
66
|
+
|
|
67
|
+
return normalized
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
def _run_json(args, runner=subprocess.run):
|
|
71
|
+
completed = runner(args, text=True, capture_output=True, check=True)
|
|
72
|
+
stdout = completed.stdout.strip()
|
|
73
|
+
if not stdout:
|
|
74
|
+
return []
|
|
75
|
+
return json.loads(stdout)
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def _try_run_json(args, runner=subprocess.run):
|
|
79
|
+
try:
|
|
80
|
+
return _run_json(args, runner=runner)
|
|
81
|
+
except (subprocess.CalledProcessError, json.JSONDecodeError):
|
|
82
|
+
return None
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
def _flatten_items(payload):
|
|
86
|
+
if not payload:
|
|
87
|
+
return []
|
|
88
|
+
if isinstance(payload, dict):
|
|
89
|
+
return [payload]
|
|
90
|
+
items = []
|
|
91
|
+
for item in payload:
|
|
92
|
+
if isinstance(item, list):
|
|
93
|
+
items.extend(item)
|
|
94
|
+
else:
|
|
95
|
+
items.append(item)
|
|
96
|
+
return items
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
def _api_items(repo, api_path, runner):
|
|
100
|
+
payload = _try_run_json(
|
|
101
|
+
["gh", "api", f"repos/{repo}/{api_path}", "--paginate", "--slurp"],
|
|
102
|
+
runner=runner,
|
|
103
|
+
)
|
|
104
|
+
if payload is None:
|
|
105
|
+
return None
|
|
106
|
+
return _flatten_items(payload)
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
def _login(payload, key):
|
|
110
|
+
value = payload.get(key)
|
|
111
|
+
if isinstance(value, dict):
|
|
112
|
+
return value.get("login")
|
|
113
|
+
return value
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
def _mentions_dd(body, github_account):
|
|
117
|
+
return f"@{github_account}" in (body or "")
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
ROBERT_WORKSTREAM_BLOCK = re.compile(r"<!--\s*robert-workstream(?P<body>.*?)-->", re.I | re.S)
|
|
121
|
+
LINKED_ISSUE_REFERENCE = re.compile(
|
|
122
|
+
r"(?im)\b(?:close[sd]?|fix(?:e[sd])?|resolve[sd]?|refs?|addresses?)\s+(?:(?P<repo>[\w.-]+/[\w.-]+))?#(?P<number>\d+)\b"
|
|
123
|
+
)
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
def parse_dd_workstream_metadata(body):
|
|
127
|
+
match = ROBERT_WORKSTREAM_BLOCK.search(body or "")
|
|
128
|
+
if not match:
|
|
129
|
+
return {}
|
|
130
|
+
metadata = {}
|
|
131
|
+
for line in match.group("body").splitlines():
|
|
132
|
+
key, sep, value = line.partition(":")
|
|
133
|
+
if not sep:
|
|
134
|
+
continue
|
|
135
|
+
key = key.strip()
|
|
136
|
+
value = value.strip()
|
|
137
|
+
if key and value:
|
|
138
|
+
metadata[key] = value
|
|
139
|
+
return metadata
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
def infer_dd_workstream_from_pr_body(body, repo_full_name):
|
|
143
|
+
for match in LINKED_ISSUE_REFERENCE.finditer(body or ""):
|
|
144
|
+
referenced_repo = match.group("repo") or repo_full_name
|
|
145
|
+
if referenced_repo != repo_full_name:
|
|
146
|
+
continue
|
|
147
|
+
issue_number = int(match.group("number"))
|
|
148
|
+
return {
|
|
149
|
+
"workstream_id": source_key(repo_full_name, "issue", issue_number),
|
|
150
|
+
"source_issue": str(issue_number),
|
|
151
|
+
"inferred_from": "pr_body_issue_reference",
|
|
152
|
+
}
|
|
153
|
+
return {}
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
def infer_intent(title, body, event_type):
|
|
157
|
+
text = f"{title or ''}\n{body or ''}".lower()
|
|
158
|
+
if event_type == "review_request":
|
|
159
|
+
return "review_request"
|
|
160
|
+
if any(word in text for word in ["bug", "fix", "修复", "报错", "失败"]):
|
|
161
|
+
return "bug_fix"
|
|
162
|
+
if any(
|
|
163
|
+
word in text
|
|
164
|
+
for word in ["分析", "analysis", "analyze", "explain", "discuss", "讨论", "方案"]
|
|
165
|
+
):
|
|
166
|
+
return "analysis"
|
|
167
|
+
return "unclear"
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
_infer_intent = infer_intent
|
|
171
|
+
|
|
172
|
+
|
|
173
|
+
def _issue_search_to_raw(item, event_type):
|
|
174
|
+
source_type = "pull_request" if item.get("isPullRequest") else "issue"
|
|
175
|
+
author = item.get("author") or {}
|
|
176
|
+
body = item.get("body") or ""
|
|
177
|
+
raw = {
|
|
178
|
+
"number": item["number"],
|
|
179
|
+
"source_type": source_type,
|
|
180
|
+
"event_type": event_type,
|
|
181
|
+
"actor_login": author.get("login"),
|
|
182
|
+
"author_association": item.get("authorAssociation"),
|
|
183
|
+
"title": item.get("title", ""),
|
|
184
|
+
"body": body,
|
|
185
|
+
"url": item.get("url"),
|
|
186
|
+
"event_at": item.get("updatedAt"),
|
|
187
|
+
"intent": _infer_intent(item.get("title", ""), body, event_type),
|
|
188
|
+
}
|
|
189
|
+
if item.get("id"):
|
|
190
|
+
raw["id"] = item.get("id")
|
|
191
|
+
else:
|
|
192
|
+
raw["event_fingerprint"] = event_fingerprint(
|
|
193
|
+
event_type,
|
|
194
|
+
f"{item.get('url') or item.get('number')}:{item.get('updatedAt') or ''}",
|
|
195
|
+
)
|
|
196
|
+
return raw
|
|
197
|
+
|
|
198
|
+
|
|
199
|
+
def _enrich_pull_request_metadata(raw, repo_config, runner):
|
|
200
|
+
if raw.get("source_type") != "pull_request":
|
|
201
|
+
return raw
|
|
202
|
+
repo = repo_config["full_name"]
|
|
203
|
+
github_account = repo_config["github_account"]
|
|
204
|
+
details = _try_run_json(["gh", "api", f"repos/{repo}/pulls/{raw['number']}"], runner=runner)
|
|
205
|
+
details = details if isinstance(details, dict) else {}
|
|
206
|
+
body = details.get("body") or raw.get("body") or ""
|
|
207
|
+
dd_workstream = parse_dd_workstream_metadata(body)
|
|
208
|
+
pr_author = _login(details, "user") or raw.get("pr_author_login")
|
|
209
|
+
is_dd_authored_pr = pr_author == github_account
|
|
210
|
+
if not dd_workstream and is_dd_authored_pr:
|
|
211
|
+
dd_workstream = infer_dd_workstream_from_pr_body(body, repo)
|
|
212
|
+
base = details.get("base") if isinstance(details.get("base"), dict) else {}
|
|
213
|
+
base_ref = base.get("ref") or raw.get("baseRefName") or raw.get("base_branch")
|
|
214
|
+
head = details.get("head") if isinstance(details.get("head"), dict) else {}
|
|
215
|
+
head_ref = head.get("ref") or raw.get("headRefName") or raw.get("head_ref")
|
|
216
|
+
|
|
217
|
+
if not dd_workstream and not is_dd_authored_pr:
|
|
218
|
+
enriched = {**raw}
|
|
219
|
+
if base_ref:
|
|
220
|
+
enriched["base_branch"] = base_ref
|
|
221
|
+
if pr_author:
|
|
222
|
+
enriched["pr_author_login"] = pr_author
|
|
223
|
+
return enriched
|
|
224
|
+
|
|
225
|
+
metadata = dict(raw.get("metadata") or {})
|
|
226
|
+
if dd_workstream:
|
|
227
|
+
metadata["dd_workstream"] = dd_workstream
|
|
228
|
+
enriched = {
|
|
229
|
+
**raw,
|
|
230
|
+
"metadata": metadata,
|
|
231
|
+
"has_open_dd_pr": True,
|
|
232
|
+
}
|
|
233
|
+
if details:
|
|
234
|
+
enriched["state"] = details.get("state") or raw.get("state")
|
|
235
|
+
enriched["merged"] = bool(details.get("merged") or details.get("merged_at"))
|
|
236
|
+
enriched["merged_at"] = details.get("merged_at")
|
|
237
|
+
if dd_workstream:
|
|
238
|
+
enriched["origin_workstream_id"] = (
|
|
239
|
+
dd_workstream.get("origin_workstream_id")
|
|
240
|
+
or dd_workstream.get("workstream_id")
|
|
241
|
+
or raw.get("origin_workstream_id")
|
|
242
|
+
)
|
|
243
|
+
if head_ref:
|
|
244
|
+
enriched["existing_pr_head_branch"] = head_ref
|
|
245
|
+
if base_ref:
|
|
246
|
+
enriched["base_branch"] = base_ref
|
|
247
|
+
if pr_author:
|
|
248
|
+
enriched["pr_author_login"] = pr_author
|
|
249
|
+
return enriched
|
|
250
|
+
|
|
251
|
+
|
|
252
|
+
def _actor_permission(repo_config, actor_login, runner):
|
|
253
|
+
if not actor_login:
|
|
254
|
+
return None
|
|
255
|
+
repo = repo_config["full_name"]
|
|
256
|
+
payload = _try_run_json(
|
|
257
|
+
["gh", "api", f"repos/{repo}/collaborators/{actor_login}/permission"],
|
|
258
|
+
runner=runner,
|
|
259
|
+
)
|
|
260
|
+
if isinstance(payload, dict):
|
|
261
|
+
return payload.get("permission")
|
|
262
|
+
return None
|
|
263
|
+
|
|
264
|
+
|
|
265
|
+
def _notification_to_raw(notification):
|
|
266
|
+
subject = notification.get("subject") or {}
|
|
267
|
+
repository = notification.get("repository") or {}
|
|
268
|
+
repo_full_name = repository.get("full_name")
|
|
269
|
+
url = subject.get("url") or ""
|
|
270
|
+
match = re.search(r"/repos/(?P<repo>[^/]+/[^/]+)/(?:issues|pulls)/(?P<number>\d+)$", url)
|
|
271
|
+
repo_full_name = repo_full_name or (match.group("repo") if match else None)
|
|
272
|
+
if not match or not repo_full_name:
|
|
273
|
+
return None
|
|
274
|
+
subject_type = (subject.get("type") or "").lower()
|
|
275
|
+
source_type = "pull_request" if "pull" in subject_type or "/pulls/" in url else "issue"
|
|
276
|
+
return {
|
|
277
|
+
"id": notification.get("id"),
|
|
278
|
+
"repo_full_name": repo_full_name,
|
|
279
|
+
"number": int(match.group("number")),
|
|
280
|
+
"source_type": source_type,
|
|
281
|
+
"event_type": "notification",
|
|
282
|
+
"actor_login": "github",
|
|
283
|
+
"trusted_trigger_found": False,
|
|
284
|
+
"authorization_lookup_complete": False,
|
|
285
|
+
"event_at": notification.get("updated_at"),
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
|
|
289
|
+
def collect_account_notifications(repo_configs, runner=subprocess.run):
|
|
290
|
+
configured = {repo["full_name"] for repo in repo_configs}
|
|
291
|
+
buckets = {repo["full_name"]: [] for repo in repo_configs}
|
|
292
|
+
pages = _run_json(
|
|
293
|
+
["gh", "api", "notifications", "--paginate", "--slurp"],
|
|
294
|
+
runner=runner,
|
|
295
|
+
)
|
|
296
|
+
for page in pages:
|
|
297
|
+
notifications = page if isinstance(page, list) else [page]
|
|
298
|
+
for notification in notifications:
|
|
299
|
+
raw = _notification_to_raw(notification)
|
|
300
|
+
if not raw:
|
|
301
|
+
continue
|
|
302
|
+
repo_full_name = raw.get("repo_full_name")
|
|
303
|
+
if repo_full_name not in configured:
|
|
304
|
+
continue
|
|
305
|
+
buckets[repo_full_name].append(raw)
|
|
306
|
+
return {repo: hints for repo, hints in buckets.items() if hints}
|
|
307
|
+
|
|
308
|
+
|
|
309
|
+
def _source_metadata(raw, repo_config, runner):
|
|
310
|
+
repo = repo_config["full_name"]
|
|
311
|
+
payload = _try_run_json(["gh", "api", f"repos/{repo}/issues/{raw['number']}"], runner=runner)
|
|
312
|
+
if not isinstance(payload, dict):
|
|
313
|
+
return raw, None
|
|
314
|
+
user = payload.get("user") if isinstance(payload.get("user"), dict) else {}
|
|
315
|
+
return (
|
|
316
|
+
{
|
|
317
|
+
**raw,
|
|
318
|
+
"title": payload.get("title") or raw.get("title", ""),
|
|
319
|
+
"state": payload.get("state") or raw.get("state", ""),
|
|
320
|
+
"state_reason": payload.get("state_reason"),
|
|
321
|
+
"source_updated_at": payload.get("updated_at"),
|
|
322
|
+
"closed_at": payload.get("closed_at"),
|
|
323
|
+
"url": payload.get("html_url") or raw.get("url"),
|
|
324
|
+
"author_login": user.get("login") or raw.get("author_login"),
|
|
325
|
+
},
|
|
326
|
+
payload.get("state"),
|
|
327
|
+
)
|
|
328
|
+
|
|
329
|
+
|
|
330
|
+
def _timeline_assignment(raw, repo_config, runner):
|
|
331
|
+
repo = repo_config["full_name"]
|
|
332
|
+
github_account = repo_config["github_account"]
|
|
333
|
+
timeline = _api_items(repo, f"issues/{raw['number']}/timeline", runner)
|
|
334
|
+
if timeline is None:
|
|
335
|
+
return {
|
|
336
|
+
**raw,
|
|
337
|
+
"assigned_to": github_account,
|
|
338
|
+
"authorization_lookup_complete": False,
|
|
339
|
+
}
|
|
340
|
+
for item in timeline:
|
|
341
|
+
if item.get("event") != "assigned":
|
|
342
|
+
continue
|
|
343
|
+
if _login(item, "assignee") != github_account:
|
|
344
|
+
continue
|
|
345
|
+
event_id = item.get("id") or raw.get("id")
|
|
346
|
+
enriched = {
|
|
347
|
+
**raw,
|
|
348
|
+
"id": event_id,
|
|
349
|
+
"assignment_actor_login": _login(item, "actor"),
|
|
350
|
+
"assigned_to": github_account,
|
|
351
|
+
"authorization_lookup_complete": True,
|
|
352
|
+
"event_at": item.get("created_at") or raw.get("event_at"),
|
|
353
|
+
}
|
|
354
|
+
if item.get("id"):
|
|
355
|
+
enriched["event_fingerprint"] = event_fingerprint(
|
|
356
|
+
"assigned",
|
|
357
|
+
item["id"],
|
|
358
|
+
)
|
|
359
|
+
return enriched
|
|
360
|
+
return {
|
|
361
|
+
**raw,
|
|
362
|
+
"assigned_to": github_account,
|
|
363
|
+
"authorization_lookup_complete": True,
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
|
|
367
|
+
def _timeline_review_request(raw, repo_config, runner):
|
|
368
|
+
if raw.get("source_type") != "pull_request":
|
|
369
|
+
return {
|
|
370
|
+
**raw,
|
|
371
|
+
"authorization_lookup_complete": True,
|
|
372
|
+
}
|
|
373
|
+
repo = repo_config["full_name"]
|
|
374
|
+
github_account = repo_config["github_account"]
|
|
375
|
+
timeline = _api_items(repo, f"issues/{raw['number']}/timeline", runner)
|
|
376
|
+
if timeline is None:
|
|
377
|
+
return {
|
|
378
|
+
**raw,
|
|
379
|
+
"authorization_lookup_complete": False,
|
|
380
|
+
}
|
|
381
|
+
for item in timeline:
|
|
382
|
+
if item.get("event") != "review_requested":
|
|
383
|
+
continue
|
|
384
|
+
requested_reviewer = _login(item, "requested_reviewer")
|
|
385
|
+
requested_team = item.get("requested_team")
|
|
386
|
+
requested_team_name = None
|
|
387
|
+
requested_team_slug = None
|
|
388
|
+
if isinstance(requested_team, dict):
|
|
389
|
+
requested_team_name = requested_team.get("name")
|
|
390
|
+
requested_team_slug = requested_team.get("slug")
|
|
391
|
+
if github_account not in {requested_reviewer, requested_team_name, requested_team_slug}:
|
|
392
|
+
continue
|
|
393
|
+
event_id = item.get("id") or raw.get("id")
|
|
394
|
+
enriched = {
|
|
395
|
+
**raw,
|
|
396
|
+
"id": event_id,
|
|
397
|
+
"event_type": "review_request",
|
|
398
|
+
"requester_login": _login(item, "actor"),
|
|
399
|
+
"requested_reviewer": requested_reviewer,
|
|
400
|
+
"requested_team": requested_team_slug or requested_team_name,
|
|
401
|
+
"authorization_lookup_complete": True,
|
|
402
|
+
"event_at": item.get("created_at") or raw.get("event_at"),
|
|
403
|
+
"intent": "review_request",
|
|
404
|
+
}
|
|
405
|
+
if item.get("id"):
|
|
406
|
+
enriched["event_fingerprint"] = event_fingerprint(
|
|
407
|
+
"review_request",
|
|
408
|
+
item["id"],
|
|
409
|
+
)
|
|
410
|
+
return enriched
|
|
411
|
+
return {
|
|
412
|
+
**raw,
|
|
413
|
+
"authorization_lookup_complete": True,
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
|
|
417
|
+
def _comment_to_raw(raw, comment, event_type="comment", repo_config=None, runner=None):
|
|
418
|
+
user = comment.get("user") or {}
|
|
419
|
+
actor_login = user.get("login")
|
|
420
|
+
association = comment.get("author_association") or comment.get("authorAssociation")
|
|
421
|
+
permission = comment.get("actor_permission")
|
|
422
|
+
if (
|
|
423
|
+
repo_config
|
|
424
|
+
and runner
|
|
425
|
+
and actor_login
|
|
426
|
+
and association in {None, "", "UNKNOWN"}
|
|
427
|
+
and not permission
|
|
428
|
+
):
|
|
429
|
+
permission = _actor_permission(repo_config, actor_login, runner)
|
|
430
|
+
event_id = comment.get("id") or raw.get("id")
|
|
431
|
+
enriched = {
|
|
432
|
+
**raw,
|
|
433
|
+
"id": event_id,
|
|
434
|
+
"event_type": event_type,
|
|
435
|
+
"actor_login": actor_login,
|
|
436
|
+
"author_association": association,
|
|
437
|
+
"actor_permission": permission,
|
|
438
|
+
"body": comment.get("body") or "",
|
|
439
|
+
"event_at": comment.get("created_at") or comment.get("submitted_at") or raw.get("event_at"),
|
|
440
|
+
}
|
|
441
|
+
if comment.get("id"):
|
|
442
|
+
enriched["event_fingerprint"] = event_fingerprint(event_type, comment["id"])
|
|
443
|
+
return enriched
|
|
444
|
+
|
|
445
|
+
|
|
446
|
+
def _raw_workstream_id(raw, repo_full_name):
|
|
447
|
+
return source_key(repo_full_name, raw.get("source_type", "issue"), raw["number"])
|
|
448
|
+
|
|
449
|
+
|
|
450
|
+
def _discussion_items(raw, repo_config, runner):
|
|
451
|
+
repo = repo_config["full_name"]
|
|
452
|
+
items = []
|
|
453
|
+
|
|
454
|
+
comments = _api_items(repo, f"issues/{raw['number']}/comments", runner)
|
|
455
|
+
if comments is None:
|
|
456
|
+
return None
|
|
457
|
+
items.extend(("comment", comment) for comment in comments)
|
|
458
|
+
|
|
459
|
+
if raw.get("source_type") != "pull_request":
|
|
460
|
+
return items
|
|
461
|
+
|
|
462
|
+
reviews = _api_items(repo, f"pulls/{raw['number']}/reviews", runner)
|
|
463
|
+
if reviews is None:
|
|
464
|
+
return None
|
|
465
|
+
items.extend(("review", review) for review in reviews)
|
|
466
|
+
|
|
467
|
+
review_comments = _api_items(repo, f"pulls/{raw['number']}/comments", runner)
|
|
468
|
+
if review_comments is None:
|
|
469
|
+
return None
|
|
470
|
+
items.extend(("review_comment", comment) for comment in review_comments)
|
|
471
|
+
|
|
472
|
+
return items
|
|
473
|
+
|
|
474
|
+
|
|
475
|
+
def _event_sort_key(event):
|
|
476
|
+
return (event.get("event_at") or "", str(event.get("id") or ""))
|
|
477
|
+
|
|
478
|
+
|
|
479
|
+
def _latest_discussion_event(raw, repo_config, runner, predicate):
|
|
480
|
+
items = _discussion_items(raw, repo_config, runner)
|
|
481
|
+
if items is None:
|
|
482
|
+
return None, False
|
|
483
|
+
|
|
484
|
+
matched = []
|
|
485
|
+
for event_type, item in items:
|
|
486
|
+
if not predicate(item, event_type):
|
|
487
|
+
continue
|
|
488
|
+
enriched = _comment_to_raw(
|
|
489
|
+
raw,
|
|
490
|
+
item,
|
|
491
|
+
event_type=event_type,
|
|
492
|
+
repo_config=repo_config,
|
|
493
|
+
runner=runner,
|
|
494
|
+
)
|
|
495
|
+
enriched["intent"] = _infer_intent(raw.get("title"), enriched.get("body"), event_type)
|
|
496
|
+
matched.append(enriched)
|
|
497
|
+
|
|
498
|
+
if not matched:
|
|
499
|
+
return None, True
|
|
500
|
+
return max(matched, key=_event_sort_key), True
|
|
501
|
+
|
|
502
|
+
|
|
503
|
+
def _enrich_mention(raw, repo_config, runner):
|
|
504
|
+
github_account = repo_config["github_account"]
|
|
505
|
+
trusted = set(repo_config.get("trusted_actors", []))
|
|
506
|
+
body_mentions_dd = _mentions_dd(raw.get("body"), github_account)
|
|
507
|
+
if body_mentions_dd and raw.get("actor_login") in trusted:
|
|
508
|
+
return {
|
|
509
|
+
**raw,
|
|
510
|
+
"authorization_lookup_complete": True,
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
latest_mention, lookup_complete = _latest_discussion_event(
|
|
514
|
+
raw,
|
|
515
|
+
repo_config,
|
|
516
|
+
runner,
|
|
517
|
+
lambda item, _event_type: _mentions_dd(item.get("body"), github_account)
|
|
518
|
+
and (item.get("user") or {}).get("login") in trusted,
|
|
519
|
+
)
|
|
520
|
+
if latest_mention:
|
|
521
|
+
return {
|
|
522
|
+
**latest_mention,
|
|
523
|
+
"authorization_lookup_complete": True,
|
|
524
|
+
}
|
|
525
|
+
if not lookup_complete:
|
|
526
|
+
return {
|
|
527
|
+
**raw,
|
|
528
|
+
"authorization_lookup_complete": False,
|
|
529
|
+
}
|
|
530
|
+
return {
|
|
531
|
+
**raw,
|
|
532
|
+
"authorization_lookup_complete": not body_mentions_dd,
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
|
|
536
|
+
def _enrich_known_workstream_context(raw, repo_config, runner):
|
|
537
|
+
github_account = repo_config["github_account"]
|
|
538
|
+
latest_context, lookup_complete = _latest_discussion_event(
|
|
539
|
+
raw,
|
|
540
|
+
repo_config,
|
|
541
|
+
runner,
|
|
542
|
+
lambda item, _event_type: (item.get("user") or {}).get("login") != github_account
|
|
543
|
+
and _mentions_dd(item.get("body"), github_account)
|
|
544
|
+
and bool((item.get("body") or "").strip()),
|
|
545
|
+
)
|
|
546
|
+
if latest_context:
|
|
547
|
+
return {
|
|
548
|
+
**latest_context,
|
|
549
|
+
"authorization_lookup_complete": True,
|
|
550
|
+
}
|
|
551
|
+
if not lookup_complete:
|
|
552
|
+
return {
|
|
553
|
+
**raw,
|
|
554
|
+
"authorization_lookup_complete": False,
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
return {
|
|
558
|
+
**raw,
|
|
559
|
+
"authorization_lookup_complete": True,
|
|
560
|
+
}
|
|
561
|
+
|
|
562
|
+
|
|
563
|
+
def _trusted_trigger_from_source(
|
|
564
|
+
raw,
|
|
565
|
+
repo_config,
|
|
566
|
+
runner,
|
|
567
|
+
known_workstreams=None,
|
|
568
|
+
):
|
|
569
|
+
trusted = set(repo_config.get("trusted_actors", []))
|
|
570
|
+
github_account = repo_config["github_account"]
|
|
571
|
+
raw = _enrich_pull_request_metadata(raw, repo_config, runner)
|
|
572
|
+
known_workstreams = set(known_workstreams or [])
|
|
573
|
+
raw_workstream_id = _raw_workstream_id(raw, repo_config["full_name"])
|
|
574
|
+
|
|
575
|
+
def _discussion_result():
|
|
576
|
+
if raw_workstream_id in known_workstreams:
|
|
577
|
+
context_event = _enrich_known_workstream_context(raw, repo_config, runner)
|
|
578
|
+
if context_event.get("authorization_lookup_complete") is False:
|
|
579
|
+
return {
|
|
580
|
+
**context_event,
|
|
581
|
+
"trusted_trigger_found": False,
|
|
582
|
+
"authorization_lookup_complete": False,
|
|
583
|
+
}
|
|
584
|
+
if context_event.get("id") != raw.get("id"):
|
|
585
|
+
return {
|
|
586
|
+
**context_event,
|
|
587
|
+
"trusted_trigger_found": False,
|
|
588
|
+
"authorization_lookup_complete": True,
|
|
589
|
+
}
|
|
590
|
+
|
|
591
|
+
mention = _enrich_mention(raw, repo_config, runner)
|
|
592
|
+
if mention.get("authorization_lookup_complete") is False:
|
|
593
|
+
return {
|
|
594
|
+
**mention,
|
|
595
|
+
"trusted_trigger_found": False,
|
|
596
|
+
"authorization_lookup_complete": False,
|
|
597
|
+
}
|
|
598
|
+
if _mentions_dd(mention.get("body"), github_account) and mention.get("actor_login") in trusted:
|
|
599
|
+
return {
|
|
600
|
+
**mention,
|
|
601
|
+
"trusted_trigger_found": True,
|
|
602
|
+
"authorization_lookup_complete": True,
|
|
603
|
+
}
|
|
604
|
+
|
|
605
|
+
return None
|
|
606
|
+
|
|
607
|
+
assigned = _timeline_assignment(raw, repo_config, runner)
|
|
608
|
+
if assigned.get("authorization_lookup_complete") is False:
|
|
609
|
+
return {
|
|
610
|
+
**raw,
|
|
611
|
+
"trusted_trigger_found": False,
|
|
612
|
+
"authorization_lookup_complete": False,
|
|
613
|
+
}
|
|
614
|
+
if assigned.get("assignment_actor_login") in trusted:
|
|
615
|
+
return {
|
|
616
|
+
**assigned,
|
|
617
|
+
"trusted_trigger_found": True,
|
|
618
|
+
"authorization_lookup_complete": True,
|
|
619
|
+
}
|
|
620
|
+
|
|
621
|
+
review_request = _timeline_review_request(raw, repo_config, runner)
|
|
622
|
+
if review_request.get("authorization_lookup_complete") is False:
|
|
623
|
+
return {
|
|
624
|
+
**raw,
|
|
625
|
+
"trusted_trigger_found": False,
|
|
626
|
+
"authorization_lookup_complete": False,
|
|
627
|
+
}
|
|
628
|
+
if review_request.get("requester_login") in trusted:
|
|
629
|
+
return {
|
|
630
|
+
**review_request,
|
|
631
|
+
"trusted_trigger_found": True,
|
|
632
|
+
"authorization_lookup_complete": True,
|
|
633
|
+
}
|
|
634
|
+
|
|
635
|
+
discussion_result = _discussion_result()
|
|
636
|
+
if discussion_result is not None:
|
|
637
|
+
return discussion_result
|
|
638
|
+
|
|
639
|
+
return {
|
|
640
|
+
**raw,
|
|
641
|
+
"trusted_trigger_found": False,
|
|
642
|
+
"authorization_lookup_complete": True,
|
|
643
|
+
}
|
|
644
|
+
|
|
645
|
+
|
|
646
|
+
def collect_live_events(
|
|
647
|
+
repo_config,
|
|
648
|
+
runner=subprocess.run,
|
|
649
|
+
limit=30,
|
|
650
|
+
known_workstreams=None,
|
|
651
|
+
notification_hints=None,
|
|
652
|
+
include_notifications=True,
|
|
653
|
+
):
|
|
654
|
+
repo = repo_config["full_name"]
|
|
655
|
+
github_account = repo_config["github_account"]
|
|
656
|
+
fields = "number,title,body,author,authorAssociation,isPullRequest,updatedAt,url"
|
|
657
|
+
raw_events = []
|
|
658
|
+
known_workstreams = set(known_workstreams or [])
|
|
659
|
+
|
|
660
|
+
assigned = _run_json(
|
|
661
|
+
[
|
|
662
|
+
"gh",
|
|
663
|
+
"search",
|
|
664
|
+
"issues",
|
|
665
|
+
"--repo",
|
|
666
|
+
repo,
|
|
667
|
+
"--assignee",
|
|
668
|
+
"@me",
|
|
669
|
+
"--state",
|
|
670
|
+
"open",
|
|
671
|
+
"--include-prs",
|
|
672
|
+
"--json",
|
|
673
|
+
fields,
|
|
674
|
+
"--limit",
|
|
675
|
+
str(limit),
|
|
676
|
+
],
|
|
677
|
+
runner=runner,
|
|
678
|
+
)
|
|
679
|
+
for item in assigned:
|
|
680
|
+
raw = _issue_search_to_raw(item, "assigned")
|
|
681
|
+
raw = _enrich_pull_request_metadata(raw, repo_config, runner)
|
|
682
|
+
raw_events.append(_timeline_assignment(raw, repo_config, runner))
|
|
683
|
+
|
|
684
|
+
mentions = _run_json(
|
|
685
|
+
[
|
|
686
|
+
"gh",
|
|
687
|
+
"search",
|
|
688
|
+
"issues",
|
|
689
|
+
"--repo",
|
|
690
|
+
repo,
|
|
691
|
+
"--mentions",
|
|
692
|
+
github_account,
|
|
693
|
+
"--state",
|
|
694
|
+
"open",
|
|
695
|
+
"--include-prs",
|
|
696
|
+
"--json",
|
|
697
|
+
fields,
|
|
698
|
+
"--limit",
|
|
699
|
+
str(limit),
|
|
700
|
+
],
|
|
701
|
+
runner=runner,
|
|
702
|
+
)
|
|
703
|
+
for item in mentions:
|
|
704
|
+
raw = _issue_search_to_raw(item, "mention")
|
|
705
|
+
if raw.get("author_association") in {None, "", "UNKNOWN"}:
|
|
706
|
+
raw["actor_permission"] = _actor_permission(repo_config, raw.get("actor_login"), runner)
|
|
707
|
+
raw = _enrich_pull_request_metadata(raw, repo_config, runner)
|
|
708
|
+
if _raw_workstream_id(raw, repo) in known_workstreams:
|
|
709
|
+
raw_events.append(_enrich_known_workstream_context(raw, repo_config, runner))
|
|
710
|
+
continue
|
|
711
|
+
raw_events.append(_enrich_mention(raw, repo_config, runner))
|
|
712
|
+
|
|
713
|
+
notification_raws = list(notification_hints or [])
|
|
714
|
+
if include_notifications and notification_hints is None:
|
|
715
|
+
notification_buckets = collect_account_notifications([repo_config], runner=runner)
|
|
716
|
+
notification_raws = notification_buckets.get(repo, [])
|
|
717
|
+
|
|
718
|
+
for raw in notification_raws:
|
|
719
|
+
if raw.get("repo_full_name") != repo:
|
|
720
|
+
continue
|
|
721
|
+
raw, source_state = _source_metadata(raw, repo_config, runner)
|
|
722
|
+
if source_state and source_state.lower() != "open":
|
|
723
|
+
continue
|
|
724
|
+
raw_events.append(
|
|
725
|
+
_trusted_trigger_from_source(
|
|
726
|
+
raw,
|
|
727
|
+
repo_config,
|
|
728
|
+
runner,
|
|
729
|
+
known_workstreams=known_workstreams,
|
|
730
|
+
)
|
|
731
|
+
)
|
|
732
|
+
|
|
733
|
+
return raw_events
|
|
734
|
+
|
|
735
|
+
|
|
736
|
+
def build_repo_config(repo, github_account, trusted_actors=None):
|
|
737
|
+
return {
|
|
738
|
+
"full_name": repo,
|
|
739
|
+
"github_account": github_account,
|
|
740
|
+
"trusted_actors": list(trusted_actors or []),
|
|
741
|
+
}
|
|
742
|
+
|
|
743
|
+
|
|
744
|
+
def load_fixture(path):
|
|
745
|
+
payload = json.loads(Path(path).read_text(encoding="utf-8"))
|
|
746
|
+
if isinstance(payload, list):
|
|
747
|
+
return payload
|
|
748
|
+
return payload.get("events", [])
|
|
749
|
+
|
|
750
|
+
|
|
751
|
+
def main(argv=None):
|
|
752
|
+
parser = argparse.ArgumentParser()
|
|
753
|
+
parser.add_argument("--fixture")
|
|
754
|
+
parser.add_argument("--repo", required=True)
|
|
755
|
+
parser.add_argument("--dd-account", required=True)
|
|
756
|
+
parser.add_argument("--trusted-actor", action="append", default=[])
|
|
757
|
+
parser.add_argument("--skip-external", action="store_true")
|
|
758
|
+
args = parser.parse_args(argv)
|
|
759
|
+
|
|
760
|
+
repo_config = build_repo_config(args.repo, args.github_account, args.trusted_actor)
|
|
761
|
+
raw_events = []
|
|
762
|
+
if args.fixture:
|
|
763
|
+
raw_events = load_fixture(args.fixture)
|
|
764
|
+
elif not args.skip_external:
|
|
765
|
+
raw_events = collect_live_events(repo_config)
|
|
766
|
+
|
|
767
|
+
events = normalize_events(
|
|
768
|
+
raw_events,
|
|
769
|
+
repo_config,
|
|
770
|
+
)
|
|
771
|
+
return emit({"ok": True, "status": "normalized", "events": events})
|
|
772
|
+
|
|
773
|
+
|
|
774
|
+
if __name__ == "__main__":
|
|
775
|
+
raise SystemExit(main())
|