-
Notifications
You must be signed in to change notification settings - Fork 0
/
githubuseradd.py
71 lines (65 loc) · 2.15 KB
/
githubuseradd.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
# An OAuth access token is needed, see: https://docs.github.com/en/free-pro-team@latest/github/authenticating-to-github/creating-a-personal-access-token
# Rate limit is 500 per day or 50 if you do not meet certain requirements.
# For more informations see: https://docs.github.com/en/free-pro-team@latest/rest/reference/orgs#set-organization-membership-for-a-user
import requests
import time
import sys
import getopt
examplecommandline = "Expecting 4 arguments: github_batchadd.py -o <your_organistionname> -u <github_username> -t <github_personal_token> -f <list_of_emails_input_file>"
if len(sys.argv) != 9:
print(examplecommandline)
sys.exit(2)
org = ""
username = ""
token = ""
inputfile = ""
try:
opts, args = getopt.getopt(
sys.argv[1:],
"ho:u:t:f:",
["organisation=", "username=", "token=", "listofemailsfile="],
)
except getopt.GetoptError:
print(examplecommandline)
sys.exit(2)
for opt, arg in opts:
if opt == "-h":
print(examplecommandline)
sys.exit(0)
elif opt in ("-o", "--iorganisation"):
org = arg
elif opt in ("-u", "--iusername"):
username = arg
elif opt in ("-t", "--itoken"):
token = arg
elif opt in ("-f", "--ifile"):
inputfile = arg
h = {"Content-type": "application/json", "Accept": "application/vnd.github.v3+json"}
try:
with open(inputfile) as f:
content = f.readlines()
except:
print("File could not be opened.")
sys.exit(3)
content = [line.strip() for line in content]
invitecount = 0
for email in content:
if email != "":
r = requests.post(
"https://api.github.com/orgs/" + org + "/invitations",
headers=h,
json={"invitee_id": email},
auth=(username, token),
)
time.sleep(1)
print(r.status_code, r.reason)
print(r.text)
if r.status_code != 201:
print(
"Error occurred. "
+ str(invitecount)
+ " have been invited. See error information above."
)
sys.exit(4)
invitecount += 1
print("Finished. " + str(invitecount) + " has been invited.")