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;
}
}