-
Notifications
You must be signed in to change notification settings - Fork 2
/
github-event-times.py
78 lines (63 loc) · 2.57 KB
/
github-event-times.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# github-event-times.py
# Dan Wallach <dwallach@rice.edu>
import requests
import json
import re
import time
import argparse
import pprint
# see installation and usage instructions in README.md
defaultGithubToken = ["YOUR_TOKEN_HERE"]
# and we're going to need the name of your GitHub "project" in which all your
# students' work lives.
# Example: for https://github.com/RiceComp215/comp215-2017-week08-operations-username,
# you might set defaultGithubProject to "RiceComp215"
defaultGithubProject = ["YOUR_PROJECT_HERE"]
# command-line argument processing
parser = argparse.ArgumentParser(description='Get event timestamps from a GitHub repo.')
parser.add_argument('--token',
nargs=1,
default=defaultGithubToken,
help='GitHub API token')
parser.add_argument('--project',
nargs=1,
default=defaultGithubProject,
help='GitHub project to scan, default: ' + defaultGithubProject[0])
parser.add_argument('repo',
nargs='+',
default="",
help='repo to query, no default')
args = parser.parse_args()
githubRepos = args.repo
githubProject = args.project[0]
githubToken = args.token[0]
requestHeaders = {
"User-Agent": "GitHubEventTimes/1.0",
"Authorization": "token " + githubToken,
}
pp = pprint.PrettyPrinter(indent=2)
for repo in githubRepos:
response = requests.get('https://api.github.com/repos/%s/%s/events' % (githubProject, repo), headers = requestHeaders)
if response.status_code != 200:
print "Error in request: " + response.json()
exit(1)
eventList = [x for x in response.json() if x['type'] == 'PushEvent'] # we don't care about other event types
print "Events for " + repo
print
print "\\begin{tabular}{lll}"
print "{\\bf Commit ID} & {\\bf Comment} & {\\bf GitHub push time} \\\\"
print "\\hline"
for event in eventList:
try:
date = event['created_at']
commits = event['payload']['commits']
for commit in commits:
commitMessage = commit['message'].splitlines()[0] # only the first line if multiline
commitHash = commit['sha'][0:7] # only the first 7 characters, consistent with how GitHub reports commitIDs on its web front-end
print "%s & %s & %s \\\\" % (commitHash, commitMessage, date)
except KeyError:
print "Error: malformed event!"
pp.pprint(event)
print "\\hline"
print "\\end{tabular}"
print