Visitar URL original
Merge branch 'feature-new-search-api' into feature-users-search · github3py/github3py@af4f3c1 · GitHub
Skip to content

Commit af4f3c1

Browse files
committed
Merge branch 'feature-new-search-api' into feature-users-search
Conflicts: github3/github.py github3/search/__init__.py tests/integration/test_api.py tests/integration/test_github.py
2 parents cbaeb44 + b6cd4c1 commit af4f3c1

7 files changed

Lines changed: 172 additions & 13 deletions

File tree

‎github3/api.py‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,62 @@ def search_code(query, sort=None, order=None, per_page=None,
411411
etag)
412412

413413

414+
def search_issues(query, sort=None, order=None, per_page=None,
415+
text_match=False, number=-1, etag=None):
416+
"""Find issues by state and keyword
417+
418+
.. warning::
419+
420+
You will only be able to make 5 calls with this or other search
421+
functions. To raise the rate-limit on this set of endpoints, create an
422+
authenticated :class:`GitHub <github3.github.GitHub>` Session with
423+
``login``.
424+
425+
The query can contain any combination of the following supported
426+
qualifers:
427+
428+
- ``type`` With this qualifier you can restrict the search to issues or
429+
pull request only.
430+
- ``in`` Qualifies which fields are searched. With this qualifier you can
431+
restrict the search to just the title, body, comments, or any
432+
combination of these.
433+
- ``author`` Finds issues created by a certain user.
434+
- ``assignee`` Finds issues that are assigned to a certain user.
435+
- ``mentions`` Finds issues that mention a certain user.
436+
- ``commenter`` Finds issues that a certain user commented on.
437+
- ``involves`` Finds issues that were either created by a certain user,
438+
assigned to that user, mention that user, or were commented on by that
439+
user.
440+
- ``state`` Filter issues based on whether they’re open or closed.
441+
- ``labels`` Filters issues based on their labels.
442+
- ``language`` Searches for issues within repositories that match a
443+
certain language.
444+
- ``created`` or ``updated`` Filters issues based on times of creation, or
445+
when they were last updated.
446+
- ``comments`` Filters issues based on the quantity of comments.
447+
- ``user`` or ``repo`` Limits searches to a specific user or repository.
448+
449+
For more information about these qualifiers, see: http://git.io/d1oELA
450+
451+
:param str query: (required), a valid query as described above, e.g.,
452+
``windows label:bug``
453+
:param str sort: (optional), how the results should be sorted;
454+
options: ``created``, ``comments``, ``updated``; default: best match
455+
:param str order: (optional), the direction of the sorted results,
456+
options: ``asc``, ``desc``; default: ``desc``
457+
:param int per_page: (optional)
458+
:param bool text_match: (optional), if True, return matching search
459+
terms. See http://git.io/QLQuSQ for more information
460+
:param int number: (optional), number of issues to return.
461+
Default: -1, returns all available issues
462+
:param str etag: (optional), previous ETag header value
463+
:return: generator of :class:`IssueSearchResult
464+
<github3.search.IssueSearchResult>`
465+
"""
466+
return gh.search_issues(query, sort, order, per_page, text_match,
467+
number, etag)
468+
469+
414470
def search_repositories(query, sort=None, order=None, per_page=None,
415471
text_match=False, number=-1, etag=None):
416472
"""Find repositories via various criteria.

‎github3/github.py‎

Lines changed: 68 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
from github3.models import GitHubCore
1818
from github3.orgs import Organization
1919
from github3.repos import Repository
20-
from github3.search import (CodeSearchResult, RepositorySearchResult,
21-
UserSearchResult)
20+
from github3.search import (CodeSearchResult, IssueSearchResult,
21+
RepositorySearchResult, UserSearchResult)
2222
from github3.structs import SearchIterator
2323
from github3.users import User, Key
2424
from github3.notifications import Thread
@@ -1018,7 +1018,7 @@ def search_code(self, query, sort=None, order=None, per_page=None,
10181018
options: ``asc``, ``desc``; default: ``desc``
10191019
:param int per_page: (optional)
10201020
:param bool text_match: (optional), if True, return matching search
1021-
terms. See http://git.io/4ct1eQ for more information
1021+
terms. See http://git.io/iRmJxg for more information
10221022
:param int number: (optional), number of repositories to return.
10231023
Default: -1, returns all available repositories
10241024
:param str etag: (optional), previous ETag header value
@@ -1043,6 +1043,71 @@ def search_code(self, query, sort=None, order=None, per_page=None,
10431043
return SearchIterator(number, url, CodeSearchResult, self, params,
10441044
etag, headers)
10451045

1046+
def search_issues(self, query, sort=None, order=None, per_page=None,
1047+
text_match=False, number=-1, etag=None):
1048+
"""Find issues by state and keyword
1049+
1050+
The query can contain any combination of the following supported
1051+
qualifers:
1052+
1053+
- ``type`` With this qualifier you can restrict the search to issues
1054+
or pull request only.
1055+
- ``in`` Qualifies which fields are searched. With this qualifier you
1056+
can restrict the search to just the title, body, comments, or any
1057+
combination of these.
1058+
- ``author`` Finds issues created by a certain user.
1059+
- ``assignee`` Finds issues that are assigned to a certain user.
1060+
- ``mentions`` Finds issues that mention a certain user.
1061+
- ``commenter`` Finds issues that a certain user commented on.
1062+
- ``involves`` Finds issues that were either created by a certain user,
1063+
assigned to that user, mention that user, or were commented on by
1064+
that user.
1065+
- ``state`` Filter issues based on whether they’re open or closed.
1066+
- ``labels`` Filters issues based on their labels.
1067+
- ``language`` Searches for issues within repositories that match a
1068+
certain language.
1069+
- ``created`` or ``updated`` Filters issues based on times of creation,
1070+
or when they were last updated.
1071+
- ``comments`` Filters issues based on the quantity of comments.
1072+
- ``user`` or ``repo`` Limits searches to a specific user or
1073+
repository.
1074+
1075+
For more information about these qualifiers, see: http://git.io/d1oELA
1076+
1077+
:param str query: (required), a valid query as described above, e.g.,
1078+
``windows label:bug``
1079+
:param str sort: (optional), how the results should be sorted;
1080+
options: ``created``, ``comments``, ``updated``;
1081+
default: best match
1082+
:param str order: (optional), the direction of the sorted results,
1083+
options: ``asc``, ``desc``; default: ``desc``
1084+
:param int per_page: (optional)
1085+
:param bool text_match: (optional), if True, return matching search
1086+
terms. See http://git.io/QLQuSQ for more information
1087+
:param int number: (optional), number of issues to return.
1088+
Default: -1, returns all available issues
1089+
:param str etag: (optional), previous ETag header value
1090+
:return: generator of :class:`IssueSearchResult
1091+
<github3.search.IssueSearchResult>`
1092+
"""
1093+
params = {'q': query}
1094+
headers = {}
1095+
1096+
if sort in ('comments', 'created', 'updated'):
1097+
params['sort'] = sort
1098+
1099+
if order in ('asc', 'desc'):
1100+
params['order'] = order
1101+
1102+
if text_match:
1103+
headers = {
1104+
'Accept': 'application/vnd.github.v3.full.text-match+json'
1105+
}
1106+
1107+
url = self._build_url('search', 'issues')
1108+
return SearchIterator(number, url, IssueSearchResult, self, params,
1109+
etag, headers)
1110+
10461111
def search_repositories(self, query, sort=None, order=None,
10471112
per_page=None, text_match=False, number=-1,
10481113
etag=None):

‎github3/search/__init__.py‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
from .code import CodeSearchResult
2+
from .issue import IssueSearchResult
23
from .repository import RepositorySearchResult
34
from .user import UserSearchResult
45

56

6-
__all__ = [CodeSearchResult, RepositorySearchResult, UserSearchResult]
7+
__all__ = [CodeSearchResult, IssueSearchResult, RepositorySearchResult,
8+
UserSearchResult]

‎github3/search/issue.py‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# -*- coding: utf-8 -*-
2+
from github3.models import GitHubCore
3+
from github3.issues import Issue
4+
5+
6+
class IssueSearchResult(GitHubCore):
7+
def __init__(self, data, session=None):
8+
result = data.copy()
9+
#: Score of the result
10+
self.score = result.pop('score')
11+
#: Text matches
12+
self.text_matches = result.pop('text_matches', [])
13+
#: Issue object
14+
self.issue = Issue(result, self)
15+
16+
def __repr__(self):
17+
return '<IssueSearchResult [{0}]>'.format(self.repository)

‎tests/cassettes/GitHub_search_issues.json‎

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.

‎tests/integration/test_api.py‎

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,6 @@ class TestAPI(IntegrationHelper):
77
def get_client(self):
88
return github3.gh
99

10-
def test_search_repositories(self):
11-
"""Test the ability to use the repository search endpoint"""
12-
cassette_name = self.cassette_name('search_repositories',
13-
cls='GitHub')
14-
with self.recorder.use_cassette(cassette_name):
15-
repos = self.gh.search_repositories('github3 language:python')
16-
assert isinstance(next(repos),
17-
github3.search.RepositorySearchResult)
18-
1910
def test_search_code(self):
2011
"""Test the ability to use the code search endpoint"""
2112
cassette_name = self.cassette_name('search_code',
@@ -35,3 +26,21 @@ def test_search_users(self):
3526
users = self.gh.search_users('tom followers:>1000')
3627
assert isinstance(next(users),
3728
github3.search.UserSearchResult)
29+
30+
def test_search_issues(self):
31+
"""Test the ability to use the issues search endpoint"""
32+
cassette_name = self.cassette_name('search_issues',
33+
cls='GitHub')
34+
with self.recorder.use_cassette(cassette_name):
35+
issues = self.gh.search_issues('github3 labels:bugs')
36+
assert isinstance(next(issues),
37+
github3.search.IssueSearchResult)
38+
39+
def test_search_repositories(self):
40+
"""Test the ability to use the repository search endpoint"""
41+
cassette_name = self.cassette_name('search_repositories',
42+
cls='GitHub')
43+
with self.recorder.use_cassette(cassette_name):
44+
repos = self.gh.search_repositories('github3 language:python')
45+
assert isinstance(next(repos),
46+
github3.search.RepositorySearchResult)

‎tests/integration/test_github.py‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,15 @@ def test_search_users_with_text_match(self):
247247
assert isinstance(users, github3.structs.SearchIterator)
248248
assert len(user_result.text_matches) > 0
249249

250+
def test_search_issues(self):
251+
"""Test the ability to use the issues search endpoint"""
252+
cassette_name = self.cassette_name('search_issues')
253+
with self.recorder.use_cassette(cassette_name):
254+
issues = self.gh.search_issues('github3 labels:bugs')
255+
assert isinstance(next(issues), github3.search.IssueSearchResult)
256+
257+
assert isinstance(issues, github3.structs.SearchIterator)
258+
250259
def test_search_repositories(self):
251260
"""Test the ability to use the repository search endpoint"""
252261
cassette_name = self.cassette_name('search_repositories')

0 commit comments

Comments
 (0)