代码
import threadingimport gitlabimport xlwt#获取所有的userdef 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#获取所有的projectdef getAllProjects():client = gitlab.Gitlab(private_host, private_token=private_token)projects = client.projects.list(all=True)return projects#获取project下所有的branchedef getAllBranchByProject(project):try:branches = project.branches.list()return branchesexcept:return ""#获取project和branch下的commitdef getCommitByBranch(project, branch):author_commits = []commits = project.commits.list(all=True, ref_name=branch.name)for commit in commits:committer_email = commit.committer_emailtitle = commit.titlemessage = commit.message#if ('Merge' in message) or ('Merge' in title):# print('Merge跳过')# continue#else:author_commits.append(commit)return author_commits#获取project项目下commit对应的codedef getCodeByCommit(commit, project):commit_info = project.commits.get(commit.id)code = commit_info.statsreturn codedef getAuthorCode(project,fenzhi):# print("project:%s" % project)users = getAllUsers()branches = getAllBranchByProject(project)if branches == "":passelse: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.namebranchdata['branchename'] = branch.nameauthor_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'] = recorddata.append(branchdata)# print(codes)# print(calculate(codes))# print(data)# for res in res1:# print(res)return data#写入execldef 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 = 0delcount = 0totalcount = 0commitcount = 0for i in range(0, len(data)):recode = data[i]j = 0sheet.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 = 0deletecount = 0totaolcount = 0for i in data:# print(i)addacount += int(i['additions'])deletecount += int(i['deletions'])totaolcount += int(i['total'])record['additions'] = addacountrecord['deletions'] = deletecountrecord['total'] = totaolcountreturn recordif __name__ == '__main__':# 用户git账户的token 6S7jy689FeCrP5w_UwgZprivate_token = '' #gitlab用户tonken# git地址private_host = '' #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('/Users/niaoshuai/Desktop/code_count.xls', data)
注意: 配置 private_host 和 private_token
安装依赖
$ pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple gitlab$ pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple requests$ pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple xlwt$ pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple python-gitlab
执行
$ python3 gitlab-api.py
