Skip to content

Commit

Permalink
Support per_page search parameter
Browse files Browse the repository at this point in the history
Update the following search_* APIs to forward the `per_page` parameter.
 - search_code
 - search_commits
 - search_issues
 - search_repositories
 - search_users

Test plan: updated unit tests to verify per_page parameter. Tested
manually by query `search_issues(..., per_page=1)` and verified the
results arrived one at a time in each http response.

Closes #1166
  • Loading branch information
ccotter committed Nov 18, 2023
1 parent 99398b4 commit fb6158c
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 7 deletions.
15 changes: 15 additions & 0 deletions src/github3/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -2248,6 +2248,9 @@ def search_code(
if sort and order in ("asc", "desc"):
params["order"] = order

if per_page is not None:
params["per_page"] = per_page

if text_match:
headers = {
"Accept": "application/vnd.github.v3.full.text-match+json"
Expand Down Expand Up @@ -2335,6 +2338,9 @@ def search_commits(
if sort and order in ("asc", "desc"):
params["order"] = order

if per_page is not None:
params["per_page"] = per_page

if text_match:
headers["Accept"] = ", ".join(
[
Expand Down Expand Up @@ -2427,6 +2433,9 @@ def search_issues(
if order in ("asc", "desc"):
params["order"] = order

if per_page is not None:
params["per_page"] = per_page

if text_match:
headers = {
"Accept": "application/vnd.github.v3.full.text-match+json"
Expand Down Expand Up @@ -2505,6 +2514,9 @@ def search_repositories(
if order in ("asc", "desc"):
params["order"] = order

if per_page is not None:
params["per_page"] = per_page

if text_match:
headers = {
"Accept": "application/vnd.github.v3.full.text-match+json"
Expand Down Expand Up @@ -2588,6 +2600,9 @@ def search_users(
if order in ("asc", "desc"):
params["order"] = order

if per_page is not None:
params["per_page"] = per_page

if text_match:
headers = {
"Accept": "application/vnd.github.v3.full.text-match+json"
Expand Down
26 changes: 19 additions & 7 deletions tests/unit/test_github.py
Original file line number Diff line number Diff line change
Expand Up @@ -1317,21 +1317,32 @@ class TestGitHubSearchIterators(helper.UnitSearchIteratorHelper):
def test_search_code(self):
"""Verify the request to search for code."""
i = self.instance.search_code(
"addClass in:file language:js repo:jquery/jquery"
"addClass in:file language:js repo:jquery/jquery", per_page=15
)
self.get_next(i)

self.session.get.assert_called_once_with(
url_for("search/code"),
params={
"per_page": 100,
"per_page": 15,
"q": "addClass in:file language:js repo:jquery/jquery",
},
headers={},
)

def test_search_commits(self):
"""Verify the request to search for commits."""
i = self.instance.search_commits("css repo:octocat/Spoon-Knife", per_page=15)
self.get_next(i)

self.session.get.assert_called_once_with(
url_for("search/commits"),
params={"per_page": 15, "q": "css repo:octocat/Spoon-Knife"},
headers={"Accept": "application/vnd.github.cloak-preview"},
)

def test_search_commits_default_per_page(self):
"""Verify the default per_page in the commits search."""
i = self.instance.search_commits("css repo:octocat/Spoon-Knife")
self.get_next(i)

Expand All @@ -1347,14 +1358,15 @@ def test_search_issues(self):
"windows label:bug language:python state:open",
sort="created",
order="asc",
per_page=15,
)
self.get_next(i)

self.session.get.assert_called_once_with(
url_for("search/issues"),
params={
"order": "asc",
"per_page": 100,
"per_page": 15,
"q": "windows label:bug language:python state:open",
"sort": "created",
},
Expand All @@ -1364,15 +1376,15 @@ def test_search_issues(self):
def test_search_repositories(self):
"""Verify the request to search for repositories."""
i = self.instance.search_repositories(
"tetris language:assembly", sort="stars", order="asc"
"tetris language:assembly", sort="stars", order="asc", per_page=15
)
self.get_next(i)

self.session.get.assert_called_once_with(
url_for("search/repositories"),
params={
"order": "asc",
"per_page": 100,
"per_page": 15,
"q": "tetris language:assembly",
"sort": "stars",
},
Expand All @@ -1381,12 +1393,12 @@ def test_search_repositories(self):

def test_search_users(self):
"""Verify the request to search for users."""
i = self.instance.search_users("tom repos:>42 followers:>1000")
i = self.instance.search_users("tom repos:>42 followers:>1000", per_page=15)
self.get_next(i)

self.session.get.assert_called_once_with(
url_for("search/users"),
params={"per_page": 100, "q": "tom repos:>42 followers:>1000"},
params={"per_page": 15, "q": "tom repos:>42 followers:>1000"},
headers={},
)

Expand Down

0 comments on commit fb6158c

Please sign in to comment.