|
|
@@ -1,22 +1,22 @@
|
|
|
package cn.reghao.autodop.dmaster.app.repository.log;
|
|
|
|
|
|
import cn.reghao.autodop.dmaster.app.entity.deploy.DeployedApp;
|
|
|
-import cn.reghao.autodop.dmaster.app.entity.log.BuildDeployLog;
|
|
|
import cn.reghao.autodop.dmaster.app.entity.log.BuildLog;
|
|
|
import cn.reghao.autodop.dmaster.app.entity.log.DeployLog;
|
|
|
import cn.reghao.autodop.dmaster.app.entity.orchestration.AppOrchestration;
|
|
|
import cn.reghao.autodop.dmaster.app.repository.deploy.DeployedAppRepository;
|
|
|
import cn.reghao.autodop.dmaster.app.repository.orchestration.AppOrchestrationRepository;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.stereotype.Repository;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
-import java.util.Date;
|
|
|
-import java.util.List;
|
|
|
-import java.util.Optional;
|
|
|
+import java.util.*;
|
|
|
|
|
|
/**
|
|
|
* @author reghao
|
|
|
* @date 2020-05-26 17:35:26
|
|
|
*/
|
|
|
+@Slf4j
|
|
|
@Repository
|
|
|
public class LogRepository {
|
|
|
private BuildLogRepository buildLogRepository;
|
|
|
@@ -34,43 +34,73 @@ public class LogRepository {
|
|
|
this.appRepository = appRepository;
|
|
|
}
|
|
|
|
|
|
+ @Transactional
|
|
|
public void saveBuildLog(BuildLog buildLog) {
|
|
|
String repo = buildLog.getCommitLog().getRemoteRepoUrl();
|
|
|
String commitId = buildLog.getCommitLog().getCommitId();
|
|
|
- // 构建日志存在时不再保存
|
|
|
+ // 构建日志存在且状态正确时不保存
|
|
|
BuildLog entity = buildLogRepository.findByCommitLogRemoteRepoUrlAndCommitLogCommitId(repo, commitId);
|
|
|
- if (entity == null) {
|
|
|
- AppOrchestration app = buildLog.getApp();
|
|
|
- Optional<AppOrchestration> appEntity = appRepository.findById(app.getId());
|
|
|
- if (appEntity.isPresent()) {
|
|
|
- buildLog.setApp(appEntity.get());
|
|
|
- buildLogRepository.save(buildLog);
|
|
|
- }
|
|
|
+ if (entity != null) {
|
|
|
+ buildLog.setId(entity.getId());
|
|
|
+ buildLog.setCreateTime(entity.getCreateTime());
|
|
|
+ buildLog.setUpdateTime(new Date());
|
|
|
+ saveOrUpdateBuildLog(buildLog);
|
|
|
+ } else {
|
|
|
+ saveOrUpdateBuildLog(buildLog);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public void saveDeployLogs(List<DeployLog> deployLogs) {
|
|
|
- deployLogRepository.saveAll(deployLogs);
|
|
|
+ private void saveOrUpdateBuildLog(BuildLog buildLog) {
|
|
|
+ AppOrchestration app = buildLog.getApp();
|
|
|
+ Optional<AppOrchestration> appEntity = appRepository.findById(app.getId());
|
|
|
+ if (appEntity.isPresent()) {
|
|
|
+ buildLog.setApp(appEntity.get());
|
|
|
+ buildLogRepository.save(buildLog);
|
|
|
+ } else {
|
|
|
+ log.error("构建日志关联的应用不存在...");
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- public void saveDeployedApp(BuildDeployLog buildDeployLog) {
|
|
|
- AppOrchestration app = buildDeployLog.getBuildLog().getApp();
|
|
|
- DeployedApp deployedApp = deployedAppRepository.findByApp(app);
|
|
|
+ @Transactional
|
|
|
+ public void saveOrUpdateDeployLog(DeployLog deployLog) {
|
|
|
+ BuildLog buildLog = deployLog.getBuildLog();
|
|
|
+ String repo = buildLog.getCommitLog().getRemoteRepoUrl();
|
|
|
+ String commitId = buildLog.getCommitLog().getCommitId();
|
|
|
+ BuildLog entity = buildLogRepository.findByCommitLogRemoteRepoUrlAndCommitLogCommitId(repo, commitId);
|
|
|
+ if (entity == null) {
|
|
|
+ log.error("部署关联的构建不存在...");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ DeployLog deployLogEntity = deployLogRepository.findByBuildLog(entity);
|
|
|
+ if (deployLogEntity == null) {
|
|
|
+ deployLogRepository.save(deployLog);
|
|
|
+ saveOrUpdateDeployedApp(buildLog);
|
|
|
+ } else {
|
|
|
+ deployLogEntity.setDeployResults(deployLog.getDeployResults());
|
|
|
+ deployLogEntity.setUpdateTime(new Date());
|
|
|
+ deployLogRepository.save(deployLogEntity);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void saveOrUpdateDeployedApp(BuildLog buildLog) {
|
|
|
+ AppOrchestration app = buildLog.getApp();
|
|
|
+ DeployedApp entity = deployedAppRepository.findByApp(app);
|
|
|
// TODO 新增或更新
|
|
|
- if (deployedApp == null) {
|
|
|
- deployedApp = deployedApp(buildDeployLog);
|
|
|
- deployedAppRepository.save(deployedApp);
|
|
|
+ if (entity == null) {
|
|
|
+ entity = deployedApp(buildLog);
|
|
|
+ deployedAppRepository.save(entity);
|
|
|
} else {
|
|
|
- deployedApp.setCommitId(buildDeployLog.getBuildLog().getCommitLog().getCommitId());
|
|
|
- deployedApp.setUpdateTime(new Date());
|
|
|
- deployedAppRepository.save(deployedApp);
|
|
|
+ entity.setCommitId(buildLog.getCommitLog().getCommitId());
|
|
|
+ entity.setUpdateTime(new Date());
|
|
|
+ deployedAppRepository.save(entity);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- private DeployedApp deployedApp(BuildDeployLog buildDeployLog) {
|
|
|
+ private DeployedApp deployedApp(BuildLog buildLog) {
|
|
|
DeployedApp deployedApp = new DeployedApp();
|
|
|
- deployedApp.setApp(buildDeployLog.getBuildLog().getApp());
|
|
|
- deployedApp.setCommitId(buildDeployLog.getBuildLog().getCommitLog().getCommitId());
|
|
|
+ deployedApp.setApp(buildLog.getApp());
|
|
|
+ deployedApp.setCommitId(buildLog.getCommitLog().getCommitId());
|
|
|
return deployedApp;
|
|
|
}
|
|
|
}
|