Last active
April 27, 2016 16:05
-
-
Save skwashd/bf9982f90a7c55f8cd16d7c0d2f8ef77 to your computer and use it in GitHub Desktop.
Audit your github pushes
This file contains hidden or 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
#!/usr/bin/env python | |
import pprint | |
import requests | |
from datetime import datetime | |
# GitHub Organisation | |
org = 'my_org' | |
# GitHub API token | |
token = 'my_pac_with_full_access' | |
# GitHub username | |
username = 'my_username' | |
# How far back to go? 1 Feb 2016 is current value | |
oldest = 1454284800 | |
headers = { | |
'Authorization': 'token ' + token, | |
'Accept': 'application/vnd.github.v3+json', | |
} | |
repos_counter = 1 | |
while True: | |
repo_params = { | |
'page': repos_counter, | |
'per_page': 100, | |
} | |
repos_url = 'https://api.github.com/orgs/{org}/repos'.format(org=org) | |
repos_reponse = requests.get(repos_url, params=repo_params, headers=headers) | |
repos_reponse.raise_for_status() | |
for repo in repos_reponse.json(): | |
print("\n\n\nProcessing: {name}".format(name=repo['name'])) | |
counter = 1 | |
url = repo['events_url'] | |
try: | |
while counter < 4: | |
params = { | |
'page': counter, | |
'per_page': 100, | |
} | |
response = requests.get(url, params=params, headers=headers) | |
response.raise_for_status() | |
for event in response.json(): | |
if event['type'] != 'PushEvent': | |
continue | |
if event['actor']['login'] != username: | |
continue | |
timestamp = datetime.strptime(event['created_at'], '%Y-%m-%dT%H:%M:%SZ') | |
if int(timestamp.strftime('%s')) < oldest: | |
raise Exception('Oldest date exceeded') | |
print("{timestamp}:{repo}".format(repo=event['repo']['name'], timestamp=timestamp.strftime('%Y-%m-%d %H:%M:%S'))) | |
for commit in event['payload']['commits']: | |
print("{hash}:{msg}".format(hash=commit['sha'][0:6], msg=commit['message'])) | |
print("--\n") | |
counter +=1 | |
except Exception as exp: | |
print(exp) | |
repos_counter +=1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment