透過 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
- 拼多多商品資料如何透過api介面獲取API
- 透過Docker Compose部署GitLab和GitLab Runner(一)DockerGitlab
- 為什麼要透過API介面來獲取資料API
- PHP 獲取程式碼執行時間和消耗的記憶體PHP記憶體
- 透過DNS TXT記錄執行powershellDNS
- svn專案遷移至gitlab流程(保留提交記錄)Gitlab
- 獲取 CPU資訊,並透過登錄檔寫入
- 正規表示式提取 git 提交記錄中的新增程式碼行Git
- 透過歷史記錄執行本地模型模型
- 程式碼提交過程
- gitLab進行CI配置記錄Gitlab
- 透過API介面獲取阿里巴巴商品詳情示例說明API阿里
- 新技能:透過程式碼快取加速 Node.js 的啟動快取Node.js
- 介面測試--獲取動態引數進階
- PHP獲取小程式openid,10行程式碼快速獲取小程式openidPHP行程
- 獲取當前修改的行記錄資料
- PbootCMS使用者提交表單和調取表單記錄boot
- 動態路由,透過id改變,改頁面路由
- 如何透過AST樹去獲取JS函式引數名ASTJS函式
- Oracle 獲取整數方式程式碼整理Oracle
- 怎麼透過Python獲取檔案指定行的內容?Python
- 透過Python SDK 獲取tushare資料Python
- 瞭解直播帶貨系統原始碼,透過html5程式碼獲取位置資訊原始碼HTML
- Docker+Jenkins+Pipline如何獲取git外掛環境變數(提交sha、分支等)以及Jenkinsfile中獲取sh執行結果(獲取git最近提交資訊)DockerJenkinsGit變數
- Jenkins + GitLab 通過 Webhook 自動觸發構建爬坑記錄JenkinsGitlabWebHook
- python獲取命令列引數的程式碼Python命令列
- Django透過request獲取客戶端IPDjango客戶端
- MySql先分組統計總記錄數,再獲取記錄數中的最大值MySql
- 草船借箭:透過ssh蜜罐來獲取常用密碼本密碼
- 為了獲取網路介面卡資訊、刪除 DNS 快取和 IP 地址,並透過 ipconfig /renew6 自動獲取 IPv6 地址,可以使用以下步驟和命令。DNS快取
- IDEA如何檢視每一行程式碼的提交記錄(人員,時間)Idea行程
- 介面測試中獲取的 token 值如何透過 des3 加密輸出S3加密
- Docker 安裝 gitlab TortoiseGit 拉取程式碼DockerGitlab
- Gitlab整合Sonarqube自動檢測程式碼併傳送報告給提交者Gitlab
- Git的修改提交記錄和變基Git
- Gitlab 實現倉庫完全遷移,包括所有提交記錄、分支、標籤Gitlab
- Laravel 分組獲取最新記錄Laravel