通過 gitlab 介面獲取程式碼提交記錄和改動行數
之前是通過讀取jenkins的快取來獲取的,後面發現了個更好的方式,官方有http請求提供出來。無開始時間,只獲取前10條資料,有開始時間,獲取到最新的資料。不需要把程式碼拉下來,直接請求遠端git。
get_git_all_url = 'http://git.xxxxxx.com/api/v4/projects/'
get_project_branch_url = get_git_all_url + '{}/repository/branches'
get_branch_commit_url = get_git_all_url + '{}/repository/commits'
get_commit_msg_url = get_git_all_url + '{}/repository/commits/{}'
base_token = {'private_token': 'xxxxx'}
def get_git_project_id(private_token, project_repo_url):
"""
:param private_token: gitlab個人token,請在gitlab上生產
:param project_repo_url: http://xxxx.git,需要帶上.git
:return:
"""
if all([private_token, project_repo_url]):
params_dict = {'private_token': private_token, 'per_page': 100}
for i in range(1, 101):
params_dict['page'] = i
response = requests.get(url=get_git_all_url, params=params_dict)
print(response.text)
if response.status_code == 200:
msg_dict = json.loads(response.text)
for j in range(len(msg_dict)):
msg = msg_dict[j]
if 'http_url_to_repo' in msg.keys() and msg['http_url_to_repo'] == project_repo_url:
if 'id' in msg.keys():
project_id = str(msg['id'])
print('查詢到該token{}名下對git地址{}的專案id為{}'.format(private_token, project_repo_url, project_id))
return project_id
print('未查詢到該token{}名下git地址{}的專案id'.format(private_token, project_repo_url))
return
def get_git_project_branchs(private_token, git_project_id):
"""
:param private_token: gitlab個人token,請在gitlab上生產
:param git_project_id: 字串型別,專案id
:return:
"""
branch_list = []
if all([private_token, git_project_id]):
param_dict = {'private_token': private_token, 'per_page': 100}
for i in range(1, 101):
param_dict['page'] = i
response = requests.get(url=get_project_branch_url.format(git_project_id), params=param_dict)
print(response.text)
if len(response.text)<=2:
break
elif response.status_code == 200:
msg_dict = json.loads(response.text)
for j in range(len(msg_dict)):
if 'name' in msg_dict[j].keys():
if not msg_dict[j]['name']:
break
branch_list.append(msg_dict[j]['name'])
print('該專案id:{}對應的所有分支:{}'.format(git_project_id,str(branch_list)))
return branch_list
def get_branch_commit(private_token, git_project_id, branch_name, begin_time=None):
"""
:param private_token: gitlab的token
:param git_project_id: git專案id
:param branch_name: 分支名稱
:param begin_time: 從這開始之後的commit時間
:return:
"""
commit_list = []
if all([private_token, git_project_id, branch_name]):
para_dict = {'private_token': private_token, 'ref_name': branch_name}
if not begin_time:
para_dict['per_page'] = 10
response = requests.get(url=get_branch_commit_url.format(git_project_id), params=para_dict)
print(response.text)
if response.status_code == 200:
msg_dict = json.loads(response.text)
for i in range(len(msg_dict)):
if 'id' in msg_dict[i].keys():
commit_id = msg_dict[i]['id']
commit_list.append(commit_id)
else:
para_dict['per_page'] = 100
for i in range(1, 101):
para_dict['page'] = i
response = requests.get(url=get_branch_commit_url.format(git_project_id), params=para_dict)
if response.status_code == 200:
msg_dict = json.loads(response.text)
for j in range(len(msg_dict)):
# 這裡有三個時間,不確定是哪個,先全部拿下來,暫時用committed_data_time
id = msg_dict[j]['id']
authored_data_time = msg_dict[j]['authored_date']
committed_data_time = msg_dict[j]['committed_date']
created_at_time = msg_dict[j]['created_at']
if compare_time(committed_data_time, begin_time):
commit_list.append(id)
print('獲取到分支{}對應的commit內容{}'.format(branch_name,str(commit_list)))
return commit_list
def get_commit_msg(private_token, git_project_id, commit_id):
commit_msg_dict = {}
if all([private_token, git_project_id, commit_id]):
para_dict = {'private_token': private_token}
response = requests.get(url=get_commit_msg_url.format(git_project_id, commit_id), params=para_dict)
print(response.text)
if response.status_code == 200:
msg_dict = json.loads(response.text)
commit_msg_dict['id'] = msg_dict['id']
commit_msg_dict['title'] = msg_dict['title']
if 'feat' in msg_dict['title'].lower():
commit_msg_dict['git_type']='feat'
elif 'fix' in msg_dict['title'].lower():
commit_msg_dict['git_type'] = 'fix'
elif 'merge' in msg_dict['title'].lower():
commit_msg_dict['git_type'] = 'merge'
else:
commit_msg_dict['git_type'] = 'update'
commit_msg_dict['message'] = msg_dict['message']
commit_msg_dict['author_name'] = msg_dict['author_name']
commit_msg_dict['committed_date'] = msg_dict['committed_date']
commit_msg_dict['additions'] = msg_dict['stats']['additions']
commit_msg_dict['deletions'] = msg_dict['stats']['deletions']
commit_msg_dict['total'] = msg_dict['stats']['total']
return commit_msg_dict
def get_gitProject_branch_commit_list(private_token, project_repo_url, branch, begin_time=None):
commit_list = []
try:
git_project_id = get_git_project_id(private_token=private_token,
project_repo_url=project_repo_url)
if git_project_id:
git_project_branch = get_git_project_branchs(private_token=private_token,
git_project_id=git_project_id)
if git_project_branch and branch in git_project_branch:
branch_commit_list = get_branch_commit(private_token=private_token,
git_project_id=git_project_id,
branch_name=branch,
begin_time=begin_time)
if branch_commit_list:
for i in range(len(branch_commit_list)):
commit_msg_dict = get_commit_msg(private_token=private_token,
git_project_id=git_project_id,
commit_id=branch_commit_list[i])
if commit_msg_dict:
commit_list.append(commit_msg_dict)
else:
print('該分支{}不在遠端分支列表中{},或獲取到的遠端分支為空'.format(branch, git_project_branch))
print('獲取到該專案地址{},分支{}的commit內容為{}'.format(project_repo_url,branch,commit_list))
except Exception as e:
print('獲取該專案地址{},分支{}的commit內容過程中發生異常'.format(project_repo_url,branch))
print('異常資訊為:{}'.format(e))
return commit_list
def compare_time(time1, time2_compare):
if any(key in time1 for key in ['T','+08:']):
time1=time1.replace('T',' ').replace('+08:','')
d1 = datetime.datetime.strptime(time1, '%Y-%m-%d %H:%M:%S.%f')
d2 = datetime.datetime.strptime(time2_compare, '%Y-%m-%d %H:%M:%S.%f')
delta = d1 - d2
print(delta.total_seconds())
if delta.total_seconds()>=0:
return True
else:
return False
# get_gitProject_branch_commit_list(private_token='xxxxx',
# project_repo_url='http://xxxxx.git',
# branch='dev',
# begin_time='2020-10-30 14:56:07.999999')
相關文章
- JB的測試之旅-使用gitlab ci獲取提交記錄Gitlab
- 通過EPROCESS獲取程式名
- 通過滑鼠拖動選取指定數字程式碼例項
- Delphi通過IE視窗控制程式碼獲取網頁介面(IWebBrowser2)網頁Web
- 【Tips】獲取結果集中偶數行記錄
- javascript通過class屬性獲取元素的程式碼JavaScript
- org.reflections 介面通過反射獲取實現類原始碼研究反射原始碼
- Jenkins + GitLab 通過 Webhook 自動觸發構建爬坑記錄JenkinsGitlabWebHook
- JavaScript獲取奇數行或者偶數行程式碼JavaScript行程
- 通過Java反射動態獲取資訊Java反射
- PHP 獲取程式碼執行時間和消耗的記憶體PHP記憶體
- Delphi自動提交網頁表單和獲取框架網頁原始碼網頁框架原始碼
- svn專案遷移至gitlab流程(保留提交記錄)Gitlab
- 歷史上的今天獲取介面程式碼
- 通過行為引數化傳遞程式碼
- 正規表示式提取 git 提交記錄中的新增程式碼行Git
- Android逆向之旅---靜態方式破解微信獲取聊天記錄和通訊錄資訊Android
- js獲取數字和字母隨機數程式碼例項JS隨機
- 介面測試--獲取動態引數進階
- 程式碼提交過程
- 通過PowerShell獲取Windows系統密碼HashWindows密碼
- 通過gitHub獲取vlc-Android原始碼GithubAndroid原始碼
- gitLab進行CI配置記錄Gitlab
- Java——通過反射獲取函式引數名稱Java反射函式
- 反射-通過反射獲取成員變數並使用反射變數
- TP5 獲取資料集記錄數
- PHP獲取程式碼段執行的毫秒時間和消耗記憶體PHP記憶體
- 獲取當前修改的行記錄資料
- iOS 通過runtime獲取某個類中所有的變數和方法iOS變數
- 通過反射獲取類的類名,方法和內部成員變數反射變數
- 【SQL】通過對分析使用者快速獲得資料庫中表記錄數SQL資料庫
- Oracle 獲取整數方式程式碼整理Oracle
- PbootCMS使用者提交表單和調取表單記錄boot
- flutter 總結: 通過 GlobalKey 獲取介面任意元素座標尺寸Flutter
- GitLab 通過Push操作提交專案,GitLab部分頁面訪問出現500錯誤Gitlab
- C#通過反射獲取類中的方法和引數個數,反射呼叫方法帶引數C#反射
- JavaScript 通過class獲取元素物件JavaScript物件
- 通過用shellcode獲取shell