Log GitHub API rate limit
* remaining API calls * timestamp when the limit is going to be reset Change-Id: Ib3aa2e09989180bb754d3dab2ccb785fe6723b63
This commit is contained in:
parent
570d01c5fb
commit
d55d7af510
|
@ -205,6 +205,7 @@ class GithubWebhookListener():
|
||||||
|
|
||||||
|
|
||||||
class GithubUser(collections.Mapping):
|
class GithubUser(collections.Mapping):
|
||||||
|
log = logging.getLogger('zuul.GithubUser')
|
||||||
|
|
||||||
def __init__(self, github, username):
|
def __init__(self, github, username):
|
||||||
self._github = github
|
self._github = github
|
||||||
|
@ -224,6 +225,7 @@ class GithubUser(collections.Mapping):
|
||||||
|
|
||||||
def _init_data(self):
|
def _init_data(self):
|
||||||
user = self._github.user(self._username)
|
user = self._github.user(self._username)
|
||||||
|
log_rate_limit(self.log, self._github)
|
||||||
data = {
|
data = {
|
||||||
'username': user.login,
|
'username': user.login,
|
||||||
'name': user.name,
|
'name': user.name,
|
||||||
|
@ -326,6 +328,7 @@ class GithubConnection(BaseConnection):
|
||||||
owner, proj = project.name.split('/')
|
owner, proj = project.name.split('/')
|
||||||
repository = self.github.repository(owner, proj)
|
repository = self.github.repository(owner, proj)
|
||||||
branches = [branch.name for branch in repository.branches()]
|
branches = [branch.name for branch in repository.branches()]
|
||||||
|
log_rate_limit(self.log, self.github)
|
||||||
return branches
|
return branches
|
||||||
|
|
||||||
def getPullUrl(self, project, number):
|
def getPullUrl(self, project, number):
|
||||||
|
@ -333,7 +336,9 @@ class GithubConnection(BaseConnection):
|
||||||
|
|
||||||
def getPull(self, project_name, number):
|
def getPull(self, project_name, number):
|
||||||
owner, proj = project_name.split('/')
|
owner, proj = project_name.split('/')
|
||||||
return self.github.pull_request(owner, proj, number).as_dict()
|
pr = self.github.pull_request(owner, proj, number).as_dict()
|
||||||
|
log_rate_limit(self.log, self.github)
|
||||||
|
return pr
|
||||||
|
|
||||||
def canMerge(self, change, allow_needs):
|
def canMerge(self, change, allow_needs):
|
||||||
# This API call may get a false (null) while GitHub is calculating
|
# This API call may get a false (null) while GitHub is calculating
|
||||||
|
@ -350,8 +355,10 @@ class GithubConnection(BaseConnection):
|
||||||
|
|
||||||
def getPullFileNames(self, project, number):
|
def getPullFileNames(self, project, number):
|
||||||
owner, proj = project.name.split('/')
|
owner, proj = project.name.split('/')
|
||||||
return [f.filename for f in
|
filenames = [f.filename for f in
|
||||||
self.github.pull_request(owner, proj, number).files()]
|
self.github.pull_request(owner, proj, number).files()]
|
||||||
|
log_rate_limit(self.log, self.github)
|
||||||
|
return filenames
|
||||||
|
|
||||||
def getUser(self, login):
|
def getUser(self, login):
|
||||||
return GithubUser(self.github, login)
|
return GithubUser(self.github, login)
|
||||||
|
@ -364,6 +371,7 @@ class GithubConnection(BaseConnection):
|
||||||
repository = self.github.repository(owner, proj)
|
repository = self.github.repository(owner, proj)
|
||||||
pull_request = repository.issue(pr_number)
|
pull_request = repository.issue(pr_number)
|
||||||
pull_request.create_comment(message)
|
pull_request.create_comment(message)
|
||||||
|
log_rate_limit(self.log, self.github)
|
||||||
|
|
||||||
def mergePull(self, project, pr_number, commit_message='', sha=None):
|
def mergePull(self, project, pr_number, commit_message='', sha=None):
|
||||||
owner, proj = project.split('/')
|
owner, proj = project.split('/')
|
||||||
|
@ -373,6 +381,7 @@ class GithubConnection(BaseConnection):
|
||||||
except MethodNotAllowed as e:
|
except MethodNotAllowed as e:
|
||||||
raise MergeFailure('Merge was not successful due to mergeability'
|
raise MergeFailure('Merge was not successful due to mergeability'
|
||||||
' conflict, original error is %s' % e)
|
' conflict, original error is %s' % e)
|
||||||
|
log_rate_limit(self.log, self.github)
|
||||||
if not result:
|
if not result:
|
||||||
raise Exception('Pull request was not merged')
|
raise Exception('Pull request was not merged')
|
||||||
|
|
||||||
|
@ -381,21 +390,35 @@ class GithubConnection(BaseConnection):
|
||||||
owner, proj = project.split('/')
|
owner, proj = project.split('/')
|
||||||
repository = self.github.repository(owner, proj)
|
repository = self.github.repository(owner, proj)
|
||||||
repository.create_status(sha, state, url, description, context)
|
repository.create_status(sha, state, url, description, context)
|
||||||
|
log_rate_limit(self.log, self.github)
|
||||||
|
|
||||||
def labelPull(self, project, pr_number, label):
|
def labelPull(self, project, pr_number, label):
|
||||||
owner, proj = project.split('/')
|
owner, proj = project.split('/')
|
||||||
pull_request = self.github.issue(owner, proj, pr_number)
|
pull_request = self.github.issue(owner, proj, pr_number)
|
||||||
pull_request.add_labels(label)
|
pull_request.add_labels(label)
|
||||||
|
log_rate_limit(self.log, self.github)
|
||||||
|
|
||||||
def unlabelPull(self, project, pr_number, label):
|
def unlabelPull(self, project, pr_number, label):
|
||||||
owner, proj = project.split('/')
|
owner, proj = project.split('/')
|
||||||
pull_request = self.github.issue(owner, proj, pr_number)
|
pull_request = self.github.issue(owner, proj, pr_number)
|
||||||
pull_request.remove_label(label)
|
pull_request.remove_label(label)
|
||||||
|
log_rate_limit(self.log, self.github)
|
||||||
|
|
||||||
def _ghTimestampToDate(self, timestamp):
|
def _ghTimestampToDate(self, timestamp):
|
||||||
return time.strptime(timestamp, '%Y-%m-%dT%H:%M:%SZ')
|
return time.strptime(timestamp, '%Y-%m-%dT%H:%M:%SZ')
|
||||||
|
|
||||||
|
|
||||||
|
def log_rate_limit(log, github):
|
||||||
|
try:
|
||||||
|
rate_limit = github.rate_limit()
|
||||||
|
remaining = rate_limit['resources']['core']['remaining']
|
||||||
|
reset = rate_limit['resources']['core']['reset']
|
||||||
|
except:
|
||||||
|
return
|
||||||
|
log.debug('GitHub API rate limit remaining: %s reset: %s' %
|
||||||
|
(remaining, reset))
|
||||||
|
|
||||||
|
|
||||||
def getSchema():
|
def getSchema():
|
||||||
github_connection = v.Any(str, v.Schema({}, extra=True))
|
github_connection = v.Any(str, v.Schema({}, extra=True))
|
||||||
return github_connection
|
return github_connection
|
||||||
|
|
Loading…
Reference in New Issue