Skip to content

Commit

Permalink
feat: create ref from commit
Browse files Browse the repository at this point in the history
  • Loading branch information
borozcod committed Feb 16, 2024
1 parent 04abd4f commit 1c2fa9a
Show file tree
Hide file tree
Showing 5 changed files with 922 additions and 775 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public interface BranchService {
RefsResponse getBranch(String projectId, String id);

RefJson createBranch(String projectId, RefJson branch);
RefJson createBranchfromCommit(String projectId, RefJson parentCommitId, NodeService nodeService);
RefJson createBranchfromCommit(String projectId, RefJson branch, NodeService nodeService);

RefsResponse deleteBranch(String projectId, String id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import org.openmbee.mms.core.objects.RefsResponse;
import org.openmbee.mms.core.objects.Rejection;
import org.openmbee.mms.core.services.BranchService;
import org.openmbee.mms.core.services.NodeService;
import org.openmbee.mms.crud.controllers.BaseController;
import org.openmbee.mms.json.RefJson;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -18,7 +17,6 @@
import org.springframework.security.core.Authentication;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.method.annotation.StreamingResponseBody;

import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -98,8 +96,6 @@ public RefsResponse createRefs(
if (branch.getParentCommitId() == null || branch.getParentCommitId().isEmpty()) {
res = branchService.createBranch(projectId, branch);
} else {
//TODO implement branching from historical commit

res = branchService.createBranchfromCommit(projectId, branch, getNodeService(projectId));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.openmbee.mms.data.domains.scoped.Branch;
import org.openmbee.mms.data.domains.scoped.Commit;
import org.openmbee.mms.data.domains.scoped.Node;
import org.openmbee.mms.json.ElementJson;
import org.openmbee.mms.json.RefJson;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -153,11 +154,24 @@ public RefJson createBranch(String projectId, RefJson branch) {

Optional<Branch> refOption = branchRepository.findByBranchId(b.getParentRefId());
if (refOption.isPresent()) {
Optional<Commit> parentCommit = commitRepository.findLatestByRef(refOption.get());
parentCommit.ifPresent(parent -> {
b.setParentCommit(parent.getId());
branch.setParentCommitId(parent.getCommitId()); //commit id is same as its docId
});
if(branch.getParentCommitId() != null){
Optional<Commit> commitRequestId = commitRepository.findByCommitId(branch.getParentCommitId());
commitRequestId.ifPresentOrElse(commit -> {
Optional<Commit> parentCommit = commitRepository.findByRefAndTimestamp(refOption.get(), commit.getTimestamp());
parentCommit.ifPresent(parent -> {
b.setParentCommit(parent.getId());
branch.setParentCommitId(parent.getCommitId());
});
},
() -> { throw new BadRequestException(new RefsResponse().addMessage("parentCommitId not found " + now.toString()));
});
} else {
Optional<Commit> parentCommit = commitRepository.findLatestByRef(refOption.get());
parentCommit.ifPresent(parent -> {
b.setParentCommit(parent.getId());
branch.setParentCommitId(parent.getCommitId()); //commit id is same as its docId
});
}
}

if (b.getParentCommit() == null) {
Expand All @@ -182,51 +196,54 @@ public RefJson createBranch(String projectId, RefJson branch) {
throw new InternalErrorException(e);
}
}

public RefJson createBranchfromCommit(String projectId, RefJson branch, NodeService nodeService) {
public RefJson createBranchfromCommit(String projectId, RefJson parentCommitIdRef, NodeService nodeService) {

Check notice

Code scanning / CodeQL

Missing Override annotation Note

This method overrides
BranchService.createBranchfromCommit
; it is advisable to add an Override annotation.
Instant now = Instant.now();

if(parentCommitIdRef.getParentCommitId().isEmpty()){
throw new BadRequestException(new RefsResponse().addMessage("parentCommitId not provided " + now.toString()));
}

ContextHolder.setContext(projectId);
// Check commit exists
// Create a new branch, from the branch that the commit was made under
// Get all elements in the commit and update the branch.
// Get all deleted elements and remove them from branch
// Return branch
// for (Node n: nodeRepository.findAll()) {
// System.out.println(n.getNodeId());
// }

// Optional<Commit> parentCommit = commitRepository.findByCommitId(branch.getParentCommitId());
// String parentCommitID = parentCommit.map(Commit::getCommitId).orElse("UNKNOWN"); // TODO: Need to remove UNKNOWN else
//
// String parentRef = parentCommit.map(Commit::getBranchId).orElse("UNKNOWN"); // TODO: Need to remove UNKNOWN else
//
// RefJson newBranch = this.createBranch(projectId, branch);

// List<Node> nodes = nodeRepository.findAll(); // database table
// try {
// Collection<ElementJson> result = nodeGetHelper.processGetJsonFromNodes(nodes, parentCommitID, nodeService)
// .getActiveElementMap().values(); // elastic search
// result.forEach(res ->{
// System.out.println(res.getDocId());
// });
// } catch (Exception e) {
// logger.error("Error in commitChanges: ", e);
// throw new InternalErrorException("Error committing changes: " + e.getMessage());
// }

// database table needs to match elastic search docIDs
//
// nodeRepository.saveAll(nodes);
// for (Node n: nodeRepository.findAll()) {
// System.out.println(n.getNodeId());
// }
//
// for (Node n: nodeRepository.findAll()) {
// nodes.add(n.getNodeId());
// }

RefJson b = new RefJson();
return b;
Optional<Commit> parentCommit = commitRepository.findByCommitId(parentCommitIdRef.getParentCommitId());

// Get Commit object
String parentCommitID = parentCommit.map(Commit::getCommitId).orElseThrow(() ->
new BadRequestException(new RefsResponse().addMessage("parentCommitId not found " + now.toString())));

// Get Commit parentRef, the branch will be created from this
String parentRef = parentCommit.map(Commit::getBranchId).orElseThrow(() ->
new BadRequestException(new RefsResponse().addMessage("Ref from parentCommitId not found" + now.toString())));

parentCommitIdRef.setParentRefId(parentRef);
ContextHolder.setContext(projectId, parentRef);

RefJson branchFromCommit = this.createBranch(projectId, parentCommitIdRef);

ContextHolder.setContext(projectId, branchFromCommit.getRefId());

// Get current nodes from database
List<Node> nodes = nodeRepository.findAll();
// Get elements from index
Collection<ElementJson> result = nodeGetHelper.processGetJsonFromNodes(nodes, parentCommitID, nodeService)
.getActiveElementMap().values();

Map<String, ElementJson> nodeCommitData = new HashMap<>();
for (ElementJson element : result) {
nodeCommitData.put(element.getId(), element);
}

// Update database table to match index
for (Node node : nodes) {
if(nodeCommitData.containsKey(node.getNodeId())){
node.setDocId(nodeCommitData.get(node.getNodeId()).getDocId());
node.setDeleted(false);
} else {
node.setDeleted(true);
}
}
nodeRepository.updateAll(nodes);

return branchFromCommit;
}

public RefsResponse deleteBranch(String projectId, String id) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ private NodeGetInfo processCommit(NodeGetInfo info, String commitId, NodeService
rejectNotFound(info, nodeId); // element not found at commit time
}
} else if (info.getExistingNodeMap().get(nodeId).isDeleted()) { // latest element is before commit, but deleted
// TODO check if time of deletion is after the commit time
rejectDeleted(info, nodeId, indexElement);
} else { // latest element version is version at commit, not deleted
info.getActiveElementMap().put(nodeId, indexElement);
Expand Down
Loading

0 comments on commit 1c2fa9a

Please sign in to comment.