一、基本概述
gitlab后台默认不自带代码仓库贡献指标统计功能。但其提供相关仓库api可以进行仓库、提交次数、提交代码量等统计。
二、实现方法
1、后台管理员获取个人访问令牌,就是一段字符串,确保该后台管理员账号具有最高权限。
2、使用python调用gitlab的API接口获取数据进行统计
# coding=utf-8 # author: ttfde import time import gitlab import collections import pandas as pd gl = gitlab.Gitlab('http://xxxxxxx/这里填写根访问地址', private_token='个人访问令牌', timeout=50, api_version='4') start_time = '2020-10-16T00:00:00Z' #扫描起始时间 end_time = '2024-06-30T23: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) try: 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("有错误, 请检查") 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")
3、将数据生成csv
- 本文固定链接: http://www.ttfde.top/index.php/post/437.html
- 转载请注明: admin 于 TTF的家园 发表
《本文》有 0 条评论