GitLab统计代码量

gitlab官方文档:https://docs.gitlab.com/ee/api/index.html

1、生成密钥

登录gitlab,编辑个人资料,设置访问令牌
在这里插入图片描述

2、获取当前用户所有可见的项目

在这里插入图片描述
接口地址

GET请求
http://gitlab访问地址/api/v4/projects?private_token=xxx 

返回参数

[
	{
		"id": 1,
		"description": "This project is automatically generated and helps monitor this GitLab instance. [Learn more](/help/administration/monitoring/gitlab_self_monitoring_project/index).",
		"name": "Monitoring",
		"name_with_namespace": "GitLab Instance / Monitoring",
		"path": "Monitoring",
		"path_with_namespace": "gitlab-instance-d71d8d97/Monitoring",
		"created_at": "2021-07-12T08:41:01.299Z",
		"default_branch": "main",
		"tag_list": [],
		"topics": [],
		"ssh_url_to_repo": "git@gitlab.example.cn:gitlab-instance-d71d8d97/Monitoring.git",
		"http_url_to_repo": "http://gitlab.example.cn/gitlab-instance-d71d8d97/Monitoring.git",
		"web_url": "http://gitlab.example.cn/gitlab-instance-d71d8d97/Monitoring",
		"readme_url": null,
		"avatar_url": null,
		"forks_count": 0,
		"star_count": 0,
		"last_activity_at": "2021-07-12T08:41:01.299Z",
		"namespace": {
			"id": 2,
			"name": "GitLab Instance",
			"path": "gitlab-instance-d71d8d97",
			"kind": "group",
			"full_path": "gitlab-instance-d71d8d97",
			"parent_id": null,
			"avatar_url": null,
			"web_url": "http://gitlab.example.cn/groups/gitlab-instance-d71d8d97"
		},
		"_links": {
			"self": "http://gitlab.example.cn/api/v4/projects/1",
			"issues": "http://gitlab.example.cn/api/v4/projects/1/issues",
			"merge_requests": "http://gitlab.example.cn/api/v4/projects/1/merge_requests",
			"repo_branches": "http://gitlab.example.cn/api/v4/projects/1/repository/branches",
			"labels": "http://gitlab.example.cn/api/v4/projects/1/labels",
			"events": "http://gitlab.example.cn/api/v4/projects/1/events",
			"members": "http://gitlab.example.cn/api/v4/projects/1/members"
		},
		"packages_enabled": true,
		
		...
		...
		...
	}
]

这里我们只需要关注项目id即可

3、获取当前项目所有分支

接口地址

GET请求
http://gitlab访问地址/api/v4/projects/项目id/repository/branches?private_token=xxx 

返回参数

[
    {
        "name": "main",
        "commit": {
            "id": "2d3e01fbedf088fccb5000303428df35c09ea07d",
            "short_id": "2d3e01fb",
            "created_at": "2023-01-06T02:52:54.000+00:00",
            "parent_ids": null,
            "title": "xxxxxx",
            "message": "xxxxxx",
            "author_name": "xxxx",
            "author_email": "xxxx@example.cn",
            "authored_date": "2023-01-06T02:52:54.000+00:00",
            "committer_name": "xxxx",
            "committer_email": "xxxx@example.cn",
            "committed_date": "2023-01-06T02:52:54.000+00:00",
            "trailers": null,
            "web_url": "http://gitlab.example.cn/example/-/commit/2d3e01fbedf088fccb5000303428df35c09ea07d"
        },
        "merged": false,
        "protected": true,
        "developers_can_push": false,
        "developers_can_merge": false,
        "can_push": true,
        "default": true,
        "web_url": "http://gitlab.example.cn/example/-/tree/main"
    }
]

4、遍历分支,根据分支name获取commits

接口地址

GET请求
http://gitlab访问地址/api/v4/projects/项目id/repository/commits?ref_name=main&private_token=xxx 

返回参数

[
	{
		"id": "8fc81980222370d51c11cd9bc609f10f3b7d9828",
		"short_id": "8fc81980",
		"created_at": "2022-07-21T08:46:35.000+00:00",
		"parent_ids": [],
		"title": "Initial commit",
		"message": "Initial commit",
		"author_name": "xxxx",
		"author_email": "xxxx@example.cn",
		"authored_date": "2022-07-21T08:46:35.000+00:00",
		"committer_name": "xxxx",
		"committer_email": "xxxx@example.cn",
		"committed_date": "2022-07-21T08:46:35.000+00:00",
		"trailers": {},
		"web_url": "http://gitlab.example.cn/example/-/commit/8fc81980222370d51c11cd9bc609f10f3b7d9828"
	}
]

