(轉)經過gitlab統計git提交的代碼量

git的代碼量大多數都是根據命令行統計,或者根據第三方插件統計。可是都不知足個人需求,由於咱們代碼都由gitlab管理,因而想到了經過gitlab暴露出來的接口獲取數據。html

 

第一步,生成私鑰

登陸你的gitlab申請私鑰private_tokengit

 

 

 

第二步,獲取當前用戶可見的全部項目(即便用戶不是成員)

 

接口地址:gitlab的地址/api/v4/projects/?private_token=xxx
返回參數:web

[{
        "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
傳入參數:無
返回參數:api

[{
        "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=xxxssh

[{
"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
返回參數:gitlab

[{
    "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爲總量。修改操做其實是刪除以後再新增。
須要注意的是,這裏統計出來的代碼量是代碼行數。url

小結
拿到這些數據以後,無論你是存數據庫,仍是存excel都很方便,徹底可讓系統天天定時去跑,不須要手動執行任何命令。
Gitlab文檔:https://docs.gitlab.com/ee/api/README.html
spa

 

 


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

相關文章
相關標籤/搜索