0%

gitlab-python-api

备份现有GitLab用户组、成员信息、成员权限

安装

https://python-gitlab.readthedocs.io/en/latest/install.html#

1
pip install --upgrade python-gitlab

使用文档

https://python-gitlab.readthedocs.io/en/latest/api-usage.html

Demo

获取个人空间下项目

代码

1
2
3
4
5
6
7
8
9
10
11
12
import gitlab
import urllib3

gitlab_url = "https://gitlab.zhaohongye.com"
gitlab_private_token = ""
urllib3.disable_warnings()
git = gitlab.Gitlab(
url=gitlab_url, private_token=gitlab_private_token, ssl_verify=False, per_page=50)
projects = git.projects.list(all=True)
for project in projects:
if project.namespace['kind'] == "user":
print(project.name,project.id,project.web_url)

获取group、member

代码

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
import gitlab
import urllib3

gitlab_url = "https://gitlab.zhaohongye.com"
gitlab_private_token = ""
urllib3.disable_warnings()
git = gitlab.Gitlab(url=gitlab_url, private_token=gitlab_private_token, ssl_verify=False, per_page=50)

groups = git.groups.list(all=True)

data = []
for group in groups:
members = group.members.list(all=True)
group_info = {}
members_info = []
if members:
for member in members:
member_info = {}
member_info['name'] = member.name
member_info['state'] = member.state
member_info['access_level'] = member.access_level
member_info['expires_at'] = member.expires_at
members_info.append(member_info)
group_info['name'] = group.name
group_info['members'] = members_info
group_info['web_url'] = group.web_url
data.append(group_info)
print(data)

效果

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
[{'name': '用户中心',
'members': [{'name': 'wanglixin_yy',
'state': 'active',
'access_level': 50,
'expires_at': None},
{'name': 'yuxuan_yy',
'state': 'active',
'access_level': 40,
'expires_at': None},
{'name': 'wanghongdong_zb',
'state': 'active',
'access_level': 40,
'expires_at': None},
{'name': 'zhaoyali',
'state': 'active',
'access_level': 40,
'expires_at': None},
{'name': 'hexinchun',
'state': 'active',
'access_level': 40,
'expires_at': None},
{'name': 'lunan_yy',
'state': 'active',
'access_level': 40,
'expires_at': None},
{'name': 'jiangxi_bj',
'state': 'active',
'access_level': 40,
'expires_at': None},
{'name': 'hanjixin_yy',
'state': 'active',
'access_level': 40,
'expires_at': None}],
'web_url': 'http://gitlab.zhaohongye.com:10080/groups/user'},
{'name': '财务中心',
'members': [{'name': 'wanglixin_yy',
'state': 'active',
'access_level': 50,
'expires_at': None},
{'name': 'niuzilian_bj',
'state': 'active',
'access_level': 40,
'expires_at': None},
{'name': 'niuzhen',
'state': 'active',
'access_level': 40,
'expires_at': None}],
'web_url': 'http://gitlab.zhaohongye.com:10080/groups/finance'}]

获取project、member写入csv

代码

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
# coding:utf-8

import csv
import sys
import gitlab
import urllib3

gitlab_url = "https://gitlab.zhaohongye.com"
gitlab_private_token = ""
urllib3.disable_warnings()
git = gitlab.Gitlab(url=gitlab_url, private_token=gitlab_private_token, ssl_verify=False, per_page=50)

groups = git.groups.list(all=True)

data = []
for group in groups:
members = group.members.list(all=True)
group_info = {}

if members:
for member in members:
members_info = []
members_info.append(group.name)
members_info.append(group.web_url)
members_info.append(member.name)
members_info.append(member.state)
members_info.append(member.access_level)
members_info.append(member.expires_at)
data.append(members_info)

with open('new.csv','w',encoding='gbk')as f:
f_csv = csv.writer(f)
f_csv.writerows(data)

效果

获取proejct、member写入csv

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
# coding:utf-8

import csv
import sys
import gitlab
import urllib3

gitlab_url = "https://gitlab.zhaohongye.com"
gitlab_private_token = ""
urllib3.disable_warnings()
git = gitlab.Gitlab(url=gitlab_url, private_token=gitlab_private_token, ssl_verify=False, per_page=50)

projects = git.projects.list(all=True)

data = [['project_name','url','maintainer','developer']]
for project in projects:
members = project.members.list(all=True)
project_info = []
project_info.append(project.name)
project_info.append(project.web_url)
if members:
dev_mem = [member.name for member in members if member.access_level == 30 ]
maintainer_mem = [member.name for member in members if member.access_level == 40 ]
project_info.append(maintainer_mem)
project_info.append(dev_mem)
data.append(project_info)

with open('projects2.csv','w',encoding='gbk')as f:
f_csv = csv.writer(f)
f_csv.writerows(data)

配置移除所有人员

代码

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
# 移除权限

import gitlab
import urllib3

gitlab_url = "https://gitlab.zhaohongye.com"
gitlab_private_token = ""
urllib3.disable_warnings()
git = gitlab.Gitlab(
url=gitlab_url, private_token=gitlab_private_token, ssl_verify=False, per_page=50)

data = {
'user_id': 1,
'access_level': 50
}
groups = git.groups.list(all=True)
for group in groups:

# 移除所有其他账号
members = group.members.list(all=True)
mem_ids = [member.id for member in members]
print(group.id, group.name, mem_ids)
if 1 not in mem_ids:
# 增加root账号为owner权限
group.members.create(data=data)
for id in mem_ids:
if id != 1:
group.members.delete(id=id)
print("group_id:{},group_name:{},mem_id:{}".format(
group.id, group.name, id))

projects = git.projects.list(all=True)
for project in projects:
if project.namespace['kind'] == "group":
# 移除所有其他账号
members = project.members.list(all=True)
mem_ids = [member.id for member in members]
print(project.id, project.name, mem_ids)
if members:
for id in mem_ids:
if id != 1:
project.members.delete(id=id)
print("project_id:{},project_name:{},mem_id:{}".format(
project.id, project.name, id))

project下人员权限升级

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
# 权限修改:将负责权限进行修改,developer改为maintainer

import gitlab
import urllib3

manger_user_id = [12, 110, 22, 11, 27, 20, 13]

gitlab_url = "https://gitlab.zhaohongye.com"
gitlab_private_token = ""
urllib3.disable_warnings()
git = gitlab.Gitlab(
url=gitlab_url, private_token=gitlab_private_token, ssl_verify=False, per_page=50)

projects = git.projects.list(all=True)
for project in projects:
if project.namespace['kind'] == "group":
members = project.members.list(all=True)
mem_ids = [member.id for member in members]
print(project.id, project.name, mem_ids)
if members:
for id in mem_ids:
if id in manger_user_id:
# 判断是否是maintainer权限
if project.members.get(id=id).access_level != 40:
project.members.delete(id=id)
data = {
'user_id': id,
'access_level': 40
}
project.members.create(data=data)
print('已将账号id为:{}的账号权限升级'.format(id))