GitLab统计代码量

news/2025/1/21 20:20:11/

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));}
}

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


http://www.ppmy.cn/news/56955.html

相关文章

好用的项目管理软件的具体功能有哪些

随着企业规模不断的扩大,项目管理往往会面临更多的挑战与难题,最常见的会出现以下几个问题:资源消耗失控,而项目部门和相关部门之间沟通越来越困难;团队凝聚力下降、项目进度难以把控,项目成本几乎失控&…

【ChatGPT】500个ChatGPT/GPT-4 Prompt技巧

文章目录 一、前言二、什么是Prompt Engineering三、ChatGPT/GPT-4 Prompt Engineering使用技巧 一、前言 随着 GPT-4 和 DALL-E等大型、强大的生成式 AI 模型变得更好、更可用,AI 的一个利基元素也同样变得突出:Prompt Engineering提示工程&#xff0c…

建筑行业如何进行高效管理文件?

建筑设计行业在日常办公中会产出大量文件,如设计图纸协作商议,项目资料等,建筑行业该如何高效管理文件呢? 建筑设计行业团队可能遇到的文件管理问题: 1,设计图纸一般较大,来回传输不仅麻烦&…

【C++初阶】类与对象:6大默认成员函数------拷贝构造和赋值运算符重载

一.拷贝构造函数 A.概念 拷贝构造函数:只有单个形参,该形参是对本类类型对象的引用(一般常用const修饰),在用已存在的类类型对象创建新对象时由编译器自动调用。 B.特性 1. 拷贝构造函数是构造函数的一个重载形式; 2. 拷贝构造函…

大型Saas系统的权限体系设计(一)

X0 概述 在2B系统开发中,权限体系设计是绕不开的问题。最简单的当然是RBAC模型,只要通过用户、角色、权限几个有限的概念,就可以建立起一套基本可用的权限体系。再复杂一点,可以增加角色的层级概念,使得角色的配置更高…

基于MATLAB实现WSN(无线传感器网络)的LEACH(低能耗自适应集群层次结构)(Matlab代码实现)

目录 💥1 概述 📚2 运行结果 🎉3 参考文献 👨‍💻4 Matlab代码 💥1 概述 低能耗自适应集群层次结构(“LEACH”)是一种基于 TDMA 的 MAC 协议,它与无线传感器网络 &a…

JavaScript中的异步编程

当我们在编写JavaScript代码时,经常会遇到需要执行长时间运行的任务的情况,例如从服务器获取数据或进行复杂的计算。在这些情况下,我们不希望阻塞用户界面,因为这会使网站看起来卡顿,甚至无响应。为了避免这种情况&…

【五一创作】【笔记】Git|如何将仓库中所有的 commit 合成一个?又名,如何清除所有 git 提交记录?(附 git rebase 机制的简要分析)

在对代码进行开源时,我们往往并不希望代码开发过程中的提交记录被其他人看到,因为提交的过程中往往会涵盖一些敏感信息。因此会存在 将仓库中所有 commit 合成一个 的需求。 直觉上,往往会用 rebase 和 squash 或 reset,不过我尝…