Skip to content

Commit a92a31d

Browse files
Add --merged-by flag to pull-requests sub command
Enable getting 'merge_by' information out of pull-requests without having to specify individual PR numbers.
1 parent ed37520 commit a92a31d

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ You can use the `--pull-request` option one or more times to load specific pull
8080

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

83-
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.
83+
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.
8484

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

github_to_sqlite/cli.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,13 @@ def issues(db_path, repo, issue_ids, auth, load):
104104
type=click.Path(file_okay=True, dir_okay=False, allow_dash=True, exists=True),
105105
help="Load pull-requests JSON from this file instead of the API",
106106
)
107-
def pull_requests(db_path, repo, pull_request_ids, auth, load):
107+
@click.option(
108+
"--merged-by",
109+
help="Enable getting missing 'merged_by' attribute when requesting all PRs",
110+
required=False,
111+
is_flag=True
112+
)
113+
def pull_requests(db_path, repo, pull_request_ids, auth, load, merged_by):
108114
"Save pull_requests for a specified repository, e.g. simonw/datasette"
109115
db = sqlite_utils.Database(db_path)
110116
token = load_token(auth)
@@ -114,6 +120,15 @@ def pull_requests(db_path, repo, pull_request_ids, auth, load):
114120
pull_requests = json.load(open(load))
115121
else:
116122
pull_requests = utils.fetch_pull_requests(repo, token, pull_request_ids)
123+
if merged_by and not pull_request_ids:
124+
pull_requests = list(pull_requests)
125+
merged_ids = [item['number'] for item in pull_requests if item["merged_at"] is not None]
126+
# fetch all merged prs by id to get missing 'merged_by' field
127+
pull_requests_merged = list(utils.fetch_pull_requests(repo, token, merged_ids))
128+
129+
for m, m_item in enumerate(pull_requests_merged):
130+
update=next(i for i, item in enumerate(pull_requests) if item["id"] == m_item['id'])
131+
pull_requests[update]=pull_requests_merged[m]
117132

118133
pull_requests = list(pull_requests)
119134
utils.save_pull_requests(db, pull_requests, repo_full)

0 commit comments

Comments
 (0)