5、根据commit的id获取代码量

接口地址

GET请求
http://gitlab访问地址/api/v4/projects/项目id/repository/commits/commit的id?private_token=xxx 

返回参数

{
    "id": "8fc81980222370d51c11cd9bc609f10f3b7d9828",
    "short_id": "8fc81980",
    "created_at": "2022-07-21T08:46:35.000+00:00",
    "parent_ids": [],
    "title": "Initial commit",
    "message": "Initial commit",
    "author_name": "xxxx",
    "author_email": "xxxx@example.cn",
    "authored_date": "2022-07-21T08:46:35.000+00:00",
    "committer_name": "xxxx",
    "committer_email": "xxxx@example.cn",
    "committed_date": "2022-07-21T08:46:35.000+00:00",
    "trailers": {},
    "web_url": "http://gitlab.example.cn/example/-/commit/8fc81980222370d51c11cd9bc609f10f3b7d9828",
    "stats": {
        "additions": 92,
        "deletions": 0,
        "total": 92
    },
    "status": null,
    "project_id": 1,
    "last_pipeline": null
}

stats节点下参数就是我们本次提交的代码量,additions为新增行数,deletions为删除行数,total为总数。

修改操作实际上是删除之后再新增。

注:通过API获取gitlab项目、分支、commits时,默认只能查到20条数据,可以增加入参指定每页数量,数量最大为50000
在这里插入图片描述
Java代码实现

private void gitLab() throws Exception {
    JSONObject params = new JSONObject();
    params.put("private_token", "xxx");
    params.put("per_page", "50000");

    //项目列表
    String result = WebUtils.doGet("http://gitlab.example.cn/api/v4/projects", params);
    JSONArray projects = JSONArray.parseArray(result);
    for (Object project : projects) {
        JSONObject projectValue = JSONObject.parseObject(project.toString());
        String projectId = projectValue.getString("id");

        //commits列表
        String url = String.format("http://gitlab.example.cn/api/v4/projects/%s/repository/commits", projectId);
        result = WebUtils.doGet(url, params);

        int additions = 0;
        int deletions = 0;
        int total = 0;

        JSONArray commits = JSONArray.parseArray(result);
        for (Object commit : commits) {
            JSONObject commitValue = JSONObject.parseObject(commit.toString());
            String commitId = commitValue.getString("id");

            url = String.format("http://gitlab.example.cn/api/v4/projects/%s/repository/commits/%s", projectId, commitId);

            //提交记录
            result = WebUtils.doGet(url, params);
            JSONObject commitStats = JSONObject.parseObject(result);
            JSONObject stats = commitStats.getJSONObject("stats");

            additions += stats.getIntValue("additions");
            deletions += stats.getIntValue("deletions");
            total += stats.getIntValue("total");
        }

        String name = projectValue.getString("name");
        LoggerUtils.info(String.format("项目:%s ,新增:%d ,删除:%d ,合计:%d", name, additions, deletions, total));
    }
}

以上是按照项目统计,扩展类似按作者统计是相同道理

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在GitLab统计贡献者的代码,你可以使用以下命令: ``` git log --author="username" --pretty=tformat: --numstat | awk '{ add += $1; subs += $2; loc += $1 - $2 } END { printf "added lines: %s, removed lines: %s, total lines: %s\n", add, subs, loc }' ``` 这个命令将统计指定用户的提交代码行数,其中`username`是你要统计的用户的用户名。 如果你想统计所有贡献者的代码,可以使用以下命令: ``` git log --format='%aN' | sort -u | while read name; do echo -en "$name\t"; git log --author="$name" --pretty=tformat: --numstat | awk '{ add += $1; subs += $2; loc += $1 - $2 } END { printf "added lines: %s, removed lines: %s, total lines: %s\n", add, subs, loc }' -; done ``` 这个命令将列出所有贡献者的用户名以及他们的代码统计。 如果你只想统计所有贡献者的提交次数,可以使用以下命令: ``` git log --pretty='%aN' | sort | uniq -c | sort -k1 -n -r ``` 这个命令将按照提交次数从高到低的顺序列出所有贡献者的用户名和提交次数。 请注意,这些命令需要在GitLab代码仓库目录中运行。 #### 引用[.reference_title] - *1* [统计Git项目各成员贡献代码行数、提交次数)](https://blog.csdn.net/sinat_34461975/article/details/128897916)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值