Browse Source

update gitlab and zentao interface

master
jiangxiaopeng 1 year ago
parent
commit
91338fafb2
24 changed files with 804 additions and 193 deletions
  1. +157
    -88
      pom.xml
  2. +3
    -0
      src/META-INF/MANIFEST.MF
  3. +6
    -2
      src/main/java/com/whn/hellospring/AgileToolsApplication.java
  4. +15
    -4
      src/main/java/com/whn/hellospring/common/StateMessage.java
  5. +14
    -0
      src/main/java/com/whn/hellospring/config/GitlabConfig.java
  6. +136
    -0
      src/main/java/com/whn/hellospring/controller/GitlabController.java
  7. +5
    -3
      src/main/java/com/whn/hellospring/controller/UserController.java
  8. +66
    -0
      src/main/java/com/whn/hellospring/controller/ZenTaoController.java
  9. +0
    -44
      src/main/java/com/whn/hellospring/handler/MoneyTypeHandler.java
  10. +14
    -0
      src/main/java/com/whn/hellospring/mapper/ZenTaoMapper.java
  11. +0
    -31
      src/main/java/com/whn/hellospring/model/MenuCoffeeDO.java
  12. +37
    -0
      src/main/java/com/whn/hellospring/model/gitlab/GitlabFile.java
  13. +5
    -4
      src/main/java/com/whn/hellospring/model/zentao/ZenTaoUserDO.java
  14. +1
    -0
      src/main/java/com/whn/hellospring/repository/UserRepository.java
  15. +5
    -3
      src/main/java/com/whn/hellospring/repository/ZenTaoRepository.java
  16. +29
    -0
      src/main/java/com/whn/hellospring/request/UpdateGitlabFileRequest.java
  17. +16
    -0
      src/main/java/com/whn/hellospring/request/UpdateGitlabUserRequest.java
  18. +16
    -0
      src/main/java/com/whn/hellospring/request/UpdateZenTaoUserRequest.java
  19. +220
    -0
      src/main/java/com/whn/hellospring/service/GitlabService.java
  20. +2
    -3
      src/main/java/com/whn/hellospring/service/UserService.java
  21. +21
    -1
      src/main/java/com/whn/hellospring/service/ZenTaoService.java
  22. +0
    -6
      src/main/java/com/whn/hellospring/utils/HttpUtils.java
  23. +9
    -4
      src/main/resources/application.yml
  24. +27
    -0
      src/main/resources/mapper/ZenTao.xml

+ 157
- 88
pom.xml View File

