Python3统计gitlab上的代码量
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 | 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
本文作者:香菜哥哥
本文链接:https://www.cnblogs.com/yizhipanghu/p/14373722.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步