It's all about the answers!

Ask a question

Finding workitem from buildresult


Kartik Gajjar (112) | asked Oct 19 '10, 12:56 p.m.
Hello,

My requirement is to find all work item ID associated with the build.

I've managed to find available build result (from the example)

IBuildResult buildResult = (IBuildResult) result;

Now, I'm trying to find all the work item associated with this build and I've no clue how to do this.

My guess, there should be a way to find work item from UUID ( buildResult.getItemId() )

Thanks

4 answers



permanent link
Nick Edgar (6.5k711) | answered Oct 19 '10, 8:18 p.m.
JAZZ DEVELOPER
Hi there,

The work items associated with a build are represented in two ways:
- "included in build" links between the build result and the work items
- a build result contribution with a content blob containing the work item item ids

For more details about how/when these are ceated, see the last part of:
https://jazz.net/wiki/bin/view/Main/BuildFAQ#LoadingJazzSCM

We have an internal helper class in Build to retrieve these items:
com.ibm.team.build.internal.client.workitem.WorkItemHelper.getFixedInBuild(ITeamRepository, IBuildResultHandle, IProgressMonitor)
whose code is:

/**
* Gets the work items fixed in the given build result.
*
* @param teamRepository
* The repository where the build result is found.
* @param buildResultHandle
* The build result with the fixed work items.
* @param monitor
* The progress monitor to track progress on or <code>null</code>
* if progress monitoring is not desired.
* @return The fixed work items or an empty array if none were fixed in the
* build; never null.
* @throws TeamRepositoryException
* @throws IllegalArgumentException
* if any parameters are null
* @LongOp This is a long operation; it may block indefinitely; must not be
* called from a responsive thread.
*/
public static IWorkItemHandle[] getFixedInBuild(ITeamRepository teamRepository,
IBuildResultHandle buildResultHandle, IProgressMonitor monitor) throws TeamRepositoryException {

ValidationHelper.validateNotNull("teamRepository", teamRepository); //$NON-NLS-1$
ValidationHelper.validateNotNull("buildResultHandle", buildResultHandle); //$NON-NLS-1$

List workItemHandles = new ArrayList();

SubMonitor subMonitor = SubMonitor.convert(monitor, 2);
IBuildResultContribution[] buildResultContributions = ClientFactory.getTeamBuildClient(teamRepository).getBuildResultContributions(
buildResultHandle, WorkItemConstants.EXTENDED_DATA_TYPE_ID, subMonitor.newChild(1));

subMonitor.setWorkRemaining(buildResultContributions.length);
for (IBuildResultContribution contribution : buildResultContributions) {
IContent content = contribution.getExtendedContributionData();
if (content != null) {
String[] workItemHandleIds = ContentUtil.contentToStringArray(teamRepository, content,
EXTENDED_DATA_DELIMITER, subMonitor.newChild(1));
for (int i = 0; i < workItemHandleIds.length; i++) {
workItemHandles.add(IWorkItem.ITEM_TYPE.createItemHandle(UUID.valueOf(workItemHandleIds[i]), null));
}
}
}
return (IWorkItemHandle[]) workItemHandles.toArray(new IWorkItemHandle[workItemHandles.size()]);
}

where:
ClientFactory is:

import com.ibm.team.build.client.ClientFactory;

com.ibm.team.build.common.WorkItemConstants.EXTENDED_DATA_TYPE_ID
is

/**
* The type id for the build result extended contribution data for fixed
* work items.
*/
public static final String EXTENDED_DATA_TYPE_ID = "fixedWorkItems"; //$NON-NLS-1$

WorkItemHelper.EXTENDED_DATA_DELIMITER is:

private static final char EXTENDED_DATA_DELIMITER = '\n';


Hope this clarifies more than confuses.

permanent link
Kartik Gajjar (112) | answered Oct 20 '10, 10:31 a.m.
Hi,

Thanks for the inputs, I will try to put all this together

permanent link
Kartik Gajjar (112) | answered Oct 20 '10, 1:50 p.m.
Hi.... here is the final code...

This is mix of example and inputs from Nick Edgar (thanks a lot :) )

What this code block can do

1.
- Will find all the builds for the build id "Production"
- find all associated work items (WI)


> If you need WI for a particular build label use following
//query.filter(buildResultQueryModel.label()._like(query.newStringArg()));
// String[] parameters = new String[] { "PROD-20101019-1002" };

> If you need WI for a particular build tag use following

//query.filter(buildResultQueryModel.label()._like(query.newStringArg()));
// String[] parameters = new String[] { "my label" };

Why you may need this information:
- you have multiple streams and want to track which WI is in which stream. this can be very useful



package snippets;

/*******************************************************************************
* Licensed Materials - Property of IBM
* (c) Copyright IBM Corporation 2006, 2008. All Rights Reserved.
*
* Note to U.S. Government Users Restricted Rights: Use,
* duplication or disclosure restricted by GSA ADP Schedule
* Contract with IBM Corp.
*******************************************************************************/
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.SubMonitor;

import com.ibm.team.build.client.ClientFactory;
import com.ibm.team.build.client.ITeamBuildClient;
import com.ibm.team.build.common.model.IBuildDefinition;
import com.ibm.team.build.common.model.IBuildResult;
import com.ibm.team.build.common.model.IBuildResultContribution;
import com.ibm.team.build.common.model.query.IBaseBuildResultQueryModel.IBuildResultQueryModel;
import com.ibm.team.build.internal.client.util.ContentUtil;
import com.ibm.team.build.internal.client.workitem.WorkItemHelper;
import com.ibm.team.repository.client.IItemManager;
import com.ibm.team.repository.client.ITeamRepository;
import com.ibm.team.repository.client.TeamPlatform;
import com.ibm.team.repository.client.ITeamRepository.ILoginHandler;
import com.ibm.team.repository.client.ITeamRepository.ILoginHandler.ILoginInfo;
import com.ibm.team.repository.common.IContent;
import com.ibm.team.repository.common.TeamRepositoryException;
import com.ibm.team.repository.common.UUID;
import com.ibm.team.repository.common.query.IItemQuery;
import com.ibm.team.repository.common.query.IItemQueryPage;
import com.ibm.team.repository.common.service.IQueryService;
import com.ibm.team.workitem.client.IWorkItemClient;
import com.ibm.team.workitem.client.IWorkItemWorkingCopyManager;
import com.ibm.team.workitem.client.WorkItemWorkingCopy;
import com.ibm.team.workitem.common.model.IWorkItem;
import com.ibm.team.workitem.common.model.IWorkItemHandle;

/**
* Plain Java Snippet: Querying for all build results with a specified tag
*/
public class QueryBuildsWithTagExample {

// public static final String EXTENDED_DATA_TYPE_ID = "fixedWorkItems";
// private static final char EXTENDED_DATA_DELIMITER = '\n';

private static class LoginHandler implements ILoginHandler, ILoginInfo {

private String fUserId;
private String fPassword;

private LoginHandler(String userId, String password) {
fUserId = userId;
fPassword = password;
}

public String getUserId() {
return fUserId;
}

public String getPassword() {
return fPassword;
}

public ILoginInfo challenge(ITeamRepository repository) {
return this;
}
}

public static void main(String[] args) {
if (args.length != 4) {
System.out.println("Usage: RequestBuild <repository> <userId> <password> <build>");
System.exit(1);
}

String repositoryURI = args[0];
String userId = args[1];
String password = args[2];
String tag = args[3];

TeamPlatform.startup();
try {
IProgressMonitor monitor = new NullProgressMonitor();
ITeamRepository teamRepository = login(repositoryURI, userId, password, monitor);
queryForBuilds(tag, teamRepository);
} catch (TeamRepositoryException e) {
System.out.println("Unable to login: " + e.getMessage());
} finally {
TeamPlatform.shutdown();
}
}

public static void queryForBuilds(String tag, ITeamRepository teamRepository) throws TeamRepositoryException {
IBuildResultQueryModel buildResultQueryModel = IBuildResultQueryModel.ROOT;
IItemQuery query = IItemQuery.FACTORY.newInstance(buildResultQueryModel);
//query.filter(buildResultQueryModel.tags()._like(query.newStringArg()));
//query.filter(buildResultQueryModel.label()._like(query.newStringArg()));
query.filter(buildResultQueryModel.buildDefinition()._eq(query.newItemHandleArg()));
query.orderByDsc(buildResultQueryModel.buildStartTime());

IProgressMonitor monitor = new NullProgressMonitor();
ITeamBuildClient buildClient = (ITeamBuildClient) teamRepository.getClientLibrary(ITeamBuildClient.class);
IBuildDefinition buildDefinition = buildClient.getBuildDefinition("Production", monitor);


// String[] parameters = new String[] { "PROD-20101019-1002" };
Object[] parameters = new Object[] { buildDefinition };
IItemQueryPage queryPage = buildClient.queryItems(query, parameters, IQueryService.ITEM_QUERY_MAX_PAGE_SIZE,
monitor);

IWorkItemClient workItemClient= (IWorkItemClient) teamRepository.getClientLibrary(IWorkItemClient.class);

List workItemHandles = new ArrayList();

if (queryPage.getSize() == 0) {
System.out.println("Zero builds found with tag '" + tag + "'");
} else {
System.out.println("Found " + queryPage.getSize() + " builds with tag '" + tag + "':");

// print out the labels of the retrieved builds
String[] properties = new String[] { IBuildResult.PROPERTY_LABEL };
List buildResults = teamRepository.itemManager().fetchPartialItems(queryPage.getItemHandles(),
IItemManager.DEFAULT, Arrays.asList(properties), monitor);

for (Object result : buildResults) {
IBuildResult buildResult = (IBuildResult) result;
IWorkItemHandle[] workItems = WorkItemHelper.getFixedInBuild(teamRepository, buildResult, monitor);

System.out.println(buildResult.getLabel() + ", Work Items :" + workItems.length);
for (int i=0; i < workItems.length; i++) {
IWorkItemClient itemClient= (IWorkItemClient) teamRepository.getClientLibrary(IWorkItemClient.class);
IWorkItemWorkingCopyManager copyManager= itemClient.getWorkItemWorkingCopyManager();
copyManager.connect(workItems[i], IWorkItem.FULL_PROFILE, monitor);
WorkItemWorkingCopy itemCopy= copyManager.getWorkingCopy(workItems[i]);
IWorkItem itemToBeModified= itemCopy.getWorkItem();
System.out.println(buildResult.getLabel() + "," + itemToBeModified.getId());
}
/*
SubMonitor subMonitor = SubMonitor.convert(monitor, 2);
IBuildResultContribution[] buildResultContributions = ClientFactory.getTeamBuildClient(teamRepository).getBuildResultContributions(
buildResult, EXTENDED_DATA_TYPE_ID, subMonitor.newChild(1));
subMonitor.setWorkRemaining(buildResultContributions.length);
for (IBuildResultContribution contribution : buildResultContributions) {
IContent content = contribution.getExtendedContributionData();
if (content != null) {
String[] workItemHandleIds = ContentUtil.contentToStringArray(teamRepository, content,
EXTENDED_DATA_DELIMITER, subMonitor.newChild(1));
for (int i = 0; i < workItemHandleIds.length; i++) {
workItemHandles.add(IWorkItem.ITEM_TYPE.createItemHandle(UUID.valueOf(workItemHandleIds[i]), null));
}
}
}
*/
}
}
}

public static ITeamRepository login(String repositoryURI, String userId, String password, IProgressMonitor monitor)
throws TeamRepositoryException {
ITeamRepository teamRepository = TeamPlatform.getTeamRepositoryService().getTeamRepository(repositoryURI);
teamRepository.registerLoginHandler(new LoginHandler(userId, password));
teamRepository.login(monitor);
return teamRepository;
}
}

permanent link
Nick Edgar (6.5k711) | answered Oct 25 '10, 9:23 a.m.
JAZZ DEVELOPER
That looks like it would work, but note that using the RTC internal classes is not recommended since we may change them at any time.

Your answer


Register or to post your answer.