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
Python3统计gitlab上的代码量的更多相关文章
- git统计项目中成员代码量
查看git上个人代码量 git log --author="username" --pretty=tformat: --numstat | awk '{ add += $1; su ...
- [chrome插件分享] gitlab-tree 更方便的浏览Gitlab上的代码
说明 经常玩Github的人肯定都知道大名鼎鼎的octotree吧,这款chrome插件可以说是浏览代码的神器,利用左侧的树形菜单可以很方便的打开目录.浏览文件等,加上Github全站本身使用了pja ...
- jenkins如何获取gitlab上的代码
如何安装jenkins和gitlab我就不重复了,请自行搜索我的博客 那么,jenkins如何获取gitlab上的代码呢? 具体配置步骤如下 1.在gitlab上配置个人访问令牌.注意事项:姓名那里需 ...
- Git拉取Gitlab上的代码时,报128的解决方法
今天拉取gitlab上的代码时出现错误,一直返回128 首先我们确定我们在存储库上有没有权限,然后我就去项目中的 Members上看是否有权限,然后发现也是有的. 然后克隆的时候发现输入一万遍密码都还 ...
- (转)通过gitlab统计git提交的代码量
git的代码量大多数都是根据命令行统计,或者根据第三方插件统计.但是都不满足我的需求,因为我们代码都由gitlab管理,于是想到了通过gitlab暴露出来的接口获取数据. 第一步,生成私钥 登录你的g ...
- gitlab上传代码及报错总结
将目录变成git可管理的仓库 git init 将文件添加到暂存区中 git add README.md 将文件提交到仓库 git commit -m "fisrt commit" ...
- 将GitLab上面的代码克隆到本地
1.安装GitLab客户端 2.去GitLab服务端找项目路径 3.去GitLab客户端去克隆代码 右键-->git Clone 4.最后结果
- gitlab 上传代码
#生成公钥ssh-keygen -t ed25519 -C "xxx@tianwang.com"#拷贝公钥pbcopy < ~/.ssh/id_ed25519.pub 在网页 ...
- windows调起git bash执行sh脚本定时统计git仓库代码量
本来挺简单的一个东西硬是弄了两天 心力交瘁 找了网上不少资料 整理一下发给大家 首先是统计每个人的代码量的git命令 在网上找的 我这里做了以下修改 git log --format='%aN'|so ...
随机推荐
- 消息中间件-ActiveMQ支持的消息协议
package com.study.mq.a1_example.helloworld.queue; import org.apache.activemq.ActiveMQConnectionFacto ...
- Mybatis3源码笔记(七)Plugin
1.Mybatis3的插件其实主要是用到了责任链和动态代理两种模式相结合而生成的.下面我们看一个例子,在执行所有update操作时,执行一个小小的测试输出. @Intercepts({@Signatu ...
- 翻译:《实用的Python编程》InstructorNotes
实用的 Python 编程--讲师说明 作者:戴维·比兹利(David Beazley) 概述 对于如何使用我的课程"实用的 Python 编程"进行教学的问题,本文档提供一些通用 ...
- Django中的CBV视图
Web 开发是一项无聊而且单调的工作,特别是在视图功能编写方面更为显著.为了减少这种痛苦,Django植入了视图类这一功能,该功能封装了视图开发常用的代码,无须编写大量代码即可快速完成数据视图的开发, ...
- JavaScript设计模式(二):工厂模式
工厂模式模式的定义与特点 工厂模式(Factory Pattern)是编程中最常用的设计模式之一.这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式.在工厂模式中,我们在创建对象时不会对 ...
- NumPy之:结构化数组详解
目录 简介 结构化数组中的字段field 结构化数据类型 创建结构化数据类型 从元组创建 从逗号分割的dtype创建 从字典创建 操作结构化数据类型 Offsets 和Alignment Field ...
- kali 中文乱码解决
在命令行输入"dpkg-reconfigure locales".进入图形化界面之后,(空格是选择,Tab是切换,*是选中),选中en_US.UTF-8和zh_CN.UTF-8,确 ...
- Linux获取权限
目录 Bash反弹shell Python反弹shell 写入命令到定时任务文件 写入SSH公钥 写入/etc/profile文件 当我们可以在远程Linux主机上执行任意命令或写入任意数据到任意文件 ...
- 线程操作与进程挂起(Windows核心编程)
0x01 线程挂起与切换 对于挂起进程,挂起线程则比较简单,利用 ResumeThread 与 SuspendThread 即可,当线挂起时线程用户状态下的一切操作都会暂停 #include < ...
- Java中实现某方法和重写某方法的区别
实现(implements) 实现一个方法,在实现某个接口,或者是继承某个抽象类,在接口和在抽象类中定义的方法,本身是没有实现的,也就是没有方法体,你在当前类中就需要去实现这个方法. 重写(overl ...