代码

  1. import threading
  2. import gitlab
  3. import xlwt
  4. #获取所有的user
  5. def getAllUsers():
  6. usersli = []
  7. client = gitlab.Gitlab(private_host, private_token=private_token)
  8. users = client.users.list(all=True)
  9. for user in users:
  10. usersli.append(user.username)
  11. return usersli
  12. #获取所有的project
  13. def getAllProjects():
  14. client = gitlab.Gitlab(private_host, private_token=private_token)
  15. projects = client.projects.list(all=True)
  16. return projects
  17. #获取project下所有的branche
  18. def getAllBranchByProject(project):
  19. try:
  20. branches = project.branches.list()
  21. return branches
  22. except:
  23. return ""
  24. #获取project和branch下的commit
  25. def getCommitByBranch(project, branch):
  26. author_commits = []
  27. commits = project.commits.list(all=True, ref_name=branch.name)
  28. for commit in commits:
  29. committer_email = commit.committer_email
  30. title = commit.title
  31. message = commit.message
  32. #if ('Merge' in message) or ('Merge' in title):
  33. # print('Merge跳过')
  34. # continue
  35. #else:
  36. author_commits.append(commit)
  37. return author_commits
  38. #获取project项目下commit对应的code
  39. def getCodeByCommit(commit, project):
  40. commit_info = project.commits.get(commit.id)
  41. code = commit_info.stats
  42. return code
  43. def getAuthorCode(project,fenzhi):
  44. # print("project:%s" % project)
  45. users = getAllUsers()
  46. branches = getAllBranchByProject(project)
  47. if branches == "":
  48. pass
  49. else:
  50. for branch in branches:
  51. # print("branch#####",branch.name)
  52. if branch.name == fenzhi:
  53. #print("branch:%s" % branch)
  54. #print('获取工程', project.name, '分支', branch.name, "的提交记录")
  55. branchdata = {}
  56. branchdata['group'] = project.name_with_namespace.split("/")[0]
  57. branchdata['projectname'] = project.name
  58. branchdata['branchename'] = branch.name
  59. author_commits = getCommitByBranch(project, branch)
  60. # print(author_commits)
  61. codes = []
  62. res1 = []
  63. for commit in author_commits:
  64. #print('获取提交', commit.id, "的代码量")
  65. code = getCodeByCommit(commit, project)
  66. # print(commit,code)
  67. # print(code)
  68. # print(commit)
  69. # print(commit.committer_name)
  70. codes.append(code)
  71. # for user in users:
  72. # if commit.committer_name == user:
  73. # res1.append(commit)
  74. record = calculate(codes)
  75. branchdata['commitcount'] = len(author_commits)
  76. branchdata['codecount'] = record
  77. data.append(branchdata)
  78. # print(codes)
  79. # print(calculate(codes))
  80. # print(data)
  81. # for res in res1:
  82. # print(res)
  83. return data
  84. #写入execl
  85. def writeExcel(excelPath, data):
  86. workbook = xlwt.Workbook()
  87. # 获取第一个sheet页
  88. sheet = workbook.add_sheet('git')
  89. row0 = ['项目组', '工程名称', '分支名称', '提交次数', '新增代码', '删除代码', '总计代码']
  90. for i in range(0, len(row0)):
  91. sheet.write(0, i, row0[i])
  92. addcount = 0
  93. delcount = 0
  94. totalcount = 0
  95. commitcount = 0
  96. for i in range(0, len(data)):
  97. recode = data[i]
  98. j = 0
  99. sheet.write(i + 1, j, recode['group'])
  100. sheet.write(i + 1, j + 1, recode['projectname'])
  101. sheet.write(i + 1, j + 2, recode['branchename'])
  102. commitcount += (int)(recode['commitcount'])
  103. sheet.write(i + 1, j + 3, recode['commitcount'])
  104. addcount += (int)(recode['codecount']['additions'])
  105. sheet.write(i + 1, j + 4, recode['codecount']['additions'])
  106. delcount += (int)(recode['codecount']['deletions'])
  107. sheet.write(i + 1, j + 5, recode['codecount']['deletions'])
  108. totalcount += (int)(recode['codecount']['total'])
  109. sheet.write(i + 1, j + 6, recode['codecount']['total'])
  110. sheet.write(len(data) + 1, 3, commitcount)
  111. sheet.write(len(data) + 1, 4, addcount)
  112. sheet.write(len(data) + 1, 5, delcount)
  113. sheet.write(len(data) + 1, 6, totalcount)
  114. workbook.save(excelPath)
  115. def calculate(data):
  116. record = {}
  117. addacount = 0
  118. deletecount = 0
  119. totaolcount = 0
  120. for i in data:
  121. # print(i)
  122. addacount += int(i['additions'])
  123. deletecount += int(i['deletions'])
  124. totaolcount += int(i['total'])
  125. record['additions'] = addacount
  126. record['deletions'] = deletecount
  127. record['total'] = totaolcount
  128. return record
  129. if __name__ == '__main__':
  130. # 用户git账户的token 6S7jy689FeCrP5w_UwgZ
  131. private_token = '' #gitlab用户tonken
  132. # git地址
  133. private_host = '' #gitlab地址
  134. data = []
  135. thread_list = []
  136. projects = getAllProjects()
  137. # print(projects)
  138. for i in projects:
  139. branches = getAllBranchByProject(i)
  140. for j in branches:
  141. t = threading.Thread(target=getAuthorCode, args=(i,j.name))
  142. thread_list.append(t)
  143. for threadname in thread_list: threadname.start()
  144. for threadname in thread_list: threadname.join()
  145. # print(data)
  146. writeExcel('/Users/niaoshuai/Desktop/code_count.xls', data)

注意: 配置 private_host 和 private_token

安装依赖

  1. $ pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple gitlab
  2. $ pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple requests
  3. $ pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple xlwt
  4. $ pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple python-gitlab

执行

  1. $ python3 gitlab-api.py

参考地址