-
Notifications
You must be signed in to change notification settings - Fork 73
/
github_find_member_with_key.py
executable file
·74 lines (67 loc) · 2.25 KB
/
github_find_member_with_key.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
#!/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 <[email protected]> <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 <[email protected]>
# - 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!")