Skip to content

Commit

Permalink
Add --merged-by flag to pull-requests sub command
Browse files Browse the repository at this point in the history
Enable getting 'merge_by' information out of pull-requests without
having to specify individual PR numbers.
  • Loading branch information
sarcasticadmin committed Aug 20, 2021
1 parent ed37520 commit 118d8a9
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ You can use the `--pull-request` option one or more times to load specific pull

$ github-to-sqlite pull-requests github.db simonw/datasette --pull-request=81

Note that the `merged_by` column on the `pull_requests` table will only be populated for pull requests that are loaded using the `--pull-request` option - the GitHub API does not return this field for pull requests that are loaded in bulk.
Note that the `merged_by` column on the `pull_requests` table will only be populated for pull requests that are loaded using the `--pull-request` or `--merged-by` (for bulk) options - the GitHub API does not return this field for pull requests that are loaded in bulk natively.

Example: [pull_requests table](https://github-to-sqlite.dogsheep.net/github/pull_requests)

Expand Down
18 changes: 17 additions & 1 deletion github_to_sqlite/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,13 @@ def issues(db_path, repo, issue_ids, auth, 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",
)
def pull_requests(db_path, repo, pull_request_ids, auth, load):
@click.option(
"--merged-by",
help="Enable getting missing 'merged_by' attribute when requesting all PRs",
required=False,
is_flag=True
)
def pull_requests(db_path, repo, pull_request_ids, auth, load, merged_by):
"Save pull_requests for a specified repository, e.g. simonw/datasette"
db = sqlite_utils.Database(db_path)
token = load_token(auth)
Expand All @@ -114,6 +120,16 @@ def pull_requests(db_path, repo, pull_request_ids, auth, load):
pull_requests = json.load(open(load))
else:
pull_requests = utils.fetch_pull_requests(repo, token, pull_request_ids)
if merged_by and not pull_request_ids:
pull_requests = list(pull_requests)
pull_request_ids=[]
merged_ids = [item['number'] for item in pull_requests if item["merged_at"] is not None]
# fetch all merged prs by id to get missing 'merged_by' field
pull_requests_merged = list(utils.fetch_pull_requests(repo, token, merged_ids))

for m, m_item in enumerate(pull_requests_merged):
update=next(i for i, item in enumerate(pull_requests) if item["id"] == m_item['id'])
pull_requests[update]=pull_requests_merged[m]

pull_requests = list(pull_requests)
utils.save_pull_requests(db, pull_requests, repo_full)
Expand Down

0 comments on commit 118d8a9

Please sign in to comment.