forked from dogsheep/github-to-sqlite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.py
648 lines (601 loc) · 18.9 KB
/
cli.py
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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
import click
import datetime
import itertools
import pathlib
import textwrap
import os
import sqlite_utils
import time
import json
from github_to_sqlite import utils
@click.group()
@click.version_option()
def cli():
"Save data from GitHub to a SQLite database"
@cli.command()
@click.option(
"-a",
"--auth",
type=click.Path(file_okay=True, dir_okay=False, allow_dash=False),
default="auth.json",
help="Path to save tokens to, defaults to auth.json",
)
def auth(auth):
"Save authentication credentials to a JSON file"
click.echo("Create a GitHub personal user token and paste it here:")
click.echo()
personal_token = click.prompt("Personal token")
if pathlib.Path(auth).exists():
auth_data = json.load(open(auth))
else:
auth_data = {}
auth_data["github_personal_token"] = personal_token
open(auth, "w").write(json.dumps(auth_data, indent=4) + "\n")
@cli.command()
@click.argument(
"db_path",
type=click.Path(file_okay=True, dir_okay=False, allow_dash=False),
required=True,
)
@click.argument("repo")
@click.option(
"--issue",
"issue_ids",
help="Just pull these issue numbers",
type=int,
multiple=True,
)
@click.option(
"-a",
"--auth",
type=click.Path(file_okay=True, dir_okay=False, allow_dash=True),
default="auth.json",
help="Path to auth.json token file",
)
@click.option(
"--load",
type=click.Path(file_okay=True, dir_okay=False, allow_dash=True, exists=True),
help="Load issues JSON from this file instead of the API",
)
def issues(db_path, repo, issue_ids, auth, load):
"Save issues for a specified repository, e.g. simonw/datasette"
db = sqlite_utils.Database(db_path)
token = load_token(auth)
repo_full = utils.fetch_repo(repo, token)
utils.save_repo(db, repo_full)
if load:
issues = json.load(open(load))
else:
issues = utils.fetch_issues(repo, token, issue_ids)
issues = list(issues)
utils.save_issues(db, issues, repo_full)
utils.ensure_db_shape(db)
@cli.command(name="pull-requests")
@click.argument(
"db_path",
type=click.Path(file_okay=True, dir_okay=False, allow_dash=False),
required=True,
)
@click.argument("repo", required=False)
@click.option(
"--pull-request",
"pull_request_ids",
help="Just pull these pull-request numbers",
type=int,
multiple=True,
)
@click.option(
"-a",
"--auth",
type=click.Path(file_okay=True, dir_okay=False, allow_dash=True),
default="auth.json",
help="Path to auth.json token file",
)
@click.option(
"--load",
type=click.Path(file_okay=True, dir_okay=False, allow_dash=True, exists=True),
help="Load pull-requests JSON from this file instead of the API",
)
@click.option(
"--org",
"orgs",
help="Fetch all pull requests from this GitHub organization",
multiple=True,
)
@click.option(
"--state",
help="Only fetch pull requests in this state",
)
@click.option(
"--search",
help="Find pull requests with a search query",
)
def pull_requests(db_path, repo, pull_request_ids, auth, load, orgs, state, search):
"Save pull_requests for a specified repository, e.g. simonw/datasette"
db = sqlite_utils.Database(db_path)
token = load_token(auth)
if load:
repo_full = utils.fetch_repo(repo, token)
utils.save_repo(db, repo_full)
pull_requests = json.load(open(load))
utils.save_pull_requests(db, pull_requests, repo_full)
elif search:
repos_seen = set()
search += " is:pr"
pull_requests = utils.fetch_searched_pulls_or_issues(search, token)
for pull_request in pull_requests:
pr_repo_url = pull_request["repository_url"]
if pr_repo_url not in repos_seen:
pr_repo = utils.fetch_repo(url=pr_repo_url)
utils.save_repo(db, pr_repo)
repos_seen.add(pr_repo_url)
utils.save_pull_requests(db, [pull_request], pr_repo)
else:
if orgs:
repos = itertools.chain.from_iterable(
utils.fetch_all_repos(token=token, org=org)
for org in orgs
)
else:
repos = [utils.fetch_repo(repo, token)]
for repo_full in repos:
utils.save_repo(db, repo_full)
repo = repo_full["full_name"]
pull_requests = utils.fetch_pull_requests(repo, state, token, pull_request_ids)
utils.save_pull_requests(db, pull_requests, repo_full)
utils.ensure_db_shape(db)
@cli.command(name="issue-comments")
@click.argument(
"db_path",
type=click.Path(file_okay=True, dir_okay=False, allow_dash=False),
required=True,
)
@click.argument("repo")
@click.option("--issue", help="Just pull comments for this issue")
@click.option(
"-a",
"--auth",
type=click.Path(file_okay=True, dir_okay=False, allow_dash=True),
default="auth.json",
help="Path to auth.json token file",
)
def issue_comments(db_path, repo, issue, auth):
"Retrieve issue comments for a specific repository"
db = sqlite_utils.Database(db_path)
token = load_token(auth)
for comment in utils.fetch_issue_comments(repo, token, issue):
utils.save_issue_comment(db, comment)
utils.ensure_db_shape(db)
@cli.command()
@click.argument(
"db_path",
type=click.Path(file_okay=True, dir_okay=False, allow_dash=False),
required=True,
)
@click.argument("username", type=str, required=False)
@click.option(
"-a",
"--auth",
type=click.Path(file_okay=True, dir_okay=False, allow_dash=True),
default="auth.json",
help="Path to auth.json token file",
)
@click.option(
"--load",
type=click.Path(file_okay=True, dir_okay=False, allow_dash=True, exists=True),
help="Load issues JSON from this file instead of the API",
)
def starred(db_path, username, auth, load):
"Save repos starred by the specified (or authenticated) username"
db = sqlite_utils.Database(db_path)
token = load_token(auth)
if load:
stars = json.load(open(load))
else:
stars = utils.fetch_all_starred(username, token)
# Which user are we talking about here?
if username:
user = utils.fetch_user(username, token)
else:
user = utils.fetch_user(token=token)
utils.save_stars(db, user, stars)
utils.ensure_db_shape(db)
@cli.command()
@click.argument(
"db_path",
type=click.Path(file_okay=True, dir_okay=False, allow_dash=False),
required=True,
)
@click.argument("repos", type=str, nargs=-1)
@click.option(
"-a",
"--auth",
type=click.Path(file_okay=True, dir_okay=False, allow_dash=True),
default="auth.json",
help="Path to auth.json token file",
)
def stargazers(db_path, repos, auth):
"Fetch the users that have starred the specified repositories"
db = sqlite_utils.Database(db_path)
token = load_token(auth)
for repo in repos:
full_repo = utils.fetch_repo(repo, token=token)
repo_id = utils.save_repo(db, full_repo)
stargazers = utils.fetch_stargazers(repo, token)
utils.save_stargazers(db, repo_id, stargazers)
utils.ensure_db_shape(db)
@cli.command()
@click.argument(
"db_path",
type=click.Path(file_okay=True, dir_okay=False, allow_dash=False),
required=True,
)
@click.argument("usernames", type=str, nargs=-1)
@click.option(
"-a",
"--auth",
type=click.Path(file_okay=True, dir_okay=False, allow_dash=True),
default="auth.json",
help="Path to auth.json token file",
)
@click.option(
"-r",
"--repo",
multiple=True,
help="Just fetch these repos",
)
@click.option(
"--load",
type=click.Path(file_okay=True, dir_okay=False, allow_dash=True, exists=True),
help="Load repos JSON from this file instead of the API",
)
@click.option(
"--readme",
is_flag=True,
help="Fetch README into 'readme' column",
)
@click.option(
"--readme-html",
is_flag=True,
help="Fetch HTML rendered README into 'readme_html' column",
)
def repos(db_path, usernames, auth, repo, load, readme, readme_html):
"Save repos owned by the specified (or authenticated) username or organization"
db = sqlite_utils.Database(db_path)
token = load_token(auth)
if load:
for loaded_repo in json.load(open(load)):
utils.save_repo(db, loaded_repo)
else:
if repo:
# Just these repos
for full_name in repo:
repo_id = utils.save_repo(db, utils.fetch_repo(full_name, token))
_repo_readme(db, token, repo_id, full_name, readme, readme_html)
else:
if not usernames:
usernames = [None]
for username in usernames:
for repo in utils.fetch_all_repos(username, token):
repo_id = utils.save_repo(db, repo)
_repo_readme(
db, token, repo_id, repo["full_name"], readme, readme_html
)
utils.ensure_db_shape(db)
def _repo_readme(db, token, repo_id, full_name, readme, readme_html):
if readme:
readme = utils.fetch_readme(token, full_name)
db["repos"].update(repo_id, {"readme": readme}, alter=True)
if readme_html:
readme_html = utils.fetch_readme(token, full_name, html=True)
db["repos"].update(repo_id, {"readme_html": readme_html}, alter=True)
@cli.command()
@click.argument(
"db_path",
type=click.Path(file_okay=True, dir_okay=False, allow_dash=False),
required=True,
)
@click.argument("repos", type=str, nargs=-1)
@click.option(
"-a",
"--auth",
type=click.Path(file_okay=True, dir_okay=False, allow_dash=True),
default="auth.json",
help="Path to auth.json token file",
)
def releases(db_path, repos, auth):
"Save releases for the specified repos"
db = sqlite_utils.Database(db_path)
token = load_token(auth)
first = True
for repo in repos:
if not first:
time.sleep(1)
first = False
repo_full = utils.fetch_repo(repo, token)
utils.save_repo(db, repo_full)
releases = utils.fetch_releases(repo, token)
utils.save_releases(db, releases, repo_full["id"])
utils.ensure_db_shape(db)
@cli.command()
@click.argument(
"db_path",
type=click.Path(file_okay=True, dir_okay=False, allow_dash=False),
required=True,
)
@click.argument("repos", type=str, nargs=-1)
@click.option(
"-a",
"--auth",
type=click.Path(file_okay=True, dir_okay=False, allow_dash=True),
default="auth.json",
help="Path to auth.json token file",
)
def tags(db_path, repos, auth):
"Save tags for the specified repos"
db = sqlite_utils.Database(db_path)
token = load_token(auth)
first = True
for repo in repos:
if not first:
time.sleep(1)
first = False
repo_full = utils.fetch_repo(repo, token)
utils.save_repo(db, repo_full)
tags = utils.fetch_tags(repo, token)
utils.save_tags(db, tags, repo_full["id"])
utils.ensure_db_shape(db)
@cli.command()
@click.argument(
"db_path",
type=click.Path(file_okay=True, dir_okay=False, allow_dash=False),
required=True,
)
@click.argument("repos", type=str, nargs=-1)
@click.option(
"-a",
"--auth",
type=click.Path(file_okay=True, dir_okay=False, allow_dash=True),
default="auth.json",
help="Path to auth.json token file",
)
def contributors(db_path, repos, auth):
"Save contributors for the specified repos"
db = sqlite_utils.Database(db_path)
token = load_token(auth)
for repo in repos:
repo_full = utils.fetch_repo(repo, token)
utils.save_repo(db, repo_full)
contributors = utils.fetch_contributors(repo, token)
utils.save_contributors(db, contributors, repo_full["id"])
time.sleep(1)
utils.ensure_db_shape(db)
@cli.command()
@click.argument(
"db_path",
type=click.Path(file_okay=True, dir_okay=False, allow_dash=False),
required=True,
)
@click.argument("repos", type=str, nargs=-1)
@click.option(
"--all",
is_flag=True,
default=False,
help="Load all commits (not just those that have not yet been saved)",
)
@click.option(
"-a",
"--auth",
type=click.Path(file_okay=True, dir_okay=False, allow_dash=True),
default="auth.json",
help="Path to auth.json token file",
)
def commits(db_path, repos, all, auth):
"Save commits for the specified repos"
db = sqlite_utils.Database(db_path)
token = load_token(auth)
def stop_when(commit):
try:
db["commits"].get(commit["sha"])
return True
except sqlite_utils.db.NotFoundError:
return False
if all:
stop_when = None
for repo in repos:
repo_full = utils.fetch_repo(repo, token)
utils.save_repo(db, repo_full)
commits = utils.fetch_commits(repo, token, stop_when)
utils.save_commits(db, commits, repo_full["id"])
time.sleep(1)
utils.ensure_db_shape(db)
@cli.command(name="scrape-dependents")
@click.argument(
"db_path",
type=click.Path(file_okay=True, dir_okay=False, allow_dash=False),
required=True,
)
@click.argument("repos", type=str, nargs=-1)
@click.option(
"-a",
"--auth",
type=click.Path(file_okay=True, dir_okay=False, allow_dash=True),
default="auth.json",
help="Path to auth.json token file",
)
@click.option(
"-v",
"--verbose",
is_flag=True,
help="Verbose output",
)
def scrape_dependents(db_path, repos, auth, verbose):
"Scrape dependents for specified repos"
try:
import bs4
except ImportError:
raise click.ClickException("Optional dependency bs4 is needed for this command")
db = sqlite_utils.Database(db_path)
token = load_token(auth)
for repo in repos:
repo_full = utils.fetch_repo(repo, token)
utils.save_repo(db, repo_full)
for dependent_repo in utils.scrape_dependents(repo, verbose):
# Don't fetch repo details if it's already in our DB
existing = list(db["repos"].rows_where("full_name = ?", [dependent_repo]))
dependent_id = None
if not existing:
dependent_full = utils.fetch_repo(dependent_repo, token)
time.sleep(1)
utils.save_repo(db, dependent_full)
dependent_id = dependent_full["id"]
else:
dependent_id = existing[0]["id"]
# Only insert if it isn't already there:
if not db["dependents"].exists() or not list(
db["dependents"].rows_where(
"repo = ? and dependent = ?", [repo_full["id"], dependent_id]
)
):
db["dependents"].insert(
{
"repo": repo_full["id"],
"dependent": dependent_id,
"first_seen_utc": datetime.datetime.utcnow().isoformat(),
},
pk=("repo", "dependent"),
foreign_keys=(
("repo", "repos", "id"),
("dependent", "repos", "id"),
),
)
utils.ensure_db_shape(db)
@cli.command()
@click.argument(
"db_path",
type=click.Path(file_okay=True, dir_okay=False, allow_dash=False),
required=True,
)
@click.option(
"-a",
"--auth",
type=click.Path(file_okay=True, dir_okay=False, allow_dash=True),
default="auth.json",
help="Path to auth.json token file",
)
@click.option(
"-f",
"--fetch",
is_flag=True,
help="Fetch the image data into a BLOB column",
)
def emojis(db_path, auth, fetch):
"Fetch GitHub supported emojis"
db = sqlite_utils.Database(db_path)
token = load_token(auth)
table = db.table("emojis", pk="name")
table.upsert_all(utils.fetch_emojis(token))
if fetch:
# Ensure table has 'image' column
if "image" not in table.columns_dict:
table.add_column("image", bytes)
with click.progressbar(
list(table.rows_where("image is null")),
show_pos=True,
show_eta=True,
show_percent=True,
) as bar:
for emoji in bar:
table.update(emoji["name"], {"image": utils.fetch_image(emoji["url"])})
@cli.command()
@click.argument("url", type=str)
@click.option(
"-a",
"--auth",
type=click.Path(file_okay=True, dir_okay=False, allow_dash=True),
default="auth.json",
help="Path to auth.json token file",
)
@click.option(
"--paginate",
is_flag=True,
help="Paginate through all results",
)
@click.option(
"--nl",
is_flag=True,
help="Output newline-delimited JSON",
)
@click.option(
"--accept",
help="Accept header to send, e.g. application/vnd.github.VERSION.html",
)
def get(url, auth, paginate, nl, accept):
"Make an authenticated HTTP GET against the specified URL"
token = load_token(auth)
first = True
should_output_closing_brace = not nl
while url:
response = utils.get(url, token, accept=accept)
if "html" in (response.headers.get("content-type") or ""):
click.echo(response.text)
return
items = response.json()
if isinstance(items, dict):
if nl:
click.echo(json.dumps(items))
else:
click.echo(json.dumps(items, indent=4))
should_output_closing_brace = False
break
if first and not nl:
click.echo("[")
for item in items:
if not first and not nl:
click.echo(",")
first = False
if not nl:
to_dump = json.dumps(item, indent=4)
click.echo(textwrap.indent(to_dump, " "), nl=False)
else:
click.echo(json.dumps(item))
if paginate:
url = response.links.get("next", {}).get("url")
else:
url = None
if should_output_closing_brace:
click.echo("\n]")
@cli.command()
@click.argument(
"db_path",
type=click.Path(file_okay=True, dir_okay=False, allow_dash=False),
required=True,
)
@click.argument("repos", type=str, nargs=-1)
@click.option(
"-a",
"--auth",
type=click.Path(file_okay=True, dir_okay=False, allow_dash=True),
default="auth.json",
help="Path to auth.json token file",
)
def workflows(db_path, repos, auth):
"Fetch details of GitHub Actions workflows for the specified repositories"
db = sqlite_utils.Database(db_path)
token = load_token(auth)
for repo in repos:
full_repo = utils.fetch_repo(repo, token=token)
repo_id = utils.save_repo(db, full_repo)
workflows = utils.fetch_workflows(token, full_repo["full_name"])
for filename, content in workflows.items():
utils.save_workflow(db, repo_id, filename, content)
utils.ensure_db_shape(db)
def load_token(auth):
try:
token = json.load(open(auth))["github_personal_token"]
except (KeyError, FileNotFoundError):
token = None
if token is None:
# Fallback to GITHUB_TOKEN environment variable
token = os.environ.get("GITHUB_TOKEN") or None
return token