diff --git a/initializer-backend/pom.xml b/initializer-backend/pom.xml index 166ab9d925322bf4fb7ebf137d4e37e8f25ce018..e7ff14fdc116808e7033be994b0ff7c278edc1ba 100644 --- a/initializer-backend/pom.xml +++ b/initializer-backend/pom.xml @@ -49,7 +49,6 @@ org.gitlab4j gitlab4j-api - 5.0.1 diff --git a/initializer-backend/src/main/java/com/schbrain/archetype/initializer/maven/MavenUtils.java b/initializer-backend/src/main/java/com/schbrain/archetype/initializer/maven/MavenUtils.java index 3f90d2135fc44766cdb93e95f48f5eda76eb9abd..6d4ab452c0a9c5b6ee5802b7667a42d520103488 100644 --- a/initializer-backend/src/main/java/com/schbrain/archetype/initializer/maven/MavenUtils.java +++ b/initializer-backend/src/main/java/com/schbrain/archetype/initializer/maven/MavenUtils.java @@ -7,6 +7,7 @@ import com.schbrain.common.exception.BaseException; import com.schbrain.common.util.IdWorker; import com.schbrain.common.util.JacksonUtils; import lombok.extern.slf4j.Slf4j; +import org.apache.commons.collections4.CollectionUtils; import org.apache.maven.cli.MavenCli; import org.apache.maven.settings.*; import org.apache.maven.settings.io.DefaultSettingsWriter; @@ -14,9 +15,8 @@ import org.eclipse.jgit.api.Git; import org.eclipse.jgit.api.errors.GitAPIException; import org.eclipse.jgit.transport.CredentialsProvider; import org.eclipse.jgit.transport.URIish; -import org.gitlab4j.api.GitLabApi; -import org.gitlab4j.api.GitLabApiException; -import org.gitlab4j.api.models.Group; +import org.gitlab4j.api.*; +import org.gitlab4j.api.models.*; import java.io.File; import java.io.IOException; @@ -77,35 +77,47 @@ public class MavenUtils { Git git = Git.init().setInitialBranch("develop").setDirectory(directory.toFile()).call(); git.add().addFilepattern(".").call(); git.commit().setAllowEmpty(true).setAuthor("initializer", "no-reply@schbrain.com").setMessage("Initial Commit").call(); + for (String branch : ADDITIONAL_BRANCHES) { + git.checkout().setName(branch).setCreateBranch(true).call(); + } - boolean pushToRemote = repoGroupId != null; - if (pushToRemote) { + if (repoGroupId != null) { GitLabApi gitLabApi = SpringUtil.getBean(GitLabApi.class); Group group = gitLabApi.getGroupApi().getGroup(repoGroupId); String groupPath = group.getFullPath(); - String repositoryUrl = String.format("%s/%s/%s.git", gitLabApi.getGitLabServerUrl(), groupPath, param.getArtifactId()); + String projectName = param.getArtifactId(); + String repositoryUrl = String.format("%s/%s/%s.git", gitLabApi.getGitLabServerUrl(), groupPath, projectName); URIish urIish = new URIish(repositoryUrl); git.remoteSetUrl().setRemoteName("origin").setRemoteUri(urIish).call(); - } - - for (String branch : ADDITIONAL_BRANCHES) { - git.checkout().setName(branch).setCreateBranch(true).call(); - } - if (pushToRemote) { git.push() .setRemote("origin") .setCredentialsProvider(SpringUtil.getBean(CredentialsProvider.class)) .setPushAll() .setForce(true) .call(); + Project project = getProject(gitLabApi, groupPath, projectName); + if (project == null) { + return; + } + updateProjectDefaultBranch(gitLabApi, project); + updateProtectedBranches(gitLabApi, project); } - git.close(); } catch (GitAPIException | GitLabApiException | URISyntaxException e) { log.warn("Git repo init failed", e); throw new BaseException("Git仓库初始化失败"); } } + private static void updateProjectDefaultBranch(GitLabApi gitLabApi, Project project) throws GitLabApiException { + project.setDefaultBranch("main"); + gitLabApi.getProjectApi().updateProject(project); + } + + private static Project getProject(GitLabApi gitLabApi, String groupPath, String projectName) throws GitLabApiException { + String projectPath = String.format("%s/%s", groupPath, projectName); + return gitLabApi.getProjectApi().getProject(projectPath); + } + private static File getSettingsFile() { if (SETTINGS_FILE.exists()) { return SETTINGS_FILE; @@ -171,4 +183,18 @@ public class MavenUtils { }; } + private static void updateProtectedBranches(GitLabApi gitlabApi, Project project) throws GitLabApiException { + ProtectedBranchesApi protectedBranchesApi = gitlabApi.getProtectedBranchesApi(); + String projectPath = project.getPathWithNamespace(); + List protectedBranches = protectedBranchesApi.getProtectedBranches(projectPath); + if (CollectionUtils.isNotEmpty(protectedBranches)) { + for (ProtectedBranch protectedBranch : protectedBranches) { + protectedBranchesApi.unprotectBranch(projectPath, protectedBranch.getName()); + } + } + for (String additionalBranch : ADDITIONAL_BRANCHES) { + protectedBranchesApi.protectBranch(projectPath, additionalBranch); + } + } + } \ No newline at end of file diff --git a/initializer-ui/package.json b/initializer-ui/package.json index 41345a1bd8e376d7edbed75cf5ce82423e89379a..e9a42b49fd16d64e92024f6b2e30ea1ceba7e3d1 100644 --- a/initializer-ui/package.json +++ b/initializer-ui/package.json @@ -7,15 +7,15 @@ }, "dependencies": { "vue": "^3.2.45", - "axios": "^1.2.0", + "axios": "^1.2.1", "ant-design-vue": "^3.2.15", - "highlight.js": "^11.6.0", + "highlight.js": "^11.7.0", "@highlightjs/vue-plugin": "^2.1.0" }, "devDependencies": { "@vitejs/plugin-vue": "^3.2.0", "typescript": "^4.9.3", - "vite": "^3.2.4", - "vue-tsc": "^1.0.9" + "vite": "^3.2.5", + "vue-tsc": "^1.0.11" } } \ No newline at end of file diff --git a/initializer-ui/src/components/starter.vue b/initializer-ui/src/components/starter.vue index 7696c14ca79c03722a33d22c0504d10364692172..ffadb709c21a824589c7fcb73acb0a5f1a89bfb8 100644 --- a/initializer-ui/src/components/starter.vue +++ b/initializer-ui/src/components/starter.vue @@ -101,13 +101,16 @@ const groupOptions = ref([ "value": "com.schbrain.kp", "label": "com.schbrain.kp" }, + { + "value": "com.schbrain.rs", + "label": "com.schbrain.rs" + }, { "value": "com.schbrain.xb", "label": "com.schbrain.xb" }, ]) - const formState = reactive({ groupId: '', artifactId: '', diff --git a/pom.xml b/pom.xml index a42a930d80cdb01cb1bf43e01496dc6d1f52bde2..4138e43097fd4cbd68c0588d23f17b41f40e1d81 100644 --- a/pom.xml +++ b/pom.xml @@ -18,9 +18,10 @@ 0.0.1-SNAPSHOT - 1.12.1 - 3.8.5 1.1.0 + 5.0.1 + 3.8.5 + 1.12.1 @@ -60,6 +61,11 @@ aether-transport-http ${aether.version} + + org.gitlab4j + gitlab4j-api + ${gitlab4j-api.version} +