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

摘要:
git的大部分代码大小是通过命令行或第三方插件计算的。但它们都不能满足我的需求,因为我们的代码是由gitlab管理的,所以我们考虑通过gitlab公开的接口获取数据。步骤1:生成私钥并登录到您的gitlab以申请私钥_令牌的第二步是获取当前用户可见的所有项目接口地址:gitlab/api/v4/projects/?

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

第一步,生成私钥

登录你的gitlab申请私钥private_token

(转)通过gitlab统计git提交的代码量第1张

第二步,获取当前用户可见的所有项目(即使用户不是成员)

接口地址:gitlab的地址/api/v4/projects/?private_token=xxx
返回参数:

[{
        "id":219,
        "description":"",
        "name":"share-5.2.3.8",
        "name_with_namespace":"develop / share-5.2.3.8",
        "path":"share-5.2.3.8",
        "path_with_namespace":"develop/share-5.2.3.8",
        "created_at":"2019-07-10T19:59:29.855+08:00",
        "default_branch":"master",
        "tag_list":[

        ],
        "ssh_url_to_repo":"git@127.0.0.1:develop/share-5.2.3.8.git",
        "http_url_to_repo":"http://127.0.0.1/develop/share-5.2.3.8.git",
        "web_url":"http://127.0.0.1/develop/share-5.2.3.8",
        "readme_url":"http://127.0.0.1/develop/share-5.2.3.8/blob/master/README.md",
        "avatar_url":null,
        "star_count":0,
        "forks_count":0,
        "last_activity_at":"2019-07-11T02:53:44.831+08:00",
        "_links":{
            "self":"http://127.0.0.1/api/v4/projects/219",
            "issues":"http://127.0.0.1/api/v4/projects/219/issues",
            "merge_requests":"http://127.0.0.1/api/v4/projects/219/merge_requests",
            "repo_branches":"http://127.0.0.1/api/v4/projects/219/repository/branches",
            "labels":"http://127.0.0.1/api/v4/projects/219/labels",
            "events":"http://127.0.0.1/api/v4/projects/219/events",
            "members":"http://127.0.0.1/api/v4/projects/219/members"
        },
        "archived":false,
        "visibility":"private",
        "resolve_outdated_diff_discussions":false,
        "container_registry_enabled":true,
        "issues_enabled":true,
        "merge_requests_enabled":true,
        "wiki_enabled":true,
        "jobs_enabled":true,
        "snippets_enabled":true,
        "shared_runners_enabled":true,
        "lfs_enabled":true,
        "creator_id":14,
        "namespace":{
            "id":17,
            "name":"develop",
            "path":"develop",
            "kind":"group",
            "full_path":"develop",
            "parent_id":null
        },
        "import_status":"none",
        "open_issues_count":0,
        "public_jobs":true,
        "ci_config_path":null,
        "shared_with_groups":[

        ],
        "only_allow_merge_if_pipeline_succeeds":false,
        "request_access_enabled":false,
        "only_allow_merge_if_all_discussions_are_resolved":false,
        "printing_merge_request_link_enabled":true,
        "merge_method":"merge",
        "permissions":{
            "project_access":null,
            "group_access":{
                "access_level":40,
                "notification_level":3
            }
        }
    },...]

  参数这么多我们从中抽取出需要的部分

[{
        "id":219,
        "name":"share-5.2.3.8",
        "name_with_namespace":"develop / share-5.2.3.8",
        "path_with_namespace":"develop/share-5.2.3.8",
        "http_url_to_repo":"http://127.0.0.1/develop/share-5.2.3.8.git",
        "created_at":"2019-07-10T19:59:29.855+08:00",
         "_links":{
            "repo_branches":"http://127.0.0.1/api/v4/projects/219/repository/branches",
        },
    },...]

第三步,遍历项目,根据项目id获取分支列表

接口地址:http://gitlab地址/api/v4/projects/项目id/repository/branches?private_token=xxx
传入参数:无
返回参数:

[{
        "name":"master",
        "commit":{
            "id":"d1b9747ba994f19fb6afb069b3751bd3cf21rrrr",
            "short_id":"d1b974123",
            "title":"添加仓库",
            "created_at":"2019-07-11T02:53:32.000+08:00",
            "message":"添加仓库",
            "author_name":"admin",
            "author_email":"admin@gmail.com",
            "authored_date":"2019-07-11T02:53:32.000+08:00",
            "committer_name":"admin",
            "committer_email":"admin@gmail.com",
            "committed_date":"2019-07-11T02:53:32.000+08:00",
            "parent_ids":[
                "25cf5c94b9ddc762bd2be73e1e542ebd26adadf"
            ]
        },
        "merged":false,
        "protected":true,
        "developers_can_push":false,
        "developers_can_merge":false
    },...]

  

第四步,遍历分支,根据分支name获取commits

注意,当title和message
接口地址:
http://gitlab地址/api/v4/projects/项目id/repository/commits?ref_name=master&private_token=xxx

[{
"id":"d1b9747ba994f19fb6afb069b3751bd3cf21ag32",
        "author_name":"admin",
        "authored_date":"2019-07-11T02:53:32.000+08:00",
        "committer_email":"admin@gmail.com",
        "committed_date":"2019-07-11T02:53:32.000+08:00",
        "created_at":"2019-07-11T02:53:32.000+08:00",
        "author_email":"admin@gmail.com",
        "short_id":"d1b9747b",
        "title":"添加仓库",
        "parent_ids":[
            "25cf5c94b9ddc762bd2be73e1e542ebd26aafd"
        ],
        "message":"添加仓库 ",
        "committer_name":"admin"
    },...]

  

第五步,根据commits的id获取代码量

接口地址:
http://gitlab地址/api/v4/projects/项目id/repository/commits/commits的id?private_token=xxx
返回参数:

[{
    "id":"d1b9747ba994f19fb6afb069b3751bd3cf21334d",
    "short_id":"d1b9747b",
    "title":"添加仓库",
    "created_at":"2019-07-11T02:53:32.000+08:00",
    "parent_ids":[
        "25cf5c94b9ddc762bd2be73e1e542ebd26ad7sdf"
    ],
    "message":"添加仓库 ",
    "author_name":"admin",
    "author_email":"admin@gmail.com",
    "authored_date":"2019-07-11T02:53:32.000+08:00",
    "committer_name":"admin",
    "committer_email":"admin@gmail.com",
    "committed_date":"2019-07-11T02:53:32.000+08:00",
    "stats":{
        "additions":21,
        "deletions":8,
        "total":29
    },
    "status":null,
    "last_pipeline":null,
    "project_id":219
},...]

  

stats里面就是我们想要的代码量了,additions为新增,deletions为删除,total为总量。修改操作实际上是删除之后再新增。
需要注意的是,这里统计出来的代码量是代码行数。

小结
拿到这些数据之后,不管你是存数据库,还是存excel都很方便,完全可以让系统每天定时去跑,不需要手动执行任何命令。
Gitlab文档:https://docs.gitlab.com/ee/api/README.html
(转)通过gitlab统计git提交的代码量第2张


原文链接:https://blog.csdn.net/wenwen513/article/details/95647364

免责声明:文章转载自《(转)通过gitlab统计git提交的代码量》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇【Modbus】Java使用 modbus-master-tcp 读取和写入Modbus服务器数据实验六:通过grub程序引导本地磁盘内核启动系统(busybox)下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

GitLab的基础使用-创建项目(projects)

         GitLab的基础使用-创建项目(projects)                                         作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任。 一.准备环境 1>.创建项目组(groups)   博主推荐阅读:     https://www.cnblogs.com/yinzh...

gitlab Runner 安装与部署

1、安装gitlab Runner    本次演示为centos 7.5环境,请各位按照自己环境调整   1)在线安装 #安装官方镜像源 [root@k8s-node02 ~]# curl -L https://packages.gitlab.com/install/repositories/runner/gitlab-runner/script.rpm...

gitlab 简介

1.gitlab 简介 1.1.定位 gitLab 是一个基于 Git 实现的在线代码仓库托管软件 可以用 gitlab 自己搭建一个类似于 Github 一样的系统,一般用于在企业、学校等内部网络搭建 git 私服。 对于软件工程质量管理非常重要。 1.2.功能 提供代码托管、提交审核和问题跟踪 可以很方便的管理权限、代码 review,创建、管理 p...

gitlab 搭建

条件:在CentOS系统上安装所需的依赖:ssh,防火墙,postfix(用于邮件通知),wget,以下这些命令也会打开系统防火墙中的HTTP和SSH端口访问。 内存不小于2G 添加GitLab镜像源并安装gitlab服务器 wget https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7/gitlab...

GitLab 数据库

访问 GitLab 数据库 步骤 用的 Docker Gitlab,首先进入容器 docker exec -it gitlab /bin/bash `` 找到数据库配置文件 ```bash /var/opt/gitlab/gitlab-rails/etc/database.yml 内容如下,记录了数据库配置信息 production: adapter...

gitlab实现webhook触发jenkins 自动,构建,测试,push webhook构子 总结

最新一直在学习 工作 + 学习 去掉 90%   所以blog  一直没更 真是很不好!  exsi ceph gitlab jenkins harbor k8s  docker-compose apollo  springboot  dubbo      太多太多 。。。整合   主题: 如何设置GitLab以及Jenkins,实现每次GitLab上有提...