Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: create ref from commit id #262

Merged
merged 9 commits into from
Feb 28, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public interface BranchService {
RefsResponse getBranch(String projectId, String id);

RefJson createBranch(String projectId, RefJson branch);
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 @@ -96,9 +96,7 @@ public RefsResponse createRefs(
if (branch.getParentCommitId() == null || branch.getParentCommitId().isEmpty()) {
res = branchService.createBranch(projectId, branch);
} else {
//TODO implement branching from historical commit
response.addRejection(new Rejection(branch, 400, "Branching from historical commits is not implemented."));
continue;
res = branchService.createBranchfromCommit(projectId, branch, getNodeService(projectId));
}

permissionService.initBranchPerms(projectId, branch.getId(), true, auth.getName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,7 @@
import org.openmbee.mms.core.config.Constants;
import org.openmbee.mms.core.config.ContextHolder;
import org.openmbee.mms.core.config.Formats;
import org.openmbee.mms.core.dao.BranchDAO;
import org.openmbee.mms.core.dao.BranchIndexDAO;
import org.openmbee.mms.core.dao.CommitDAO;
import org.openmbee.mms.core.dao.NodeDAO;
import org.openmbee.mms.core.dao.NodeIndexDAO;
import org.openmbee.mms.core.dao.*;
import org.openmbee.mms.core.exceptions.BadRequestException;
import org.openmbee.mms.core.exceptions.DeletedException;
import org.openmbee.mms.core.exceptions.InternalErrorException;
Expand All @@ -18,9 +14,11 @@
import org.openmbee.mms.core.objects.RefsResponse;
import org.openmbee.mms.core.services.BranchService;
import org.openmbee.mms.core.services.EventService;
import org.openmbee.mms.core.services.NodeService;
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 All @@ -45,6 +43,15 @@
private NodeIndexDAO nodeIndex;

protected Collection<EventService> eventPublisher;
protected NodeGetHelper nodeGetHelper;



@Autowired
public void setNodeGetHelper(NodeGetHelper nodeGetHelper) {
this.nodeGetHelper = nodeGetHelper;
}


@Autowired
public void setBranchRepository(BranchDAO branchRepository) {
Expand Down Expand Up @@ -145,18 +152,26 @@
b.setParentRefId(Constants.MASTER_BRANCH);
}

//This service cannot create branches from historic versions
if (branch.getParentCommitId() != null) {
throw new BadRequestException("Internal Error: Invalid branch creation logic.");
}

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());
});
},
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method was updated so that if a RefJson has a parentCommitId, we don't set the new branch's parentCommitId to the the latest from the parent ref.

() -> { 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 @@ -181,6 +196,55 @@
throw new InternalErrorException(e);
}
}
public RefJson createBranchfromCommit(String projectId, RefJson parentCommitIdRef, NodeService nodeService) {
Instant now = Instant.now();

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

ContextHolder.setContext(projectId);
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);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also set node lastCommitId to be the element json's commitId

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need to do what line 189 is doing also - and not do line 189 if it wasn't branch from latest

} else {
node.setDeleted(true);
}
}
nodeRepository.updateAll(nodes);

return branchFromCommit;
}

public RefsResponse deleteBranch(String projectId, String id) {
ContextHolder.setContext(projectId);
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
Loading