教你怎么调用Gitlab API
程序员文章站
2022-03-04 08:53:02
1、生成Personal Access Tokens 选择右上角用户信息setting—>Access Tokens 2、常用Gitlab API #获取所有的项目信息 #private_token来自Access Tokens http://192.168.199.184/api/v3/proje ......
1、生成Personal Access Tokens
选择右上角用户信息setting—>Access Tokens
2、常用Gitlab API
#获取所有的项目信息
#private_token来自Access Tokens
http://192.168.199.184/api/v3/projects?private_token=gqv1hvjbGCLs6uAUmBV8&per_page=10
#获取项目信息
#15 为项目ID,来自所有的项目信息
http://192.168.199.184/api/v3/projects/15
#查看用户信息
http://192.168.199.184/api/v3/projects/15/users?private_token=gqv1hvjbGCLs6uAUmBV8&per_page=10
#获取commits提交信息
#15 为项目ID,来自所有的项目信息
http://192.168.199.184/api/v3/projects/15/repository/commits/master?private_token=gqv1hvjbGCLs6uAUmBV8&per_page=10
3、实战案例
获取项目信息
#!/usr/bin/env python #-*-coding:utf-8-*- import requests url = 'http://192.168.199.184/api/v3/projects?private_token=oMJwN5ErC8_n1QvTsyDR&per_page=50' user_url= 'http://192.168.199.184/api/v3/projects/15/users?private_token=oMJwN5ErC8_n1QvTsyDR&per_page=10' #获取项目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://192.168.199.184/api/v3/projects?private_token=oMJwN5ErC8_n1QvTsyDR&per_page=10' 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://192.168.199.184/api/v3/projects/15/repository/commits/master?private_token=gqv1hvjbGCLs6uAUmBV8&per_page=10' % 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])
推荐阅读
-
【Python进阶】无论API怎么变,SDK都可以根据URL实现完全动态的调用
-
手把手教你调用百度人脸识别API
-
教你怎么用SpringBoot整合Swagger作为API
-
javascript - google地图api怎么调用它的测距功能
-
请问一下,laravel框架,怎么做好接口api,调用频率及敏感请求参数的监控?
-
android - 怎么用PHP开发API供其他客户端调用?
-
php调用百度百科api实现查询功能的代码实现,该怎么处理
-
android - 怎么用PHP开发API供其他客户端调用?
-
请问这个 sina 的 api 用php 怎么调用呢?
-
请问一下,laravel框架,怎么做好接口api,调用频率及敏感请求参数的监控?