Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --merged-by flag to pull-requests sub command #66

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
17 changes: 16 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,15 @@ 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)
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