-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgitlab-invite.py
More file actions
52 lines (41 loc) · 1.62 KB
/
gitlab-invite.py
File metadata and controls
52 lines (41 loc) · 1.62 KB
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
from typing import List
import os
import sys
import gitlab
from gitlab import const, exceptions
import pandas as pd
from dotenv import load_dotenv
def invite_users_to_group(gl: gitlab.Gitlab, group_name: str, user_ids: List[int], access_level=const.AccessLevel.DEVELOPER):
try:
group = gl.groups.get(group_name)
except exceptions.GitlabGetError as e:
print(f"❌ Failed to get group '{group_name}': {e}")
sys.exit(1)
for uid in user_ids:
try:
# 检查是否已经是成员
try:
group.members.get(uid)
print(f"ℹ️ User {uid} is already a member of '{group_name}'")
continue
except exceptions.GitlabGetError:
pass # 用户不是成员,可以继续邀请
group.invitations.create(data={
"user_id": str(uid),
"access_level": access_level,
})
print(f"✅ Invitation sent to user {uid} for group '{group_name}'")
except exceptions.GitlabCreateError as e:
print(f"❌ Failed to invite user {uid}: {e}")
if __name__ == "__main__":
load_dotenv()
token = os.getenv("GITLAB_PERSONAL_ACCESS_TOKEN")
if not token:
print("❌ Please provide private token via GITLAB_PERSONAL_ACCESS_TOKEN env var")
sys.exit(1)
url = os.getenv("GITLAB_URL", "https://git.lug.ustc.edu.cn/")
group_name = os.getenv("GITLAB_GROUP", "Compiler25")
gl = gitlab.Gitlab(url=url, private_token=token)
df = pd.read_csv("uid.csv")
uids = df["uid"].tolist()
invite_users_to_group(gl, group_name, uids)