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