Python统计gitlab上的代码量

年底了,各大公司都要求每个人统计自己今年的代码量。

使用python写了一个统计gitlab上代码量。

首先安装python-gitlab插件。具体代码如下:

#!/usr/bin/python

import gitlab
import xlwt

#用户git账户的token
private_token = ''
#git地址
private_host = ''
#提交代码的域账户名称1
author_email1 = ''
#提交代码的域账户名称2
author_email2 = ''

def getAllProjects():
    client = gitlab.Gitlab(private_host,private_token=private_token)
    projects = client.projects.list(membership=True,all=True)
    return projects

def getAllBranchByProject(project):
    branches = project.branches.list()
    return branches

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:
            if (str(author_email1) and committer_email.find(author_email1) >= 0) or (str(author_email2) and committer_email.find(author_email2) >= 0):
                author_commits.append(commit)
    return author_commits

def getCodeByCommit(commit,project):
    commit_info = project.commits.get(commit.id)
    code =commit_info.stats
    return code

def getAuthorCode():
    data=[]
    projects = getAllProjects()
    for project in projects:
        branches = getAllBranchByProject(project)
        for branch in branches:
            print('获取工程',project.name,'分支',branch.name,"的提交记录")
            branchdata = {}
            branchdata['projectname'] = project.name
            branchdata['branchename'] = branch.name
            author_commits = getCommitByBranch(project,branch)
            codes = []
            for commit in author_commits:
                print('获取提交',commit.id,"的代码量")
                code = getCodeByCommit(commit,project)
                codes.append(code)
            record=calculate(codes)
            branchdata['commitcount'] = len(author_commits)
            branchdata['codecount'] = record
            data.append(branchdata)
    return data

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['projectname'])
        sheet.write(i+1,j+1,recode['branchename'])
        commitcount +=(int)(recode['commitcount'])
        sheet.write(i+1,j+2,recode['commitcount'])
        addcount += (int)(recode['codecount']['additions'])
        sheet.write(i+1,j+3,recode['codecount']['additions'])
        delcount +=(int)(recode['codecount']['deletions'])
        sheet.write(i+1,j+4,recode['codecount']['deletions'])
        totalcount +=(int)(recode['codecount']['total'])
        sheet.write(i+1,j+5,recode['codecount']['total'])
    sheet.write(len(data)+1,2,commitcount)
    sheet.write(len(data)+1,3,addcount)
    sheet.write(len(data)+1,4,delcount)
    sheet.write(len(data)+1,5,totalcount)
    workbook.save(excelPath)

def calculate(data):
    record ={}
    addacount =0
    deletecount =0
    totaolcount =0
    for i in data:
        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__':
    data = getAuthorCode()
    writeExcel('d:/11.xls',data)

 

  • 2
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值