Procházet zdrojové kódy

refactor: 移除group功能(未完全实现)

370774330@qq.com před 5 roky
rodič
revize
5285143781

+ 6 - 15
web/src/main/java/seecoder/devcloud/web/controller/project/ProjectController.java

@@ -21,17 +21,15 @@ import java.util.List;
 @RestController
 @RequestMapping("/project")
 public class ProjectController {
-    @Autowired
-    ProjectService projectService;
 
+    private final ProjectService projectService;
 
-    @ApiOperation(value = "根据group获取Project列表", httpMethod = "GET")
-    @GetMapping("/listByGroup/{groupId}")
-    @ApiImplicitParam(name = "groupId",dataType ="int", paramType = "path")
-    public Response<List<ProjectVO>> listProjectsByGroupId(@PathVariable("groupId")int groupId){
-        return Response.buildSuccess(projectService.listProjectsByGroupId(groupId));
+    @Autowired
+    public ProjectController(ProjectService projectService) {
+        this.projectService = projectService;
     }
 
+
     @ApiOperation(value = "根据user获取Project列表", httpMethod = "GET")
     @GetMapping("/listByUser/{userId}")
     @ApiImplicitParam(value = "userId",dataType ="int", paramType = "path")
@@ -53,12 +51,5 @@ public class ProjectController {
         return Response.buildSuccess(projectService.createProject(project));
     }
 
-//    @Deprecated
-//    @ApiOperation(value = "根据projectId删除项目", httpMethod = "DELETE")
-//    @DeleteMapping("/delete/{projectId}")
-//    @ApiImplicitParam(name = "projectId",dataType ="int", paramType = "path")
-//    public Response deleteProject(@PathVariable("projectId")int projectId){
-//        projectService.deleteProject(projectId);
-//        return Response.buildSuccess();
-//    }
+
 }

+ 71 - 71
web/src/main/java/seecoder/devcloud/web/controller/user/GroupController.java

@@ -1,71 +1,71 @@
-package seecoder.devcloud.web.controller.user;
-
-import io.swagger.annotations.Api;
-import io.swagger.annotations.ApiImplicitParam;
-import io.swagger.annotations.ApiImplicitParams;
-import io.swagger.annotations.ApiOperation;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.validation.annotation.Validated;
-import org.springframework.web.bind.annotation.*;
-import seecoder.devcloud.common.exceptions.ServiceException;
-import seecoder.devcloud.web.model.vo.Response;
-import seecoder.devcloud.web.model.vo.user.GroupVO;
-import seecoder.devcloud.web.service.user.GroupService;
-
-import javax.validation.constraints.Pattern;
-import java.util.List;
-
-/**
- * @author PuHong Weng
- * @date 2021/3/18
- * @description:
- */
-@RestController
-@RequestMapping("/groups")
-@Api(tags = "组 api")
-public class GroupController {
-
-    private final GroupService groupService;
-
-    @Autowired
-    public GroupController(GroupService groupService) {
-        this.groupService = groupService;
-    }
-
-    @ApiOperation(value = "根据组id获取组", httpMethod = "GET")
-    @ApiImplicitParam(name = "groupId",dataType ="string", paramType = "query")
-    @GetMapping
-    public Response<GroupVO> getGroup(@RequestParam("groupId") Integer groupId){
-        return Response.buildSuccess(groupService.getGroup(groupId));
-    }
-
-    @ApiOperation(value = "根据组id获取组成员id列表", httpMethod = "GET")
-    @ApiImplicitParam(name = "groupId",dataType ="string", paramType = "query")
-    @GetMapping("/members")
-    public Response<List<Integer>> getGroupMemberIds(@RequestParam("groupId") Integer groupId){
-        return Response.buildSuccess(groupService.getGroupMemberIds(groupId));
-    }
-
-
-    @ApiOperation(value = "创建组", httpMethod = "POST")
-    @ApiImplicitParam(name = "name",dataType ="string", paramType = "query")
-    @PostMapping()
-    public Response<GroupVO> createGroup(@RequestParam("name")
-                                    @Validated
-                                    @Pattern(regexp = "^\\w{3,20}$", message = "组名称只能为长度为3-20且由数字、26个英文字母或者下划线组成的字符串")
-                                            String name) throws ServiceException {
-        return Response.buildSuccess(groupService.createGroup(name));
-    }
-
-    @ApiOperation(value = "添加组成员", httpMethod = "POST")
-    @ApiImplicitParams({
-            @ApiImplicitParam(name = "invitedUserId",dataType ="int", paramType = "query"),
-            @ApiImplicitParam(name = "groupId",dataType ="int", paramType = "query")
-    })
-    @PostMapping("/members")
-    public Response addMember(@RequestParam("invitedUserId") Integer invitedUserId,
-                              @RequestParam("groupId") Integer groupId) throws ServiceException {
-        groupService.addMember(invitedUserId, groupId);
-        return Response.buildSuccess();
-    }
-}
+//package seecoder.devcloud.web.controller.user;
+//
+//import io.swagger.annotations.Api;
+//import io.swagger.annotations.ApiImplicitParam;
+//import io.swagger.annotations.ApiImplicitParams;
+//import io.swagger.annotations.ApiOperation;
+//import org.springframework.beans.factory.annotation.Autowired;
+//import org.springframework.validation.annotation.Validated;
+//import org.springframework.web.bind.annotation.*;
+//import seecoder.devcloud.common.exceptions.ServiceException;
+//import seecoder.devcloud.web.model.vo.Response;
+//import seecoder.devcloud.web.model.vo.user.GroupVO;
+//import seecoder.devcloud.web.service.user.GroupService;
+//
+//import javax.validation.constraints.Pattern;
+//import java.util.List;
+//
+///**
+// * @author PuHong Weng
+// * @date 2021/3/18
+// * @description:
+// */
+//@RestController
+//@RequestMapping("/groups")
+//@Api(tags = "组 api")
+//public class GroupController {
+//
+//    private final GroupService groupService;
+//
+//    @Autowired
+//    public GroupController(GroupService groupService) {
+//        this.groupService = groupService;
+//    }
+//
+//    @ApiOperation(value = "根据组id获取组", httpMethod = "GET")
+//    @ApiImplicitParam(name = "groupId",dataType ="string", paramType = "query")
+//    @GetMapping
+//    public Response<GroupVO> getGroup(@RequestParam("groupId") Integer groupId){
+//        return Response.buildSuccess(groupService.getGroup(groupId));
+//    }
+//
+//    @ApiOperation(value = "根据组id获取组成员id列表", httpMethod = "GET")
+//    @ApiImplicitParam(name = "groupId",dataType ="string", paramType = "query")
+//    @GetMapping("/members")
+//    public Response<List<Integer>> getGroupMemberIds(@RequestParam("groupId") Integer groupId){
+//        return Response.buildSuccess(groupService.getGroupMemberIds(groupId));
+//    }
+//
+//
+//    @ApiOperation(value = "创建组", httpMethod = "POST")
+//    @ApiImplicitParam(name = "name",dataType ="string", paramType = "query")
+//    @PostMapping()
+//    public Response<GroupVO> createGroup(@RequestParam("name")
+//                                    @Validated
+//                                    @Pattern(regexp = "^\\w{3,20}$", message = "组名称只能为长度为3-20且由数字、26个英文字母或者下划线组成的字符串")
+//                                            String name) throws ServiceException {
+//        return Response.buildSuccess(groupService.createGroup(name));
+//    }
+//
+//    @ApiOperation(value = "添加组成员", httpMethod = "POST")
+//    @ApiImplicitParams({
+//            @ApiImplicitParam(name = "invitedUserId",dataType ="int", paramType = "query"),
+//            @ApiImplicitParam(name = "groupId",dataType ="int", paramType = "query")
+//    })
+//    @PostMapping("/members")
+//    public Response addMember(@RequestParam("invitedUserId") Integer invitedUserId,
+//                              @RequestParam("groupId") Integer groupId) throws ServiceException {
+//        groupService.addMember(invitedUserId, groupId);
+//        return Response.buildSuccess();
+//    }
+//}

+ 4 - 11
web/src/main/java/seecoder/devcloud/web/dao/project/ProjectMapper.java

@@ -12,17 +12,9 @@ import java.util.List;
 public interface ProjectMapper {
 
     @Select("select * from project")
-    @Results(id="projectMap",value = {
-            @Result(column = "git_remote_url", property = "gitRemoteUrl"),
-            @Result(column = "group_id", property = "groupId")
-    })
     List<Project> listAll();
 
-    /**
-     * 插入,自增ID
-     */
     @InsertProvider(type = GeneralInsertUpdateSqlProvider.class, method = "insert")
-    @Options(useGeneratedKeys = true, keyProperty = "param1.id")
     int insertProject(Project project, String... ignoredCols);
 
     @UpdateProvider(type= GeneralInsertUpdateSqlProvider.class, method="updateById")
@@ -32,15 +24,16 @@ public interface ProjectMapper {
     @ResultMap(value = "projectMap")
     List<Project> listProjectsByGroupId(int groupId);
 
-    @Select("select * from project where group_id in(select unique group_id from group_member where user_id=#{userId}")
-    @ResultMap(value = "projectMap")
+    @Select("select * from project where project_id in (select project_id from project_user where user_id=#{userId}")
     List<Project> listProjectsByUserId(int userId);
 
     @Select("select * from project where id=#{projectId}")
-    @ResultMap(value = "projectMap")
     Project getProjectById(int projectId);
 
     @Select("select * from project where name=#{name} and group_id=#{groupId}")
     Project getProjectByNameAndGroupId(String name,Integer groupId);
 
+    @Select("select count(*) from project where git_remote_url = #{url}")
+    Integer getProjectCountsByGitRemoteUrl(String url);
+
 }

+ 3 - 1
web/src/main/java/seecoder/devcloud/web/model/po/project/Project.java

@@ -13,9 +13,11 @@ import lombok.NoArgsConstructor;
 @NoArgsConstructor
 @AllArgsConstructor
 public class Project {
+    /**
+     * id来源:统一代码服务生成的id, 并非此后端生成.
+     */
     private Integer id;
     private String name;
     private String gitRemoteUrl;
     private String description;
-    private Integer groupId;
 }

+ 0 - 2
web/src/main/java/seecoder/devcloud/web/model/vo/project/ProjectVO.java

@@ -26,8 +26,6 @@ public class ProjectVO {
     private String gitRemoteUrl;
     @ApiModelProperty("项目描述")
     private String description;
-    @ApiModelProperty("项目所属组id")
-    private Integer groupId;
 
     public ProjectVO(Project project){
         BeanUtils.copyProperties(project,this);

+ 30 - 0
web/src/main/java/seecoder/devcloud/web/service/impl/pipeline/DeploymentServiceImpl.java

@@ -1,6 +1,14 @@
 package seecoder.devcloud.web.service.impl.pipeline;
 
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
+import seecoder.devcloud.core.pipeline.Pipeline;
+import seecoder.devcloud.core.pipeline.PipelineException;
+import seecoder.devcloud.core.pipeline.PipelineFactory;
+import seecoder.devcloud.web.dao.pipeline.PipelineMapper;
+import seecoder.devcloud.web.dao.project.ProjectMapper;
+import seecoder.devcloud.web.model.po.pipeline.PipelinePO;
+import seecoder.devcloud.web.model.po.project.Project;
 import seecoder.devcloud.web.model.vo.pipeline.DeploymentInfoVO;
 import seecoder.devcloud.web.service.pipeline.DeploymentService;
 
@@ -13,6 +21,19 @@ import java.util.List;
  */
 @Service
 public class DeploymentServiceImpl implements DeploymentService {
+
+    private final PipelineMapper pipelineMapper;
+
+    private final ProjectMapper projectMapper;
+
+
+    @Autowired
+    public DeploymentServiceImpl(PipelineMapper pipelineMapper, ProjectMapper projectMapper) {
+        this.pipelineMapper = pipelineMapper;
+        this.projectMapper = projectMapper;
+    }
+
+
     @Override
     public List<DeploymentInfoVO> retrieveDeploymentInfos(Integer projectId) {
         //todo  需要远端查看k8s实例的status
@@ -21,6 +42,15 @@ public class DeploymentServiceImpl implements DeploymentService {
 
     @Override
     public void deploy(Integer projectId, Integer pipelineId) {
+        PipelinePO pipelinePO = pipelineMapper.selectById(pipelineId);
+        Project project = projectMapper.getProjectById(projectId);
+        try {
+            Pipeline pipeline = PipelineFactory.init(pipelinePO.getConfigJson(),project.getName(), pipelinePO.getName());
+            //todo 异步记录结果
+            pipeline.start();
+        } catch (PipelineException pipelineException) {
+            pipelineException.printStackTrace();
+        }
 
     }
 

+ 11 - 11
web/src/main/java/seecoder/devcloud/web/service/impl/project/ProjectServiceImpl.java

@@ -29,16 +29,17 @@ public class ProjectServiceImpl implements ProjectService {
         this.seecoderGitlabApi = seecoderGitlabApi;
     }
 
-    @Override
-    public List<ProjectVO> listProjectsByGroupId(int groupId) {
-        List<Project> projects = projectMapper.listProjectsByGroupId(groupId);
-        return projects.stream().map(ProjectVO::new).collect(Collectors.toList());
-    }
+//    @Override
+//    public List<ProjectVO> listProjectsByGroupId(int groupId) {
+//        List<Project> projects = projectMapper.listProjectsByGroupId(groupId);
+//        return projects.stream().map(ProjectVO::new).collect(Collectors.toList());
+//    }
 
     @Override
     // TODO:该方法待测试
     public List<ProjectVO> listProjectsByUserId(int userId) {
         List<Project> projects = projectMapper.listProjectsByUserId(userId);
+        //todo 改成从gitlab读取
         return projects.stream().map(ProjectVO::new).collect(Collectors.toList());
     }
 
@@ -52,17 +53,16 @@ public class ProjectServiceImpl implements ProjectService {
     public ProjectVO createProject(ProjectVO projectVO) throws ServiceException {
         Project project = new Project();
         BeanUtils.copyProperties(projectVO,project);
-        if (projectMapper.getProjectByNameAndGroupId(projectVO.getName(),projectVO.getGroupId())!=null){
-            throw new ConflictException("创建的项目名称已存在");
+        if (projectMapper.getProjectCountsByGitRemoteUrl(project.getGitRemoteUrl())!=0){
+            throw new ConflictException("创建的项目git url已存在");
         }
-
         try {
-            seecoderGitlabApi.createProject(projectVO.getName(), VisibilityForm.PRIVATE);
+            int projectId = seecoderGitlabApi.createProject(projectVO.getName(), VisibilityForm.PRIVATE);
+            project.setId(projectId);
         } catch (SeecoderGitlabException e) {
-            throw new ServiceException("创建项目时发生异常[" + e.getMessage() + "]", e);
+            throw new ServiceException("创建项目时发生异常[" + e.getMessage() + "]", e);
         }
         projectMapper.insertProject(project);
-
         // TODO:在对应的gitlab仓库中注册WEBHOOK
         return new ProjectVO(project);
 

+ 8 - 6
web/src/main/java/seecoder/devcloud/web/service/pipeline/PipelineService.java

@@ -1,5 +1,7 @@
 package seecoder.devcloud.web.service.pipeline;
 
+import seecoder.devcloud.common.exceptions.AccessDeniedException;
+import seecoder.devcloud.common.exceptions.ServiceException;
 import seecoder.devcloud.web.model.vo.pipeline.PipelineConfigVO;
 import seecoder.devcloud.web.model.vo.pipeline.PipelineCreateVO;
 import seecoder.devcloud.web.model.vo.pipeline.PipelineHistoryVO;
@@ -25,33 +27,33 @@ public interface PipelineService {
     /**
      * 获取一个项目所有的流水线信息
      */
-    List<PipelineInfoVO> retrievePipelineInfos(Integer projectId);
+    List<PipelineInfoVO> retrievePipelineInfos(Integer projectId) throws AccessDeniedException;
 
     /**
      * 获取指定一条流水线的配置json信息
      */
-    PipelineConfigVO retrievePipelineConfig(Integer projectId, Integer pipelineId);
+    PipelineConfigVO retrievePipelineConfig(Integer projectId, Integer pipelineId) throws AccessDeniedException;
 
     /**
      * 获取指定一条流水线的历史构建信息
      */
-    List<PipelineHistoryVO> retrievePipelineHistory(Integer projectId, Integer pipelineId);
+    List<PipelineHistoryVO> retrievePipelineHistory(Integer projectId, Integer pipelineId) throws AccessDeniedException;
 
     /**
      * 依据模板创建一个流水线配置
      */
-    void createPipelineConfig(PipelineCreateVO pipelineCreateVO);
+    void createPipelineConfig(PipelineCreateVO pipelineCreateVO) throws ServiceException;
 
 
     /**
      * 保存一条流水线的配置json信息
      */
-    void savePipelineConfig(Integer projectId,Integer pipelineId, String configJson);
+    void savePipelineConfig(Integer projectId,Integer pipelineId, String configJson) throws AccessDeniedException;
 
     /**
      * 删除一条流水线
      */
-    void deletePipelineConfig(Integer projectId, Integer pipelineId);
+    void deletePipelineConfig(Integer projectId, Integer pipelineId) throws AccessDeniedException;
 
 
     List<String> getTemplateNames();

+ 5 - 5
web/src/main/java/seecoder/devcloud/web/service/project/ProjectService.java

@@ -6,11 +6,11 @@ import seecoder.devcloud.web.model.vo.project.ProjectVO;
 import java.util.List;
 
 public interface ProjectService {
-    /**
-     * 根据group找projects
-     * @return
-     */
-    List<ProjectVO> listProjectsByGroupId(int groupId);
+//    /**
+//     * 根据group找projects
+//     * @return
+//     */
+//    List<ProjectVO> listProjectsByGroupId(int groupId);
 
     /**
      * 根据user找Project

+ 39 - 39
web/src/main/java/seecoder/devcloud/web/service/user/GroupService.java

@@ -1,39 +1,39 @@
-package seecoder.devcloud.web.service.user;
-
-
-import org.springframework.stereotype.Service;
-import seecoder.devcloud.common.exceptions.EntityNotFoundException;
-import seecoder.devcloud.common.exceptions.ServiceException;
-import seecoder.devcloud.web.model.vo.user.GroupVO;
-
-import java.util.List;
-
-/**
- * @InterfaceName GroupService
- * @PackageName com.moekr.moocoder.logic.service.impl
- * @Author sheen
- * @Date 2019/1/9
- * @Version 1.0
- * @Description
- **/
-@Service
-public interface GroupService {
-
-    GroupVO getGroup(Integer groupId);
-
-    List<Integer> getGroupMemberIds(Integer groupId);
-
-
-    /**
-     * 创建组,组的名字前面会加上 GROUP_NAME_PREFIX = "DEVCLOUD_" 的前缀
-     */
-    GroupVO createGroup(String name) throws ServiceException;
-
-    void addMember(Integer invitedUserId, Integer groupId) throws ServiceException;
-
-    /**
-     * todo 目前统一代码服务没有提供对应接口,此方法过期,待统一代码服务api更新后再补回
-     */
-    @Deprecated
-    void quitGroup(Integer groupId, Integer userId) throws ServiceException;
-}
+//package seecoder.devcloud.web.service.user;
+//
+//
+//import org.springframework.stereotype.Service;
+//import seecoder.devcloud.common.exceptions.EntityNotFoundException;
+//import seecoder.devcloud.common.exceptions.ServiceException;
+//import seecoder.devcloud.web.model.vo.user.GroupVO;
+//
+//import java.util.List;
+//
+///**
+// * @InterfaceName GroupService
+// * @PackageName com.moekr.moocoder.logic.service.impl
+// * @Author sheen
+// * @Date 2019/1/9
+// * @Version 1.0
+// * @Description
+// **/
+//@Service
+//public interface GroupService {
+//
+//    GroupVO getGroup(Integer groupId);
+//
+//    List<Integer> getGroupMemberIds(Integer groupId);
+//
+//
+//    /**
+//     * 创建组,组的名字前面会加上 GROUP_NAME_PREFIX = "DEVCLOUD_" 的前缀
+//     */
+//    GroupVO createGroup(String name) throws ServiceException;
+//
+//    void addMember(Integer invitedUserId, Integer groupId) throws ServiceException;
+//
+//    /**
+//     * todo 目前统一代码服务没有提供对应接口,此方法过期,待统一代码服务api更新后再补回
+//     */
+//    @Deprecated
+//    void quitGroup(Integer groupId, Integer userId) throws ServiceException;
+//}

+ 0 - 7
web/src/main/resources/SQLscripts/group.sql

@@ -1,7 +0,0 @@
-create table if not exists devcloud.`group`
-(
-    id         int                      not null
-        primary key,
-    name       varchar(23) charset utf8 not null
-);
-

+ 0 - 8
web/src/main/resources/SQLscripts/group_member.sql

@@ -1,8 +0,0 @@
-create table if not exists devcloud.group_member
-(
-    group_id int not null,
-    user_id  int not null,
-    constraint group_member_group_id_fk
-        foreign key (group_id) references devcloud.`group` (id)
-            on delete cascade
-);

+ 0 - 14
web/src/main/resources/SQLscripts/user.sql

@@ -1,14 +0,0 @@
-create table if not exists devcloud.user
-(
-    id         int                                 not null
-        primary key,
-    username   varchar(23)                         not null,
-    nickname   varchar(23)                         null,
-    password   char(64)                            not null,
-    phone      varchar(23)                         null,
-    email      varchar(63)                         not null,
-    role       varchar(15) charset utf8            null,
-    created_at timestamp default CURRENT_TIMESTAMP null,
-    namespace  int                                 null,
-    token      varchar(63)                         not null
-);