Java 调用 GitLab API

前言:

上一篇我们使用了 webhook 的方式获取用户提交代码的信息,本篇我简单分享一下使用 GitLab API 来获取用户提交代码的信息。

业务分析:

我们需要统计每一个用户的提交代码的信息,那 GitLab 是否有这样的接口呢?GitLab API 官网如下:

GitLab API 官网

我简单的看了一下官网的 API,初步判断是没有直接查询用户的提交记录信息的 API,查询了很多资料也没有看到直接查询用户提交记录信息的 API,个人推断是没有该 API 的,那我们该如何获取用户的提交信息呢?

我这边简单调研了一下,可以通过以下步骤来获取用户提交信息

  1. 获取项目信息(每个项目都有自己的唯一项目id)。
  2. 根据项目信息获取分支信息。
  3. 根据分支信息获取分支提交信息。

代码演示

获取项目信息

java 复制代码
public void queryGitLabAllProject(String url) {
	//获取所有项目信息
	String baseUrl = "https://git.xxx.com/" + "api/v4/projects?per_page=100&page=1";
	baseUrl = String.format(baseUrl, 1775);
	Map<String, Object> params = new HashMap<>();
	params.put("page", 1);
	params.put("per_page", 1000);
	HttpResponse response = httpGet(baseUrl, params);
	String body = response.body();
	if (body == null) {
		throw new BusinessException("没有获取到任何项目信息");
	}
}

根据项目信息获取分支信息

java 复制代码
public void queryGitLabAllBranch() {
	//获取所有项目信息
	String baseUrl = "https://git.xxx.com/" + "api/v4/projects/%d/repository/branches";
	baseUrl = String.format(baseUrl, 2692);
	Map<String, Object> params = new HashMap<>();
	params.put("with_stats", true);
	params.put("page", 1);
	params.put("per_page", 1000);
	HttpResponse response = httpGet(baseUrl, params);
	String body = response.body();
	if (body == null) {
		throw new BusinessException("没有获取到任何分支信息");
	}
}

根据项目分支信息获取代码提交信息

java 复制代码
public void queryGitLabCommitsByBranch(String branchName) {
	Long projectId = 1775L;
	String baseUrl = "https://git.xxx.com/api/v4/projects/%d/repository/commits";
	baseUrl = String.format(baseUrl, 2692);
	Map<String, Object> params = new HashMap<>();
	params.put("ref_name", branchName);   // 指定分支名
	params.put("ref_name", "feature-xxxx/xxx-xxx-xxx");   // 指定分支名
	params.put("with_stats", true);
	params.put("page", 1);
	params.put("per_page", 100);
	HttpResponse response = httpGet(baseUrl, params);
	String body = response.body();
	if (body == null) {
		throw new BusinessException("没有获取到任何提交信息");
	}
}

获取到分支提交信息后,就可以对结果集进行分析,其中就包含用户代码提交信息,这里我只是简单的预研,并没有完整的代码。

我这里使用的是 hutool 工具包的 HttpRequest 完成 Http 调用的,代码如下:

java 复制代码
public HttpResponse httpGet(String url, Map<String, Object> body) {
	HttpRequest request = HttpRequest.get(url)
			.header(Header.CONTENT_TYPE, "application/json")
			.header("Private-Token", "xxxxxxxxx");// 设置授权头
	if (CollectionUtil.isNotEmpty(body)) {
		for (Map.Entry<String, Object> form : body.entrySet()) {
			request.form(form.getKey(), form.getValue());
		}
	}
	return request.timeout(20000).execute();
}

GitLab 创建个人访问令牌

  1. 登录 GitLab,点击右上角头像 → Settings。
  2. 左侧菜单选择 Access Tokens。
  3. 填写令牌名称和过期日期。
  4. 选择权限范围。
  5. 点击 Create personal access token 完成创建,同时立即复制生成的 Token 并保存好,因为页面刷新后将永远无法再次查看。

总结:本篇简单分享了如何使用 GitLab 的 API 获取用户提交信息,希望可以帮助到正需要的你,GitLab 还有很多 API 我没有时间去研究,你如果发现了更好的 API 调用方式,欢迎你的分享。

如有不正确的地方欢迎各位指出纠正。

相关推荐
珹洺13 分钟前
Java-Spring入门指南(二十一)Thymeleaf 视图解析器
java·开发语言·spring
Predestination王瀞潞17 分钟前
类的多态(Num020)
开发语言·c++
Predestination王瀞潞18 分钟前
类的继承(Num019)
开发语言·c++
源码集结号24 分钟前
一套智慧工地云平台源码,支持监管端、项目管理端,Java+Spring Cloud +UniApp +MySql技术开发
java·mysql·spring cloud·uni-app·源码·智慧工地·成品系统
EnCi Zheng26 分钟前
Spring Security 最简配置完全指南-从入门到精通前后端分离安全配置
java·安全·spring
程序员小假27 分钟前
为什么这些 SQL 语句逻辑相同,性能却差异巨大?
java·后端
万粉变现经纪人36 分钟前
如何解决 pip install -r requirements.txt 无效可编辑项 ‘e .‘(-e 拼写错误)问题
开发语言·python·r语言·beautifulsoup·pandas·pip·scipy
泉城老铁42 分钟前
导出大量数据时如何优化内存使用?SXSSFWorkbook的具体实现方法是什么?
spring boot·后端·excel
渣哥44 分钟前
从配置文件到 SpEL 表达式:@Value 在 Spring 中到底能做什么?
javascript·后端·面试