Skip to content

Commit

Permalink
merged_by as foreign key to users, if available
Browse files Browse the repository at this point in the history
  • Loading branch information
simonw committed Nov 30, 2020
1 parent 22a0164 commit cae005f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
5 changes: 5 additions & 0 deletions github_to_sqlite/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,9 @@ def save_pull_requests(db, pull_requests, repo):
# Extract user
pull_request["user"] = save_user(db, pull_request["user"])
labels = pull_request.pop("labels")
# Extract merged_by, if it exists
if pull_request.get("merged_by"):
pull_request["merged_by"] = save_user(db, pull_request["merged_by"])
# Head sha
pull_request["head"] = pull_request["head"]["sha"]
pull_request["base"] = pull_request["base"]["sha"]
Expand All @@ -196,6 +199,7 @@ def save_pull_requests(db, pull_requests, repo):
pk="id",
foreign_keys=[
("user", "users", "id"),
("merged_by", "users", "id"),
("assignee", "users", "id"),
("milestone", "milestones", "id"),
("repo", "repos", "id"),
Expand All @@ -209,6 +213,7 @@ def save_pull_requests(db, pull_requests, repo):
"repo": int,
"title": str,
"body": str,
"merged_by": int,
},
)
# m2m for labels
Expand Down
32 changes: 22 additions & 10 deletions tests/test_pull_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,11 @@ def db(pull_requests):

def test_tables(db):
assert {"pull_requests", "users", "repos", "milestones"} == set(db.table_names())
assert {
ForeignKey(
table="pull_requests", column="repo", other_table="repos", other_column="id"
),
assert set(db["pull_requests"].foreign_keys) == {
ForeignKey(
table="pull_requests",
column="milestone",
other_table="milestones",
column="merged_by",
other_table="users",
other_column="id",
),
ForeignKey(
Expand All @@ -41,10 +38,19 @@ def test_tables(db):
other_table="users",
other_column="id",
),
ForeignKey(
table="pull_requests",
column="milestone",
other_table="milestones",
other_column="id",
),
ForeignKey(
table="pull_requests", column="repo", other_table="repos", other_column="id"
),
ForeignKey(
table="pull_requests", column="user", other_table="users", other_column="id"
),
} == set(db["pull_requests"].foreign_keys)
}


def test_pull_requests(db):
Expand Down Expand Up @@ -74,7 +80,7 @@ def test_pull_requests(db):
"mergeable": None,
"rebaseable": None,
"mergeable_state": "unknown",
"merged_by": '{"login": "simonw", "id": 9599, "node_id": "MDQ6VXNlcjk1OTk=", "avatar_url": "https://avatars0.githubusercontent.com/u/9599?v=4", "gravatar_id": "", "url": "https://api.github.com/users/simonw", "html_url": "https://github.com/simonw", "followers_url": "https://api.github.com/users/simonw/followers", "following_url": "https://api.github.com/users/simonw/following{/other_user}", "gists_url": "https://api.github.com/users/simonw/gists{/gist_id}", "starred_url": "https://api.github.com/users/simonw/starred{/owner}{/repo}", "subscriptions_url": "https://api.github.com/users/simonw/subscriptions", "organizations_url": "https://api.github.com/users/simonw/orgs", "repos_url": "https://api.github.com/users/simonw/repos", "events_url": "https://api.github.com/users/simonw/events{/privacy}", "received_events_url": "https://api.github.com/users/simonw/received_events", "type": "User", "site_admin": false}',
"merged_by": 9599,
"comments": 0,
"review_comments": 0,
"maintainer_can_modify": 0,
Expand Down Expand Up @@ -106,10 +112,16 @@ def test_users(db):


def test_foreign_keys(db):
assert [
assert db["pull_requests"].foreign_keys == [
ForeignKey(
table="pull_requests", column="repo", other_table="repos", other_column="id"
),
ForeignKey(
table="pull_requests",
column="merged_by",
other_table="users",
other_column="id",
),
ForeignKey(
table="pull_requests",
column="milestone",
Expand All @@ -125,4 +137,4 @@ def test_foreign_keys(db):
ForeignKey(
table="pull_requests", column="user", other_table="users", other_column="id"
),
] == db["pull_requests"].foreign_keys
]

0 comments on commit cae005f

Please sign in to comment.