1.调试代码实现
import gitlab
import os
import time
import xlrd
import xlwt
from xlutils.copy import copy
import time
import sys
basedir = 'D:\\gitlab_codestat\\'
xls_path = basedir + '20200219-代码行数统计结果.xls'
project_name = []
branch_name = []
name = []
department = []
commit_time = []
add_line = []
delete_line = []
total_line = []
commit_id = []
count = 0
start_time='2020-02-18'
end_time='2020-02-20'
def codeStat():
global depart_1, depart_2, depart_3
count = 0
client = gitlab.Gitlab('http://gitlab.xxx.com.cn/', private_token='xxxxxxx', timeout=1800,
api_version='4')
client.auth()
project = client.projects.list(all=True)
users = client.users.list(all=True)
for user in users:
if user.name == 'jet':
#print(user)
count += 1
events = user.events.list(all=True,action='pushed',after=start_time,before=end_time)
#events = user.events.list(all=True,action='pushed',after='2020-02-06',before='2020-02-08')
for eve in events:
print(eve)
commits = eve.data['commits']
#print(commits)
commit_len = len(commits)
#print(commit_len)
if len(commits) > 0:
for i in range(commit_len):
#if 'uat' not in str(eve.data['ref'].split('/')[2]) or 'test' not in str(eve.data['ref'].split('/')[2]):
if str(eve.data['ref'].split('/')[2]) not in ['uat','test']:
if 'Merge' not in commits[i].get('message'):
user_information = user.identities
if str(user_information[0].get('extern_uid').split(',')[0].lstrip('CN=')) != 'gitlab':
if start_time < commits[i].get('timestamp').split('T')[0] < end_time:
#if eve.data['user_name'] == commits[i]['author']['name']:
project_name.append(eve.data['repository']['name'])
branch_name.append(eve.data['ref'].split('/')[2])
name.append(user_information[0].get('extern_uid').split(',')[0].lstrip('CN='))
depart = user_information[0].get('extern_uid').split(',')
d_pt = []
for dept in depart:
if "OU=" in dept and dept != "OU=xxxxxxx" and dept != "OU=xxxxx":
d_pt.append(dept.lstrip('OU='))
dpt = '-'.join(d_pt)
#department.append(str(user_information[0].get('extern_uid').split(',')[1].lstrip('OU=')) + '-' + str(user_information[0].get('extern_uid').split(',')[2].lstrip('OU=')) + '-' + str(user_information[0].get('extern_uid').split(',')[3].lstrip('OU=')))
department.append(dpt)
project_id = str(eve.project_id)
for pro in project:
if str(pro.id) == project_id:
commit_id.append(commits[i].get('id')[0:9])
info = pro.commits.get(commits[i].get('id'))
print(info)
c_time = info.committed_date.split('.')[0].split('T')[0]
commit_time.append(c_time)
add_line.append(info.stats['additions'])
delete_line.append(info.stats['deletions'])
total_line.append(info.stats['total'])
#else:
#continue
#print(count)
if __name__ == '__main__' :
startTime = time.perf_counter()
codeStat()
if os.path.exists(xls_path):
os.remove(xls_path)
workbook = xlwt.Workbook(encoding='utf-8') # 新建一个工作簿
sheet = workbook.add_sheet('all') # 在工作簿中新建一个表格
row0 = ["项目名", "分支名","commit_id","提交者","部门","提交时间", "增加行数","删除行数","总计","备注"]
for row in range(len(row0)):
sheet.write(0, row, row0[row])
pro = i = j = k = l = m = n = w = commit_int = branch_i = 1
for p_name in project_name:
sheet.write(pro, 0, p_name)
pro += 1
for branch in branch_name:
sheet.write(branch_i, j, branch) # 像表格中写入数据(对应的行和列)
branch_i += 1
for id in commit_id:
sheet.write(commit_int, j + 1, id)
commit_int += 1
for commiter in name:
sheet.write(i, j + 2, commiter)
i += 1
for depart in department:
depart = depart.rstrip(',DC=xxxxx,DC=com,DC=cn')
sheet.write(w, j + 3, depart)
w += 1
for c_time in commit_time:
sheet.write(k, j + 4, c_time)
k += 1
for addd in add_line:
sheet.write(l, j + 5, addd)
l += 1
for delete_stat in delete_line:
sheet.write(m, j + 6, delete_stat)
m += 1
for t_stat in total_line:
sheet.write(n, j + 7, t_stat)
n += 1
workbook.save(xls_path)
print('Done! Cost Time: %0.2f second' % (time.perf_counter() - startTime))```
2.优化后结构
```# coding=utf-8
import datetime
import json
import operator
import time
import requests
import gitlab
#bk_url = "http://xxxx.xxxxx.com.cn:8000/api/auto/git/data/"
bk_url = "https://xxxx.xxxxx.com.cn/o/xxxxx/api/auto/git/data/"
def date_range(start_time, end_time):
dates = []
dt = datetime.datetime.strptime(start_time, "%Y-%m-%d")
date = start_time[:]
while date <= end_time:
dates.append(date)
dt = dt + datetime.timedelta(1)
date = dt.strftime("%Y-%m-%d")
return dates
def code_stat(start_time, end_time):
total_list = []
client = gitlab.Gitlab('http://gitlab.xxxxxx.com.cn/', private_token='xxxxxxxx', timeout=1800,
api_version='4')
time_list = date_range(start_time, end_time)
client.auth()
project = client.projects.list(all=True)
users = client.users.list(all=True)
for user in users:
# 用户ID
print(user.name)
user_id = user.id
events = user.events.list(all=True, action='pushed', after=start_time, before=end_time)
user_information = user.identities
for eve in events:
commits = eve.data['commits']
# 分支名
branch_name = eve.data['ref'].split('/')[2]
if len(commits) > 0 and branch_name not in ['uat', 'test']:
for i in range(len(commits)):
# 提交者
name = user_information[0].get('extern_uid').split(',OU')[0].lstrip('CN=')
if name != 'gitlab' and 'Merge' not in commits[i].get('message'):
# 部门
#print(user_information[0].get('extern_uid'))
d_pt = []
depart = user_information[0].get('extern_uid').split(',')
print(depart)
for dept in depart:
if "OU=" in dept and dept != "OU=xxxxxx" and dept != "OU=xxxxxxx":
d_pt.append(dept.lstrip('OU='))
dpt = '-'.join(d_pt)
department = dpt
#department = str(user_information[0].get('extern_uid').split(',')[1].lstrip('OU=')) + '-' \
# + str(user_information[0].get('extern_uid').split(',')[2].lstrip('OU=')) + '-' \
# + str(user_information[0].get('extern_uid').split(',')[3].lstrip('OU='))
print(department)
# 项目名
project_name = eve.data['repository']['name']
# 项目id
project_id = eve.project_id
for pro in project:
if pro.id == project_id:
info = pro.commits.get(commits[i].get('id'))
# commit_id
commit_id = commits[i].get('id')[0:9]
# 提交时间
c_time = info.committed_date.split('.')[0].split('T')[0]
# 增加行数
add_line = info.stats['additions']
# 删除行数
delete_line = info.stats['deletions']
# 总行数
total_line = info.stats['total']
if int(total_line) != 0 and c_time in time_list:
total_list.append({"user_id": user_id, "project_id": project_id,
"project_name": project_name, "submission": name,
"department": department, "commit_time": c_time,
"add_rows": add_line, "del_rows": delete_line,
"total": total_line, "git_branch": branch_name,
"commit_id": commit_id})
sorted_total = sorted(total_list, key=operator.itemgetter('commit_time'))
return sorted_total
if __name__ == '__main__':
res = code_stat('2020-02-28', '2020-03-01')
print(len(res))
lent = len(res) / 100
print(lent)
for i in range( int(lent) + 1 ):
data = res[i * 100:(i + 1) * 100]
if data:
headers = {'Content-Type': 'application/json'}
try:
data = {"data_list": data}
resp = requests.post(bk_url, data=json.dumps(data), headers=headers)
if resp.status_code == 200:
print(resp.text)
except Exception as e:
print(e)
time.sleep(1)```