File size: 11,503 Bytes
dbf7313
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from __future__ import annotations

import json
import os
import tempfile
from pathlib import Path
from typing import Any

import pyarrow as pa
import pyarrow.parquet as pq

SCHEMAS: dict[str, pa.Schema] = {
    "issues": pa.schema(
        [
            ("repo", pa.string()),
            ("github_id", pa.int64()),
            ("github_node_id", pa.string()),
            ("number", pa.int64()),
            ("html_url", pa.string()),
            ("api_url", pa.string()),
            ("title", pa.string()),
            ("body", pa.string()),
            ("state", pa.string()),
            ("state_reason", pa.string()),
            ("locked", pa.bool_()),
            ("comments_count", pa.int64()),
            ("labels", pa.list_(pa.string())),
            ("assignees", pa.list_(pa.string())),
            ("created_at", pa.string()),
            ("updated_at", pa.string()),
            ("closed_at", pa.string()),
            ("author_association", pa.string()),
            ("milestone_title", pa.string()),
            ("snapshot_id", pa.string()),
            ("extracted_at", pa.string()),
            ("author_login", pa.string()),
            ("author_id", pa.int64()),
            ("author_node_id", pa.string()),
            ("author_type", pa.string()),
            ("author_site_admin", pa.bool_()),
        ]
    ),
    "pull_requests": pa.schema(
        [
            ("repo", pa.string()),
            ("github_id", pa.int64()),
            ("github_node_id", pa.string()),
            ("number", pa.int64()),
            ("html_url", pa.string()),
            ("api_url", pa.string()),
            ("title", pa.string()),
            ("body", pa.string()),
            ("state", pa.string()),
            ("state_reason", pa.string()),
            ("locked", pa.bool_()),
            ("comments_count", pa.int64()),
            ("labels", pa.list_(pa.string())),
            ("assignees", pa.list_(pa.string())),
            ("created_at", pa.string()),
            ("updated_at", pa.string()),
            ("closed_at", pa.string()),
            ("author_association", pa.string()),
            ("merged_at", pa.string()),
            ("merge_commit_sha", pa.string()),
            ("merged", pa.bool_()),
            ("mergeable", pa.bool_()),
            ("mergeable_state", pa.string()),
            ("draft", pa.bool_()),
            ("additions", pa.int64()),
            ("deletions", pa.int64()),
            ("changed_files", pa.int64()),
            ("commits", pa.int64()),
            ("review_comments_count", pa.int64()),
            ("maintainer_can_modify", pa.bool_()),
            ("head_ref", pa.string()),
            ("head_sha", pa.string()),
            ("head_repo_full_name", pa.string()),
            ("base_ref", pa.string()),
            ("base_sha", pa.string()),
            ("base_repo_full_name", pa.string()),
            ("snapshot_id", pa.string()),
            ("extracted_at", pa.string()),
            ("author_login", pa.string()),
            ("author_id", pa.int64()),
            ("author_node_id", pa.string()),
            ("author_type", pa.string()),
            ("author_site_admin", pa.bool_()),
        ]
    ),
    "comments": pa.schema(
        [
            ("repo", pa.string()),
            ("github_id", pa.int64()),
            ("github_node_id", pa.string()),
            ("parent_kind", pa.string()),
            ("parent_number", pa.int64()),
            ("html_url", pa.string()),
            ("api_url", pa.string()),
            ("issue_api_url", pa.string()),
            ("body", pa.string()),
            ("created_at", pa.string()),
            ("updated_at", pa.string()),
            ("author_association", pa.string()),
            ("snapshot_id", pa.string()),
            ("extracted_at", pa.string()),
            ("author_login", pa.string()),
            ("author_id", pa.int64()),
            ("author_node_id", pa.string()),
            ("author_type", pa.string()),
            ("author_site_admin", pa.bool_()),
        ]
    ),
    "reviews": pa.schema(
        [
            ("repo", pa.string()),
            ("github_id", pa.int64()),
            ("github_node_id", pa.string()),
            ("pull_request_number", pa.int64()),
            ("html_url", pa.string()),
            ("api_url", pa.string()),
            ("body", pa.string()),
            ("state", pa.string()),
            ("submitted_at", pa.string()),
            ("commit_id", pa.string()),
            ("author_association", pa.string()),
            ("snapshot_id", pa.string()),
            ("extracted_at", pa.string()),
            ("author_login", pa.string()),
            ("author_id", pa.int64()),
            ("author_node_id", pa.string()),
            ("author_type", pa.string()),
            ("author_site_admin", pa.bool_()),
        ]
    ),
    "review_comments": pa.schema(
        [
            ("repo", pa.string()),
            ("github_id", pa.int64()),
            ("github_node_id", pa.string()),
            ("pull_request_number", pa.int64()),
            ("review_id", pa.int64()),
            ("html_url", pa.string()),
            ("api_url", pa.string()),
            ("pull_request_api_url", pa.string()),
            ("body", pa.string()),
            ("path", pa.string()),
            ("commit_id", pa.string()),
            ("original_commit_id", pa.string()),
            ("position", pa.int64()),
            ("original_position", pa.int64()),
            ("line", pa.int64()),
            ("start_line", pa.int64()),
            ("side", pa.string()),
            ("start_side", pa.string()),
            ("subject_type", pa.string()),
            ("created_at", pa.string()),
            ("updated_at", pa.string()),
            ("author_association", pa.string()),
            ("snapshot_id", pa.string()),
            ("extracted_at", pa.string()),
            ("author_login", pa.string()),
            ("author_id", pa.int64()),
            ("author_node_id", pa.string()),
            ("author_type", pa.string()),
            ("author_site_admin", pa.bool_()),
        ]
    ),
    "pr_files": pa.schema(
        [
            ("repo", pa.string()),
            ("pull_request_number", pa.int64()),
            ("sha", pa.string()),
            ("filename", pa.string()),
            ("status", pa.string()),
            ("additions", pa.int64()),
            ("deletions", pa.int64()),
            ("changes", pa.int64()),
            ("blob_url", pa.string()),
            ("raw_url", pa.string()),
            ("contents_url", pa.string()),
            ("previous_filename", pa.string()),
            ("patch", pa.string()),
            ("snapshot_id", pa.string()),
            ("extracted_at", pa.string()),
        ]
    ),
    "pr_diffs": pa.schema(
        [
            ("repo", pa.string()),
            ("pull_request_number", pa.int64()),
            ("html_url", pa.string()),
            ("api_url", pa.string()),
            ("diff", pa.string()),
            ("snapshot_id", pa.string()),
            ("extracted_at", pa.string()),
        ]
    ),
    "links": pa.schema(
        [
            ("repo", pa.string()),
            ("source_type", pa.string()),
            ("source_number", pa.int64()),
            ("source_github_id", pa.int64()),
            ("target_owner", pa.string()),
            ("target_repo", pa.string()),
            ("target_number", pa.int64()),
            ("link_type", pa.string()),
            ("link_origin", pa.string()),
            ("snapshot_id", pa.string()),
            ("extracted_at", pa.string()),
        ]
    ),
    "events": pa.schema(
        [
            ("repo", pa.string()),
            ("parent_kind", pa.string()),
            ("parent_number", pa.int64()),
            ("event", pa.string()),
            ("created_at", pa.string()),
            ("actor_login", pa.string()),
            ("source_issue_number", pa.int64()),
            ("source_issue_title", pa.string()),
            ("source_issue_url", pa.string()),
            ("commit_id", pa.string()),
            ("label_name", pa.string()),
            ("snapshot_id", pa.string()),
            ("extracted_at", pa.string()),
        ]
    ),
    "new_contributors": pa.schema(
        [
            ("repo", pa.string()),
            ("snapshot_id", pa.string()),
            ("report_generated_at", pa.string()),
            ("window_days", pa.int64()),
            ("author_login", pa.string()),
            ("name", pa.string()),
            ("profile_url", pa.string()),
            ("repo_pull_requests_url", pa.string()),
            ("repo_issues_url", pa.string()),
            ("repo_first_seen_at", pa.string()),
            ("repo_last_seen_at", pa.string()),
            ("repo_primary_artifact_count", pa.int64()),
            ("repo_artifact_count", pa.int64()),
            ("snapshot_issue_count", pa.int64()),
            ("snapshot_pr_count", pa.int64()),
            ("snapshot_comment_count", pa.int64()),
            ("snapshot_review_count", pa.int64()),
            ("snapshot_review_comment_count", pa.int64()),
            ("repo_association", pa.string()),
            ("new_to_repo", pa.bool_()),
            ("first_seen_in_snapshot", pa.bool_()),
            ("report_reason", pa.string()),
            ("account_age_days", pa.int64()),
            ("young_account", pa.bool_()),
            ("follow_through_score", pa.string()),
            ("breadth_score", pa.string()),
            ("automation_risk_signal", pa.string()),
            ("heuristic_note", pa.string()),
            ("public_orgs", pa.list_(pa.string())),
            ("visible_authored_pr_count", pa.int64()),
            ("merged_pr_count", pa.int64()),
            ("closed_unmerged_pr_count", pa.int64()),
            ("open_pr_count", pa.int64()),
            ("merged_pr_rate", pa.float64()),
            ("closed_unmerged_pr_rate", pa.float64()),
            ("still_open_pr_rate", pa.float64()),
            ("distinct_repos_with_authored_prs", pa.int64()),
            ("distinct_repos_with_open_prs", pa.int64()),
            ("fetch_error", pa.string()),
        ]
    ),
}


