Skip to content

Commit 01b43a1

Browse files
committed
implement add file use case
1 parent 727e6ab commit 01b43a1

File tree

4 files changed

+124
-0
lines changed

4 files changed

+124
-0
lines changed

src/main/java/com/redknee/cc/ClearCaseCommandBuilder.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ public class ClearCaseCommandBuilder {
1010

1111
private static final String COPY_FILE_COMMAND = "cp %s %s";
1212

13+
private static final String MK_ELEM_COMMAND = "cd %s && /usr/atria/bin/cleartool mkelem -c '%s' %s";
14+
1315
public static String buildCheckOutCommand(String viewName, String path, String fileName) {
1416
String checkoutCommand = String.format(CHECK_OUT_COMMAND, path, fileName);
1517
return String.format(CLEAR_TOOL_VIEW_COMMAND, checkoutCommand, viewName);
@@ -24,4 +26,9 @@ public static String buildCopyCommand(String viewName, String sourceFile, String
2426
String copyCommand = String.format(COPY_FILE_COMMAND, sourceFile, destinationFile);
2527
return String.format(CLEAR_TOOL_VIEW_COMMAND, copyCommand, viewName);
2628
}
29+
30+
public static String buildMakeElementCommand(String viewName, String path, String commitMessage, String fileName) {
31+
String makeElementCommand = String.format(MK_ELEM_COMMAND, path, commitMessage, fileName);
32+
return String.format(CLEAR_TOOL_VIEW_COMMAND, makeElementCommand, viewName);
33+
}
2734
}

src/main/java/com/redknee/service/EventHandler.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.redknee.rest.dto.EventDto;
44
import com.redknee.rest.dto.EventDto.Commit;
55
import com.redknee.rest.dto.EventDto.Repository;
6+
import com.redknee.service.event.AddElementEvent;
67
import com.redknee.service.event.ModifyElementEvent;
78
import com.redknee.service.event.SourceCodeEvent;
89
import com.redknee.service.event.ValidationEvent;
@@ -43,6 +44,12 @@ public void handleMessage(EventDto event) {
4344
new ModifyElementEvent(repository.getFullName(), repository.getName(), repository.getId(),
4445
attachCommitIdToMessage(commit), modified));
4546
}
47+
48+
if (!CollectionUtils.isEmpty(commit.getAdded())) {
49+
publisher.publishEvent(
50+
new AddElementEvent(repository.getFullName(), repository.getName(), repository.getId(),
51+
attachCommitIdToMessage(commit), commit.getAdded()));
52+
}
4653
}
4754

4855

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.redknee.service.event;
2+
3+
import java.util.List;
4+
import lombok.AllArgsConstructor;
5+
import lombok.Getter;
6+
import lombok.NoArgsConstructor;
7+
import lombok.Setter;
8+
9+
@Getter
10+
@Setter
11+
@NoArgsConstructor
12+
@AllArgsConstructor
13+
public class AddElementEvent {
14+
15+
private String repoFullName;
16+
private String repoName;
17+
private String repoId;
18+
private String commitMessage;
19+
private List<String> newFiles;
20+
21+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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

Comments
 (0)