Commit 85372c2d authored by Shrikrishna Singh's avatar Shrikrishna Singh

org/cron.py: Fix contributors cron with new IGitt

This uses IGitt new methods to fix the contributors cron
job failure, which involves passing GitHub token object
to the GitHub.get() method instead of string GITHUB_API_KEY
and adding base API_URL to the relative API URLs.

Closes coala/landing#62
parent bafb226b
import json
from collections import defaultdict
from time import sleep
import urllib
import requests
from IGitt.GitHub import GitHub
from IGitt.GitHub.GitHub import GitHub as GitHubMain
from IGitt.Interfaces import get_response
from coala_web.settings import GITHUB_API_KEY
from org.models import Contributor, Team
from org.git import get_org
request_headers = {'Authorization': 'token %s' % GITHUB_API_KEY}
from org.git import (
get_org,
GITHUB_TOKEN,
get_igitt_org,
)
def fetch_org():
data = defaultdict(lambda: defaultdict(int, {'name': '', 'bio': '',
'teams': []}))
repo_url = '/orgs/coala/repos'
response_repo = GitHub.get(GITHUB_API_KEY, repo_url)
for repo in response_repo:
github_org = get_igitt_org('github')
repos = github_org.repositories
for repo in repos:
commit_data = get_commits(repo, data)
data = get_issues(repo, commit_data)
session = requests.Session()
for contributor in data:
response = \
requests.get('https://api.github.com/search/issues?q=commenter:' +
contributor + '%20is:pr%20user:coala',
headers=request_headers, timeout=3000)
r_json = json.loads(response.text)
query_param = 'commenter:' + contributor + ' is:pr user:coala'
encoded_query_param = urllib.parse.quote(query_param)
search_url = GitHubMain.absolute_url(
'/search/issues?q=' + encoded_query_param)
response = get_response(session.get,
search_url,
GITHUB_TOKEN.auth)
r_json, other_data = response
data[contributor]['reviews'] = r_json.get('total_count', 0)
if int(response.headers['X-RateLimit-Remaining']) == 1:
rate_limit_url = GitHubMain.absolute_url('/rate_limit')
rate = GitHub.get(GITHUB_TOKEN, rate_limit_url)
if rate['resources']['search']['remaining'] == 1:
sleep(60)
for contributor in data:
user = GitHub.get(GITHUB_API_KEY, '/users/' + contributor)
url = GitHubMain.absolute_url('/users/' + contributor)
user = GitHub.get(GITHUB_TOKEN, url)
if user['bio']:
data[contributor]['bio'] = user['bio']
if user['name']:
......@@ -70,8 +80,9 @@ def fetch_org():
def get_commits(repo, data):
url = '/repos/coala/' + repo['name'] + '/contributors'
response = GitHub.get(GITHUB_API_KEY, url)
url = GitHubMain.absolute_url('/repos/' +
repo.full_name + '/contributors')
response = GitHub.get(GITHUB_TOKEN, url)
for contributor in response:
data[contributor['login']]['contributions'] +=\
contributor['contributions']
......@@ -79,8 +90,7 @@ def get_commits(repo, data):
def get_issues(repo, data):
url = '/repos/coala/' + repo['name'] + '/issues'
response = GitHub.get(GITHUB_API_KEY, url)
for issue_author in response:
data[issue_author['user']['login']]['issues'] += 1
issues = repo.filter_issues(state='all')
for issue in issues:
data[issue.author.username]['issues'] += 1
return data
......@@ -10,6 +10,8 @@ from coala_web.settings import (
GITLAB_API_KEY,
)
GITHUB_TOKEN = GitHubToken(GITHUB_API_KEY)
# Get PyGithub org object to query the GitHub APIs
# with the methods available in PyGithub
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment