简介
在开发中,偶尔会有一些关于Gitlab的二开需求,本文将介绍如果在Java中使用Gitlab提供的API
功能介绍
示例中代码,主要的功能如下:
- 读取整个仓库中的所有文件,读取后进行相关的处理
- 使用Webhook,接收gitlab的Webhook请求,进行代码push事件的监听处理
下面具体的示例代码
代码示例
依赖导入
在maven中导入gitlab api的仓库
<dependency>
<groupId>org.gitlab4j</groupId>
<artifactId>gitlab4j-api</artifactId>
<version>4.19.0</version>
</dependency>
配置相关参考
示例中使用的Spring boot,我们在配置文件中添加相关的gitlab配置信息
主要是服务地址和相关的认证信息等,如下
application:
gitlab:
host: http://127.0.0.1:81/
accessToken: xxxxxxxxxxx
namespace: user
projectName: testProject
branch: test
下面是对应的配置类:
@Data
@Configuration
@ConfigurationProperties(prefix = "application.gitlab")
public class GitlabConfig {
private String host;
private String accessToken;
private String namespace;
private String projectName;
private String branch;
}
webhook回调接口
这里简单介绍下Webhook,其主要作用是:
- 当设置的仓库事件发生时(比如代码push,进行了一次提交)
- gitlab就会调用这个回调的接口,发送这次push相关的信息给你的服务
- 回调时里面有相关的提交信息,你可以根据相关的信息来处理
下面这个是回调的接口:
@RestController
@RequestMapping("/")
public class GitlabController {
@PostMapping("/event/push")
public RespResult<String> pushEvent(@RequestBody GitlabPushEvent gitlabPushEvent) {
gitlabService.pushEvent(gitlabPushEvent);
return RespResult.<String>builder().code("200").data("success").build();
}
}
这个是回调的参考,里面有很多,这个类只取了主要的信息,如下所示:
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class GitlabPushEvent {
private String ref;
private List<Commit> commits;
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public static class Commit {
private List<String> added;
private List<String> modified;
private List<String> removed;
}
}
相关的处理逻辑
下面是简单使用示例
@Slf4j
@Service
public class GitlabService {
private final GitlabConfig gitlabConfig;
private final GitLabApi gitLabApi;
public GitlabService(final GitlabConfig gitlabConfig, final GitbookConfig gitbookConfig) {
this.gitlabConfig = gitlabConfig;
gitLabApi = new GitLabApi(gitlabConfig.getHost(), gitlabConfig.getAccessToken());
}
private void readAllFile(final Project project) {
final Project project = gitLabApi.getProjectApi().getProject(gitlabConfig.getNamespace(), gitlabConfig.getProjectName());
final List<String> allFil = getAllFiles(project.getId(), gitlabConfig.getDirectory(), gitlabConfig.getBranch());
allFil.forEach(filePath -> {
final InputStream inputStream = gitLabApi.getRepositoryFileApi().getRawFile(projectId, branch, filePath);
final BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
final StringBuilder stringBuilder = new StringBuilder();
while (reader.ready()) {
final String lineContent = reader.readLine();
}
stringBuilder.append(lineContent);
}
});
}
@SneakyThrows
private List<String> getAllFiles(final Integer projectId, final String directory, final String branch) {
List<String> fileNames = new ArrayList<>();
gitLabApi.getRepositoryApi().getTree(projectId, directory, branch).forEach(file -> {
if (file.getType().equals(TreeItem.Type.TREE)) {
fileNames.addAll(getAllFiles(projectId, file.getPath(), branch));
return;
}
final String filePath = String.join("/",directory, file.getName());
fileNames.add(filePath);
log.info("add file: {}", filePath);
});
return fileNames;
}
@SneakyThrows
public void pushEvent(GitlabPushEvent gitlabPushEvent) {
log.info("收到Gitlab Webhook请求");
if (!gitlabPushEvent.getRef().equals(String.format("refs/heads/%s", gitlabConfig.getBranch()))) {
log.info("非设定分支的push事件,不进行更新");
return;
}
for (GitlabPushEvent.Commit commit: gitlabPushEvent.getCommits()) {
for (String filePath: commit.getAdded()) {
}
for (String filePath: commit.getModified()) {
}
for (String filePath: commit.getRemoved()) {
}
}
}
}
|