Skip to content

Commit

Permalink
Merge pull request #6 from fridex/avoid-github-caching
Browse files Browse the repository at this point in the history
  • Loading branch information
jpopelka authored May 5, 2017
2 parents 6f61c67 + b3bc21d commit bb9f2b6
Showing 1 changed file with 23 additions and 7 deletions.
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

0 comments on commit bb9f2b6

Please sign in to comment.