mirror of
https://github.com/status-im/PyGithub.git
synced 2026-08-31 19:01:15 +00:00
Add highlight search to search_code function (#925)
* ✨ add highlight search to search_code function * ✅ add highlighting search code tests
This commit is contained in:
@@ -89,3 +89,6 @@ mediaTypeOrganizationInvitationPreview = "application/vnd.github.dazzler-preview
|
||||
|
||||
# https://developer.github.com/changes/2018-03-16-protected-branches-required-approving-reviews/
|
||||
mediaTypeRequireMultipleApprovingReviews = "application/vnd.github.luke-cage-preview+json"
|
||||
|
||||
# https://developer.github.com/v3/search/#highlighting-code-search-results-1
|
||||
highLightSearchPreview = "application/vnd.github.v3.text-match+json"
|
||||
|
||||
@@ -165,8 +165,17 @@ class ContentFile(github.GithubObject.CompletableGithubObject):
|
||||
self._completeIfNotSet(self._url)
|
||||
return self._url.value
|
||||
|
||||
@property
|
||||
def text_matches(self):
|
||||
"""
|
||||
:type: string
|
||||
"""
|
||||
self._completeIfNotSet(self._text_matches)
|
||||
return self._text_matches.value
|
||||
|
||||
def _initAttributes(self):
|
||||
self._content = github.GithubObject.NotSet
|
||||
self._text_matches = github.GithubObject.NotSet
|
||||
self._encoding = github.GithubObject.NotSet
|
||||
self._download_url = github.GithubObject.NotSet
|
||||
self._git_url = github.GithubObject.NotSet
|
||||
@@ -206,3 +215,5 @@ class ContentFile(github.GithubObject.CompletableGithubObject):
|
||||
self._type = self._makeStringAttribute(attributes["type"])
|
||||
if "url" in attributes: # pragma no branch
|
||||
self._url = self._makeStringAttribute(attributes["url"])
|
||||
if "text_matches" in attributes: # pragma no branch
|
||||
self._text_matches = self._makeListOfDictsAttribute(attributes["text_matches"])
|
||||
|
||||
@@ -188,6 +188,10 @@ class GithubObject(object):
|
||||
def _makeListOfIntsAttribute(value):
|
||||
return GithubObject.__makeSimpleListAttribute(value, int)
|
||||
|
||||
@staticmethod
|
||||
def _makeListOfDictsAttribute(value):
|
||||
return GithubObject.__makeSimpleListAttribute(value, dict)
|
||||
|
||||
@staticmethod
|
||||
def _makeListOfListOfStringsAttribute(value):
|
||||
return GithubObject.__makeSimpleListAttribute(value, list)
|
||||
|
||||
+6
-3
@@ -459,12 +459,13 @@ class Github(object):
|
||||
url_parameters
|
||||
)
|
||||
|
||||
def search_code(self, query, sort=github.GithubObject.NotSet, order=github.GithubObject.NotSet, **qualifiers):
|
||||
def search_code(self, query, sort=github.GithubObject.NotSet, order=github.GithubObject.NotSet, highlight=False, **qualifiers):
|
||||
"""
|
||||
:calls: `GET /search/code <http://developer.github.com/v3/search>`_
|
||||
:param query: string
|
||||
:param sort: string ('indexed')
|
||||
:param order: string ('asc', 'desc')
|
||||
:param highlight: boolean (True, False)
|
||||
:param qualifiers: keyword dict query qualifiers
|
||||
:rtype: :class:`github.PaginatedList.PaginatedList` of :class:`github.ContentFile.ContentFile`
|
||||
"""
|
||||
@@ -487,14 +488,16 @@ class Github(object):
|
||||
url_parameters["q"] = ' '.join(query_chunks)
|
||||
assert url_parameters["q"], "need at least one qualifier"
|
||||
|
||||
headers = {"Accept": Consts.highLightSearchPreview} if highlight else None
|
||||
|
||||
return github.PaginatedList.PaginatedList(
|
||||
github.ContentFile.ContentFile,
|
||||
self.__requester,
|
||||
"/search/code",
|
||||
url_parameters
|
||||
url_parameters,
|
||||
headers=headers
|
||||
)
|
||||
|
||||
|
||||
def search_commits(self, query, sort=github.GithubObject.NotSet, order=github.GithubObject.NotSet, **qualifiers):
|
||||
"""
|
||||
:calls: `GET /search/commits <http://developer.github.com/v3/search>`_
|
||||
|
||||
File diff suppressed because one or more lines are too long
@@ -78,6 +78,10 @@ class Search(Framework.TestCase):
|
||||
else:
|
||||
self.assertEqual(files[0].decoded_content[:30], "https\nGET\napi.github.com\nNone\n")
|
||||
|
||||
def testSearchHighlightingCode(self):
|
||||
files = self.g.search_code("toto", sort="indexed", order="asc", user="jacquev6", highlight=True)
|
||||
self.assertTrue(files[0].text_matches)
|
||||
|
||||
def testUrlquotingOfQualifiers(self):
|
||||
# Example taken from #236
|
||||
issues = self.g.search_issues("repo:saltstack/salt-api type:Issues", updated=">2014-03-04T18:28:11Z")
|
||||
|
||||
Reference in New Issue
Block a user