Skip to content

Commit

Permalink
add github_find_member_with_key.py
Browse files Browse the repository at this point in the history
  • Loading branch information
jantman committed Nov 23, 2016
1 parent eee86b7 commit 5c9bc42
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -34,6 +34,7 @@ file.
* __gist.py__ - Simple python script to upload a file as a private Gist on GitHub. Prompts interactively for Auth token, so usable from shared servers.
* __git_repo_diff.py__ - uses GitPython to compare 2 git repo clones and report on branches that only exist in one, or have different head commits in the two repos
* __github_clone_setup.py__ - script using github3.py to add upstream remote for any clone of a github fork, and add refs to check out pull requests from origin and upstream.
* __github_find_member_with_key.py__ - Script using PyGithub to list an organization's members, and then find who has a specified public key.
* __github_irc_hooks.py__ - script to setup GitHub repo IRC service hooks
* __github_label_setup.py__ - script to setup a given set of labels on all of your (or an org's) GitHub repos.
* __har_urls.py__ - Script to dump all URLs and their status codes from a JSON [HTTP Archive (HAR)](http://www.softwareishard.com/blog/firebug/http-archive-specification/) file, such as those generated by the [Firebug NetExport extension](http://getfirebug.com/wiki/index.php/Firebug_Extensions#NetExport)
Expand Down
74 changes: 74 additions & 0 deletions github_find_member_with_key.py
@@ -0,0 +1,74 @@
#!/usr/bin/env python
#
# Script using PyGithub to list an organization's members, and then find who
# has a specified public key.
#
# Copyright 2016 Jason Antman <jason@jasonantman.com> <http://www.jasonantman.com>
# Free for any use provided that patches are submitted back to me.
#
# The latest version of this script can be found at:
# <https://github.com/jantman/misc-scripts/blob/master/github_find_member_with_key.py>
#
# Requires PyGithub - `pip install PyGithub` (tested against 1.23.0)
# tested with py27 and py32
#
# Assumes you have a GitHub API Token, either in ~/.ssh/apikeys.py or
# in a GITHUB_TOKEN environment variable.
#
# CHANGELOG:
#
# * 2016-11-23 Jason Antman <jason@jasonantman.com>
# - initial script
#

from github import Github
import os
import sys
import argparse

parser = argparse.ArgumentParser()
parser.add_argument('orgname', type=str, help='github org name')
parser.add_argument('KEY_FILE_PATH', type=str, help='path to public key')
args = parser.parse_args()

TOKEN = None
try:
# look for GITHUB_TOKEN defined in ~/.ssh/apikeys.py
sys.path.append(os.path.abspath(os.path.join(os.path.expanduser('~'), '.ssh')))
from apikeys import GITHUB_TOKEN
TOKEN = GITHUB_TOKEN
except ImportError:
pass

if TOKEN is None:
try:
TOKEN = os.environ['GITHUB_TOKEN']
except KeyError:
sys.stderr.write("ERROR: you must either set GITHUB_TOKEN in ~/.ssh/apikeys.py or export it as an env variable.\n")
sys.exit(1)

print("Logging in to github")
g = Github(login_or_token=TOKEN)
with open(args.KEY_FILE_PATH, 'r') as fh:
priv_key = fh.read().strip()
# strip the title
priv_key = ' '.join(priv_key.split(' ')[0:2]).strip()


members = [m for m in g.get_organization(args.orgname).get_members()]
print("Organization %s has %d members; searching their keys" % (args.orgname, len(members)))
for m in members:
keys = [k for k in m.get_keys()]
print("Checking %s (%d keys)" % (m.login, len(keys)))
for k in keys:
try:
title = k.title
except AttributeError:
title = ''
key_s = "Key %d %s" % (k.id, title)
if k.key == priv_key:
print("\tMATCH: %s" % key_s)
raise SystemExit(0)
else:
print("\tno match: %s" % key_s)
print("NO MATCH!")

0 comments on commit 5c9bc42

Please sign in to comment.