@ -6,7 +6,7 @@
<parent> <parent>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId> <artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.1.RELEASE</version>
<version>2.5.6</version>
<relativePath/> <!-- lookup parent from repository --> <relativePath/> <!-- lookup parent from repository -->
</parent> </parent>
<groupId>com.whn</groupId> <groupId>com.whn</groupId>
@ -25,13 +25,6 @@
<dependencies> <dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.8</version>
</dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId> <artifactId>spring-boot-starter-aop</artifactId>
@ -129,11 +122,11 @@
<artifactId>spring-boot-starter-data-jpa</artifactId> <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency> </dependency>
<dependency>
<groupId>org.joda</groupId>
<artifactId>joda-money</artifactId>
<version>LATEST</version>
</dependency>
<!-- <dependency>-->
<!-- <groupId>org.joda</groupId>-->
<!-- <artifactId>joda-money</artifactId>-->
<!-- <version>LATEST</version>-->
<!-- </dependency>-->
<dependency> <dependency>
<groupId>org.jadira.usertype</groupId> <groupId>org.jadira.usertype</groupId>
<artifactId>usertype.core</artifactId> <artifactId>usertype.core</artifactId>
@ -157,7 +150,7 @@
<dependency> <dependency>
<groupId>com.baomidou</groupId> <groupId>com.baomidou</groupId>
<artifactId>dynamic-datasource-spring-boot-starter</artifactId> <artifactId>dynamic-datasource-spring-boot-starter</artifactId>
<version>3.0.0</version>
<version>3.2.0</version>
</dependency> </dependency>
<dependency> <dependency>
@ -190,11 +183,11 @@
<artifactId>mysql-connector-java</artifactId> <artifactId>mysql-connector-java</artifactId>
</dependency> </dependency>
<dependency>
<groupId>com.googlecode.log4jdbc</groupId>
<artifactId>log4jdbc</artifactId>
<version>RELEASE</version>
</dependency>
<!-- <dependency>-->
<!-- <groupId>com.googlecode.log4jdbc</groupId>-->
<!-- <artifactId>log4jdbc</artifactId>-->
<!-- <version>RELEASE</version>-->
<!-- </dependency>-->
<dependency> <dependency>
<groupId>com.spring4all</groupId> <groupId>com.spring4all</groupId>
@ -224,91 +217,167 @@
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.6</version>
</dependency>
<dependency>
<groupId>org.gitlab4j</groupId>
<artifactId>gitlab4j-api</artifactId>
<version>4.19.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
</dependencies> </dependencies>
<build> <build>
<finalName>test</finalName>
<finalName>um-backend</finalName>
<plugins> <plugins>
<plugin> <plugin>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId> <artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<version>${kotlin.version}</version>
<executions>
<execution>
<id>compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>test-compile</id>
<phase>test-compile</phase>
<goals>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
<configuration>
<jvmTarget>1.8</jvmTarget>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<executions>
<execution>
<id>compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>testCompile</id>
<phase>test-compile</phase>
<goals>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>1.2.2</version>
<executions> <executions>
<execution> <execution>
<id>build-image</id>
<phase>package</phase>
<goals> <goals>
<goal>build</goal>
<goal>repackage</goal>
</goals> </goals>
</execution> </execution>
</executions> </executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration> <configuration>
<imageName>testc</imageName>
<forceTags>true</forceTags><!-- 每次新的构建上覆盖镜像tags -->
<imageTags>
<imageTag>111</imageTag> <!-- -DpushImageTags没有-DdockerImageTags=xxx时,默认推送这个版本 -->
</imageTags>
<baseImage>openjdk:8-jdk-alpine</baseImage>
<entryPoint>["sh", "-c", "java $JAVA_OPTIONS -jar /${project.build.finalName}.jar"]</entryPoint>
<resources>
<resource>
<targetPath>/</targetPath>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
<source>8</source>
<target>8</target>
</configuration> </configuration>
</plugin> </plugin>
</plugins>
</plugins>
<resources>
<resource>
<directory>${project.basedir}/lib/</directory>
<targetPath>BOOT-INF/lib/</targetPath>
<includes>
<include>**/*.jar</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.xml</include>
<include>**/*.yml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build> </build>
<!-- <build>-->
<!-- <finalName>um-backend</finalName>-->
<!-- <plugins>-->
<!-- <plugin>-->
<!-- <groupId>org.apache.maven.plugins</groupId>-->
<!-- <artifactId>maven-jar-plugin</artifactId>-->
<!-- <configuration>-->
<!-- <archive>-->
<!-- <manifest>-->
<!-- <addClasspath>true</addClasspath>-->
<!-- <mainClass>com.whn.hellospring.AgileToolsApplication</mainClass> &lt;!&ndash; 此处为主入口&ndash;&gt;-->
<!-- </manifest>-->
<!-- </archive>-->
<!-- </configuration>-->
<!-- </plugin>-->
<!-- <plugin>-->
<!-- <groupId>org.jetbrains.kotlin</groupId>-->
<!-- <artifactId>kotlin-maven-plugin</artifactId>-->
<!-- <version>${kotlin.version}</version>-->
<!-- <executions>-->
<!-- <execution>-->
<!-- <id>compile</id>-->
<!-- <phase>compile</phase>-->
<!-- <goals>-->
<!-- <goal>compile</goal>-->
<!-- </goals>-->
<!-- </execution>-->
<!-- <execution>-->
<!-- <id>test-compile</id>-->
<!-- <phase>test-compile</phase>-->
<!-- <goals>-->
<!-- <goal>test-compile</goal>-->
<!-- </goals>-->
<!-- </execution>-->
<!-- </executions>-->
<!-- <configuration>-->
<!-- <jvmTarget>1.8</jvmTarget>-->
<!-- </configuration>-->
<!-- </plugin>-->
<!-- <plugin>-->
<!-- <groupId>org.apache.maven.plugins</groupId>-->
<!-- <artifactId>maven-compiler-plugin</artifactId>-->
<!-- <configuration>-->
<!-- <source>8</source>-->
<!-- <target>8</target>-->
<!-- </configuration>-->
<!-- <executions>-->
<!-- <execution>-->
<!-- <id>compile</id>-->
<!-- <phase>compile</phase>-->
<!-- <goals>-->
<!-- <goal>compile</goal>-->
<!-- </goals>-->
<!-- </execution>-->
<!-- <execution>-->
<!-- <id>testCompile</id>-->
<!-- <phase>test-compile</phase>-->
<!-- <goals>-->
<!-- <goal>testCompile</goal>-->
<!-- </goals>-->
<!-- </execution>-->
<!-- </executions>-->
<!-- </plugin>-->
<!-- <plugin>-->
<!-- <groupId>com.spotify</groupId>-->
<!-- <artifactId>docker-maven-plugin</artifactId>-->
<!-- <version>1.2.2</version>-->
<!-- <executions>-->
<!-- <execution>-->
<!-- <id>build-image</id>-->
<!-- <phase>package</phase>-->
<!-- <goals>-->
<!-- <goal>build</goal>-->
<!-- </goals>-->
<!-- </execution>-->
<!-- </executions>-->
<!-- <configuration>-->
<!-- <imageName>testc</imageName>-->
<!-- <forceTags>true</forceTags>&lt;!&ndash; 每次新的构建上覆盖镜像tags &ndash;&gt;-->
<!-- <imageTags>-->
<!-- <imageTag>111</imageTag> &lt;!&ndash; -DpushImageTags没有-DdockerImageTags=xxx时,默认推送这个版本 &ndash;&gt;-->
<!-- </imageTags>-->
<!-- <baseImage>openjdk:8-jdk-alpine</baseImage>-->
<!-- <entryPoint>["sh", "-c", "java $JAVA_OPTIONS -jar /${project.build.finalName}.jar"]</entryPoint>-->
<!-- <resources>-->
<!-- <resource>-->
<!-- <targetPath>/</targetPath>-->
<!-- <directory>${project.build.directory}</directory>-->
<!-- <include>${project.build.finalName}.jar</include>-->
<!-- </resource>-->
<!-- </resources>-->
<!-- </configuration>-->
<!-- </plugin>-->
<!-- </plugins>-->
<!-- </build>-->
</project> </project>

+ 3
- 0
src/META-INF/MANIFEST.MF View File

@ -0,0 +1,3 @@
Manifest-Version: 1.0
Main-Class: com.whn.hellospring.AgileToolsApplication

+ 6
- 2
src/main/java/com/whn/hellospring/AgileToolsApplication.java View File

@ -1,6 +1,8 @@
package com.whn.hellospring; package com.whn.hellospring;
import com.baomidou.mybatisplus.autoconfigure.MybatisPlusAutoConfiguration;
import com.whn.hellospring.service.JuheService; import com.whn.hellospring.service.JuheService;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments; import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner; 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.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan; import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.ComponentScan;
@ -30,11 +33,12 @@ import redis.clients.jedis.Jedis;
////@EntityScan(basePackages="com.whn.hellospring") ////@EntityScan(basePackages="com.whn.hellospring")
@ComponentScan("com") @ComponentScan("com")
@SpringBootApplication
@RestController @RestController
@Slf4j @Slf4j
@EnableJpaRepositories
@EnableJpaRepositories("com.whn.hellospring.repository")
@MapperScan("com.whn.hellospring.mapper")
@EnableTransactionManagement @EnableTransactionManagement
@SpringBootApplication(exclude={DataSourceAutoConfiguration.class, DataSourceTransactionManagerAutoConfiguration.class, MybatisPlusAutoConfiguration.class})
public class AgileToolsApplication extends SpringBootServletInitializer { public class AgileToolsApplication extends SpringBootServletInitializer {


+ 15
- 4
src/main/java/com/whn/hellospring/common/StateMessage.java View File

@ -1,5 +1,4 @@
package com.whn.hellospring.common; package com.whn.hellospring.common;
/** /**
* @author lfx * @author lfx
* 返回客户端结果定义 * 返回客户端结果定义
@ -7,20 +6,32 @@ package com.whn.hellospring.common;
public enum StateMessage { public enum StateMessage {
SUCCESS(true, "000", "成功"), SUCCESS(true, "000", "成功"),
PHONE_ERROR(false, "001", "手机号不匹配"),
LOSE_PARAM(false, "002", "缺少参数"),
USER_NOT_EXIST(false, "003", "用户不存在"), USER_NOT_EXIST(false, "003", "用户不存在"),
USER_EXIST(false, "105", "用户已存在"), 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_OVERDUE(false, "004", "验证码过期"),
CAPTCHA_FAILURE(false, "005", "验证码不匹配"), CAPTCHA_FAILURE(false, "005", "验证码不匹配"),
SIGN_IN_FAILED(false, "006", "签到失败"),
LOGIN_OTHER_DEVICE(false, "007", "异常登录"), LOGIN_OTHER_DEVICE(false, "007", "异常登录"),
TOKEN_ERROR(false, "008", "用户凭证错误"), TOKEN_ERROR(false, "008", "用户凭证错误"),
OPERATION_FAILED(false, "009", "操作失败"), OPERATION_FAILED(false, "009", "操作失败"),
OUT_PERMISSION(false, "010", "权限不足"), OUT_PERMISSION(false, "010", "权限不足"),
PARAMETER_FORMAT_ERROR(false, "011", "参数格式不匹配"), PARAMETER_FORMAT_ERROR(false, "011", "参数格式不匹配"),
ACCOUNT_EXIST(false, "016", "用户名重复"), ACCOUNT_EXIST(false, "016", "用户名重复"),
EMAIL_ERROR(false, "717", "禅道电子邮箱格式错误"),
UN_KNOW_REASON(false, "9999", "未知错误!"); UN_KNOW_REASON(false, "9999", "未知错误!");


+ 14
- 0
src/main/java/com/whn/hellospring/config/GitlabConfig.java View File

@ -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;
}

+ 136
- 0
src/main/java/com/whn/hellospring/controller/GitlabController.java View File

@ -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<Project> 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<GitlabFile> 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<String> 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);
}
}
}

+ 5
- 3
src/main/java/com/whn/hellospring/controller/UserController.java View File

@ -1,5 +1,6 @@
package com.whn.hellospring.controller; package com.whn.hellospring.controller;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.whn.hellospring.common.StateMessage; import com.whn.hellospring.common.StateMessage;
import com.whn.hellospring.common.Status; import com.whn.hellospring.common.Status;
import com.whn.hellospring.log.OperationLogAnnotation; 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.DO2DTOUtil;
import com.whn.hellospring.utils.MergeObjectsUtil; import com.whn.hellospring.utils.MergeObjectsUtil;
import org.springframework.beans.factory.annotation.Autowired; 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 org.springframework.web.bind.annotation.*;
import javax.validation.Valid; import javax.validation.Valid;
import java.util.List; import java.util.List;
@RestController //处理http请求默认返回json格式数据
@RequestMapping(value = "/user") //配置映射
@RestController
@RequestMapping(value = "/user")
public class UserController { public class UserController {
@Autowired @Autowired
@ -220,7 +223,6 @@ public class UserController {
} }
} }
/** /**
* 根据token 获取客户 * 根据token 获取客户
*/ */


+ 66
- 0
src/main/java/com/whn/hellospring/controller/ZenTaoController.java View File

@ -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);
}
}
}

+ 0
- 44
src/main/java/com/whn/hellospring/handler/MoneyTypeHandler.java View File

@ -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<Money> {
@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);
}
}

+ 14
- 0
src/main/java/com/whn/hellospring/mapper/ZenTaoMapper.java View File

@ -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);
}

+ 0
- 31
src/main/java/com/whn/hellospring/model/MenuCoffeeDO.java View File

@ -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;
}

+ 37
- 0
src/main/java/com/whn/hellospring/model/gitlab/GitlabFile.java View File

@ -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;
}

+ 5
- 4
src/main/java/com/whn/hellospring/model/zentao/ZenTaoUserDO.java View File

@ -4,6 +4,7 @@ import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.*; import lombok.*;
import javax.persistence.*; import javax.persistence.*;
import javax.validation.constraints.Pattern;
import java.io.Serializable; import java.io.Serializable;
import java.util.Date; import java.util.Date;
@ -62,8 +63,8 @@ public class ZenTaoUserDO implements Serializable {
private Long visits; private Long visits;
private String visions; private String visions;
private String ip; private String ip;
private int last;
private int fails;
private Integer last;
private Integer fails;
private Date locked; private Date locked;
@ -71,11 +72,11 @@ public class ZenTaoUserDO implements Serializable {
private String ranzhi; private String ranzhi;
private String ldap; private String ldap;
private int score;
private Integer score;
@Column( name="scorelevel" ) @Column( name="scorelevel" )
private int scoreLevel;
private Integer scoreLevel;
@Column( name="resettoken" ) @Column( name="resettoken" )


+ 1
- 0
src/main/java/com/whn/hellospring/repository/UserRepository.java View File

@ -1,5 +1,6 @@
package com.whn.hellospring.repository; package com.whn.hellospring.repository;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.whn.hellospring.model.UserDO; import com.whn.hellospring.model.UserDO;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying; import org.springframework.data.jpa.repository.Modifying;


+ 5
- 3
src/main/java/com/whn/hellospring/repository/ZenTaoRepository.java View File

@ -3,11 +3,13 @@ package com.whn.hellospring.repository;
import com.baomidou.dynamic.datasource.annotation.DS; import com.baomidou.dynamic.datasource.annotation.DS;
import com.whn.hellospring.model.UserDO; import com.whn.hellospring.model.UserDO;
import com.whn.hellospring.model.zentao.ZenTaoUserDO; 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.JpaRepository;
import org.springframework.data.jpa.repository.Query; import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface ZenTaoRepository extends JpaRepository<ZenTaoUserDO,Long> { public interface ZenTaoRepository extends JpaRepository<ZenTaoUserDO,Long> {
@ -19,9 +21,9 @@ public interface ZenTaoRepository extends JpaRepository<ZenTaoUserDO,Long> {
// public List<ZenTaoUserDO> findbyUser_id(Long user_id); // public List<ZenTaoUserDO> 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);
} }

+ 29
- 0
src/main/java/com/whn/hellospring/request/UpdateGitlabFileRequest.java View File

@ -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;
}

+ 16
- 0
src/main/java/com/whn/hellospring/request/UpdateGitlabUserRequest.java View File

@ -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;
}

+ 16
- 0
src/main/java/com/whn/hellospring/request/UpdateZenTaoUserRequest.java View File

@ -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;
}

