Skip to content

Commit fa5aa9e

Browse files
committed
Support multiple --issue and --pull-request options
Refs dogsheep#48
1 parent dff0834 commit fa5aa9e

File tree

3 files changed

+47
-21
lines changed

3 files changed

+47
-21
lines changed

README.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -61,19 +61,19 @@ You can point to a different location of `auth.json` using `-a`:
6161

6262
$ github-to-sqlite issues github.db simonw/datasette -a /path/to/auth.json
6363

64-
You can use the `--issue` option to only load just one specific issue:
64+
You can use the `--issue` option one or more times to load specific issues:
6565

6666
$ github-to-sqlite issues github.db simonw/datasette --issue=1
6767

68-
## Fetching pull-requests for a repository
68+
## Fetching pull requests for a repository
6969

70-
While pull-requests are a type of issue, you will get more information on pull-requests by pulling them separately. For example, whether a pull-request has been merged and when.
70+
While pull requests are a type of issue, you will get more information on pull requests by pulling them separately. For example, whether a pull request has been merged and when.
7171

72-
Following the API of issues, the `pull-requests` command retrieves all of the pull-requests belonging to a specified repository.
72+
Following the API of issues, the `pull-requests` command retrieves all of the pull requests belonging to a specified repository.
7373

7474
$ github-to-sqlite pull-requests github.db simonw/datasette
7575

76-
You can use the `--pull-request` option to only load just one specific pull-request:
76+
You can use the `--pull-request` option one or more times to load specific pull request:
7777

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

github_to_sqlite/cli.py

+22-7
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,14 @@ def auth(auth):
4242
type=click.Path(file_okay=True, dir_okay=False, allow_dash=False),
4343
required=True,
4444
)
45-
@click.argument("repo", required=False)
46-
@click.option("--issue", help="Just pull this issue number")
45+
@click.argument("repo")
46+
@click.option(
47+
"--issue",
48+
"issue_ids",
49+
help="Just pull these issue numbers",
50+
type=int,
51+
multiple=True,
52+
)
4753
@click.option(
4854
"-a",
4955
"--auth",
@@ -56,28 +62,36 @@ def auth(auth):
5662
type=click.Path(file_okay=True, dir_okay=False, allow_dash=True, exists=True),
5763
help="Load issues JSON from this file instead of the API",
5864
)
59-
def issues(db_path, repo, issue, auth, load):
65+
def issues(db_path, repo, issue_ids, auth, load):
6066
"Save issues for a specified repository, e.g. simonw/datasette"
6167
db = sqlite_utils.Database(db_path)
6268
token = load_token(auth)
6369
repo_full = utils.fetch_repo(repo, token)
70+
utils.save_repo(db, repo_full)
6471
if load:
6572
issues = json.load(open(load))
6673
else:
67-
issues = utils.fetch_issues(repo, token, issue)
74+
issues = utils.fetch_issues(repo, token, issue_ids)
6875

6976
issues = list(issues)
7077
utils.save_issues(db, issues, repo_full)
7178
utils.ensure_db_shape(db)
7279

80+
7381
@cli.command(name="pull-requests")
7482
@click.argument(
7583
"db_path",
7684
type=click.Path(file_okay=True, dir_okay=False, allow_dash=False),
7785
required=True,
7886
)
7987
@click.argument("repo", required=False)
80-
@click.option("--pull-request", help="Just pull this pull-request number")
88+
@click.option(
89+
"--pull-request",
90+
"pull_request_ids",
91+
help="Just pull these pull-request numbers",
92+
type=int,
93+
multiple=True,
94+
)
8195
@click.option(
8296
"-a",
8397
"--auth",
@@ -90,15 +104,16 @@ def issues(db_path, repo, issue, auth, load):
90104
type=click.Path(file_okay=True, dir_okay=False, allow_dash=True, exists=True),
91105
help="Load pull-requests JSON from this file instead of the API",
92106
)
93-
def pull_requests(db_path, repo, pull_request, auth, load):
107+
def pull_requests(db_path, repo, pull_request_ids, auth, load):
94108
"Save pull_requests for a specified repository, e.g. simonw/datasette"
95109
db = sqlite_utils.Database(db_path)
96110
token = load_token(auth)
97111
repo_full = utils.fetch_repo(repo, token)
112+
utils.save_repo(db, repo_full)
98113
if load:
99114
pull_requests = json.load(open(load))
100115
else:
101-
pull_requests = utils.fetch_pull_requests(repo, token, pull_request)
116+
pull_requests = utils.fetch_pull_requests(repo, token, pull_request_ids)
102117

103118
pull_requests = list(pull_requests)
104119
utils.save_pull_requests(db, pull_requests, repo_full)

github_to_sqlite/utils.py

+20-9
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,9 @@ def save_pull_requests(db, pull_requests, repo):
178178
pull_request["base"] = pull_request["base"]["sha"]
179179
# Extract milestone
180180
if pull_request["milestone"]:
181-
pull_request["milestone"] = save_milestone(db, pull_request["milestone"], repo["id"])
181+
pull_request["milestone"] = save_milestone(
182+
db, pull_request["milestone"], repo["id"]
183+
)
182184
# For the moment we ignore the assignees=[] array but we DO turn assignee
183185
# singular into a foreign key reference
184186
pull_request.pop("assignees", None)
@@ -329,21 +331,30 @@ def save_license(db, license):
329331
return db["licenses"].insert(license, pk="key", replace=True).last_pk
330332

331333

332-
def fetch_issues(repo, token=None, issue=None):
334+
def fetch_issues(repo, token=None, issue_ids=None):
333335
headers = make_headers(token)
334-
if issue is not None:
335-
url = "https://api.github.com/repos/{}/issues/{}".format(repo, issue)
336-
yield from [requests.get(url).json()]
336+
if issue_ids is not None:
337+
for issue_id in issue_ids:
338+
url = "https://api.github.com/repos/{}/issues/{}".format(repo, issue_id)
339+
response = requests.get(url)
340+
response.raise_for_status()
341+
yield response.json()
337342
else:
338343
url = "https://api.github.com/repos/{}/issues?state=all&filter=all".format(repo)
339344
for issues in paginate(url, headers):
340345
yield from issues
341346

342-
def fetch_pull_requests(repo, token=None, pull_request=None):
347+
348+
def fetch_pull_requests(repo, token=None, pull_request_ids=None):
343349
headers = make_headers(token)
344-
if pull_request is not None:
345-
url = "https://api.github.com/repos/{}/pulls/{}".format(repo, pull_request)
346-
yield from [requests.get(url).json()]
350+
if pull_request_ids is not None:
351+
for pull_request_id in pull_request_ids:
352+
url = "https://api.github.com/repos/{}/pulls/{}".format(
353+
repo, pull_request_id
354+
)
355+
response = requests.get(url)
356+
response.raise_for_status()
357+
yield response.json()
347358
else:
348359
url = "https://api.github.com/repos/{}/pulls?state=all&filter=all".format(repo)
349360
for pull_requests in paginate(url, headers):

0 commit comments

Comments
 (0)