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

Python3统计gitlab上的代码量的更多相关文章

  1. git统计项目中成员代码量

    查看git上个人代码量 git log --author="username" --pretty=tformat: --numstat | awk '{ add += $1; su ...

  2. [chrome插件分享] gitlab-tree 更方便的浏览Gitlab上的代码

    说明 经常玩Github的人肯定都知道大名鼎鼎的octotree吧,这款chrome插件可以说是浏览代码的神器,利用左侧的树形菜单可以很方便的打开目录.浏览文件等,加上Github全站本身使用了pja ...

  3. jenkins如何获取gitlab上的代码

    如何安装jenkins和gitlab我就不重复了,请自行搜索我的博客 那么,jenkins如何获取gitlab上的代码呢? 具体配置步骤如下 1.在gitlab上配置个人访问令牌.注意事项:姓名那里需 ...

  4. Git拉取Gitlab上的代码时,报128的解决方法

    今天拉取gitlab上的代码时出现错误,一直返回128 首先我们确定我们在存储库上有没有权限,然后我就去项目中的 Members上看是否有权限,然后发现也是有的. 然后克隆的时候发现输入一万遍密码都还 ...

  5. (转)通过gitlab统计git提交的代码量

    git的代码量大多数都是根据命令行统计,或者根据第三方插件统计.但是都不满足我的需求,因为我们代码都由gitlab管理,于是想到了通过gitlab暴露出来的接口获取数据. 第一步,生成私钥 登录你的g ...

  6. gitlab上传代码及报错总结

    将目录变成git可管理的仓库 git init 将文件添加到暂存区中 git add README.md 将文件提交到仓库 git commit -m "fisrt commit" ...

  7. 将GitLab上面的代码克隆到本地

    1.安装GitLab客户端 2.去GitLab服务端找项目路径 3.去GitLab客户端去克隆代码 右键-->git Clone 4.最后结果

  8. gitlab 上传代码

    #生成公钥ssh-keygen -t ed25519 -C "xxx@tianwang.com"#拷贝公钥pbcopy < ~/.ssh/id_ed25519.pub 在网页 ...

  9. windows调起git bash执行sh脚本定时统计git仓库代码量

    本来挺简单的一个东西硬是弄了两天 心力交瘁 找了网上不少资料 整理一下发给大家 首先是统计每个人的代码量的git命令 在网上找的 我这里做了以下修改 git log --format='%aN'|so ...

随机推荐

  1. ASP.NET Core可视化日志组件使用

    前言 今天站长推荐一款日志可视化组件LogDashboard,可以不用安装第三方进程,只需要在项目中安装相应的Nuget包,添加数行代码,就可以实现拥有带Web页面的日志管理面板,十分nice哦. 下 ...

  2. JVM(三)类加载与字节码技术

    1.类文件结构 首先获得.class字节码文件 方法: 在文本文档里写入java代码(文件名与类名一致),将文件类型改为.java 在文件对应目录下运行cmd,执行javac XXX.java 以下是 ...

  3. 开坑:mysql相关问题

    一. 先过滤后连表和先连表后在mysql中选择的哪一种? 二. left join 和inner join使用场景有什么区别? 三. 第二个问题的衍生问题:left join中where 条件使用对n ...

  4. Firefox 启动带有配置信息

    若不设置进行下述配置,那么 webdriver 每次启动火狐浏览器,默认都是一个不太有任何插件的浏览器被启动. 通过配置的方式,指定一个浏览器设置来启动,就可以使用以前安装的插件或配置信息了. 步骤一 ...

  5. Pandas的loc,iloc与ix的用法及区别

    1.先来谈一谈loc,loc这个方法就是你有啥我就用啥,你没有的我不用,pandas对象的index,columns有什么,pd.loc[index,column],index就是pd.index的其 ...

  6. MyBatisPlus入门学习

    目录 MyBatisPlus 概述 快速入门 配置日志输出 CRUD拓展 插入 主键生成策略 更新操作 自动填充 乐观锁 查询操作 分页查询 删除操作 逻辑删除 性能分析插件 条件构造器 代码自动生成 ...

  7. 【译】Android NDK API 规范

    [译]Android NDK API 规范 译者按: 修改R代码遇到Lint tool的报错,搜到了这篇文档,aosp仓库地址:Android NDK API Guidelines. 975a589 ...

  8. misdirection靶机work_through

    web打点 nmap扫描 Nmap scan report for 192.168.218.135 Host is up (0.000014s latency). Not shown: 65531 c ...

  9. 织梦DedeCMS自定义表单diy_list.htm

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  10. hdu4403暴力搜索

    题意:      给你一个数字串,让你在里面添加一个=和若干个+,使等式成立. 思路:      lmax最大是15,直接暴搜,无压力,关键是判重,要在答案的时候判重,一开始在进队列之前判的,各种wa ...