feat: workspace 创建/删除 同步更新 GIT 仓库

1. workspace 创建时初始化 GIT 仓库
2. workspace 删除时删除 GIT 仓库目录
This commit is contained in:
shizhendong
2024-10-21 16:39:25 +08:00
parent eb5396437d
commit 8f451c9375

View File

@@ -1,6 +1,7 @@
package net.geedge.asw.module.workspace.service.impl;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.log.Log;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
@@ -12,6 +13,8 @@ import net.geedge.asw.module.workspace.entity.WorkspaceEntity;
import net.geedge.asw.module.workspace.entity.WorkspaceMemberEntity;
import net.geedge.asw.module.workspace.service.IWorkspaceMemberService;
import net.geedge.asw.module.workspace.service.IWorkspaceService;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@@ -23,6 +26,8 @@ import java.util.Map;
@Service
public class WorkspaceServiceImpl extends ServiceImpl<WorkspaceDao, WorkspaceEntity> implements IWorkspaceService {
private final static Log log = Log.get();
@Autowired
private IWorkspaceService workspaceService;
@@ -57,6 +62,16 @@ public class WorkspaceServiceImpl extends ServiceImpl<WorkspaceDao, WorkspaceEnt
workspaceMemberService.saveBatch(members);
}
try {
Git.init()
.setBare(true)
.setDirectory(T.FileUtil.file(T.WebPathUtil.getRootPath(), "workspace", workspace.getId()))
.setInitialBranch("main")
.call();
} catch (GitAPIException e) {
log.error(e, "[saveWorkspace] [git init error]");
throw new RuntimeException(e);
}
return workspace;
}
@@ -90,8 +105,17 @@ public class WorkspaceServiceImpl extends ServiceImpl<WorkspaceDao, WorkspaceEnt
if (workspaceIds.contains("1")) {
throw new ASWException(RCode.WORKSPACE_CANNOT_DELETE);
}
List<WorkspaceEntity> entityList = workspaceService.list(new LambdaQueryWrapper<WorkspaceEntity>().in(WorkspaceEntity::getId, workspaceIds));
if (T.CollUtil.isNotEmpty(entityList)) {
workspaceService.removeBatchByIds(workspaceIds);
workspaceMemberService.remove(new LambdaQueryWrapper<WorkspaceMemberEntity>().in(WorkspaceMemberEntity::getWorkspaceId, workspaceIds));
// remove git repo
for (WorkspaceEntity entity : entityList) {
T.FileUtil.del(T.FileUtil.file(T.WebPathUtil.getRootPath(), "workspace", entity.getId()));
}
}
}
private void validateWorkspaceInfo(WorkspaceEntity workspace, boolean isUpdate) {