统计gitlab代码仓库开发人员代码贡献数量
1. 背景
快年底了,领导需要统计每个项目代码开发人员不定时间内的代码贡献数, 并自动导出Excel表。能快速知道哪些开发人员一天一天的在摸鱼
2. 分析需求, 上代码开撸
# coding=utf-8 # author: zhangzihong import time import gitlab import collections import pandas as pd gl = gitlab.Gitlab('http://code.xxx.com/gitlab',private_token='ve9ifEu9ZtAeqWik',timeout=50, api_version='4') start_time = '2020-10-16T00:00:00Z' end_time = '2020-11-16T23:00:00Z' def get_gitlab(): """ gitlab API """ list2 = [] projects=gl.projects.list(owned=True, all=True) num=0 for project in projects: num+=1 print("查看了%d个项目" %num) for branch in project.branches.list(): commits=project.commits.list(all=True,query_parameters={'since': start_time,'until':end_time, 'ref_name': branch.name}) for commit in commits: com = project.commits.get(commit.id) pro={} try: #print(project.path_with_namespace,com.author_name,com.stats["total"]) pro["projectName"]=project.path_with_namespace pro["authorName"]=com.author_name pro["branch"]=branch.name pro["additions"]=com.stats["additions"] pro["deletions"]=com.stats["deletions"] pro["commitNum"]=com.stats["total"] list2.append(pro) except : print("有错误, 请检查") return list2 def data(): """ 数据去重 key split """ ret = {} for ele in get_gitlab(): key = ele["projectName"]+ele["authorName"]+ele["branch"] 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("authorName") 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("./gitlab.csv")
结果: