Skip to content

Instantly share code, notes, and snippets.

@o8r
Created February 2, 2018 15:40
Show Gist options
  • Save o8r/8e510348323dfb4ae062757c9e39e533 to your computer and use it in GitHub Desktop.
Save o8r/8e510348323dfb4ae062757c9e39e533 to your computer and use it in GitHub Desktop.
Add /search/commits to pygithub
# Define search function because pygithub does not support /search/commits API yet.
import github
def __Github_search_commits(self, word, sort=github.GithubObject.NotSet, order=github.GithubObject.NotSet, **kwargs):
"""
:calls: `GET /search/commit`
:param str word: string search keyword
:param str sort: string ('author-date', 'commiter-date') (default: best match)
:param str order: string ('desc', 'asc') (default: 'desc')
:param dict kwargs: dictionary including query parameter set
:rtype: :class:`github.PaginatedList.PaginatedList` of :class:`github.Commit`
"""
#assert self._Github__requester._Requester__api_preview
assert isinstance(word, (str, )), word # for Python2, check if word is (str, unicode)
url = '/search/commits'
url_parameters = dict()
q = '+'.join([k+':'+v for (k,v) in kwargs.items()])
q += '+' + word
#url_parameters["q"] = q github.Requester encodes the url_parameters and Github reject it.
url += '?q=' + q # So, We directly include the parameters into URL itself
if sort is not github.GithubObject.NotSet:
#url_parameters["sort"] = sort
url += '&sort=' + sort
if order is not github.GithubObject.NotSet:
#url_parameters["order"] = order
url += '&order=' + order
return github.PaginatedList.PaginatedList(
github.Commit.Commit,
self._Github__requester, # access Github.__requester
url,
url_parameters,
headers = {'Accept': 'application/vnd.github.cloak-preview'}
)
# Factory of Github object supporting /search/commits
def _Github(login_or_token=None, password=None, base_url='https://api.github.com', timeout=10, client_id=None, client_secret=None, user_agent='PyGithub/Python', per_page=30):
import types
g = github.Github(login_or_token, password, base_url, timeout, client_id, client_secret, user_agent, per_page, api_preview=False)
g.search_commits = types.MethodType(__Github_search_commits, g)
return g
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment