It's all about the answers!

Ask a question

How to retrieve changeset from baseline (build) with Java API?

Lai Bao Liu (334) | asked Sep 19 '12, 2:24 a.m.
How to retrieve changeset from baseline (build) with Java API? I would like to get changeset with Java API, Thanks.

Accepted answer

permanent link
Chris McGee (50511117) | answered Oct 12 '12, 2:09 p.m.
First, you need to log in to the repository:

        ITeamRepository repo = TeamPlatform.getTeamRepositoryService().getTeamRepository("https://localhost:9443/jazz");
        repo.registerLoginHandler(new ILoginHandler() {
            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);


        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.
Lai Bao Liu selected this answer as the correct answer

Lai Bao Liu commented Oct 15 '12, 12:25 a.m.

Thanks a lot.

One other answer

permanent link
Lai Bao Liu (334) | answered Oct 15 '12, 12:24 a.m.
Thanks a lot.

Your answer

Register or to post your answer.