gitlab Api接口使用
官方文档
https://docs.gitlab.com/search/?q=api&idx=gitlab&p=1
示例:获取每个项目下的用户信息
#!/usr/bin/env python #-*-coding:utf-8-*- import requests url = 'http://x.x.x.x/api/v3/projects?private_token=oMJwN5ErC8_n1QvTsyDR&per_page=50' #private_token为必须项,这里为获取所有的项目信息 user_url= 'http://x.x.x.x/api/v3/projects/{}/users?private_token=oMJwN5ErC8_n1QvTsyDR&per_page=100' #获取每个项目下的用户信息 #http://10.10.10.217/api/v3/projects/45/users?private_token=oMJwN5ErC8_n1QvTsyDR #获取项目id为45的信息
#获取项目id和项目名称 def GetProject_id(project_url): r = requests.get(project_url) data = r.json() ProjectId_list = [] ProjectName_list = [] for i in data: ProjectId_list.append(i['id']) ProjectName_list.append(i['name']) return ProjectId_list,ProjectName_list
#根据项目id获取项目下的用户信息 def GetProject_userlist(): IdList = GetProject_id(url) project_id = IdList[0] project_name = IdList[1] for id in project_id: l = [] project_user = requests.get(user_url.format(id)) #生成完整的用于显示项目下所有user的连接 req_data = project_user.json() for i in req_data: l.append(i['name']) print (project_name[project_id.index(id)],l) GetProject_userlist()
执行结果:项目名称,用户列表
示例:获取某些项目是否提交代码以及提交时间
#-*-coding:utf-8-*- import requests import re url = 'http://10.10.10.217/api/v3/projects?private_token=oMJwN5ErC8_n1QvTsyDR&per_page=50' r =requests.get(url) p_group = ['HJ'] data = r.json() print ("项目名称",' '*20,'最近提交时间') for i in data: if i['ssh_url_to_repo'].split(':')[1].split('/')[0] in p_group: r1 = requests.get( 'http://x.x.xx.x/api/v3/projects/%d/repository/commits/master?private_token=xxxxxxxxxxxxxxxxx' % i['id']) data2 = r1.json() if data2['message'].strip() == '404 Commit Not Found': print (i['ssh_url_to_repo'].split(':')[1],' '*11,'未提交任何代码') else: print(i['ssh_url_to_repo'].split(':')[1], ' ' * 11, data2['committed_date'][:10])