def _tmp_path(path: Path) -> Path:
    path.parent.mkdir(parents=True, exist_ok=True)
    fd, raw_path = tempfile.mkstemp(prefix=f".{path.name}.", suffix=".tmp", dir=path.parent)
    os.close(fd)
    return Path(raw_path)


def write_parquet(rows: list[dict[str, Any]], path: Path, table_name: str) -> None:
    schema = SCHEMAS[table_name]
    table = pa.Table.from_pylist(rows, schema=schema)
    tmp_path = _tmp_path(path)
    try:
        pq.write_table(table, tmp_path)
        tmp_path.replace(path)
    finally:
        if tmp_path.exists():
            tmp_path.unlink()


def write_json(data: Any, path: Path) -> None:
    tmp_path = _tmp_path(path)
    try:
        tmp_path.write_text(json.dumps(data, indent=2, sort_keys=True) + "\n", encoding="utf-8")
        tmp_path.replace(path)
    finally:
        if tmp_path.exists():
            tmp_path.unlink()


def read_json(path: Path) -> Any:
    return json.loads(path.read_text(encoding="utf-8"))


def write_text(content: str, path: Path) -> None:
    tmp_path = _tmp_path(path)
    try:
        tmp_path.write_text(content, encoding="utf-8")
        tmp_path.replace(path)
    finally:
        if tmp_path.exists():
            tmp_path.unlink()


def read_parquet_rows(path: Path) -> list[dict[str, Any]]:
    if not path.exists():
        return []
    return pq.read_table(path).to_pylist()