-
-
Save Deadlyelder/30710d03fdfde0b791fd1dd1b16062e0 to your computer and use it in GitHub Desktop.
Add /search/commits to pygithub
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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