-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathgithub_preprocess.py
More file actions
419 lines (338 loc) · 13.3 KB
/
github_preprocess.py
File metadata and controls
419 lines (338 loc) · 13.3 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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
"""
Shared Pinecone preprocessing helpers for GitHub issues and PRs.
Reads raw JSON files from workspace/raw/github_activity_tracker/<owner>/<repo>/
and produces document dicts for cppa_pinecone_sync.sync.sync_to_pinecone.
Public API:
Single-repo (used by clang_github_tracker):
preprocess_issues(owner, repo, failed_ids, final_sync_at)
preprocess_prs(owner, repo, failed_ids, final_sync_at)
Multi-repo (used by boost_library_tracker):
preprocess_all_issues(owner, failed_ids, final_sync_at)
preprocess_all_prs(owner, failed_ids, final_sync_at)
Document dict shape (per docs/Pinecone_preprocess_guideline.md):
{
"content": <markdown string>,
"metadata": {
"doc_id": <html_url>,
"source_ids": "<repo>:issue:<number>" or "<repo>:pr:<number>",
"type": "issue" | "pr",
"number": <int>,
"title": <str>,
"url": <str>,
"author": <str>,
"state": <str>,
"created_at": <float timestamp>,
"updated_at": <float timestamp>,
"closed_at": <float timestamp>,
"repo_name": <str>,
},
}
"""
from __future__ import annotations
import json
import logging
from datetime import datetime, timezone
from pathlib import Path
from typing import Any, Generator, Literal
from core.operations.md_ops.issue_to_md import issue_json_to_md
from core.operations.md_ops.pr_to_md import pr_json_to_md
from github_activity_tracker.workspace import (
get_raw_source_issues_dir,
get_raw_source_prs_dir,
get_raw_source_root,
)
logger = logging.getLogger(__name__)
# ---------------------------------------------------------------------------
# Internal utilities
# ---------------------------------------------------------------------------
def _parse_updated_at(info: dict[str, Any]) -> datetime | None:
"""Parse updated_at ISO string from issue_info or pr_info. Returns UTC datetime or None."""
raw = info.get("updated_at") or info.get("created_at")
if not raw:
return None
try:
dt = datetime.fromisoformat(raw.replace("Z", "+00:00"))
if dt.tzinfo is None:
dt = dt.replace(tzinfo=timezone.utc)
return dt.astimezone(timezone.utc)
except (ValueError, AttributeError):
return None
def _to_timestamp(iso_str: str | None) -> float:
"""Convert ISO datetime string to Unix timestamp float. Returns 0.0 if missing/invalid."""
if not iso_str:
return 0.0
try:
dt = datetime.fromisoformat(iso_str.replace("Z", "+00:00"))
return dt.timestamp()
except (ValueError, AttributeError):
return 0.0
def _file_modified_at(path: Path) -> datetime | None:
"""Return the file's modification time as UTC datetime, or None if not available."""
try:
mtime = path.stat().st_mtime
return datetime.fromtimestamp(mtime, tz=timezone.utc)
except OSError:
return None
def _iter_json_files(
directory: Path,
) -> Generator[tuple[Path, dict[str, Any]], None, None]:
"""Yield (path, parsed_data) for each *.json in directory. Skips unreadable files."""
if not directory.is_dir():
return
for path in sorted(directory.glob("*.json")):
try:
data = json.loads(path.read_text(encoding="utf-8", errors="replace"))
except (json.JSONDecodeError, OSError) as exc:
logger.debug("Skipping %s: %s", path, exc)
continue
if isinstance(data, dict):
yield path, data
def get_ids_for_pinecone(repo: str, type: Literal["issue", "pr"], number: int) -> str:
"""Get the ids for Pinecone from a repo, type, and number."""
return f"{repo}:{type}:{number}"
# ---------------------------------------------------------------------------
# Public iterators
# ---------------------------------------------------------------------------
def iter_raw_repos(owner: str) -> Generator[str, None, None]:
"""Yield repo names (subdirectory names) under workspace/raw/github_activity_tracker/<owner>/."""
owner_dir = get_raw_source_root() / owner
if not owner_dir.is_dir():
return
for entry in sorted(owner_dir.iterdir()):
if entry.is_dir():
yield entry.name
def iter_raw_issue_jsons(
owner: str, repo: str
) -> Generator[tuple[Path, dict[str, Any]], None, None]:
"""Yield (path, data) for each issue JSON under workspace/raw/.../issues/."""
yield from _iter_json_files(get_raw_source_issues_dir(owner, repo))
def iter_raw_pr_jsons(
owner: str, repo: str
) -> Generator[tuple[Path, dict[str, Any]], None, None]:
"""Yield (path, data) for each PR JSON under workspace/raw/.../prs/."""
yield from _iter_json_files(get_raw_source_prs_dir(owner, repo))
# ---------------------------------------------------------------------------
# Document builders
# ---------------------------------------------------------------------------
def build_issue_document(
path: Path,
data: dict[str, Any],
repo: str,
) -> dict[str, Any] | None:
"""Build a Pinecone document dict from a raw issue JSON file.
Returns None if issue_info is missing, html_url is absent, or content is empty.
"""
info = data.get("issue_info")
if not isinstance(info, dict):
logger.debug("Skipping %s: no issue_info", path.name)
return None
html_url = (info.get("html_url") or "").strip()
if not html_url:
logger.debug("Skipping %s: no html_url", path.name)
return None
content = issue_json_to_md(data).strip()
if not content:
logger.debug("Skipping %s: empty markdown content", path.name)
return None
number = info.get("number") or -1
return {
"content": content,
"metadata": {
"doc_id": html_url,
"source_ids": get_ids_for_pinecone(repo, "issue", number),
"type": "issue",
"number": number,
"title": (info.get("title") or "").strip(),
"url": html_url,
"author": (info.get("user") or {}).get("login", "") or "",
"state": (info.get("state") or "").lower(),
"state_reason": (info.get("state_reason") or "").lower(),
"created_at": _to_timestamp(info.get("created_at")),
"updated_at": _to_timestamp(info.get("updated_at")),
"closed_at": _to_timestamp(info.get("closed_at")),
"repo_name": repo,
},
}
def build_pr_document(
path: Path,
data: dict[str, Any],
repo: str,
) -> dict[str, Any] | None:
"""Build a Pinecone document dict from a raw PR JSON file.
Returns None if pr_info is missing, html_url is absent, or content is empty.
"""
info = data.get("pr_info")
if not isinstance(info, dict):
logger.debug("Skipping %s: no pr_info", path.name)
return None
html_url = (info.get("html_url") or "").strip()
if not html_url:
logger.debug("Skipping %s: no html_url", path.name)
return None
content = pr_json_to_md(data).strip()
if not content:
logger.debug("Skipping %s: empty markdown content", path.name)
return None
number = info.get("number") or -1
return {
"content": content,
"metadata": {
"doc_id": html_url,
"source_ids": get_ids_for_pinecone(repo, "pr", number),
"type": "pr",
"number": number,
"title": (info.get("title") or "").strip(),
"url": html_url,
"author": (info.get("user") or {}).get("login", "") or "",
"state": (info.get("state") or "").lower(),
"created_at": _to_timestamp(info.get("created_at")),
"updated_at": _to_timestamp(info.get("updated_at")),
"closed_at": _to_timestamp(info.get("closed_at")),
"repo_name": repo,
},
}
# ---------------------------------------------------------------------------
# Single-repo preprocessors (used by clang_github_tracker)
# ---------------------------------------------------------------------------
def preprocess_issues(
owner: str,
repo: str,
failed_ids: list[str],
final_sync_at: datetime | None,
) -> tuple[list[dict[str, Any]], bool]:
"""Build Pinecone documents for issues from one raw repo directory.
Selects issues where:
- updated_at > final_sync_at (incremental), OR
- ids value is in failed_ids (retry)
Args:
owner: GitHub owner (e.g. "llvm").
repo: Repository name (e.g. "llvm-project").
failed_ids: Previously failed ids strings for retry.
final_sync_at: Last successful sync timestamp; None means first run (sync all).
Returns:
(documents, is_chunked=False)
"""
failed_set = set(failed_ids or [])
documents: list[dict[str, Any]] = []
seen: set[str] = set()
for path, data in iter_raw_issue_jsons(owner, repo):
info = data.get("issue_info") or {}
number = info.get("number") or -1
ids_val = get_ids_for_pinecone(repo, "issue", number)
is_failed = ids_val in failed_set
updated_at = _parse_updated_at(info)
file_modified_at = _file_modified_at(path)
is_new = final_sync_at is None or (
(updated_at is not None and updated_at > final_sync_at)
or (file_modified_at is not None and file_modified_at > final_sync_at)
)
if not is_failed and not is_new:
continue
if ids_val in seen:
continue
seen.add(ids_val)
doc = build_issue_document(path, data, repo)
if doc is not None:
documents.append(doc)
logger.info(
"preprocess_issues: owner=%s repo=%s → %d documents",
owner,
repo,
len(documents),
)
return documents, False
def preprocess_prs(
owner: str,
repo: str,
failed_ids: list[str],
final_sync_at: datetime | None,
) -> tuple[list[dict[str, Any]], bool]:
"""Build Pinecone documents for PRs from one raw repo directory.
Selects PRs where:
- updated_at > final_sync_at (incremental), OR
- ids value is in failed_ids (retry)
Args:
owner: GitHub owner (e.g. "llvm").
repo: Repository name (e.g. "llvm-project").
failed_ids: Previously failed ids strings for retry.
final_sync_at: Last successful sync timestamp; None means first run (sync all).
Returns:
(documents, is_chunked=False)
"""
failed_set = set(failed_ids or [])
documents: list[dict[str, Any]] = []
seen: set[str] = set()
for path, data in iter_raw_pr_jsons(owner, repo):
info = data.get("pr_info") or {}
number = info.get("number") or -1
ids_val = get_ids_for_pinecone(repo, "pr", number)
is_failed = ids_val in failed_set
updated_at = _parse_updated_at(info)
file_modified_at = _file_modified_at(path)
is_new = final_sync_at is None or (
(updated_at is not None and updated_at > final_sync_at)
or (file_modified_at is not None and file_modified_at > final_sync_at)
)
if not is_failed and not is_new:
continue
if ids_val in seen:
continue
seen.add(ids_val)
doc = build_pr_document(path, data, repo)
if doc is not None:
documents.append(doc)
logger.info(
"preprocess_prs: owner=%s repo=%s → %d documents", owner, repo, len(documents)
)
return documents, False
# ---------------------------------------------------------------------------
# Multi-repo preprocessors (used by boost_library_tracker)
# ---------------------------------------------------------------------------
def preprocess_all_issues(
owner: str,
failed_ids: list[str],
final_sync_at: datetime | None,
) -> tuple[list[dict[str, Any]], bool]:
"""Build Pinecone documents for issues across all repos under owner.
Iterates all subdirectories of workspace/raw/github_activity_tracker/<owner>/
and calls preprocess_issues for each repo, merging results.
Args:
owner: GitHub owner (e.g. "boostorg").
failed_ids: Previously failed ids strings for retry.
final_sync_at: Last successful sync timestamp; None means first run.
Returns:
(documents, is_chunked=False)
"""
all_documents: list[dict[str, Any]] = []
for repo in iter_raw_repos(owner):
docs, _ = preprocess_issues(owner, repo, failed_ids, final_sync_at)
all_documents.extend(docs)
logger.info(
"preprocess_all_issues: owner=%s → %d total documents",
owner,
len(all_documents),
)
return all_documents, False
def preprocess_all_prs(
owner: str,
failed_ids: list[str],
final_sync_at: datetime | None,
) -> tuple[list[dict[str, Any]], bool]:
"""Build Pinecone documents for PRs across all repos under owner.
Iterates all subdirectories of workspace/raw/github_activity_tracker/<owner>/
and calls preprocess_prs for each repo, merging results.
Args:
owner: GitHub owner (e.g. "boostorg").
failed_ids: Previously failed ids strings for retry.
final_sync_at: Last successful sync timestamp; None means first run.
Returns:
(documents, is_chunked=False)
"""
all_documents: list[dict[str, Any]] = []
for repo in iter_raw_repos(owner):
docs, _ = preprocess_prs(owner, repo, failed_ids, final_sync_at)
all_documents.extend(docs)
logger.info(
"preprocess_all_prs: owner=%s → %d total documents", owner, len(all_documents)
)
return all_documents, False