Get latest component baseline within Stream via RTC API
Hi
I am using following code to retrieve the latest baseline of a component within a stream:
// Get baselines for component
IComponent component = (IComponent) item;
IHistoricBaselineIterator baselineIterator =
workspaceConn.getBaselinesInHistory(component, 1, new NullProgressMonitor());
List<? extends IBaselineHandle> baselines = baselineIterator.getBaselines();
for (IBaselineHandle iBaselineHandle : baselines) {
IItem itemBaseline =
this.repository.itemManager().fetchCompleteItem(
iBaselineHandle,
IItemManager.UNSHARED,
new SubProgressMonitor(monitor, IProgressMonitor.UNKNOWN,
SubProgressMonitor.PREPEND_MAIN_LABEL_TO_SUBTASK));
For some streams it works (I think) but I have a few streams where the resulting baseline is the overall newest baseline and not the newest baseline (is the "Initial baseline") in the stream context. (like it is shown in the Eclipse client UI in the Team Artifacts" view).
If this code is wrong can you point me to a source with the right piece of code?
Thank you
Accepted answer
If you only want to get the latest baseline (i.e. the basis) of a component, then it's simply:
// Fetch a handle to the last baseline (basis)
ITeamRepository repo = <repo_of_the_workspace>
IWorkspaceManager manager = SCMPlatform.getWorkspaceManager(repo);
IWorkspaceHandle workspaceHandle = <this_is_the_handle_for_the_stream>
IWorkspaceConnection wc = manager.getWorkspaceConnection(workspaceHandle , monitor);
IComponentHandle componentHandle = <this_is_the_component_you_care_about>
IBaselineHandle basisHandle = wc.getComponentInfo(componentHandle ).basis();
// Fetch the full baseline
IFetchResult fetchResult = repo.itemManager().fetchCompleteItemsPermissionAware(Collections.singletonList(basisHandle), ItemManager.DEFAULT, monitor);
IBaseline fetchedBaseline;
if (fetchResult.getRetrievedItems().size() == 1) {
fetchedBaseline = fetchResult.getRetrievedItems().get(0);
}
// Fetch a handle to the last baseline (basis)
ITeamRepository repo = <repo_of_the_workspace>
IWorkspaceManager manager = SCMPlatform.getWorkspaceManager(repo);
IWorkspaceHandle workspaceHandle = <this_is_the_handle_for_the_stream>
IWorkspaceConnection wc = manager.getWorkspaceConnection(workspaceHandle , monitor);
IComponentHandle componentHandle = <this_is_the_component_you_care_about>
IBaselineHandle basisHandle = wc.getComponentInfo(componentHandle ).basis();
// Fetch the full baseline
IFetchResult fetchResult = repo.itemManager().fetchCompleteItemsPermissionAware(Collections.singletonList(basisHandle), ItemManager.DEFAULT, monitor);
IBaseline fetchedBaseline;
if (fetchResult.getRetrievedItems().size() == 1) {
fetchedBaseline = fetchResult.getRetrievedItems().get(0);
}
2 other answers
Found the solution by looking at the RTC plugin code in class BaselinesInWorkspaceQuery.
Solution:
// Get component for component handle
IComponentHandle compHandle = (IComponentHandle) compObj;
IItem item =
this.repository.itemManager().fetchCompleteItem(
compHandle,
IItemManager.UNSHARED,
new SubProgressMonitor(monitor, IProgressMonitor.UNKNOWN,
SubProgressMonitor.PREPEND_MAIN_LABEL_TO_SUBTASK));
if (monitor.isCanceled()) {
return Status.OK_STATUS;
}
IComponent component = (IComponent) item;
// Get current baseline of component in workspace
ICurrentComponentInfo currCompInfo = wsConn.getComponentInfo(compHandle);
long numBasisInHistory = currCompInfo.numBasisInHistory();
List<IComponentInfo> componentInfos =
wsConn.getComponentAuditTrail(compHandle, numBasisInHistory - 1, 1, new NullProgressMonitor());
for (IComponentInfo compInfo : componentInfos) {
ItemId<IBaseline> itemId = new ItemId<IBaseline>(compInfo.basis());
IItemHandle baseLineHandle = itemId.toHandle();
IItem itemBaseline =
this.repository.itemManager().fetchCompleteItem(
baseLineHandle,
IItemManager.UNSHARED,
new SubProgressMonitor(monitor, IProgressMonitor.UNKNOWN,
SubProgressMonitor.PREPEND_MAIN_LABEL_TO_SUBTASK));
if (monitor.isCanceled()) {
return Status.OK_STATUS;
}
IBaseline baseline = (IBaseline) itemBaseline;
PS. The API is really really NOT intuitive to use.
For getting all the baselines as seen from ui right clicking on the component of a specific repository workspace or stream(Show > Baselines) and getting IBaseline from IComponentInfo in a more concise way...:
currentComponentInfo = workspaceConnection.getComponentInfo(componentHandle); // the number of past basis long numBasisInHistory = currentComponentInfo.numBasisInHistory(); log.trace(" numBasisInHistory :" + numBasisInHistory); //BE CAREFUL: from oldest to newest... componentInfos = workspaceConnection.getComponentAuditTrail( componentHandle, 0, numBasisInHistory ,
progressMonitor); for (IComponentInfo componentInfo : componentInfos) { IBaselineHandle baselineHandle = componentInfo.basis(); IBaseline baseline = (IBaseline) itemManager.fetchCompleteItem(baselineHandle, IItemManager.DEFAULT, progressMonitor); log.trace("Baseline UUID =>" + baseline.getItemId() + "<="); log.trace(" Baseline id =>" + baseline.getId() + "<="); log.trace(" Baseline name =>" + baseline.getName() + "<="); }