-
-
Notifications
You must be signed in to change notification settings - Fork 14
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
Changes from all commits
04abd4f
1c2fa9a
130306d
4170355
f9df731
ebb1148
9f86bb6
af678d9
db448e5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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; | ||
|
@@ -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) { | ||
|
@@ -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); | ||
|
@@ -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()); | ||
}); | ||
}, | ||
() -> { 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) { | ||
|
@@ -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; | ||
|
@@ -181,6 +200,60 @@ public RefJson createBranch(String projectId, RefJson branch) { | |
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.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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. also set node lastCommitId to be the element json's commitId There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
|
There was a problem hiding this comment.
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.