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
3 changes: 2 additions & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ jobs:
docker create -v /etc/newman --name mms_test_configs alpine:3.4 /bin/true
docker cp example/. mms_test_configs:/etc/newman
docker run --volumes-from mms_test_configs --network container:mms -t postman/newman run crud.postman_collection.json -e test-env.json --delay-request 500
docker run --volumes-from mms_test_configs --network container:mms -t postman/newman run getAtCommits.postman_collection.json -e test-env.json --delay-request 500
docker run --volumes-from mms_test_configs --network container:mms -t postman/newman run getAtCommits.postman_collection.json -e test-env.json --delay-request 500
docker run --volumes-from mms_test_configs --network container:mms -t postman/newman run makeBranchFromCommit.postman_collection.json -e test-env.json --delay-request 500
docker run --volumes-from mms_test_configs --network container:mms -t postman/newman run cameo.postman_collection.json -e test-env.json --delay-request 1000
docker run --volumes-from mms_test_configs --network container:mms -t postman/newman run jupyter.postman_collection.json -e test-env.json --delay-request 500
docker run --volumes-from mms_test_configs --network container:mms -t postman/newman run localauth.postman_collection.json -e test-env.json --delay-request 500
Expand Down
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 @@ public class DefaultBranchService implements BranchService {
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 @@ -130,6 +137,7 @@ public RefJson createBranch(String projectId, RefJson branch) {
branch.setDeleted(false);
branch.setProjectId(projectId);
branch.setStatus("created");
boolean fromCommit = branch.getParentCommitId() == null ? false : true;

if (branch.getDocId() == null || branch.getDocId().isEmpty()) {
String docId = branchIndex.createDocId(branch);
Expand All @@ -145,18 +153,26 @@ public RefJson createBranch(String projectId, RefJson branch) {
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 @@ -167,11 +183,14 @@ public RefJson createBranch(String projectId, RefJson branch) {
try {
branchIndex.update(branch);
branchRepository.save(b);
Set<String> docIds = new HashSet<>();
for (Node n: nodeRepository.findAllByDeleted(false)) {
docIds.add(n.getDocId());
if(!fromCommit) {
Set<String> docIds = new HashSet<>();
for (Node n: nodeRepository.findAllByDeleted(false)) {
docIds.add(n.getDocId());
}

try { nodeIndex.addToRef(docIds); } catch(Exception e) {}
}
try { nodeIndex.addToRef(docIds); } catch(Exception e) {}
eventPublisher.forEach((pub) -> pub.publish(
EventObject.create(projectId, branch.getId(), "branch_created", branch)));
return branch;
Expand All @@ -181,6 +200,60 @@ public RefJson createBranch(String projectId, RefJson branch) {
throw new InternalErrorException(e);
}
}
public RefJson createBranchfromCommit(String projectId, RefJson parentCommitIdRef, NodeService nodeService) {
Dismissed Show dismissed Hide dismissed
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.getId());

// 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
Set<String> docIds = new HashSet<>();
for (Node node : nodes) {
if(nodeCommitData.containsKey(node.getNodeId())){
node.setDocId(nodeCommitData.get(node.getNodeId()).getDocId());
node.setLastCommit(nodeCommitData.get(node.getNodeId()).getCommitId());
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

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

try { nodeIndex.addToRef(docIds); } catch(Exception e) {}

return branchFromCommit;
}

public RefsResponse deleteBranch(String projectId, String id) {
ContextHolder.setContext(projectId);
Expand Down
Loading
Loading