-
Notifications
You must be signed in to change notification settings - Fork 0
/
add_users_to_project.py
65 lines (52 loc) · 1.95 KB
/
add_users_to_project.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
#!/usr/bin/env python3
import gitlab
import sys
import urllib.parse
import csv
if len(sys.argv) != 3:
print("Please provide a CSV file and a project.")
sys.exit(1)
file = sys.argv[1]
project = sys.argv[2]
gl = gitlab.Gitlab.from_config()
# verify the project exists
# first convert the / to url encode
project_encoded = urllib.parse.quote_plus(project)
try:
project = gl.projects.get(project_encoded)
except gitlab.GitlabGetError:
print("No project with that id")
sys.exit(1)
# get all existing users
users = gl.users.list(all=True)
with open(file, newline='') as csv_file:
data = csv.reader(csv_file, delimiter=',')
header = next(data)
for row in data:
email = row[0]
username = row[1]
name = row[2]
# check if user already exists
user_id = None
for user in users:
if user.email == email:
user_id = user.id
if user_id is None:
new_user = gl.users.create({'email': email,
'reset_password': 'yes',
'username': username,
'name': name})
user_id = new_user.id
print('Created user with email: ' + email + " - id: " + str(new_user.id))
else:
print('User already exists with email: ' + email + " - id: " + str(user_id))
# Add the user to the project
existing_members = project.members.list(all=True)
user_is_already_member = False
for member in existing_members:
if member.id == user_id:
user_is_already_member = True
print("User user with email: " + email + " already a member of the project")
if not user_is_already_member:
member = project.members.create({'user_id': user_id, 'access_level': gitlab.Group.DEVELOPER_ACCESS})
print("Added user user with email: " + email + " to the project")