diff --git a/文档/技术实现/20250613-Java使用Maven及Gitlab.md b/文档/技术实现/20250613-Java使用Maven及Gitlab.md index 10cb9c3..67c1cbd 100644 --- a/文档/技术实现/20250613-Java使用Maven及Gitlab.md +++ b/文档/技术实现/20250613-Java使用Maven及Gitlab.md @@ -110,14 +110,6 @@ public class MavenUtils { } 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 参数 String[] mvnArgs = new String[]{ "archetype:generate", @@ -259,6 +251,7 @@ public class MavenUtils { package org.jeecg.modules.devops.archetype; import cn.hutool.core.collection.CollUtil; +import cn.hutool.core.util.StrUtil; import com.alibaba.fastjson.JSONObject; import lombok.SneakyThrows; import lombok.extern.slf4j.Slf4j; @@ -267,6 +260,7 @@ import org.gitlab4j.api.GitLabApiException; import org.gitlab4j.api.models.*; import java.util.List; +import java.util.Objects; /** * insert description here @@ -297,10 +291,10 @@ public class GitlabUtils { @SneakyThrows public static Project createProject(String group, String project, String desc) { Group groupInfo = getGroupId(group); - if (groupInfo == null || groupInfo.getId() == null) { + if (Objects.isNull(groupInfo) || Objects.isNull(groupInfo.getId())) { throw new RuntimeException("获取分组信息失败"); } - Project gitLabRepository = createRepository(groupInfo.getId(), project, desc); + Project gitLabRepository = createRepository(groupInfo, project, desc); if (gitLabRepository == null || gitLabRepository.getId() == null) { throw new RuntimeException("创建仓库失败"); } @@ -309,7 +303,6 @@ public class GitlabUtils { @SneakyThrows public static ProtectedBranch protectedBranch(Long projectId, Integer retryTimes) { - try { // 获取已保护的分支 List protectedBranches = gitLabApi.getProtectedBranchesApi().getProtectedBranches(projectId); @@ -347,21 +340,26 @@ public class GitlabUtils { * 根据分组路径获取分组ID */ private static Group getGroupId(String groupPath) throws GitLabApiException { - return gitLabApi.getGroupApi().getGroup(groupPath); + return gitLabApi.getGroupApi().getOptionalGroup(groupPath).orElse(null); } /** * 在 GitLab 创建新仓库 */ - private static Project createRepository(Long groupId, + private static Project createRepository(Group group, String projectName, String desc) { + Project exists = gitLabApi.getProjectApi().getOptionalProject(group.getName(), projectName).orElse(null); + if (Objects.nonNull(exists)) { + throw new RuntimeException("项目已经存在"); + } + try { Project project = new Project() .withName(projectName) .withDefaultBranch("master") .withDescription(desc) - .withNamespaceId(groupId) + .withNamespaceId(group.getId()) .withVisibility(Visibility.PRIVATE); // 私有库用 PRIVATE return gitLabApi.getProjectApi().createProject(project); // 返回 HTTPS 仓库地址 @@ -371,8 +369,69 @@ public class GitlabUtils { 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调用工具类 ```java