当前位置:   article > 正文

按用户,时间统计gitlab代码量_gitlab代码行数分用户统计

gitlab代码行数分用户统计

python代码,详情如下:


 

  1. #!/usr/bin/python3
  2. # coding=utf8
  3. import json
  4. import requests
  5. import datetime
  6. from dateutil.parser import parse
  7. gitlab_url = "https://www.xxxx.cn/" # GitLab 地址
  8. private_token = "glpat-123456" # GitLab Access Tokens(管理员权限)
  9. get_total_branche_name = "sit" # 需要统计的分支
  10. info = []
  11. project_info = dict()
  12. headers = {
  13. 'Connection': 'close',
  14. }
  15. # UTC时间转时间戳
  16. def utc_time(time):
  17. dt = parse(time)
  18. return int(dt.timestamp())
  19. # 输出格式化
  20. def str_format(txt):
  21. lenTxt = len(txt)
  22. lenTxt_utf8 = len(txt.encode('utf-8'))
  23. size = int((lenTxt_utf8 - lenTxt) / 2 + lenTxt)
  24. length = 20 - size
  25. return length
  26. # 获取 GitLab 上的所有项目
  27. def gitlab_projects():
  28. project_ids = []
  29. project_name = []
  30. page = 1
  31. while True:
  32. url = gitlab_url + "api/v4/projects/?private_token=" + private_token + "&page=" + str(page) + "&per_page=20"
  33. # print(url)
  34. while True:
  35. try:
  36. res = requests.get(url, headers=headers, timeout=10)
  37. break
  38. except Exception as e:
  39. continue
  40. projects = json.loads(res.text)
  41. if len(projects) == 0:
  42. break
  43. else:
  44. for project in projects:
  45. if project["id"] > 1:
  46. project_ids.append(project["id"])
  47. project_name.append(project["name"])
  48. project_info[project["id"]] = project["name"]
  49. page += 1
  50. print(project_info)
  51. return project_ids
  52. # 获取 GitLab 上的项目 id 中的分支
  53. def project_branches(project_id):
  54. branch_names = []
  55. page = 1
  56. while True:
  57. url = gitlab_url + "api/v4/projects/" + str(
  58. project_id) + "/repository/branches?private_token=" + private_token + "&page=" + str(page) + "&per_page=20"
  59. while True:
  60. try:
  61. res = requests.get(url, headers=headers, timeout=10)
  62. break
  63. except Exception as e:
  64. print(e)
  65. continue
  66. branches = json.loads(res.text)
  67. '''Debug
  68. print(url)
  69. print('--' * 10)
  70. print(branches)
  71. print('*' * 10)
  72. '''
  73. if len(branches) == 0:
  74. break
  75. else:
  76. for branch in branches:
  77. branch_names.append(branch["name"])
  78. page += 1
  79. return branch_names
  80. # 获取 GitLab 上的项目分支中的 commits,当 title 或 message 首单词为 Merge 时,表示合并操作,剔除此代码量
  81. def project_commits(project_id, branch, start_time, end_time):
  82. commit_ids = []
  83. page = 1
  84. while True:
  85. url = gitlab_url + "api/v4/projects/" + str(
  86. project_id) + "/repository/commits?ref_name=" + branch + "&private_token=" + private_token + "&page=" + str(
  87. page) + "&per_page=20"
  88. while True:
  89. try:
  90. res = requests.get(url, headers=headers, timeout=10)
  91. break
  92. except Exception as e:
  93. print(e)
  94. continue
  95. commits = json.loads(res.text)
  96. if len(commits) == 0:
  97. break
  98. else:
  99. for commit in commits:
  100. # print(commit)
  101. if "Merge" in commit["title"] or "Merge" in commit["message"] or "合并" in commit["title"] or "合并" in \
  102. commit["message"]: # 不统计合并操作
  103. continue
  104. # 不按时间段统计,统计所有时间的;
  105. # 如果需要按时间段查询,开启下面注释
  106. # elif utc_time(commit["authored_date"]) < utc_time(start_time) or utc_time(
  107. # commit["authored_date"]) > utc_time(end_time): # 不满足时间区间
  108. # continue
  109. else:
  110. commit_ids.append(commit["id"])
  111. page += 1
  112. return commit_ids
  113. # 根据 commits 的 id 获取代码量
  114. def commit_code(project_id, commit_id):
  115. global info
  116. url = gitlab_url + "api/v4/projects/" + str(
  117. project_id) + "/repository/commits/" + commit_id + "?private_token=" + private_token
  118. while True:
  119. try:
  120. res = requests.get(url, headers=headers, timeout=10)
  121. break
  122. except Exception as e:
  123. print(e)
  124. continue
  125. data = json.loads(res.text)
  126. # print(res.text)
  127. temp = {"name": data["author_name"], "additions": data["stats"]["additions"],
  128. "deletions": data["stats"]["deletions"], "total": data["stats"]["total"]} # Git工具用户名,新增代码数,删除代码数,总计代码数
  129. info.append(temp)
  130. # GitLab 数据查询
  131. def gitlab_info(start_time, end_time):
  132. for project_id in gitlab_projects(): # 遍历所有项目ID
  133. print(f"项目信息:id={project_id},name={project_info[project_id]}")
  134. for branche_name in project_branches(project_id): # 遍历每个项目中的分支
  135. if branche_name == get_total_branche_name:
  136. for commit_id in project_commits(project_id, get_total_branche_name, start_time,
  137. end_time): # 遍历每个分支中的 commit id
  138. commit_code(project_id, commit_id) # 获取代码提交量
  139. else:
  140. continue
  141. if __name__ == "__main__":
  142. startTime = datetime.datetime.now()
  143. print(f"正在统计数据,请耐心等待,开始时间:{startTime}")
  144. gitlab_info('2000-01-01 00:00:00', '2023-06-01 23:59:59') # 起-止时间
  145. name = [] # Git工具用户名
  146. additions = [] # 新增代码数
  147. deletions = [] # 删除代码数
  148. total = [] # 总计代码数
  149. res = {}
  150. # 生成元组
  151. for i in info:
  152. for key, value in i.items():
  153. if key == "name":
  154. name.append(value)
  155. if key == "additions":
  156. additions.append(value)
  157. if key == "deletions":
  158. deletions.append(value)
  159. if key == "total":
  160. total.append(value)
  161. data = list(zip(name, additions, deletions, total))
  162. # 去重累加
  163. for j in data:
  164. name = j[0]
  165. additions = j[1]
  166. deletions = j[2]
  167. # total = j[3] # 返回的行数是添加行数+删除行数的总和,gitlab平台修改的总行数
  168. total = int(j[1]) - int(j[2]) # 计算代码实际行数值,添加行数-删除行数
  169. if name in res.keys():
  170. res[name][0] += additions
  171. res[name][1] += deletions
  172. res[name][2] += total
  173. else:
  174. res.update({name: [additions, deletions, total]})
  175. # 打印结果
  176. print("Git用户名\t新增代码数\t删除代码数\t总计代码数", end="")
  177. print()
  178. for k in res.keys():
  179. print(k + "\t" + str(res[k][0]) + "\t" + str(res[k][1]) + "\t" + str(res[k][2]), end="")
  180. print()
  181. endTime = datetime.datetime.now()
  182. print(f"结束时间{endTime},耗费时间:{(endTime - startTime).seconds}秒")

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/417838
推荐阅读
相关标签
  

闽ICP备14008679号