[2025-06-17] 完善文档
All checks were successful
Publish to Confluence / confluence (push) Successful in 52s

This commit is contained in:
liuxiaohua 2025-06-17 18:12:00 +08:00
parent 73e053e2a9
commit 26a842b04f

View File

@ -110,14 +110,6 @@ public class MavenUtils {
} }
private static void generateMavenProject(File baseDir, Path settingsFile, YuanMengOnlineService archetypeInfo) { private static void generateMavenProject(File baseDir, Path settingsFile, YuanMengOnlineService archetypeInfo) {
// String[] argsClean = new String[]{
// "-X", "dependency:resolve", "-s", settingsFile.getAbsolutePath()
// };
// int i = new MavenCli().doMain(argsClean, baseDir.getAbsolutePath(), System.out, System.err);
// boolean del = FileUtil.del("/tmp/repository");
// log.info("delete tmp directory: {}", del);
// 构建 Maven 参数 // 构建 Maven 参数
String[] mvnArgs = new String[]{ String[] mvnArgs = new String[]{
"archetype:generate", "archetype:generate",
@ -259,6 +251,7 @@ public class MavenUtils {
package org.jeecg.modules.devops.archetype; package org.jeecg.modules.devops.archetype;
import cn.hutool.core.collection.CollUtil; import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import lombok.SneakyThrows; import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
@ -267,6 +260,7 @@ import org.gitlab4j.api.GitLabApiException;
import org.gitlab4j.api.models.*; import org.gitlab4j.api.models.*;
import java.util.List; import java.util.List;
import java.util.Objects;
/** /**
* insert description here * insert description here
@ -297,10 +291,10 @@ public class GitlabUtils {
@SneakyThrows @SneakyThrows
public static Project createProject(String group, String project, String desc) { public static Project createProject(String group, String project, String desc) {
Group groupInfo = getGroupId(group); Group groupInfo = getGroupId(group);
if (groupInfo == null || groupInfo.getId() == null) { if (Objects.isNull(groupInfo) || Objects.isNull(groupInfo.getId())) {
throw new RuntimeException("获取分组信息失败"); throw new RuntimeException("获取分组信息失败");
} }
Project gitLabRepository = createRepository(groupInfo.getId(), project, desc); Project gitLabRepository = createRepository(groupInfo, project, desc);
if (gitLabRepository == null || gitLabRepository.getId() == null) { if (gitLabRepository == null || gitLabRepository.getId() == null) {
throw new RuntimeException("创建仓库失败"); throw new RuntimeException("创建仓库失败");
} }
@ -309,7 +303,6 @@ public class GitlabUtils {
@SneakyThrows @SneakyThrows
public static ProtectedBranch protectedBranch(Long projectId, Integer retryTimes) { public static ProtectedBranch protectedBranch(Long projectId, Integer retryTimes) {
try { try {
// 获取已保护的分支 // 获取已保护的分支
List<ProtectedBranch> protectedBranches = gitLabApi.getProtectedBranchesApi().getProtectedBranches(projectId); List<ProtectedBranch> protectedBranches = gitLabApi.getProtectedBranchesApi().getProtectedBranches(projectId);
@ -347,21 +340,26 @@ public class GitlabUtils {
* 根据分组路径获取分组ID * 根据分组路径获取分组ID
*/ */
private static Group getGroupId(String groupPath) throws GitLabApiException { private static Group getGroupId(String groupPath) throws GitLabApiException {
return gitLabApi.getGroupApi().getGroup(groupPath); return gitLabApi.getGroupApi().getOptionalGroup(groupPath).orElse(null);
} }
/** /**
* 在 GitLab 创建新仓库 * 在 GitLab 创建新仓库
*/ */
private static Project createRepository(Long groupId, private static Project createRepository(Group group,
String projectName, String projectName,
String desc) { String desc) {
Project exists = gitLabApi.getProjectApi().getOptionalProject(group.getName(), projectName).orElse(null);
if (Objects.nonNull(exists)) {
throw new RuntimeException("项目已经存在");
}
try { try {
Project project = new Project() Project project = new Project()
.withName(projectName) .withName(projectName)
.withDefaultBranch("master") .withDefaultBranch("master")
.withDescription(desc) .withDescription(desc)
.withNamespaceId(groupId) .withNamespaceId(group.getId())
.withVisibility(Visibility.PRIVATE); // 私有库用 PRIVATE .withVisibility(Visibility.PRIVATE); // 私有库用 PRIVATE
return gitLabApi.getProjectApi().createProject(project); // 返回 HTTPS 仓库地址 return gitLabApi.getProjectApi().createProject(project); // 返回 HTTPS 仓库地址
@ -371,8 +369,69 @@ public class GitlabUtils {
return null; return null;
} }
public static ProjectHook addWebhook(Long projectId, String webhookUrl, Integer retryTimes) {
try {
// 配置 webhook
ProjectHook projectHook = gitLabApi.getProjectApi().addHook(
projectId, webhookUrl, Boolean.FALSE, Boolean.FALSE, Boolean.TRUE);
log.info("add project hook: {}", JSONObject.toJSONString(projectHook));
return projectHook;
} catch (GitLabApiException e) {
log.warn("add project hook failed: {}", e.getMessage());
}
if (retryTimes < 3) {
return addWebhook(projectId, webhookUrl, ++retryTimes);
}
log.warn("add project hook failure: {}", retryTimes);
throw new RuntimeException(ArchetypeUtils.WARN_MESSAGE_2);
}
public static Member addMembers(Long projectId, String email, Integer retryTimes) {
try {
if (StrUtil.isBlank(email)) {
email = "liuxiaohua@keyfil.com";
}
if (!email.endsWith("@keyfil.com")) {
email += "@keyfil.com";
}
User userByEmail = gitLabApi.getUserApi().getOptionalUserByEmail(email).orElse(null);
if (userByEmail == null) {
log.warn("get user by email error");
throw new RuntimeException(ArchetypeUtils.WARN_MESSAGE_3);
}
log.info("add members user: {}", JSONObject.toJSONString(userByEmail));
Member member = gitLabApi.getProjectApi().getOptionalMember(projectId, userByEmail.getId()).orElse(null);
log.info("member exists: {}", JSONObject.toJSONString(member));
if (Objects.nonNull(member)) {
if (member.getAccessLevel().value < AccessLevel.MAINTAINER.value) {
member = gitLabApi.getProjectApi().updateMember(projectId, userByEmail.getId(), AccessLevel.MAINTAINER);
}
} else {
member = gitLabApi.getProjectApi()
.getOptionalMember(projectId, userByEmail.getId(), Boolean.TRUE)
.orElse(null);
if (Objects.isNull(member) || member.getAccessLevel().value < AccessLevel.MAINTAINER.value) {
member = gitLabApi.getProjectApi().addMember(projectId, userByEmail.getId(), AccessLevel.MAINTAINER);
}
}
return member;
} catch (GitLabApiException e) {
log.warn("add members user: failed: {}", e.getMessage());
}
if (retryTimes < 3) {
return addMembers(projectId, email, ++retryTimes);
}
log.warn("add members user failure: {}", retryTimes);
throw new RuntimeException(ArchetypeUtils.WARN_MESSAGE_3);
}
} }
``` ```
#### Git调用工具类 #### Git调用工具类
```java ```java