|
|
@@ -7,10 +7,10 @@ import cn.seecoder.web.model.po.user.UserPO;
|
|
|
import cn.seecoder.web.model.vo.fork.ForkVO;
|
|
|
import cn.seecoder.web.service.fork.ForkService;
|
|
|
import com.nju.edu.gitlab.SeecoderGitlabApi;
|
|
|
-import com.nju.edu.gitlab.SeecoderGitlabException;
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
import org.springframework.security.core.context.SecurityContextHolder;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.util.StringUtils;
|
|
|
|
|
|
import java.util.List;
|
|
|
import java.util.Objects;
|
|
|
@@ -23,12 +23,32 @@ public class ForkServiceImpl implements ForkService {
|
|
|
private final ApplicationProperties properties;
|
|
|
|
|
|
@Override
|
|
|
- public ForkVO forkProject(int projectId) {
|
|
|
+ public ForkVO forkProject(ForkVO forkVO) {
|
|
|
UserPO principal = (UserPO) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
|
|
|
Integer userId = principal.getId();
|
|
|
try {
|
|
|
- Integer newProjectId = gitlabApi.forkProject(projectId, userId);
|
|
|
+ ProjectPO oldProject = projectMapper.getProjectById(forkVO.getProjectId());
|
|
|
+ if (oldProject == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ String newName;
|
|
|
+ if (StringUtils.isEmpty(forkVO.getName())) {
|
|
|
+ // 默认命名为 原项目名-用户名
|
|
|
+ newName = oldProject.getName() + "-" + principal.getUsername();
|
|
|
+ } else {
|
|
|
+ // 有用户自定义命名时,使用自定义命名
|
|
|
+ newName = forkVO.getName();
|
|
|
+ }
|
|
|
List<com.nju.edu.gitlab.vo.ProjectVO> allProjects = gitlabApi.getAllProjectsByUserId(userId);
|
|
|
+ // 检测是否已经有重名项目
|
|
|
+ for (com.nju.edu.gitlab.vo.ProjectVO project: allProjects) {
|
|
|
+ if (Objects.equals(project.getProjectName(), newName)) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ Integer newProjectId = gitlabApi.forkProject(forkVO.getProjectId(), userId);
|
|
|
+ allProjects = gitlabApi.getAllProjectsByUserId(userId);
|
|
|
+ // 拿 Fork 出来的项目的对象,主要是要拿到它的 Web URL
|
|
|
com.nju.edu.gitlab.vo.ProjectVO newProject = null;
|
|
|
for (com.nju.edu.gitlab.vo.ProjectVO project: allProjects) {
|
|
|
if (Objects.equals(project.getProjectId(), newProjectId)) {
|
|
|
@@ -39,18 +59,20 @@ public class ForkServiceImpl implements ForkService {
|
|
|
if (newProject == null) {
|
|
|
return null;
|
|
|
}
|
|
|
+ // 向 Devcloud 数据库中插入新项目的 PO
|
|
|
+ // 添加 Gitlab Web Hook 回调
|
|
|
ProjectPO projectPO = new ProjectPO(newProjectId,
|
|
|
- newProject.getProjectName(),
|
|
|
+ newName,
|
|
|
newProject.getWebUrl(),
|
|
|
- "");
|
|
|
+ oldProject.getDescription());
|
|
|
projectMapper.insertProject(projectPO);
|
|
|
- gitlabApi.addWebHook(projectPO.getId(),
|
|
|
+ gitlabApi.addWebHook(newProjectId,
|
|
|
properties.getGitlab().getWebhookProxy(),
|
|
|
true,
|
|
|
false,
|
|
|
false);
|
|
|
- return new ForkVO(newProjectId);
|
|
|
- } catch (SeecoderGitlabException e) {
|
|
|
+ return new ForkVO(newProjectId, newName);
|
|
|
+ } catch (Exception e) {
|
|
|
return null;
|
|
|
}
|
|
|
}
|