python代码,详情如下:


 

#!/usr/bin/python3
# coding=utf8


import json
import requests
import datetime
from dateutil.parser import parse

gitlab_url = "https://www.xxxx.cn/"  # GitLab 地址
private_token = "glpat-123456"  # GitLab Access Tokens(管理员权限)
get_total_branche_name = "sit"  # 需要统计的分支
info = []

project_info = dict()

headers = {
    'Connection': 'close',
}


# UTC时间转时间戳
def utc_time(time):
    dt = parse(time)
    return int(dt.timestamp())


# 输出格式化
def str_format(txt):
    lenTxt = len(txt)
    lenTxt_utf8 = len(txt.encode('utf-8'))
    size = int((lenTxt_utf8 - lenTxt) / 2 + lenTxt)
    length = 20 - size
    return length


# 获取 GitLab 上的所有项目
def gitlab_projects():
    project_ids = []
    project_name = []
    page = 1
    while True:
        url = gitlab_url + "api/v4/projects/?private_token=" + private_token + "&page=" + str(page) + "&per_page=20"
        # print(url)
        while True:
            try:
                res = requests.get(url, headers=headers, timeout=10)
                break
            except Exception as e:
                continue

        projects = json.loads(res.text)

        if len(projects) == 0:
            break
        else:
            for project in projects:
                if project["id"] > 1:
                    project_ids.append(project["id"])
                    project_name.append(project["name"])
                    project_info[project["id"]] = project["name"]
            page += 1
    print(project_info)
    return project_ids


# 获取 GitLab 上的项目 id 中的分支
def project_branches(project_id):
    branch_names = []
    page = 1
    while True:
        url = gitlab_url + "api/v4/projects/" + str(
            project_id) + "/repository/branches?private_token=" + private_token + "&page=" + str(page) + "&per_page=20"
        while True:
            try:
                res = requests.get(url, headers=headers, timeout=10)
                break
            except Exception as e:
                print(e)
                continue
        branches = json.loads(res.text)
        '''Debug
        print(url)
        print('--' * 10)
        print(branches)
        print('*' * 10)
        '''
        if len(branches) == 0:
            break
        else:
            for branch in branches:
                branch_names.append(branch["name"])
            page += 1
    return branch_names


# 获取 GitLab 上的项目分支中的 commits,当 title 或 message 首单词为 Merge 时,表示合并操作,剔除此代码量
def project_commits(project_id, branch, start_time, end_time):
    commit_ids = []
    page = 1
    while True:
        url = gitlab_url + "api/v4/projects/" + str(
            project_id) + "/repository/commits?ref_name=" + branch + "&private_token=" + private_token + "&page=" + str(
            page) + "&per_page=20"

        while True:
            try:
                res = requests.get(url, headers=headers, timeout=10)
                break
            except Exception as e:
                print(e)
                continue
        commits = json.loads(res.text)

        if len(commits) == 0:
            break
        else:
            for commit in commits:
                # print(commit)
                if "Merge" in commit["title"] or "Merge" in commit["message"] or "合并" in commit["title"] or "合并" in \
                        commit["message"]:  # 不统计合并操作
                    continue
                # 不按时间段统计,统计所有时间的;
                # 如果需要按时间段查询,开启下面注释
                # elif utc_time(commit["authored_date"]) < utc_time(start_time) or utc_time(
                #         commit["authored_date"]) > utc_time(end_time):  # 不满足时间区间
                #     continue
                else:
                    commit_ids.append(commit["id"])
            page += 1
    return commit_ids


# 根据 commits 的 id 获取代码量
def commit_code(project_id, commit_id):
    global info
    url = gitlab_url + "api/v4/projects/" + str(
        project_id) + "/repository/commits/" + commit_id + "?private_token=" + private_token
    while True:
        try:
            res = requests.get(url, headers=headers, timeout=10)
            break
        except Exception as e:
            print(e)
            continue

    data = json.loads(res.text)
    # print(res.text)

    temp = {"name": data["author_name"], "additions": data["stats"]["additions"],
            "deletions": data["stats"]["deletions"], "total": data["stats"]["total"]}  # Git工具用户名,新增代码数,删除代码数,总计代码数
    info.append(temp)


# GitLab 数据查询
def gitlab_info(start_time, end_time):
    for project_id in gitlab_projects():  # 遍历所有项目ID
        print(f"项目信息:id={project_id},name={project_info[project_id]}")
        for branche_name in project_branches(project_id):  # 遍历每个项目中的分支
            if branche_name == get_total_branche_name:
                for commit_id in project_commits(project_id, get_total_branche_name, start_time,
                                                 end_time):  # 遍历每个分支中的 commit id
                    commit_code(project_id, commit_id)  # 获取代码提交量
            else:
                continue


if __name__ == "__main__":

    startTime = datetime.datetime.now()

    print(f"正在统计数据,请耐心等待,开始时间:{startTime}")

    gitlab_info('2000-01-01 00:00:00', '2023-06-01 23:59:59')  # 起-止时间
    name = []  # Git工具用户名
    additions = []  # 新增代码数
    deletions = []  # 删除代码数
    total = []  # 总计代码数
    res = {}

    # 生成元组
    for i in info:
        for key, value in i.items():
            if key == "name":
                name.append(value)
            if key == "additions":
                additions.append(value)
            if key == "deletions":
                deletions.append(value)
            if key == "total":
                total.append(value)
    data = list(zip(name, additions, deletions, total))

    # 去重累加
    for j in data:
        name = j[0]
        additions = j[1]
        deletions = j[2]
        # total = j[3]  # 返回的行数是添加行数+删除行数的总和,gitlab平台修改的总行数
        total = int(j[1]) - int(j[2])  # 计算代码实际行数值,添加行数-删除行数
        if name in res.keys():
            res[name][0] += additions
            res[name][1] += deletions
            res[name][2] += total
        else:
            res.update({name: [additions, deletions, total]})

    # 打印结果
    print("Git用户名\t新增代码数\t删除代码数\t总计代码数", end="")
    print()
    for k in res.keys():
        print(k + "\t" + str(res[k][0]) + "\t" + str(res[k][1]) + "\t" + str(res[k][2]), end="")
        print()

    endTime = datetime.datetime.now()
    print(f"结束时间{endTime},耗费时间:{(endTime - startTime).seconds}秒")

Logo

瓜分20万奖金 获得内推名额 丰厚实物奖励 易参与易上手

更多推荐