-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGithub.py
124 lines (95 loc) · 3.47 KB
/
Github.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
from __future__ import print_function
from github import NamedUser
from joblib import Parallel, delayed
import multiprocessing
import ids
import github
acm = ids.github.get_organization("rowanacm")
def add_username_to_members(username):
members_team = acm.get_team(ids.github_members_team_id)
user = ids.github.get_user(username)
return members_team.add_to_members(user)
def add_member_to_team(username, team_id):
team = acm.get_team(team_id)
user = ids.github.get_user(username)
return team.add_to_members(user)
def get_recursively(search_dict, field):
"""Takes a dict with nested lists and dicts,
and searches all dicts for a key of the field
provided.
"""
fields_found = []
for key, value in search_dict.iteritems():
if key == field:
fields_found.append(value)
elif isinstance(value, dict):
results = get_recursively(value, field)
for result in results:
fields_found.append(result)
elif isinstance(value, list):
for item in value:
if isinstance(item, dict):
more_results = get_recursively(item, field)
for another_result in more_results:
fields_found.append(another_result)
return fields_found
def get_split_name(name):
space_index = name.find(" ")
first = name[:space_index]
last = name[space_index + 1:]
return first, last
def get_users_emails(user, fast=True):
emails = []
events = user.get_events()
event_count = 0
for event in events:
event_count += 1
if fast and event_count > 10:
break
email = get_recursively(event._rawData, "email")
if email is not None:
for address in email:
if "@users.noreply.github.com" not in address \
and "rowanads" not in address \
and not address.endswith(".local") \
and not address.endswith(".localdomain"):
emails.append(address)
return emails
def check_github_member(github_member, email):
is_rowan_email_address = email.endswith("students.rowan.edu")
if is_rowan_email_address:
at_index = email.find("@")
first_initial = email[at_index - 2]
last_name = email[:at_index - 2]
#emails = get_users_emails(github_member)
#if email in emails:
# return True
if is_rowan_email_address:
name = github_member.name
if name is not None and " " in name:
first, last = get_split_name(name)
if last.lower() == last_name.lower() and first.lower().startswith(first_initial.lower()):
return True
return False
def get_github_username(email):
for member in acm.get_members():
if check_github_member(member, email):
return member._login.value
return None
def add_to_members(member, role="member"):
"""
:calls: `PUT /orgs/:org/memberships/:user <http://developer.github.com/v3/orgs/members>`_
:param member: :class:`github.NamedUser.NamedUser`
:param role: string
:rtype: None
"""
assert isinstance(role, (str, unicode)), role
assert isinstance(member, github.NamedUser.NamedUser), member
url_parameters = {
"role": role,
}
headers, data = acm._requester.requestJsonAndCheck(
"PUT",
acm.url + "/memberships/" + member._identity, parameters=url_parameters
)
print("OK")