Skip to content

Instantly share code, notes, and snippets.

@yueban
Created April 10, 2021 10:14
Show Gist options
  • Save yueban/590451e5972bea3a0130c984b729ce3f to your computer and use it in GitHub Desktop.
Save yueban/590451e5972bea3a0130c984b729ce3f to your computer and use it in GitHub Desktop.
统计 gitlab 一段时间内提交的代码行数
import gitlab
# gitlab 地址
url = ''
# gitlab private token(设置 api 访问权限)
private_token = ''
# 获取 gitlab 操作对象
gl = gitlab.Gitlab(url, private_token)
# 设置时间范围
start_time = '2021-04-05T00:00:00Z'
end_time = '2021-04-11T00:00:00Z'
# 遍历项目
projects = gl.projects.list(all=True)
result = {}
for project in projects:
if project.last_activity_at < start_time:
continue
# 遍历分支
try:
branches = project.branches.list()
except:
pass
for branch in branches:
# 根据时间、分支名遍历该分支下面所有的 commits
commits = project.commits.list(all=True, query_parameters={
'since': start_time, 'until': end_time, 'ref_name': branch.name})
# 遍历 commit,查询提交人和代码修改量
for commit in commits:
commit_detail = project.commits.get(commit.id)
email = commit_detail.author_email
if email in result:
result[email]['count'] += commit_detail.stats['additions']
else:
user = {}
user['name'] = commit_detail.author_name
user['email'] = email
user['count'] = commit_detail.stats['additions']
result[email] = user
# 按代码修改量排序
result = sorted(result.values(), key=lambda item: item['count'], reverse=True)
# 按照 Markdown 表格形式输出结果
print('|', end='')
print('name', end='|')
print('email', end='|')
print('count', end='|\n')
print('|', end='')
print('-', end='|')
print('-', end='|')
print('-', end='|\n')
for user in result:
print('|', end='')
print(user['name'], end='|')
print(user['email'], end='|')
print(user['count'], end='|\n')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment