Skip to content

Instantly share code, notes, and snippets.

@macbre
Last active December 15, 2017 10:22
Show Gist options
  • Save macbre/52099a3aefde78187e9b8845092f8ae3 to your computer and use it in GitHub Desktop.
Save macbre/52099a3aefde78187e9b8845092f8ae3 to your computer and use it in GitHub Desktop.
SUS-2289

You can obtain GitHub token and use it by setting GITHUB_AUTH env variable:

$ GITHUB_AUTH=<user_name>:<token> python get_lines_stats.py

GraphQL

https://developer.github.com/v4/explorer/

query { 
  repository(owner:"Wikia", name:"app") {
    pullRequests(labels:"SUS", states: MERGED, first: 100, orderBy:{field: UPDATED_AT, direction: DESC}) {
      edges {
    		node {
      		author {login},
          title,
          number,
          mergedAt,
          mergeCommit {oid}
    		}
      }
    }
  }
}
# 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)
requests==2.18.1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment