From 91338fafb2b88a1d742d7f97b4b598ef93ae57bf Mon Sep 17 00:00:00 2001 From: jiangxiaopeng Date: Fri, 23 Dec 2022 14:42:51 +0800 Subject: [PATCH] update gitlab and zentao interface --- pom.xml | 245 +++++++++++++-------- src/META-INF/MANIFEST.MF | 3 + .../com/whn/hellospring/AgileToolsApplication.java | 8 +- .../com/whn/hellospring/common/StateMessage.java | 19 +- .../com/whn/hellospring/config/GitlabConfig.java | 14 ++ .../hellospring/controller/GitlabController.java | 136 ++++++++++++ .../whn/hellospring/controller/UserController.java | 8 +- .../hellospring/controller/ZenTaoController.java | 66 ++++++ .../whn/hellospring/handler/MoneyTypeHandler.java | 44 ---- .../com/whn/hellospring/mapper/ZenTaoMapper.java | 14 ++ .../com/whn/hellospring/model/MenuCoffeeDO.java | 31 --- .../whn/hellospring/model/gitlab/GitlabFile.java | 37 ++++ .../whn/hellospring/model/zentao/ZenTaoUserDO.java | 9 +- .../whn/hellospring/repository/UserRepository.java | 1 + .../hellospring/repository/ZenTaoRepository.java | 8 +- .../request/UpdateGitlabFileRequest.java | 29 +++ .../request/UpdateGitlabUserRequest.java | 16 ++ .../request/UpdateZenTaoUserRequest.java | 16 ++ .../com/whn/hellospring/service/GitlabService.java | 220 ++++++++++++++++++ .../com/whn/hellospring/service/UserService.java | 5 +- .../com/whn/hellospring/service/ZenTaoService.java | 22 +- .../java/com/whn/hellospring/utils/HttpUtils.java | 6 - src/main/resources/application.yml | 13 +- src/main/resources/mapper/ZenTao.xml | 27 +++ 24 files changed, 804 insertions(+), 193 deletions(-) create mode 100644 src/META-INF/MANIFEST.MF create mode 100644 src/main/java/com/whn/hellospring/config/GitlabConfig.java create mode 100644 src/main/java/com/whn/hellospring/controller/GitlabController.java create mode 100644 src/main/java/com/whn/hellospring/controller/ZenTaoController.java delete mode 100644 src/main/java/com/whn/hellospring/handler/MoneyTypeHandler.java create mode 100644 src/main/java/com/whn/hellospring/mapper/ZenTaoMapper.java delete mode 100644 src/main/java/com/whn/hellospring/model/MenuCoffeeDO.java create mode 100644 src/main/java/com/whn/hellospring/model/gitlab/GitlabFile.java create mode 100644 src/main/java/com/whn/hellospring/request/UpdateGitlabFileRequest.java create mode 100644 src/main/java/com/whn/hellospring/request/UpdateGitlabUserRequest.java create mode 100644 src/main/java/com/whn/hellospring/request/UpdateZenTaoUserRequest.java create mode 100644 src/main/java/com/whn/hellospring/service/GitlabService.java create mode 100644 src/main/resources/mapper/ZenTao.xml diff --git a/pom.xml b/pom.xml index 5ce3c5f..140607e 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ org.springframework.boot spring-boot-starter-parent - 2.3.1.RELEASE + 2.5.6 com.whn @@ -25,13 +25,6 @@ - - - org.aspectj - aspectjweaver - 1.8.8 - - org.springframework.boot spring-boot-starter-aop @@ -129,11 +122,11 @@ spring-boot-starter-data-jpa - - org.joda - joda-money - LATEST - + + + + + org.jadira.usertype usertype.core @@ -157,7 +150,7 @@ com.baomidou dynamic-datasource-spring-boot-starter - 3.0.0 + 3.2.0 @@ -190,11 +183,11 @@ mysql-connector-java - - com.googlecode.log4jdbc - log4jdbc - RELEASE - + + + + + com.spring4all @@ -224,91 +217,167 @@ test + + org.springframework.boot + spring-boot-starter-aop + + + org.aspectj + aspectjweaver + 1.9.6 + + + + org.gitlab4j + gitlab4j-api + 4.19.0 + + + + org.springframework.boot + spring-boot-configuration-processor + true + + - test + um-backend org.springframework.boot spring-boot-maven-plugin - - - org.jetbrains.kotlin - kotlin-maven-plugin - ${kotlin.version} - - - compile - compile - - compile - - - - test-compile - test-compile - - test-compile - - - - - 1.8 - - - - org.apache.maven.plugins - maven-compiler-plugin - - - compile - compile - - compile - - - - testCompile - test-compile - - testCompile - - - - - - - com.spotify - docker-maven-plugin - 1.2.2 - build-image - package - build + repackage + + + org.apache.maven.plugins + maven-compiler-plugin - testc - true - - 111 - - openjdk:8-jdk-alpine - ["sh", "-c", "java $JAVA_OPTIONS -jar /${project.build.finalName}.jar"] - - - / - ${project.build.directory} - ${project.build.finalName}.jar - - + 8 + 8 - + + + + ${project.basedir}/lib/ + BOOT-INF/lib/ + + **/*.jar + + + + src/main/resources + + **/*.xml + **/*.yml + + false + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/META-INF/MANIFEST.MF b/src/META-INF/MANIFEST.MF new file mode 100644 index 0000000..a567b37 --- /dev/null +++ b/src/META-INF/MANIFEST.MF @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 +Main-Class: com.whn.hellospring.AgileToolsApplication + diff --git a/src/main/java/com/whn/hellospring/AgileToolsApplication.java b/src/main/java/com/whn/hellospring/AgileToolsApplication.java index 491093c..6172aa2 100644 --- a/src/main/java/com/whn/hellospring/AgileToolsApplication.java +++ b/src/main/java/com/whn/hellospring/AgileToolsApplication.java @@ -1,6 +1,8 @@ package com.whn.hellospring; +import com.baomidou.mybatisplus.autoconfigure.MybatisPlusAutoConfiguration; import com.whn.hellospring.service.JuheService; +import org.mybatis.spring.annotation.MapperScan; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.ApplicationArguments; import org.springframework.boot.ApplicationRunner; @@ -9,6 +11,7 @@ import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.domain.EntityScan; import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; +import org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; import org.springframework.context.annotation.ComponentScan; @@ -30,11 +33,12 @@ import redis.clients.jedis.Jedis; ////@EntityScan(basePackages="com.whn.hellospring") @ComponentScan("com") -@SpringBootApplication @RestController @Slf4j -@EnableJpaRepositories +@EnableJpaRepositories("com.whn.hellospring.repository") +@MapperScan("com.whn.hellospring.mapper") @EnableTransactionManagement +@SpringBootApplication(exclude={DataSourceAutoConfiguration.class, DataSourceTransactionManagerAutoConfiguration.class, MybatisPlusAutoConfiguration.class}) public class AgileToolsApplication extends SpringBootServletInitializer { diff --git a/src/main/java/com/whn/hellospring/common/StateMessage.java b/src/main/java/com/whn/hellospring/common/StateMessage.java index c06a0cd..7568cfc 100644 --- a/src/main/java/com/whn/hellospring/common/StateMessage.java +++ b/src/main/java/com/whn/hellospring/common/StateMessage.java @@ -1,5 +1,4 @@ package com.whn.hellospring.common; - /** * @author lfx * 返回客户端结果定义 @@ -7,20 +6,32 @@ package com.whn.hellospring.common; public enum StateMessage { SUCCESS(true, "000", "成功"), - PHONE_ERROR(false, "001", "手机号不匹配"), - LOSE_PARAM(false, "002", "缺少参数"), USER_NOT_EXIST(false, "003", "用户不存在"), USER_EXIST(false, "105", "用户已存在"), + UNIT_Test_EXIST(false, "106", "单元测试名称已存在"), + UNIT_Test_ID_NOT_EXIST(false, "107", "单元测试id不存在"), + UNIT_Test_NAME_EMPTY(false, "106", "单元测试名称不能为空"), + + Test_Seq_EXIST(false, "106", "测试序列名称已存在"), + Test_Seq_ID_NOT_EXIST(false, "107", "测试序列id不存在"), + Test_Seq_NAME_EMPTY(false, "108", "测试序列名称不能为空"), + + EXCEL_FILE_NOT_EXISTS(false, "201", "不存在该文件,请检查路径"), + + GITLAB_USERNAME_ERROR(false, "301", "用户名不符合规范,只能由字母、数字、“-”、“_”、“.”组成,且不能以“-”开头,不能以“.”、“.git”或者“.atom”结尾"), + CAPTCHA_OVERDUE(false, "004", "验证码过期"), CAPTCHA_FAILURE(false, "005", "验证码不匹配"), - SIGN_IN_FAILED(false, "006", "签到失败"), LOGIN_OTHER_DEVICE(false, "007", "异常登录"), TOKEN_ERROR(false, "008", "用户凭证错误"), OPERATION_FAILED(false, "009", "操作失败"), OUT_PERMISSION(false, "010", "权限不足"), PARAMETER_FORMAT_ERROR(false, "011", "参数格式不匹配"), ACCOUNT_EXIST(false, "016", "用户名重复"), + EMAIL_ERROR(false, "717", "禅道电子邮箱格式错误"), + + UN_KNOW_REASON(false, "9999", "未知错误!"); diff --git a/src/main/java/com/whn/hellospring/config/GitlabConfig.java b/src/main/java/com/whn/hellospring/config/GitlabConfig.java new file mode 100644 index 0000000..061e707 --- /dev/null +++ b/src/main/java/com/whn/hellospring/config/GitlabConfig.java @@ -0,0 +1,14 @@ +package com.whn.hellospring.config; + +import lombok.Data; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.context.annotation.Configuration; + +@Data +@Configuration +@ConfigurationProperties(prefix = "application.gitlab") +public class GitlabConfig { + private String host; + private String accessToken; + private String name; +} diff --git a/src/main/java/com/whn/hellospring/controller/GitlabController.java b/src/main/java/com/whn/hellospring/controller/GitlabController.java new file mode 100644 index 0000000..550e45c --- /dev/null +++ b/src/main/java/com/whn/hellospring/controller/GitlabController.java @@ -0,0 +1,136 @@ +package com.whn.hellospring.controller; + +import com.whn.hellospring.common.StateMessage; +import com.whn.hellospring.common.Status; +import com.whn.hellospring.model.gitlab.GitlabFile; +import com.whn.hellospring.request.UpdateGitlabFileRequest; +import com.whn.hellospring.request.UpdateGitlabUserRequest; +import com.whn.hellospring.service.GitlabService; +import org.apache.commons.lang3.StringUtils; +import org.gitlab4j.api.models.Project; +import org.gitlab4j.api.models.User; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.*; + +import java.util.ArrayList; +import java.util.List; + +@RestController +@RequestMapping("/gitlab") +public class GitlabController { + + @Autowired + private GitlabService gitlabService; + + @GetMapping(value = "/list") + public Status getGitlabProjectList(@RequestHeader String token){ + try{ + //TODO 后续需要对token进行校验 + List allProjects = gitlabService.getAllProjects(); + return new Status(StateMessage.SUCCESS, allProjects); + }catch (Exception e){ + return new Status(StateMessage.UN_KNOW_REASON); + } + } + + @GetMapping(value = "fileList") + public Status getGitlabFileList(@RequestHeader String token, + @RequestParam Integer projectId, + @RequestParam String branchName, + @RequestParam(required = false) String path){ + try{ + //TODO 后续需要对token进行校验 + if(StringUtils.isBlank(path)){ + path = ""; + } + List dirsAndFiles = gitlabService.getDirsAndFiles(projectId, path, branchName); + return new Status(StateMessage.SUCCESS, dirsAndFiles); + }catch (Exception e){ + return new Status(StateMessage.UN_KNOW_REASON); + } + } + + @GetMapping(value = "/allBranch") + public Status getAllBranchInOneProject(@RequestHeader String token, + @RequestParam int projectId){ + try{ + //TODO 后续需要对token进行校验 + List branchNames = new ArrayList<>(); + gitlabService.getAllBranchInOneProject(projectId).forEach( v -> { + branchNames.add(v.getName()); + }); + return new Status(StateMessage.SUCCESS, branchNames); + }catch (Exception e){ + return new Status(StateMessage.UN_KNOW_REASON); + } + } + + @GetMapping(value = "/fileContent") + public Status getFileContent(@RequestHeader String token, + @RequestParam Integer projectId, + @RequestParam String branchName, + @RequestParam String filePath){ + try{ + //TODO 后续需要对token进行校验 + String fileContent = gitlabService.getFileContent(projectId, filePath, branchName); + return new Status(StateMessage.SUCCESS, fileContent); + }catch (Exception e){ + return new Status(StateMessage.UN_KNOW_REASON); + } + } + + @PostMapping(value = "/updateFile") + public Status updateFile(@RequestHeader String token, + @RequestBody UpdateGitlabFileRequest request){ + try{ + //TODO 后续需要对token进行校验 + boolean resp = gitlabService.updateOneFile(request.getProjectId(), + request.getFileContent(), request.getFilePath(), + request.getFileName(), request.getBranchName(), request.getCommitMessage()); + if(resp){ + return new Status(StateMessage.SUCCESS); + }else{ + return new Status(StateMessage.UN_KNOW_REASON); + } + }catch (Exception e){ + return new Status(StateMessage.UN_KNOW_REASON); + } + } + + @GetMapping(value = "/userDetails") + public Status getUserDetails(@RequestHeader String token, + @RequestParam Integer userId){ + try{ + //TODO 后续需要对token进行校验 + User resp = gitlabService.getUserDetails(userId); + return new Status(StateMessage.SUCCESS, resp); + }catch (Exception e){ + return new Status(StateMessage.UN_KNOW_REASON); + } + } + + @PostMapping(value = "/updateUser") + public Status updateUser(@RequestHeader String token, + @RequestBody UpdateGitlabUserRequest request){ + try{ + //TODO 后续需要对token进行校验 + //检验用户名是否符合gitlab的要求 + if(request.getUser() != null && request.getUser().getUsername() != null){ + String reg = "^(?!.*\\.(?:git|atom)?$)[\\w.][-\\w.]*$"; + if(!request.getUser().getUsername().matches(reg)){ + return new Status(StateMessage.GITLAB_USERNAME_ERROR); + } + } + boolean resp = gitlabService.updateUser(request.getUser(), request.getPassword()); + if(resp){ + return new Status(StateMessage.SUCCESS); + }else{ + return new Status(StateMessage.UN_KNOW_REASON); + } + }catch (Exception e){ + return new Status(StateMessage.UN_KNOW_REASON); + } + } + + +} diff --git a/src/main/java/com/whn/hellospring/controller/UserController.java b/src/main/java/com/whn/hellospring/controller/UserController.java index 68fc2c1..377335c 100644 --- a/src/main/java/com/whn/hellospring/controller/UserController.java +++ b/src/main/java/com/whn/hellospring/controller/UserController.java @@ -1,5 +1,6 @@ package com.whn.hellospring.controller; +import com.baomidou.dynamic.datasource.annotation.DS; import com.whn.hellospring.common.StateMessage; import com.whn.hellospring.common.Status; import com.whn.hellospring.log.OperationLogAnnotation; @@ -12,13 +13,15 @@ import com.whn.hellospring.service.ZenTaoService; import com.whn.hellospring.utils.DO2DTOUtil; import com.whn.hellospring.utils.MergeObjectsUtil; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.transaction.annotation.Propagation; +import org.springframework.transaction.annotation.Transactional; import org.springframework.web.bind.annotation.*; import javax.validation.Valid; import java.util.List; -@RestController //处理http请求,默认返回json格式数据 -@RequestMapping(value = "/user") //配置映射 +@RestController +@RequestMapping(value = "/user") public class UserController { @Autowired @@ -220,7 +223,6 @@ public class UserController { } } - /** * 根据token 获取客户 */ diff --git a/src/main/java/com/whn/hellospring/controller/ZenTaoController.java b/src/main/java/com/whn/hellospring/controller/ZenTaoController.java new file mode 100644 index 0000000..baab05f --- /dev/null +++ b/src/main/java/com/whn/hellospring/controller/ZenTaoController.java @@ -0,0 +1,66 @@ +package com.whn.hellospring.controller; + +import com.whn.hellospring.common.StateMessage; +import com.whn.hellospring.common.Status; +import com.whn.hellospring.model.zentao.ZenTaoUserDO; +import com.whn.hellospring.request.UpdateZenTaoUserRequest; +import com.whn.hellospring.service.UserService; +import com.whn.hellospring.service.ZenTaoService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.transaction.annotation.Propagation; +import org.springframework.transaction.annotation.Transactional; +import org.springframework.web.bind.annotation.*; + +@RestController +@RequestMapping(value = "/zentao") +public class ZenTaoController { + + @Autowired + private UserService userService; + + @Autowired + private ZenTaoService zenTaoService; + + @GetMapping(value = "/userDetails") + @Transactional(propagation = Propagation.REQUIRES_NEW) + public Status getZenTaoUserDetails(@RequestHeader String token, + @RequestParam Long userId){ + try { + if(!userService.checkToken(token)){ + return new Status(StateMessage.TOKEN_ERROR); + } + ZenTaoUserDO user = zenTaoService.getUserByParentUserId(userId); + Status status = new Status(StateMessage.SUCCESS, user); + return status; + } catch (Exception e) { + return new Status(StateMessage.UN_KNOW_REASON); + } + } + + @PostMapping(value = "/updateUser") + @Transactional(propagation = Propagation.REQUIRES_NEW) + public Status updateZenTaoUser(@RequestHeader String token, + @RequestBody UpdateZenTaoUserRequest request){ + try { + if(!userService.checkToken(token)){ + return new Status(StateMessage.TOKEN_ERROR); + } + if(request.getUser().getEmail() != null){ + if(!request.getUser().getEmail().matches("[a-zA-Z0-9]+@[a-zA-Z0-9]+\\.[a-zA-Z0-9]+")){ + return new Status(StateMessage.EMAIL_ERROR); + } + } + boolean req = zenTaoService.updateUserById(request.getUserId(), request.getUser()); + if(req){ + return new Status(StateMessage.SUCCESS); + }else{ + return new Status(StateMessage.UN_KNOW_REASON); + } + } catch (Exception e) { + return new Status(StateMessage.UN_KNOW_REASON); + } + } + + + +} diff --git a/src/main/java/com/whn/hellospring/handler/MoneyTypeHandler.java b/src/main/java/com/whn/hellospring/handler/MoneyTypeHandler.java deleted file mode 100644 index 05c1a9d..0000000 --- a/src/main/java/com/whn/hellospring/handler/MoneyTypeHandler.java +++ /dev/null @@ -1,44 +0,0 @@ -package com.whn.hellospring.handler; - -import org.apache.ibatis.type.BaseTypeHandler; -import org.apache.ibatis.type.JdbcType; -import org.joda.money.CurrencyUnit; -import org.joda.money.Money; - -import java.sql.CallableStatement; -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.SQLException; - -/** - * 在Money与Long之间转换的TypeHandler,处理CNY和人民币 - */ -public class MoneyTypeHandler extends BaseTypeHandler { - @Override - public void setNonNullParameter(PreparedStatement preparedStatement, int i, Money money, JdbcType jdbcType) throws SQLException { - preparedStatement.setLong(i,money.getAmountMinorLong()); //存储为Long类型,精确到分 - } - - @Override - public Money getNullableResult(ResultSet resultSet, String s) throws SQLException { - return parseMoney(resultSet.getLong(s)); - } - - @Override - public Money getNullableResult(ResultSet resultSet, int i) throws SQLException { - return parseMoney(resultSet.getLong(i)); - } - - @Override - public Money getNullableResult(CallableStatement callableStatement, int i) throws SQLException { - return parseMoney(callableStatement.getLong(i)); - } - - /** - * 将Long值转换为Money - */ - private Money parseMoney(Long value){ - return Money.ofMinor(CurrencyUnit.of("CNY"),value); - } - -} diff --git a/src/main/java/com/whn/hellospring/mapper/ZenTaoMapper.java b/src/main/java/com/whn/hellospring/mapper/ZenTaoMapper.java new file mode 100644 index 0000000..8929a08 --- /dev/null +++ b/src/main/java/com/whn/hellospring/mapper/ZenTaoMapper.java @@ -0,0 +1,14 @@ +package com.whn.hellospring.mapper; + +import com.whn.hellospring.model.zentao.ZenTaoUserDO; +import org.apache.ibatis.annotations.Param; +import org.springframework.stereotype.Repository; + + +@Repository +public interface ZenTaoMapper{ + + boolean updateUser(@Param("id") Long userId, @Param("user") ZenTaoUserDO user); + +} + diff --git a/src/main/java/com/whn/hellospring/model/MenuCoffeeDO.java b/src/main/java/com/whn/hellospring/model/MenuCoffeeDO.java deleted file mode 100644 index 5f1a839..0000000 --- a/src/main/java/com/whn/hellospring/model/MenuCoffeeDO.java +++ /dev/null @@ -1,31 +0,0 @@ -package com.whn.hellospring.model; - -import lombok.*; -import org.hibernate.annotations.Type; -import org.joda.money.Money; - -import javax.persistence.Column; -import javax.persistence.Entity; -import javax.persistence.Table; -import java.io.Serializable; - -@Entity -@Table(name = "T_MENU_COFFEE") -@Builder -@Data -@ToString(callSuper = true) -@NoArgsConstructor -@AllArgsConstructor -public class MenuCoffeeDO extends BaseEntity implements Serializable { - private Long menu_id_fk; - - private String name; - - @Column - @Type(type = "org.jadira.usertype.moneyandcurrency.joda.PersistentMoneyAmount", - parameters = {@org.hibernate.annotations.Parameter(name = "currencyCode", value = "CNY")}) - private Money price; - - -} - diff --git a/src/main/java/com/whn/hellospring/model/gitlab/GitlabFile.java b/src/main/java/com/whn/hellospring/model/gitlab/GitlabFile.java new file mode 100644 index 0000000..99fcfce --- /dev/null +++ b/src/main/java/com/whn/hellospring/model/gitlab/GitlabFile.java @@ -0,0 +1,37 @@ +package com.whn.hellospring.model.gitlab; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.util.Date; + +@Data +@AllArgsConstructor +@NoArgsConstructor +public class GitlabFile { + /** + * 文件名 + */ + private String name; + /** + * 是否是一个文件夹 + */ + private boolean isDir; + + /** + * 完整路径 + */ + private String path; + + /** + * 最后修改日期 + */ + private Date updateTime; + + /** + * 修改人 + */ + private String auth; + +} diff --git a/src/main/java/com/whn/hellospring/model/zentao/ZenTaoUserDO.java b/src/main/java/com/whn/hellospring/model/zentao/ZenTaoUserDO.java index be4ebd3..6b1a465 100644 --- a/src/main/java/com/whn/hellospring/model/zentao/ZenTaoUserDO.java +++ b/src/main/java/com/whn/hellospring/model/zentao/ZenTaoUserDO.java @@ -4,6 +4,7 @@ import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import lombok.*; import javax.persistence.*; +import javax.validation.constraints.Pattern; import java.io.Serializable; import java.util.Date; @@ -62,8 +63,8 @@ public class ZenTaoUserDO implements Serializable { private Long visits; private String visions; private String ip; - private int last; - private int fails; + private Integer last; + private Integer fails; private Date locked; @@ -71,11 +72,11 @@ public class ZenTaoUserDO implements Serializable { private String ranzhi; private String ldap; - private int score; + private Integer score; @Column( name="scorelevel" ) - private int scoreLevel; + private Integer scoreLevel; @Column( name="resettoken" ) diff --git a/src/main/java/com/whn/hellospring/repository/UserRepository.java b/src/main/java/com/whn/hellospring/repository/UserRepository.java index b3f19e9..37a2bcc 100644 --- a/src/main/java/com/whn/hellospring/repository/UserRepository.java +++ b/src/main/java/com/whn/hellospring/repository/UserRepository.java @@ -1,5 +1,6 @@ package com.whn.hellospring.repository; +import com.baomidou.dynamic.datasource.annotation.DS; import com.whn.hellospring.model.UserDO; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Modifying; diff --git a/src/main/java/com/whn/hellospring/repository/ZenTaoRepository.java b/src/main/java/com/whn/hellospring/repository/ZenTaoRepository.java index c150a7d..b5d9f06 100644 --- a/src/main/java/com/whn/hellospring/repository/ZenTaoRepository.java +++ b/src/main/java/com/whn/hellospring/repository/ZenTaoRepository.java @@ -3,11 +3,13 @@ package com.whn.hellospring.repository; import com.baomidou.dynamic.datasource.annotation.DS; import com.whn.hellospring.model.UserDO; import com.whn.hellospring.model.zentao.ZenTaoUserDO; +import org.apache.ibatis.annotations.Param; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; +import org.springframework.stereotype.Repository; -import java.util.List; +@Repository public interface ZenTaoRepository extends JpaRepository { @@ -19,9 +21,9 @@ public interface ZenTaoRepository extends JpaRepository { // public List findbyUser_id(Long user_id); + @Query(value = "select * from zt_user where id=?1", nativeQuery = true) + ZenTaoUserDO getDetailByUserId(Long user_id); - @Query(value = "select * from zt_user where user_id=?1", nativeQuery = true) - ZenTaoUserDO getDetailByUserId(Long user_id); } diff --git a/src/main/java/com/whn/hellospring/request/UpdateGitlabFileRequest.java b/src/main/java/com/whn/hellospring/request/UpdateGitlabFileRequest.java new file mode 100644 index 0000000..15e2033 --- /dev/null +++ b/src/main/java/com/whn/hellospring/request/UpdateGitlabFileRequest.java @@ -0,0 +1,29 @@ +package com.whn.hellospring.request; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.NoArgsConstructor; + +import java.io.Serializable; + +@Data +@EqualsAndHashCode(callSuper = false) +@AllArgsConstructor +@NoArgsConstructor +public class UpdateGitlabFileRequest implements Serializable { + private static final long serialVersionUID = 1L; + + private Integer projectId; + + private String fileName; + + private String filePath; + + private String fileContent; + + private String branchName; + + private String commitMessage; + +} diff --git a/src/main/java/com/whn/hellospring/request/UpdateGitlabUserRequest.java b/src/main/java/com/whn/hellospring/request/UpdateGitlabUserRequest.java new file mode 100644 index 0000000..1b00737 --- /dev/null +++ b/src/main/java/com/whn/hellospring/request/UpdateGitlabUserRequest.java @@ -0,0 +1,16 @@ +package com.whn.hellospring.request; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.NoArgsConstructor; +import org.gitlab4j.api.models.User; + +@Data +@EqualsAndHashCode(callSuper = false) +@AllArgsConstructor +@NoArgsConstructor +public class UpdateGitlabUserRequest { + private User user; + private String password; +} diff --git a/src/main/java/com/whn/hellospring/request/UpdateZenTaoUserRequest.java b/src/main/java/com/whn/hellospring/request/UpdateZenTaoUserRequest.java new file mode 100644 index 0000000..5cca0b1 --- /dev/null +++ b/src/main/java/com/whn/hellospring/request/UpdateZenTaoUserRequest.java @@ -0,0 +1,16 @@ +package com.whn.hellospring.request; + +import com.whn.hellospring.model.zentao.ZenTaoUserDO; +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@NoArgsConstructor +@AllArgsConstructor +public class UpdateZenTaoUserRequest { + + private Long userId; + private ZenTaoUserDO user; + +} diff --git a/src/main/java/com/whn/hellospring/service/GitlabService.java b/src/main/java/com/whn/hellospring/service/GitlabService.java new file mode 100644 index 0000000..efb78f3 --- /dev/null +++ b/src/main/java/com/whn/hellospring/service/GitlabService.java @@ -0,0 +1,220 @@ +package com.whn.hellospring.service; + +import com.whn.hellospring.config.GitlabConfig; +import com.whn.hellospring.model.gitlab.GitlabFile; +import lombok.SneakyThrows; +import lombok.extern.slf4j.Slf4j; +import org.gitlab4j.api.GitLabApi; +import org.gitlab4j.api.models.*; +import org.springframework.stereotype.Service; + +import java.io.BufferedReader; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.util.ArrayList; +import java.util.List; + +@Slf4j +@Service +public class GitlabService { + + private final GitlabConfig gitlabConfig; + private final GitLabApi gitLabApi; + + public GitlabService(final GitlabConfig gitlabConfig) { + this.gitlabConfig = gitlabConfig; + // 传入gitlab服务地址和token,初始化服务 + gitLabApi = new GitLabApi(gitlabConfig.getHost(), gitlabConfig.getAccessToken()); + } + + /** + * 获取所有的项目 + */ + @SneakyThrows + public List getAllProjects(){ + List projects = gitLabApi.getProjectApi().getProjects(); + return projects; + } + + /** + * 获得一个项目所有的分支 + */ + @SneakyThrows + public List getAllBranchInOneProject(int projectId){ + List branches = gitLabApi.getRepositoryApi().getBranches(projectId); + return branches; + } + + /** + * 获取分支下某一目录的一层文件和文件夹,不会获取文件夹内的内容 + */ + @SneakyThrows + public List getDirsAndFiles(final Integer projectId, final String directory, final String branch){ + List fileNames = new ArrayList<>(); + List tree = gitLabApi.getRepositoryApi().getTree(projectId, directory, branch); + for( TreeItem treeItem : tree){ +// final String filePath = String.join("/",directory, treeItem.getName()); + GitlabFile gitlabFile = new GitlabFile(); + gitlabFile.setName(treeItem.getName()); + gitlabFile.setDir(false); + gitlabFile.setPath(treeItem.getPath()); + // 如果当前是目录,则继续获取其下的文件列表 + if (treeItem.getType().equals(TreeItem.Type.TREE)) { + gitlabFile.setDir(true); + } + //一次性调太多接口时间太久 +// List commits = gitLabApi.getCommitsApi().getCommits(projectId, branch, treeItem.getPath()); +// gitlabFile.setUpdateTime(commits.get(0).getCreatedAt()); +// gitlabFile.setAuth(commits.get(0).getAuthorName()); + fileNames.add(gitlabFile); +// log.info("add file: {}", filePath); + } + return fileNames; + } + + /** + * 读取一个文件的内容 + */ + @SneakyThrows + public String getFileContent(int projectId, String filePath, String branchName){ + final InputStream inputStream = gitLabApi.getRepositoryFileApi().getRawFile(projectId, branchName, filePath); + final BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); + final StringBuilder stringBuilder = new StringBuilder(); + while (reader.ready()) { + stringBuilder.append(reader.readLine()); + stringBuilder.append("\r\n"); + } + return stringBuilder.toString(); + } + + /** + * 查询一个项目的所有流水线触发器 + */ + @SneakyThrows + public List getPipelineTriggers(int projectId){ + return gitLabApi.getPipelineApi().getPipelineTriggers(projectId); + } + + /** + * 查询一个项目上是否存在流水线触发器 + */ + public boolean checkPipeLineTrigger(int projectId){ + if(getPipelineTriggers(projectId).size() == 0){ + return false; + } + return true; + } + + /** + * 在一个项目上创建流水线触发器 + */ + @SneakyThrows + public void createPipeLineTrigger(int projectId){ + gitLabApi.getPipelineApi().createPipelineTrigger(projectId, "auto created by api"); + } + + /** + * 携带参数触发流水线 + */ + @SneakyThrows + public void triggerPipeline(int projectId, String token, String branchName, List variables){ + gitLabApi.getPipelineApi().triggerPipeline(projectId, token, branchName, variables); + } + + /** + * 不带参数触发流水线 + */ + @SneakyThrows + public void triggerPipeline(int projectId, String token, String branchName){ + gitLabApi.getPipelineApi().triggerPipeline(projectId, token, branchName, new ArrayList<>()); + } + + /** + * 更新单个文件到项目仓库 + * @param filePath 文件完整的路径,包括文件名 Ex:cls/gg.yml + * @return + */ + @SneakyThrows + public boolean updateOneFile(int projectId, String fileContent, String filePath, + String fileName, String branchName, String commitMessage){ + RepositoryFile repositoryFile = new RepositoryFile(); + repositoryFile.setContent(fileContent); + repositoryFile.setFileName(fileName); + repositoryFile.setFilePath(filePath); +// repositoryFile.setEncoding(Constants.Encoding.TEXT); + RepositoryFile repositoryFile1 = gitLabApi.getRepositoryFileApi().updateFile(projectId, repositoryFile, branchName, commitMessage); + if(repositoryFile1 != null){ + return true; + } + return false; + } + + /** + * 获取Runners + */ + @SneakyThrows + public List getRunners(){ + List runners = gitLabApi.getRunnersApi().getRunners(); + return runners; + } + + /** + * 注册一个Runner + * xxx 注册之后仍需手动配置 + */ + @SneakyThrows + public boolean registerRunner(String token){ + RunnerDetail register_by_api = gitLabApi.getRunnersApi().registerRunner( + token, + "register by api", + true, new ArrayList(), + true, false, null); + return register_by_api != null; + } + + @SneakyThrows + public User getUserDetails(Integer userId){ + User user = gitLabApi.getUserApi().getUser(userId); + return user; + } + + /** + * 更新用户信息 + * @param user id指定更新的用户,剩余变量值都可为null + * @param password 可为null + */ + @SneakyThrows + public boolean updateUser(User user, String password){ + gitLabApi.getUserApi().updateUser(user, password); + return true; + } + + + /** + * 会获取分支下所有的文件,如果文件很多则消耗时间较多,尽量不使用这个方法 + */ + public void getFilesInOneBranch(int projectId, String dir, String branchName){ + List allFiles = getAllFiles(projectId, dir, branchName); + } + + /** + * 会获取分支下所有的文件,如果文件很多则消耗时间较多,尽量不使用这个方法 + */ + @SneakyThrows + private List getAllFiles(final Integer projectId, final String directory, final String branch) { + List fileNames = new ArrayList<>(); + gitLabApi.getRepositoryApi().getTree(projectId, directory, branch).forEach(file -> { + // 如果当前是目录,则继续获取其下的文件列表 + if (file.getType().equals(TreeItem.Type.TREE)) { + fileNames.addAll(getAllFiles(projectId, file.getPath(), branch)); + return; + } + // 如果是文件类型,直接添加 + final String filePath = String.join("/",directory, file.getName()); + fileNames.add(filePath); + log.info("add file: {}", filePath); + }); + return fileNames; + } + +} diff --git a/src/main/java/com/whn/hellospring/service/UserService.java b/src/main/java/com/whn/hellospring/service/UserService.java index 22137cd..2a79eda 100644 --- a/src/main/java/com/whn/hellospring/service/UserService.java +++ b/src/main/java/com/whn/hellospring/service/UserService.java @@ -12,6 +12,8 @@ import io.jsonwebtoken.Claims; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Propagation; +import org.springframework.transaction.annotation.Transactional; import java.util.ArrayList; import java.util.Arrays; @@ -46,7 +48,6 @@ public class UserService { /** * 获取一个用户详情 */ - @DS("master") public UserDTO getUserDetail(long user_id){ UserDO userDO = userRepository.getDetail(user_id); if(userDO==null){ @@ -244,7 +245,6 @@ public class UserService { return msg; } - @DS("master") public UserDO getUserWithToken(String token) throws Exception { Claims claims = verifyJwt(token); if(claims==null){ @@ -255,7 +255,6 @@ public class UserService { return one; } - @DS("master") public Status getUserWithTokenStatus(String token) throws Exception { Claims claims = verifyJwt(token); if(claims==null){ diff --git a/src/main/java/com/whn/hellospring/service/ZenTaoService.java b/src/main/java/com/whn/hellospring/service/ZenTaoService.java index f795b1c..2d29bbd 100644 --- a/src/main/java/com/whn/hellospring/service/ZenTaoService.java +++ b/src/main/java/com/whn/hellospring/service/ZenTaoService.java @@ -4,6 +4,9 @@ package com.whn.hellospring.service; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.baomidou.dynamic.datasource.annotation.DS; +import com.whn.hellospring.common.StateMessage; +import com.whn.hellospring.common.Status; +import com.whn.hellospring.mapper.ZenTaoMapper; import com.whn.hellospring.model.UserDTO; import com.whn.hellospring.model.zentao.ZenTaoLoginDO; import com.whn.hellospring.model.zentao.ZenTaoSessionDO; @@ -40,6 +43,9 @@ public class ZenTaoService { @Autowired ZenTaoRepository zenTaoRepository; + @Autowired + ZenTaoMapper zenTaoMapper; + String ChanDaoHost = "http://101.37.147.15:21080"; @@ -60,11 +66,25 @@ public class ZenTaoService { * 根据user_id获取禅道用户 */ @DS("zentao") - @Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = Exception.class) + @Transactional(propagation = Propagation.REQUIRES_NEW) public ZenTaoUserDO getUserByParentUserId(Long user_id) { return zenTaoRepository.getDetailByUserId(user_id); } + /** + * 更新用户信息 + * @param userId 更新用户的id + * @param user 更新内容 + * @return + */ + @DS("zentao") + @Transactional(propagation = Propagation.REQUIRES_NEW) + public boolean updateUserById(Long userId, ZenTaoUserDO user){ + return zenTaoMapper.updateUser(userId, user); + } + + + // @Async("taskExecutor") // @DS("zentao") diff --git a/src/main/java/com/whn/hellospring/utils/HttpUtils.java b/src/main/java/com/whn/hellospring/utils/HttpUtils.java index 3c8147d..430e675 100644 --- a/src/main/java/com/whn/hellospring/utils/HttpUtils.java +++ b/src/main/java/com/whn/hellospring/utils/HttpUtils.java @@ -31,12 +31,6 @@ import java.util.ArrayList; import java.util.List; import java.util.Map; -/** - * 功能描述: - * - * @author Administrator - * @date 2021/7/23 - */ public class HttpUtils { /** diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 42c9d45..3f3b00e 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -46,6 +46,8 @@ spring: properties: hibernate: format_sql: true + main: + allow-circular-references: true server: @@ -53,7 +55,10 @@ server: mybatis: configuration: - map-underscore-to-camel-case=true: - - - + map-underscore-to-camel-case: true + mapper-locations: classpath:mapper/*.xml +application: + gitlab: + host: http://101.37.147.15:9980/ + accessToken: glpat-2ULV5qCJLL3tzbnWeRDo + name: root diff --git a/src/main/resources/mapper/ZenTao.xml b/src/main/resources/mapper/ZenTao.xml new file mode 100644 index 0000000..cea9b45 --- /dev/null +++ b/src/main/resources/mapper/ZenTao.xml @@ -0,0 +1,27 @@ + + + + + + update zt_user + + + role=#{user.role}, + + + realname=#{user.realname}, + + + gender=#{user.gender}, + + + email=#{user.email}, + + + where id=#{id} + + + + +