一、生成Personal Access Tokenspython
選擇右上角用戶信息setting—>Access Tokensjson
二、經常使用Gitlab APIapi
#獲取全部的項目信息app
#private_token來自Access Tokensssh
http://192.168.199.184/api/v3/projects?private_token=gqv1hvjbGCLs6uAUmBV8&per_page=10curl
#獲取項目信息url
#15 爲項目ID,來自全部的項目信息spa
http://192.168.199.184/api/v3/projects/15 code
#查看用戶信息orm
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
三、實戰案例
獲取項目信息
#!/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])