|
# Get lines added / removed stats from merged pull requests from a given repository with a given tag |
|
import logging |
|
import requests |
|
|
|
from collections import Counter |
|
from os import getenv |
|
|
|
GITHUB_REPO='Wikia/app' |
|
GITHUB_LABEL='user-rename-tool' |
|
GITHUB_SINCE='2017-10-01' # ignore PRs older than given date |
|
|
|
logging.basicConfig(level=logging.INFO) |
|
|
|
http = requests.Session() |
|
|
|
# add OAuth authentication if GITHUB_AUTH env variable are present (in a form of "<user>:<password>" |
|
GITHUB_AUTH=getenv('GITHUB_AUTH') |
|
|
|
if GITHUB_AUTH is not None: |
|
(user, password) = GITHUB_AUTH.split(':') |
|
http.auth = (user, password) |
|
|
|
|
|
def get_pull_requests(repo, label, since): |
|
page = 1 |
|
|
|
while True: |
|
# @see https://api.github.com/search/issues?q=label:SUS+is%3Amerged%20repo:Wikia/app |
|
url = 'https://api.github.com/search/issues?q=label:{label}+is%3Amerged%20repo:{repo}&per_page={per_page}&page={page}'.format(label=label, repo=repo, per_page=100, page=page) |
|
|
|
res = http.get(url) |
|
res.raise_for_status() |
|
|
|
for pr in res.json()['items']: |
|
# only PRs older than given date will follow, we can leave now |
|
# 2017-07-05T14:01:16Z |
|
if pr['closed_at'] < since: |
|
return |
|
|
|
yield { |
|
'id': pr['number'], |
|
'title': pr['title'], |
|
'author': pr['user']['login'], |
|
'timestamp': pr['closed_at'][:10] # only the date |
|
} |
|
|
|
# next page |
|
page += 1 |
|
|
|
|
|
def get_pull_request_stats(repo, issue_id): |
|
# @see https://api.github.com/repos/Wikia/app/pulls/13248 |
|
url = 'https://api.github.com/repos/{repo}/pulls/{issue_id}'.format(repo=repo, issue_id=int(issue_id)) |
|
|
|
res = http.get(url) |
|
res.raise_for_status() |
|
|
|
info = res.json() |
|
|
|
return { |
|
"commits": info['commits'], |
|
"additions": info['additions'], |
|
"deletions": info['deletions'], |
|
} |
|
|
|
|
|
def get_stats(repo, label, since): |
|
logger = logging.getLogger('get_stats') |
|
|
|
# @see https://docs.python.org/2/library/collections.html#collections.Counter |
|
stats = Counter() |
|
|
|
for pr in get_pull_requests(repo, label, since): |
|
logger.info("#{id} | {title} ({timestamp})".format(**pr)) |
|
|
|
pr_info = get_pull_request_stats(repo, pr['id']) |
|
logger.info("- {deletions} + {additions}".format(**pr_info)) |
|
|
|
stats.update(pr_info) |
|
stats.update(dict(pull_requests=1)) |
|
|
|
logger.info("SUS stats: {pull_requests} pull requests, *{deletions} deletions*, {additions} additions".format(**stats)) |
|
|
|
|
|
if __name__ == "__main__": |
|
get_stats(GITHUB_REPO, GITHUB_LABEL, GITHUB_SINCE) |