统计开发人员上传的代码量,从另一维度判断工作饱和度
import time
import gitlab
import collections
import pandas as pd
gl = gitlab.Gitlab('http://gilab.com(git页面地址))', private_token='sfasdf--asfavvz(去gitlab用户设置里令牌访问下生成token)', timeout=60, api_version='4')
start_time = '2024-07-01T00:00:00Z' ##起始时间
end_time = '2024-07-31T23:00:00Z' ##结束时间
def get_gitlab():
"""
gitlab API
"""
list2 = []
projects = gl.projects.list(all=True)
num = 0
branch001 = 0
for project in projects:
num += 1
print("查看了%d个项目" % num)
# print(project.name)
for branch in project.branches.list(get_all=True):
commits = project.commits.list(all=True, query_parameters={'since': start_time, 'until': end_time, 'ref_name': branch.name})
# print(branch.name)
for commit in commits:
print(branch.name)
print(commit.committed_date) # commit.created_at
com = project.commits.get(commit.id)
pro = {}
try:
print(project.path_with_namespace,com.committer_name,com.stats["total"])
pro["projectName"] = project.path_with_namespace
# pro["author_Username"] = com.author_username
pro["authorName"] = com.committer_name
pro["email"] = com.committer_email
pro["branch"] = branch.name
pro["additions"] = com.stats["additions"]
pro["deletions"] = com.stats["deletions"]
pro["commitNum"] = com.stats["total"]
list2.append(pro)
# print(com.stats)
except:
print("有错误, 请检查")
return list2
def data():
"""
数据去重
key split
"""
ret = {}
for ele in get_gitlab():
key = ele["projectName"] + ele["authorName"] + ele["branch"] + ele["email"]
if key not in ret:
ret[key] = ele
ret[key]["commitTotal"] = 1
else:
ret[key]["additions"] += ele["additions"]
ret[key]["deletions"] += ele["deletions"]
ret[key]["commitNum"] += ele["commitNum"]
ret[key]["commitTotal"] += 1
list1 = []
for key, v in ret.items():
v["项目名"] = v.pop("projectName")
# v["用户名"] = v.pop("authorUsername")
v["姓名"] = v.pop("authorName")
v["邮箱"] = v.pop("email")
v["分支"] = v.pop("branch")
v["添加代码行数"] = v.pop("additions")
v["删除代码行数"] = v.pop("deletions")
v["提交总行数"] = v.pop("commitNum")
v["提交次数"] = v["commitTotal"]
list1.append(v)
print(list1)
return list1
def csv(csvName):
"""
csv
"""
df = pd.DataFrame(data(), columns=["项目名", "姓名", "邮箱", "分支", "添加代码行数", "删除代码行数", "提交总行数", "提交次数"])
df.to_csv(csvName, index=False, encoding="utf_8_sig")
if __name__ == "__main__":
csv("./gitlab20240730.xls") #放到当前目录下的位置