Skip to content

Commit 412ae1d

Browse files
committed
basic support for events
1 parent ed37520 commit 412ae1d

2 files changed

Lines changed: 69 additions & 2 deletions

File tree

github_to_sqlite/cli.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,31 @@ def contributors(db_path, repos, auth):
364364
utils.ensure_db_shape(db)
365365

366366

367+
@cli.command()
368+
@click.argument(
369+
"db_path",
370+
type=click.Path(file_okay=True, dir_okay=False, allow_dash=False),
371+
required=True,
372+
)
373+
@click.argument("namespaces", type=str, nargs=-1)
374+
@click.option(
375+
"-a",
376+
"--auth",
377+
type=click.Path(file_okay=True, dir_okay=False, allow_dash=True),
378+
default="auth.json",
379+
help="Path to auth.json token file",
380+
)
381+
def events(db_path, namespaces, auth):
382+
"Save events for the specified namespaces"
383+
db = sqlite_utils.Database(db_path)
384+
token = load_token(auth)
385+
for ns in namespaces:
386+
events = utils.fetch_events(ns, token)
387+
utils.save_events(db, events, ns)
388+
time.sleep(1)
389+
utils.ensure_db_shape(db)
390+
391+
367392
@cli.command()
368393
@click.argument(
369394
"db_path",

github_to_sqlite/utils.py

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -305,8 +305,14 @@ def save_repo(db, repo):
305305
for key, value in repo.items()
306306
if (key == "html_url") or not key.endswith("url")
307307
}
308-
to_save["owner"] = save_user(db, to_save["owner"])
309-
to_save["license"] = save_license(db, to_save["license"])
308+
if "owner" in to_save:
309+
to_save["owner"] = save_user(db, to_save["owner"])
310+
else:
311+
to_save["owner"] = None
312+
if "license" in to_save:
313+
to_save["license"] = save_license(db, to_save["license"])
314+
else:
315+
to_save["license"] = None
310316
if "organization" in to_save:
311317
to_save["organization"] = save_user(db, to_save["organization"])
312318
else:
@@ -396,6 +402,13 @@ def fetch_contributors(repo, token=None):
396402
yield from contributors
397403

398404

405+
def fetch_events(ns, token=None):
406+
headers = make_headers(token)
407+
url = "https://api.github.com/{}/events".format(ns)
408+
for events in paginate(url, headers):
409+
yield from events
410+
411+
399412
def fetch_tags(repo, token=None):
400413
headers = make_headers(token)
401414
url = "https://api.github.com/repos/{}/tags".format(repo)
@@ -564,6 +577,35 @@ def save_contributors(db, contributors, repo_id):
564577
)
565578

566579

580+
def save_events(db, events, ns):
581+
event_rows_to_add = []
582+
for event in events:
583+
user_id = save_user(db, event["actor"])
584+
repo_id = save_repo(db, event["repo"])
585+
created_at = event["created_at"]
586+
public = bool(event["public"])
587+
event_rows_to_add.append({
588+
"user_id": user_id,
589+
"repo_id": repo_id,
590+
"created_at": created_at,
591+
"public": public,
592+
"type": event["type"],
593+
"payload": event["payload"],
594+
})
595+
db["events"].insert_all(
596+
event_rows_to_add,
597+
pk=(
598+
"repo_id",
599+
"user_id",
600+
),
601+
foreign_keys=[
602+
("repo_id", "repos", "id"),
603+
("user_id", "users", "id")
604+
],
605+
replace=True,
606+
)
607+
608+
567609
def save_tags(db, tags, repo_id):
568610
if not db["tags"].exists():
569611
db["tags"].create(

0 commit comments

Comments
 (0)