How to retrieve changeset from baseline (build) with Java API?
Accepted answer
First, you need to log in to the repository:
ITeamRepository repo = TeamPlatform.getTeamRepositoryService().getTeamRepository("https://localhost:9443/jazz");
repo.registerLoginHandler(new ILoginHandler() {
@Override
public ILoginInfo challenge(ITeamRepository repository) {
... /* return a new ILoginInfo with the credentials for the repository */
};
}
});
repo.login(/* Progress monitor goes here*/);
Once you are logged in you will need both a build client for build related calls and a workspace manager for the SCM side.
ITeamBuildClient buildClient = (ITeamBuildClient) repo.getClientLibrary(ITeamBuildClient.class);
IWorkspaceManager workspaceManager = (IWorkspaceManager)repo.getClientLibrary(IWorkspaceManager.class);
You can retrieve the build definition object for the build definition that you want like this:
IBuildDefinition def = buildClient.getBuildDefinition("MyBuild", /* Progress monitor */ );
The build results will need to be queried somehow. I'm not sure what your criteria would be for picking a particular result. This will find the latest 512 non-personal completed build results in a non-optimal way:
UUID currDefinitionUUID = def.getItemId();
IBuildResultQueryModel buildResultQueryModel = IBuildResultQueryModel.ROOT;
IItemQuery query = IItemQuery.FACTORY.newInstance(buildResultQueryModel);
query.filter((buildResultQueryModel.buildDefinition()._eq(query.newItemHandleArg()))._and(
buildResultQueryModel.personalBuild()._isFalse())._and(
buildResultQueryModel.buildState()._eq(BuildState.COMPLETED.name())));
query.orderByDsc(buildResultQueryModel.buildStartTime());
IItemQueryPage queryPage = client.queryItems(query, new Object[] { currDefinitionUUID },
IQueryService.ITEM_QUERY_MAX_PAGE_SIZE, /* Progress monitor*/);
if (queryPage.getSize() == 0) {
System.out.println("Zero builds found with tag '" + currDefinitionUUID + "'"); //$NON-NLS-1$ //$NON-NLS-2$
return null;
} else {
IFetchResult fetchResult = repo.itemManager().fetchCompleteItemsPermissionAware(queryPage.getItemHandles(), IItemManager.DEFAULT, new NullProgressMonitor());
List<IBuildResult> buildResults = fetchResult.getRetrievedItems();
return buildResults;
}
From those IBuildResults you pick two and you can compare the snapshots and get change sets that are in one but not the other like this:
IBuildResult result1 = ...
IBuildResult result2 = ...
IBuildResultContribution[] snapshotContribution1 = buildClient.getBuildResultContributions(result1,
new String[] { ScmConstants.EXTENDED_DATA_TYPE_ID_BUILD_SNAPSHOT }, /* Progress Monitor*/);
IBuildResultContribution[] snapshotContribution2 = buildClient.getBuildResultContributions(result2,
new String[] { ScmConstants.EXTENDED_DATA_TYPE_ID_BUILD_SNAPSHOT }, /* Progress Monitor*/);
if (snapshotContribution1.length > 0 && snapshotContribution2.length > 0) {
IBaselineSetHandle baselineSetHandle1 = (IBaselineSetHandle) snapshotContribution1[0].getExtendedContribution();
IBaselineSetHandle baselineSetHandle2 = (IBaselineSetHandle) snapshotContribution2[0].getExtendedContribution();
IChangeHistorySyncReport syncReport = workspaceManager.compareBaselineSets(baselineSetHandle1, baselineSetHandle2, null, /* Progress Monitor */));
for (Object o: syncReport.outgoingChangeSets()) {
IChangeSetHandle csHandle = (IChangeSetHandle)o;
IChangeSet cs = (IChangeSet)repo.itemManager().fetchCompleteItem(csHandle, IItemManager.DEFAULT, /* Progress Monitor */);
/* Do whatever you need to do with each change set */
}
}
Note that the above approach can and should be optimized depending on what you are doing. Also, all of the progress monitors are important because there are alot of calls to the server and each one can take time.
I hope that this is helpful.
ITeamRepository repo = TeamPlatform.getTeamRepositoryService().getTeamRepository("https://localhost:9443/jazz");
repo.registerLoginHandler(new ILoginHandler() {
@Override
public ILoginInfo challenge(ITeamRepository repository) {
... /* return a new ILoginInfo with the credentials for the repository */
};
}
});
repo.login(/* Progress monitor goes here*/);
Once you are logged in you will need both a build client for build related calls and a workspace manager for the SCM side.
ITeamBuildClient buildClient = (ITeamBuildClient) repo.getClientLibrary(ITeamBuildClient.class);
IWorkspaceManager workspaceManager = (IWorkspaceManager)repo.getClientLibrary(IWorkspaceManager.class);
You can retrieve the build definition object for the build definition that you want like this:
IBuildDefinition def = buildClient.getBuildDefinition("MyBuild", /* Progress monitor */ );
The build results will need to be queried somehow. I'm not sure what your criteria would be for picking a particular result. This will find the latest 512 non-personal completed build results in a non-optimal way:
UUID currDefinitionUUID = def.getItemId();
IBuildResultQueryModel buildResultQueryModel = IBuildResultQueryModel.ROOT;
IItemQuery query = IItemQuery.FACTORY.newInstance(buildResultQueryModel);
query.filter((buildResultQueryModel.buildDefinition()._eq(query.newItemHandleArg()))._and(
buildResultQueryModel.personalBuild()._isFalse())._and(
buildResultQueryModel.buildState()._eq(BuildState.COMPLETED.name())));
query.orderByDsc(buildResultQueryModel.buildStartTime());
IItemQueryPage queryPage = client.queryItems(query, new Object[] { currDefinitionUUID },
IQueryService.ITEM_QUERY_MAX_PAGE_SIZE, /* Progress monitor*/);
if (queryPage.getSize() == 0) {
System.out.println("Zero builds found with tag '" + currDefinitionUUID + "'"); //$NON-NLS-1$ //$NON-NLS-2$
return null;
} else {
IFetchResult fetchResult = repo.itemManager().fetchCompleteItemsPermissionAware(queryPage.getItemHandles(), IItemManager.DEFAULT, new NullProgressMonitor());
List<IBuildResult> buildResults = fetchResult.getRetrievedItems();
return buildResults;
}
From those IBuildResults you pick two and you can compare the snapshots and get change sets that are in one but not the other like this:
IBuildResult result1 = ...
IBuildResult result2 = ...
IBuildResultContribution[] snapshotContribution1 = buildClient.getBuildResultContributions(result1,
new String[] { ScmConstants.EXTENDED_DATA_TYPE_ID_BUILD_SNAPSHOT }, /* Progress Monitor*/);
IBuildResultContribution[] snapshotContribution2 = buildClient.getBuildResultContributions(result2,
new String[] { ScmConstants.EXTENDED_DATA_TYPE_ID_BUILD_SNAPSHOT }, /* Progress Monitor*/);
if (snapshotContribution1.length > 0 && snapshotContribution2.length > 0) {
IBaselineSetHandle baselineSetHandle1 = (IBaselineSetHandle) snapshotContribution1[0].getExtendedContribution();
IBaselineSetHandle baselineSetHandle2 = (IBaselineSetHandle) snapshotContribution2[0].getExtendedContribution();
IChangeHistorySyncReport syncReport = workspaceManager.compareBaselineSets(baselineSetHandle1, baselineSetHandle2, null, /* Progress Monitor */));
for (Object o: syncReport.outgoingChangeSets()) {
IChangeSetHandle csHandle = (IChangeSetHandle)o;
IChangeSet cs = (IChangeSet)repo.itemManager().fetchCompleteItem(csHandle, IItemManager.DEFAULT, /* Progress Monitor */);
/* Do whatever you need to do with each change set */
}
}
Note that the above approach can and should be optimized depending on what you are doing. Also, all of the progress monitors are important because there are alot of calls to the server and each one can take time.
I hope that this is helpful.