|
| 1 | +package com.redknee.service.event.listener; |
| 2 | + |
| 3 | +import com.redknee.cc.ClearCaseCommandBuilder; |
| 4 | +import com.redknee.cc.ClearCaseCommandExecutor; |
| 5 | +import com.redknee.config.ApplicationProperty; |
| 6 | +import com.redknee.service.event.AddElementEvent; |
| 7 | +import java.util.Arrays; |
| 8 | +import java.util.Collections; |
| 9 | +import java.util.List; |
| 10 | +import lombok.extern.slf4j.Slf4j; |
| 11 | +import org.springframework.context.event.EventListener; |
| 12 | +import org.springframework.stereotype.Service; |
| 13 | + |
| 14 | +@Slf4j |
| 15 | +@Service |
| 16 | +public class AddElementListener { |
| 17 | + |
| 18 | + private final ApplicationProperty applicationProperty; |
| 19 | + private final ClearCaseCommandExecutor clearCaseCommandExecutor; |
| 20 | + |
| 21 | + public AddElementListener(ApplicationProperty applicationProperty, |
| 22 | + ClearCaseCommandExecutor clearCaseCommandExecutor) { |
| 23 | + this.applicationProperty = applicationProperty; |
| 24 | + this.clearCaseCommandExecutor = clearCaseCommandExecutor; |
| 25 | + } |
| 26 | + |
| 27 | + @EventListener |
| 28 | + public void handle(AddElementEvent event) { |
| 29 | + String vobPath = applicationProperty.getPathMapper().get(event.getRepoFullName()); |
| 30 | + log.info("Found VOB path {}", vobPath); |
| 31 | + String viewName = applicationProperty.getClearCase().getViewName(); |
| 32 | + List<String> newFiles = event.getNewFiles(); |
| 33 | + newFiles.stream().forEach(file -> { |
| 34 | + String dir = buildFilePath(file, vobPath); |
| 35 | + String checkOutCommand = ClearCaseCommandBuilder.buildCheckOutCommand(viewName, dir, "."); |
| 36 | + log.info("Executing checkout command {}", checkOutCommand); |
| 37 | + clearCaseCommandExecutor.executeCommand(Collections.singletonList(checkOutCommand)); |
| 38 | + |
| 39 | + //make element |
| 40 | + String[] fileParts = file.split("/"); |
| 41 | + String makeElementCommand = ClearCaseCommandBuilder |
| 42 | + .buildMakeElementCommand(viewName, dir, event.getCommitMessage(), fileParts[fileParts.length - 1]); |
| 43 | + log.info("Executing make element command {}", makeElementCommand); |
| 44 | + clearCaseCommandExecutor.executeCommand(Collections.singletonList(makeElementCommand)); |
| 45 | + |
| 46 | + //copy file to remote temp location |
| 47 | + String localFile = buildDirPath(event) + "/" + file; |
| 48 | + String remoteFile = |
| 49 | + applicationProperty.getClearCase().getServer().getWorkspace() + fileParts[fileParts.length - 1]; |
| 50 | + log.info("Trying to copy local file {} to remote {}", localFile, remoteFile); |
| 51 | + clearCaseCommandExecutor.copyFile(localFile, remoteFile); |
| 52 | + |
| 53 | + //copy file from temp to view |
| 54 | + String destinationFile = vobPath + file; |
| 55 | + String copyCommand = ClearCaseCommandBuilder.buildCopyCommand(viewName, remoteFile, destinationFile); |
| 56 | + log.info("Executing copy command {}", copyCommand); |
| 57 | + clearCaseCommandExecutor.executeCommand(Collections.singletonList(copyCommand)); |
| 58 | + |
| 59 | + //checkin file |
| 60 | + String checkInCommand = ClearCaseCommandBuilder |
| 61 | + .buildCheckInCommand(viewName, dir, fileParts[fileParts.length - 1], event.getCommitMessage()); |
| 62 | + log.info("Executing checkin command {}", checkInCommand); |
| 63 | + clearCaseCommandExecutor.executeCommand(Collections.singletonList(checkInCommand)); |
| 64 | + |
| 65 | + //checkin dir |
| 66 | + String checkInDirCommand = ClearCaseCommandBuilder |
| 67 | + .buildCheckInCommand(viewName, dir, ".", event.getCommitMessage()); |
| 68 | + log.info("Executing directory checkin command {}", checkInDirCommand); |
| 69 | + clearCaseCommandExecutor.executeCommand(Collections.singletonList(checkInDirCommand)); |
| 70 | + }); |
| 71 | + } |
| 72 | + |
| 73 | + private String buildFilePath(String file, String vob) { |
| 74 | + String[] fileParts = file.split("/"); |
| 75 | + if (fileParts.length > 1) { |
| 76 | + String[] dirs = Arrays.copyOf(fileParts, fileParts.length - 1); |
| 77 | + return vob + String.join("/", dirs); |
| 78 | + } else { |
| 79 | + //root level |
| 80 | + return vob; |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + private String buildDirPath(AddElementEvent event) { |
| 85 | + String workspace = applicationProperty.getGitServer().getWorkspace(); |
| 86 | + return workspace + event.getRepoName() + "-" + event.getRepoId(); |
| 87 | + } |
| 88 | + |
| 89 | +} |
0 commit comments