Commit 2e1c48d2 authored by liaozan's avatar liaozan 🏀

Update MavenUtils to support update protect branches when generate

parent df7991a6
...@@ -49,7 +49,6 @@ ...@@ -49,7 +49,6 @@
<dependency> <dependency>
<groupId>org.gitlab4j</groupId> <groupId>org.gitlab4j</groupId>
<artifactId>gitlab4j-api</artifactId> <artifactId>gitlab4j-api</artifactId>
<version>5.0.1</version>
</dependency> </dependency>
</dependencies> </dependencies>
......
...@@ -7,6 +7,7 @@ import com.schbrain.common.exception.BaseException; ...@@ -7,6 +7,7 @@ import com.schbrain.common.exception.BaseException;
import com.schbrain.common.util.IdWorker; import com.schbrain.common.util.IdWorker;
import com.schbrain.common.util.JacksonUtils; import com.schbrain.common.util.JacksonUtils;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.maven.cli.MavenCli; import org.apache.maven.cli.MavenCli;
import org.apache.maven.settings.*; import org.apache.maven.settings.*;
import org.apache.maven.settings.io.DefaultSettingsWriter; import org.apache.maven.settings.io.DefaultSettingsWriter;
...@@ -14,9 +15,8 @@ import org.eclipse.jgit.api.Git; ...@@ -14,9 +15,8 @@ import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException; import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.transport.CredentialsProvider; import org.eclipse.jgit.transport.CredentialsProvider;
import org.eclipse.jgit.transport.URIish; import org.eclipse.jgit.transport.URIish;
import org.gitlab4j.api.GitLabApi; import org.gitlab4j.api.*;
import org.gitlab4j.api.GitLabApiException; import org.gitlab4j.api.models.*;
import org.gitlab4j.api.models.Group;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
...@@ -77,35 +77,47 @@ public class MavenUtils { ...@@ -77,35 +77,47 @@ public class MavenUtils {
Git git = Git.init().setInitialBranch("develop").setDirectory(directory.toFile()).call(); Git git = Git.init().setInitialBranch("develop").setDirectory(directory.toFile()).call();
git.add().addFilepattern(".").call(); git.add().addFilepattern(".").call();
git.commit().setAllowEmpty(true).setAuthor("initializer", "no-reply@schbrain.com").setMessage("Initial Commit").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 (repoGroupId != null) {
if (pushToRemote) {
GitLabApi gitLabApi = SpringUtil.getBean(GitLabApi.class); GitLabApi gitLabApi = SpringUtil.getBean(GitLabApi.class);
Group group = gitLabApi.getGroupApi().getGroup(repoGroupId); Group group = gitLabApi.getGroupApi().getGroup(repoGroupId);
String groupPath = group.getFullPath(); 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); URIish urIish = new URIish(repositoryUrl);
git.remoteSetUrl().setRemoteName("origin").setRemoteUri(urIish).call(); git.remoteSetUrl().setRemoteName("origin").setRemoteUri(urIish).call();
}
for (String branch : ADDITIONAL_BRANCHES) {
git.checkout().setName(branch).setCreateBranch(true).call();
}
if (pushToRemote) {
git.push() git.push()
.setRemote("origin") .setRemote("origin")
.setCredentialsProvider(SpringUtil.getBean(CredentialsProvider.class)) .setCredentialsProvider(SpringUtil.getBean(CredentialsProvider.class))
.setPushAll() .setPushAll()
.setForce(true) .setForce(true)
.call(); .call();
Project project = getProject(gitLabApi, groupPath, projectName);
if (project == null) {
return;
}
updateProjectDefaultBranch(gitLabApi, project);
updateProtectedBranches(gitLabApi, project);
} }
git.close();
} catch (GitAPIException | GitLabApiException | URISyntaxException e) { } catch (GitAPIException | GitLabApiException | URISyntaxException e) {
log.warn("Git repo init failed", e); log.warn("Git repo init failed", e);
throw new BaseException("Git仓库初始化失败"); 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() { private static File getSettingsFile() {
if (SETTINGS_FILE.exists()) { if (SETTINGS_FILE.exists()) {
return SETTINGS_FILE; return SETTINGS_FILE;
...@@ -171,4 +183,18 @@ public class MavenUtils { ...@@ -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 @@ ...@@ -7,15 +7,15 @@
}, },
"dependencies": { "dependencies": {
"vue": "^3.2.45", "vue": "^3.2.45",
"axios": "^1.2.0", "axios": "^1.2.1",
"ant-design-vue": "^3.2.15", "ant-design-vue": "^3.2.15",
"highlight.js": "^11.6.0", "highlight.js": "^11.7.0",
"@highlightjs/vue-plugin": "^2.1.0" "@highlightjs/vue-plugin": "^2.1.0"
}, },
"devDependencies": { "devDependencies": {
"@vitejs/plugin-vue": "^3.2.0", "@vitejs/plugin-vue": "^3.2.0",
"typescript": "^4.9.3", "typescript": "^4.9.3",
"vite": "^3.2.4", "vite": "^3.2.5",
"vue-tsc": "^1.0.9" "vue-tsc": "^1.0.11"
} }
} }
\ No newline at end of file
...@@ -101,13 +101,16 @@ const groupOptions = ref<SelectProps['options']>([ ...@@ -101,13 +101,16 @@ const groupOptions = ref<SelectProps['options']>([
"value": "com.schbrain.kp", "value": "com.schbrain.kp",
"label": "com.schbrain.kp" "label": "com.schbrain.kp"
}, },
{
"value": "com.schbrain.rs",
"label": "com.schbrain.rs"
},
{ {
"value": "com.schbrain.xb", "value": "com.schbrain.xb",
"label": "com.schbrain.xb" "label": "com.schbrain.xb"
}, },
]) ])
const formState = reactive<FormState>({ const formState = reactive<FormState>({
groupId: '', groupId: '',
artifactId: '', artifactId: '',
......
...@@ -18,9 +18,10 @@ ...@@ -18,9 +18,10 @@
<properties> <properties>
<revision>0.0.1-SNAPSHOT</revision> <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> <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> </properties>
<modules> <modules>
...@@ -60,6 +61,11 @@ ...@@ -60,6 +61,11 @@
<artifactId>aether-transport-http</artifactId> <artifactId>aether-transport-http</artifactId>
<version>${aether.version}</version> <version>${aether.version}</version>
</dependency> </dependency>
<dependency>
<groupId>org.gitlab4j</groupId>
<artifactId>gitlab4j-api</artifactId>
<version>${gitlab4j-api.version}</version>
</dependency>
</dependencies> </dependencies>
</dependencyManagement> </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