• 通过gitlab远程统计git代码量


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

    第一步,生成私钥

    登录你的gitlab申请私钥private_token

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

    接口地址: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首单词为Merge,表示合并操作,剔除此代码量
    接口地址:
    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

  • 相关阅读:
    Atitit fms Strait (海峡) lst 数据列表目录1. 4大洋 12. 著名的海大约40个,总共约55个海 13. 海区列表 23.1. 、波利尼西亚(Polynesia,
    Atitit trave islands list 旅游资源列表岛屿目录1. 东南亚著名的旅游岛屿 21.1. Cjkv 日韩 冲绳 琉球 济州岛 北海道 21.2. 中国 涠洲岛 南澳
    Atitit Major island groups and archipelagos 主要的岛群和群岛目录资料目录1. 岛群 波利尼西亚(Polynesia, 美拉尼西亚(Melanesia,
    Atitit glb 3tie city lst 三线城市列表 数据目录1. 全球范围内约90个城市 三线 12. 世界性三线城市全球共
    Atitit glb 1tie 2tie city lst 一二线城市列表数据约50个一线城市Alpha ++ 阿尔法++,,London 伦敦,,New York 纽约,,Alpha +
    Attit 现代编程语言重要特性目录第一章 类型系统 基本三大类型 2第一节 字符串 数字 bool 2第二节 推断局部变量 2第三节 动态类型 2第二章 可读性与开发效率 简单性 2
    Atitit 未来数据库新特性展望目录1. 统一的翻页 21.1. 2 Easy Top-N
    使用Chrome DevTools(console ande elements panel)进行xpath/css/js定位
    chrome -console妙用之定位xpath/js/css
    表达式树之构建Lambda表达式
  • 原文地址:https://www.cnblogs.com/wpcnblog/p/13895454.html
Copyright © 2020-2023  润新知