Reverse the changeset through API
Hi,
Am trying to reverse the changeset programmatically,but couldn't get through as not sure the actual arguments to pass to commit operation.Have followed the link to some extent but couldn't figure the working code.Can you please have a look on below code and let me know where I am wrong.It would be great if you able to share working version.
IWorkspaceManager workspaceManager = SCMPlatform
.getWorkspaceManager(teamRepository);
IWorkspaceSearchCriteria streamSearchCriteria = WorkspaceSearchCriteria.FACTORY
.newInstance();
streamSearchCriteria.setKind(IWorkspaceSearchCriteria.WORKSPACES);
streamSearchCriteria.setPartialName("TEST_CHANGE_REVERSE");
List<IWorkspaceHandle> sourceStreamList = workspaceManager
.findWorkspaces(streamSearchCriteria, Integer.MAX_VALUE,
null);
System.out.println("No of Workspaces---->"+sourceStreamList.size());
IWorkspaceConnection wsConn = workspaceManager
.getWorkspaceConnection(sourceStreamList.get(0), null);
IConfigurationOpFactory opFactory = wsConn.configurationOpFactory();
IWorkItem taskWi = workItemClient.findWorkItemById(
Integer.parseInt("134293"), IWorkItem.FULL_PROFILE, null);
IItemReference linkTarget = IReferenceFactory.INSTANCE
.createReferenceToItem(taskWi);
ILinkQueryPage changeSets = linkManager.findLinksByTarget(
WorkItemLinkTypes.CHANGE_SET, linkTarget,
new NullProgressMonitor());
ILinkCollection linkCollection = changeSets.getLinks();
for (ILink iLink : linkCollection) {
IChangeSetHandle changeSetHandle = (IChangeSetHandle) iLink
.getSourceRef().resolve();
String csComment = iLink.getSourceRef().getComment();
System.out.println("Comment--->" + csComment);
IItemManager itemManager = teamRepository.itemManager();
IChangeSet changeset = (IChangeSet) itemManager
.fetchCompleteItem(changeSetHandle,
IItemManager.DEFAULT, null);
for (Object chng : changeset.changes()) {
Change change = (Change) chng;
if (change.afterState() instanceof IVersionableHandle)
//opFactory.revert(change.beforeState());
wsConn.commit(changeSetHandle, Collections.singleton(opFactory.revert(change.afterState())), null);
}
Accepted answer
As David suggests, you should use IWorkspaceConnection#createChangeSet() to create a new change set, then commit changes into that.
An active change set is one that you can commit to. It's shown without a green check-mark in the UI. When a change set is completed/closed, it is no longer active and cannot be committed to. Completing a change set is done automatically on delivery.
If there are other active change sets in the IWorkspace that modify the same item, your commit will fail. You can use IWorkspaceConnection#activeChangeSets() to get a listing of the active change sets and iterate over the items that they modify (again, be aware that IChange can return null for many of the fields, so include a null check).
If you have created the new workspace from the stream as part of your operation, then there won't be any active change sets, so you can skip the previous paragraph. ;)
One other answer
Suresh, you cannot re-open a closed change set. You would need to check-in the reversal changes into a new change.
A new change set can be created using:
IChangeSetHandle cset = wsConn.createChangeSet(component, progress.newChild(25));
Then you can deliver this change set to the stream to reverse the changes (the other option is to discard the change set from the stream, although if changes are build on top of that one, it might not always be possible (or easy) to discard that one change set).
A new change set can be created using:
IChangeSetHandle cset = wsConn.createChangeSet(component, progress.newChild(25));
Then you can deliver this change set to the stream to reverse the changes (the other option is to discard the change set from the stream, although if changes are build on top of that one, it might not always be possible (or easy) to discard that one change set).
Comments
Evan Hughes
JAZZ DEVELOPER Feb 13 '15, 11:22 a.m.suresh k
Feb 17 '15, 1:09 a.m.suresh k
Feb 17 '15, 1:18 a.m.suresh k
Feb 17 '15, 4:34 a.m.suresh k
Feb 18 '15, 2:06 a.m.