-
-
Save eliheuer/117da5f99f7e61c4412e0791a9e4b716 to your computer and use it in GitHub Desktop.
gh-timesheet.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
from dateutil.relativedelta import relativedelta, FR | |
from datetime import datetime | |
lastweek = datetime.utcnow().replace(hour=0,microsecond=0) + relativedelta(weekday=FR(-1)) | |
headers = {"Authorization": "bearer "+GITHUB_TOKEN} | |
def run_query(query): | |
request = requests.post('https://api.github.com/graphql', json={'query': query}, headers=headers) | |
if request.status_code == 200: | |
return request.json() | |
else: | |
raise Exception("Query failed to run by returning code of {}. {}".format(request.status_code, query)) | |
query = """ | |
query{ | |
viewer { | |
contributionsCollection(from: "%s") { | |
totalRepositoryContributions | |
totalIssueContributions | |
totalPullRequestContributions | |
totalCommitContributions | |
commitContributionsByRepository { | |
repository { | |
owner { | |
login | |
} | |
name | |
} | |
url | |
contributions(first: 100, orderBy: {field: COMMIT_COUNT, direction: DESC}) { | |
nodes { | |
commitCount | |
user { | |
login | |
} | |
occurredAt | |
} | |
} | |
} | |
issueContributionsByRepository { | |
repository { | |
owner { | |
login | |
} | |
name | |
} | |
contributions(first: 100) { | |
nodes { | |
issue { | |
number | |
title | |
} | |
} | |
} | |
} | |
pullRequestContributionsByRepository { | |
repository { | |
owner { | |
login | |
} | |
name | |
} | |
contributions(first: 100) { | |
nodes { | |
pullRequest { | |
number | |
title | |
} | |
} | |
} | |
} | |
repositoryContributions(first: 20) { | |
nodes { | |
repository { | |
name | |
description | |
createdAt | |
url | |
} | |
} | |
} | |
} | |
} | |
} | |
""" % lastweek.isoformat() | |
result = run_query(query)["data"]["viewer"]["contributionsCollection"] | |
print("Commits:") | |
for repo in result["commitContributionsByRepository"]: | |
repo_name = repo["repository"]["owner"]["login"]+"/"+repo["repository"]["name"] | |
contributions = sum([x["commitCount"] for x in repo["contributions"]["nodes"]]) | |
print("%s: %i" % (repo_name, contributions)) | |
print("\nCreated issues:") | |
for repo in result["issueContributionsByRepository"]: | |
repo_name = repo["repository"]["owner"]["login"]+"/"+repo["repository"]["name"] | |
for c in repo["contributions"]["nodes"]: | |
no = c["issue"]["number"] | |
title = c["issue"]["title"] | |
print("%s#%i: %s" % (repo_name, no, title)) | |
print("\nPRs:") | |
for repo in result["pullRequestContributionsByRepository"]: | |
repo_name = repo["repository"]["owner"]["login"]+"/"+repo["repository"]["name"] | |
for c in repo["contributions"]["nodes"]: | |
no = c["pullRequest"]["number"] | |
title = c["pullRequest"]["title"] | |
print("%s#%i: %s" % (repo_name, no, title)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment