Commit 2e1c48d2 authored by liaozan's avatar liaozan 🏀

Update MavenUtils to support update protect branches when generate

parent df7991a6
......@@ -49,7 +49,6 @@
<dependency>
<groupId>org.gitlab4j</groupId>
<artifactId>gitlab4j-api</artifactId>
<version>5.0.1</version>
</dependency>
</dependencies>
......
......@@ -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<ProtectedBranch> 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
......@@ -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
......@@ -101,13 +101,16 @@ const groupOptions = ref<SelectProps['options']>([
"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<FormState>({
groupId: '',
artifactId: '',
......
......@@ -18,9 +18,10 @@
<properties>
<revision>0.0.1-SNAPSHOT</revision>
<frontend-maven-plugin.version>1.12.1</frontend-maven-plugin.version>
<maven-embedder.version>3.8.5</maven-embedder.version>
<aether.version>1.1.0</aether.version>
<gitlab4j-api.version>5.0.1</gitlab4j-api.version>
<maven-embedder.version>3.8.5</maven-embedder.version>
<frontend-maven-plugin.version>1.12.1</frontend-maven-plugin.version>
</properties>
<modules>
......@@ -60,6 +61,11 @@
<artifactId>aether-transport-http</artifactId>
<version>${aether.version}</version>
</dependency>
<dependency>
<groupId>org.gitlab4j</groupId>
<artifactId>gitlab4j-api</artifactId>
<version>${gitlab4j-api.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment