From 2e1c48d244fc278cd77067007c1ebf1c770d10d3 Mon Sep 17 00:00:00 2001
From: liaozan <378024053@qq.com>
Date: Wed, 7 Dec 2022 01:40:52 +0800
Subject: [PATCH] Update MavenUtils to support update protect branches when
generate
---
initializer-backend/pom.xml | 1 -
.../initializer/maven/MavenUtils.java | 52 ++++++++++++++-----
initializer-ui/package.json | 8 +--
initializer-ui/src/components/starter.vue | 5 +-
pom.xml | 10 +++-
5 files changed, 55 insertions(+), 21 deletions(-)
diff --git a/initializer-backend/pom.xml b/initializer-backend/pom.xml
index 166ab9d..e7ff14f 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 3f90d21..6d4ab45 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 41345a1..e9a42b4 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 7696c14..ffadb70 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 a42a930..4138e43 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}
+
--
GitLab