Python3统计gitlab上的代码量
import threading import gitlab import xlwt #获取所有的user def getAllUsers(): usersli = [] client = gitlab.Gitlab(private_host, private_token=private_token) users = client.users.list(all=True) for user in users: usersli.append(user.username) return usersli #获取所有的project def getAllProjects(): client = gitlab.Gitlab(private_host, private_token=private_token) projects = client.projects.list(all=True) return projects #获取project下所有的branche def getAllBranchByProject(project): try: branches = project.branches.list() return branches except: return "" #获取project和branch下的commit def getCommitByBranch(project, branch): author_commits = [] commits = project.commits.list(all=True, ref_name=branch.name) for commit in commits: committer_email = commit.committer_email title = commit.title message = commit.message #if ('Merge' in message) or ('Merge' in title): # print('Merge跳过') # continue #else: author_commits.append(commit) return author_commits #获取project项目下commit对应的code def getCodeByCommit(commit, project): commit_info = project.commits.get(commit.id) code = commit_info.stats return code def getAuthorCode(project,fenzhi): # print("project:%s" % project) users = getAllUsers() branches = getAllBranchByProject(project) if branches == "": pass else: for branch in branches: # print("branch#####",branch.name) if branch.name == fenzhi: #print("branch:%s" % branch) #print('获取工程', project.name, '分支', branch.name, "的提交记录") branchdata = {} branchdata['group'] = project.name_with_namespace.split("/")[0] branchdata['projectname'] = project.name branchdata['branchename'] = branch.name author_commits = getCommitByBranch(project, branch) # print(author_commits) codes = [] res1 = [] for commit in author_commits: #print('获取提交', commit.id, "的代码量") code = getCodeByCommit(commit, project) # print(commit,code) # print(code) # print(commit) # print(commit.committer_name) codes.append(code) # for user in users: # if commit.committer_name == user: # res1.append(commit) record = calculate(codes) branchdata['commitcount'] = len(author_commits) branchdata['codecount'] = record data.append(branchdata) # print(codes) # print(calculate(codes)) # print(data) # for res in res1: # print(res) return data #写入execl def writeExcel(excelPath, data): workbook = xlwt.Workbook() # 获取第一个sheet页 sheet = workbook.add_sheet('git') row0 = ['项目组', '工程名称', '分支名称', '提交次数', '新增代码', '删除代码', '总计代码'] for i in range(0, len(row0)): sheet.write(0, i, row0[i]) addcount = 0 delcount = 0 totalcount = 0 commitcount = 0 for i in range(0, len(data)): recode = data[i] j = 0 sheet.write(i + 1, j, recode['group']) sheet.write(i + 1, j + 1, recode['projectname']) sheet.write(i + 1, j + 2, recode['branchename']) commitcount += (int)(recode['commitcount']) sheet.write(i + 1, j + 3, recode['commitcount']) addcount += (int)(recode['codecount']['additions']) sheet.write(i + 1, j + 4, recode['codecount']['additions']) delcount += (int)(recode['codecount']['deletions']) sheet.write(i + 1, j + 5, recode['codecount']['deletions']) totalcount += (int)(recode['codecount']['total']) sheet.write(i + 1, j + 6, recode['codecount']['total']) sheet.write(len(data) + 1, 3, commitcount) sheet.write(len(data) + 1, 4, addcount) sheet.write(len(data) + 1, 5, delcount) sheet.write(len(data) + 1, 6, totalcount) workbook.save(excelPath) def calculate(data): record = {} addacount = 0 deletecount = 0 totaolcount = 0 for i in data: # print(i) addacount += int(i['additions']) deletecount += int(i['deletions']) totaolcount += int(i['total']) record['additions'] = addacount record['deletions'] = deletecount record['total'] = totaolcount return record if __name__ == '__main__': # 用户git账户的token 6S7jy689FeCrP5w_UwgZ private_token = 'T3Nz2xCxq4FcVQ4wytr1' #gitlab用户tonken # git地址 private_host = 'http://10.0.0.1:8888/' #gitlab地址 data = [] thread_list = [] projects = getAllProjects() # print(projects) for i in projects: branches = getAllBranchByProject(i) for j in branches: t = threading.Thread(target=getAuthorCode, args=(i,j.name)) thread_list.append(t) for threadname in thread_list: threadname.start() for threadname in thread_list: threadname.join() # print(data) writeExcel('d:/code_count.xls', data)
来源:https://blog.csdn.net/sinat_25318461/article/details/103489793