2015-06-29 10:30:59 +00:00
|
|
|
__author__ = 'sagi'
|
|
|
|
import requests
|
2015-06-29 11:41:21 +00:00
|
|
|
from GithubAPI.GithubAPI import GitHubAPI_Keys
|
2015-06-29 18:48:28 +00:00
|
|
|
from models.Task import Task
|
|
|
|
from google.appengine.ext import db
|
2015-06-29 10:30:59 +00:00
|
|
|
|
2015-06-29 11:41:21 +00:00
|
|
|
githubKeys = GitHubAPI_Keys()
|
2015-06-29 10:30:59 +00:00
|
|
|
|
|
|
|
def get_repo_general_info(repo_url):
|
2015-06-29 11:41:21 +00:00
|
|
|
url = 'https://api.github.com/repos/' + repo_url + '?client_id='+githubKeys.getId()+'&client_secret=' + githubKeys.getSecret()
|
2015-06-29 10:30:59 +00:00
|
|
|
req = requests.get(url)
|
|
|
|
return req.json()
|
|
|
|
|
|
|
|
|
|
|
|
def get_repo_stats(repo_url):
|
2015-06-29 11:41:21 +00:00
|
|
|
url = 'https://api.github.com/repos/' + repo_url + '/stats/contributors' + '?client_id='+githubKeys.getId()+'&client_secret=' + githubKeys.getSecret()
|
2015-06-29 10:30:59 +00:00
|
|
|
req = requests.get(url)
|
|
|
|
return req.json()
|
|
|
|
|
|
|
|
|
|
|
|
def get_repo_issues(repo_url):
|
2015-06-29 11:41:21 +00:00
|
|
|
url = 'https://api.github.com/repos/' + repo_url + '/issues' + '?client_id=' + githubKeys.getId() + '&client_secret=' + githubKeys.getSecret()
|
2015-06-29 10:30:59 +00:00
|
|
|
req = requests.get(url)
|
|
|
|
return req.json()
|
|
|
|
|
2015-06-29 13:38:39 +00:00
|
|
|
def get_repo_weekly_commits(repo_url):
|
|
|
|
url = 'https://api.github.com/repos/' + repo_url + '/stats/participation' + '?client_id=' + githubKeys.getId() + '&client_secret=' + githubKeys.getSecret()
|
|
|
|
req = requests.get(url)
|
2015-06-29 18:03:04 +00:00
|
|
|
try:
|
|
|
|
return req.json()['all']
|
|
|
|
except:
|
|
|
|
return []
|
2015-06-29 10:30:59 +00:00
|
|
|
|
2015-06-29 18:48:28 +00:00
|
|
|
def make_macro(stats, info, project_id):
|
2015-06-29 13:47:57 +00:00
|
|
|
macro = {'labels': [], 'data': [[0]]}
|
2015-06-29 13:38:39 +00:00
|
|
|
macro['labels'].append('Commits')
|
|
|
|
macro['labels'].append('Open Issues')
|
2015-06-29 18:48:28 +00:00
|
|
|
macro['labels'].append('Unfinished Tasks')
|
2015-06-29 13:38:39 +00:00
|
|
|
for stat in stats:
|
2015-06-29 13:50:16 +00:00
|
|
|
macro['data'][0][0] += stat['total']
|
2015-06-29 13:52:25 +00:00
|
|
|
macro['data'][0].append(info['open_issues'])
|
2015-06-29 18:48:28 +00:00
|
|
|
macro['data'][0].append(get_unfinished_tasks_number(project_id))
|
2015-06-29 13:38:39 +00:00
|
|
|
|
|
|
|
return macro
|
|
|
|
|
2015-06-29 14:56:58 +00:00
|
|
|
def make_micro(stats, issues):
|
|
|
|
micro = {'labels': [], 'data': [], 'series': []}
|
|
|
|
micro['labels'].append('Commits')
|
|
|
|
micro['labels'].append('Open Issues')
|
|
|
|
for stat in stats:
|
|
|
|
micro['series'].append(stat['author']['login'])
|
|
|
|
micro['data'].append([stat['total'], get_issue_num(issues, stat['author']['login'])])
|
|
|
|
|
|
|
|
return micro
|
|
|
|
|
|
|
|
def get_issue_num(issues, user):
|
|
|
|
numOfIssues = 0
|
|
|
|
for issue in issues:
|
|
|
|
if issue['user']['login'] == user:
|
|
|
|
numOfIssues += 1
|
|
|
|
return numOfIssues
|
2015-06-29 13:38:39 +00:00
|
|
|
|
2015-06-29 18:48:28 +00:00
|
|
|
def get_github_data(repo_url, project_id):
|
2015-06-29 13:38:39 +00:00
|
|
|
project_info = {'stats': {}}
|
|
|
|
github_stats = get_repo_stats(repo_url) #first Call
|
2015-06-29 10:30:59 +00:00
|
|
|
project_info['info'] = get_repo_general_info(repo_url)
|
2015-06-29 13:38:39 +00:00
|
|
|
issues = get_repo_issues(repo_url)
|
2015-06-29 18:48:28 +00:00
|
|
|
weekly_commits = [get_repo_weekly_commits(repo_url)]
|
2015-06-29 13:38:39 +00:00
|
|
|
github_stats = get_repo_stats(repo_url) #Second Call
|
2015-06-29 18:48:28 +00:00
|
|
|
project_info['stats']['macro'] = make_macro(github_stats, project_info['info'], project_id)
|
2015-06-29 14:56:58 +00:00
|
|
|
project_info['stats']['micro'] = make_micro(github_stats, issues)
|
2015-06-29 13:38:39 +00:00
|
|
|
project_info['stats']['weekly_commits'] = weekly_commits
|
|
|
|
project_info['issues'] = issues
|
2015-06-29 10:30:59 +00:00
|
|
|
|
2015-06-29 13:38:39 +00:00
|
|
|
return project_info
|
2015-06-29 18:48:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
def get_unfinished_tasks_number(project_id):
|
|
|
|
# query = Task.all().filter("isPersonal =", False, "")
|
|
|
|
return 7
|