diff --git a/README.md b/README.md index d4c1f5b..65c3ce3 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/github_to_sqlite/cli.py b/github_to_sqlite/cli.py index fbd3321..164830b 100644 --- a/github_to_sqlite/cli.py +++ b/github_to_sqlite/cli.py @@ -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) @@ -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)