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. ;)
suresh k selected this answer as the correct answer
|
Comments
What error is preventing your solution from working? Could you provide the stack trace and error message?
Am sorry for delay,Getting below error when tried revert() on afterchange.Am not sure how beforeState() and afterState() works as struggling to find documentation for API Change class.
com.ibm.team.scm.common.CompletedChangeSetException
: Changes can only be checked into a new change set or a change set that is active in a workspace.
at com.ibm.team.scm.service.internal.utils.ChangeSetUtils$2.createException(
ChangeSetUtils.java:73)
t com.ibm.team.scm.service.internal.utils.IExceptionFactory$AbstractExceptionFactory.createException(
IExceptionFactory.java:35)
at com.ibm.team.scm.service.internal.operations.commit.CommitChangeTracker.determineTargetChangeSets(
CommitChangeTracker.java:182)
at com.ibm.team.scm.service.internal.operations.commit.CommitOperation.run(
CommitOperation.java:351)
at com.ibm.team.scm.service.internal.ScmService$22.run(
ScmService.java:1215)
my comments to the points you outlined.
Thanks Evan The fourth assumption is relevant to my issue here,Could reverse the change if changeset is active(new changeset check into repo workspace,but not delivered to stream),but I wanted to reverse closed changesets and it would be helpful if you guide me how to reopen the closed changeset or other better ways.
Thanks David and Evan,Am all set .It's working now