/
github_repo.py
110 lines (89 loc) · 2.97 KB
/
github_repo.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Author: "Chris Ward" <cward@redhat.com>
'''
Github API repo membership
'''
# PY3 COMPAT
from __future__ import unicode_literals, absolute_import
import logging
from github import Github
logr = logging.getLogger(__name__)
# List of Github API calls available
TARGETS = {
'collaborators': lambda x: x.get_collaborators(),
'assignees': lambda x: x.get_assignees(),
'contributors': lambda x: x.get_contributors(),
'stargazers': lambda x: x.get_stargazers(),
'teams': lambda x: x.get_teams(),
'watchers': lambda x: x.get_watchers(),
}
def _logins(users, user_attrs=None):
'''
FIXME: DOCS...
'''
# FIXME: check for support attrs
# Supported attrs:
# login # DEFAULT, no auth required
# email
# bio
# company
# created_at
# hireable
# location
# updated_at
# url
# 'login' will be the dict index key; remove it from user_attr columns
if 'login' in user_attrs:
if user_attrs.index('login') >= 0:
# get this out of the remaining attrs to parse
del user_attrs[user_attrs.index('login')]
_users = {}
for u in users:
l = u.login
logr.debug('LOGIN: {}'.format(l))
_users[l] = {}
for a in user_attrs:
logr.debug('user: {}'.format(u))
logr.debug('attr: {}'.format(a))
_users[l][a] = getattr(u, a)
return _users
def extract(repo_url=None, target=None, user=None, password=None, token=None,
user_attrs=None):
'''
FIXME: DOCS...
'''
if not repo_url:
raise RuntimeError("URI must be specified")
if target not in TARGETS.keys():
raise RuntimeError("Unknown members target [{}]".format(target))
# eg, login, email; user.ATTR to return as ['member', 'member', ... ']
user_attrs = user_attrs or []
# user_attrs can be a list (of strings) or a string
assert isinstance(user_attrs, (unicode, str, list))
# if we get a string, put convert it to a single item list
if isinstance(user_attrs, (unicode, str)):
user_attrs = [user_attrs]
# Should only have a list at this point... empty or with strings
assert isinstance(user_attrs, list)
logr.debug("user_attrs: {}".format(user_attrs))
# use token if available, otherwise, password
if token:
logr.debug(" ... USING AUTH TOKEN")
password = token
else:
assert password != '' # is not null
if user and password:
logr.debug("AUTHENTICATING USER (WITH PASSOWRD): {}".format(user))
g = Github(user, password)
else:
if user:
logr.debug("NOT AUTHENTICATING USER: {}".format(user))
else:
logr.debug("NO USER / PASSWORD set")
g = Github()
repo = g.get_repo(repo_url)
# run the github api call for members depending on requested target
users = TARGETS[target](repo)
members = _logins(users=users, user_attrs=user_attrs)
return members