Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Retry GitHub API request if no cached data available #6

Merged
merged 1 commit into from
May 5, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 23 additions & 7 deletions cucoslib/workers/githuber.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,8 @@
import datetime
import github
import random
import os
import time
from collections import OrderedDict
from selinon import StoragePool

from cucoslib.schemas import SchemaRef
from cucoslib.base import BaseTask
Expand Down Expand Up @@ -72,8 +71,24 @@ def create_test_instance(cls, repo_name, repo_url):
return instance

@staticmethod
def _get_last_years_commits(repo):
activity = repo.get_stats_commit_activity()
def _retry_no_cached(call, sleep_time=2, retry_count=10):
""" Deal with cached results from GitHub as PyGitHub does not check this

https://developer.github.com/v3/repos/statistics/#a-word-about-caching
"""
result = None

for _ in range(retry_count):
result = call()
if result:
break
time.sleep(sleep_time)

return result

@classmethod
def _get_last_years_commits(cls, repo):
activity = cls._retry_no_cached(repo.get_stats_commit_activity)
if not activity:
return []
return [x.total for x in activity]
Expand All @@ -91,11 +106,12 @@ def _issues_or_prs_count(self, gh, query):
items = gh.search_issues(query=query)
return getattr(items, 'totalCount', -1)

@staticmethod
def _get_repo_stats(repo):
@classmethod
def _get_repo_stats(cls, repo):
# len(list()) is workaround for totalCount being None
# https://github.com/PyGithub/PyGithub/issues/415
d = {'contributors_count': len(list(repo.get_contributors()))}
contributors = cls._retry_no_cached(repo.get_contributors)
d = {'contributors_count': len(list(contributors)) if contributors is not None else 'N/A'}
for prop in REPO_PROPS:
d[prop] = repo.raw_data.get(prop, -1)
return d
Expand Down