+ 220
- 0
src/main/java/com/whn/hellospring/service/GitlabService.java View File

@ -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<Project> getAllProjects(){
List<Project> projects = gitLabApi.getProjectApi().getProjects();
return projects;
}
/**
* 获得一个项目所有的分支
*/
@SneakyThrows
public List<Branch> getAllBranchInOneProject(int projectId){
List<Branch> branches = gitLabApi.getRepositoryApi().getBranches(projectId);
return branches;
}
/**
* 获取分支下某一目录的一层文件和文件夹不会获取文件夹内的内容
*/
@SneakyThrows
public List<GitlabFile> getDirsAndFiles(final Integer projectId, final String directory, final String branch){
List<GitlabFile> fileNames = new ArrayList<>();
List<TreeItem> 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<Commit> 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<Trigger> 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<Variable> 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<Runner> getRunners(){
List<Runner> 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<String>(),
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<String> allFiles = getAllFiles(projectId, dir, branchName);
}
/**
* 会获取分支下所有的文件如果文件很多则消耗时间较多尽量不使用这个方法
*/
@SneakyThrows
private List<String> getAllFiles(final Integer projectId, final String directory, final String branch) {
List<String> 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;
}
}

+ 2
- 3
src/main/java/com/whn/hellospring/service/UserService.java View File

@ -12,6 +12,8 @@ import io.jsonwebtoken.Claims;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
@ -46,7 +48,6 @@ public class UserService {
/** /**
* 获取一个用户详情 * 获取一个用户详情
*/ */
@DS("master")
public UserDTO getUserDetail(long user_id){ public UserDTO getUserDetail(long user_id){
UserDO userDO = userRepository.getDetail(user_id); UserDO userDO = userRepository.getDetail(user_id);
if(userDO==null){ if(userDO==null){
@ -244,7 +245,6 @@ public class UserService {
return msg; return msg;
} }
@DS("master")
public UserDO getUserWithToken(String token) throws Exception { public UserDO getUserWithToken(String token) throws Exception {
Claims claims = verifyJwt(token); Claims claims = verifyJwt(token);
if(claims==null){ if(claims==null){
@ -255,7 +255,6 @@ public class UserService {
return one; return one;
} }
@DS("master")
public Status getUserWithTokenStatus(String token) throws Exception { public Status getUserWithTokenStatus(String token) throws Exception {
Claims claims = verifyJwt(token); Claims claims = verifyJwt(token);
if(claims==null){ if(claims==null){


+ 21
- 1
src/main/java/com/whn/hellospring/service/ZenTaoService.java View File

@ -4,6 +4,9 @@ package com.whn.hellospring.service;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.baomidou.dynamic.datasource.annotation.DS; 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.UserDTO;
import com.whn.hellospring.model.zentao.ZenTaoLoginDO; import com.whn.hellospring.model.zentao.ZenTaoLoginDO;
import com.whn.hellospring.model.zentao.ZenTaoSessionDO; import com.whn.hellospring.model.zentao.ZenTaoSessionDO;
@ -40,6 +43,9 @@ public class ZenTaoService {
@Autowired @Autowired
ZenTaoRepository zenTaoRepository; ZenTaoRepository zenTaoRepository;
@Autowired
ZenTaoMapper zenTaoMapper;
String ChanDaoHost = "http://101.37.147.15:21080"; String ChanDaoHost = "http://101.37.147.15:21080";
@ -60,11 +66,25 @@ public class ZenTaoService {
* 根据user_id获取禅道用户 * 根据user_id获取禅道用户
*/ */
@DS("zentao") @DS("zentao")
@Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = Exception.class)
@Transactional(propagation = Propagation.REQUIRES_NEW)
public ZenTaoUserDO getUserByParentUserId(Long user_id) { public ZenTaoUserDO getUserByParentUserId(Long user_id) {
return zenTaoRepository.getDetailByUserId(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") // @Async("taskExecutor")
// @DS("zentao") // @DS("zentao")


+ 0
- 6
src/main/java/com/whn/hellospring/utils/HttpUtils.java View File

@ -31,12 +31,6 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
/**
* 功能描述
*
* @author Administrator
* @date 2021/7/23
*/
public class HttpUtils { public class HttpUtils {
/** /**


+ 9
- 4
src/main/resources/application.yml View File

@ -46,6 +46,8 @@ spring:
properties: properties:
hibernate: hibernate:
format_sql: true format_sql: true
main:
allow-circular-references: true
server: server:
@ -53,7 +55,10 @@ server:
mybatis: mybatis:
configuration: 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

+ 27
- 0
src/main/resources/mapper/ZenTao.xml View File

@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.whn.hellospring.mapper.ZenTaoMapper">
<update id="updateUser" >
update zt_user
<set>
<if test="user.role!=null">
role=#{user.role},
</if>
<if test="user.realname!=null">
realname=#{user.realname},
</if>
<if test="user.gender!=null">
gender=#{user.gender},
</if>
<if test="user.email!=null">
email=#{user.email},
</if>
</set>
where id=#{id}
</update>
</mapper>

Loading…
Cancel
Save