Question:As title, I have use RTC plain java client libraries to query the RTC system and retrieve the desired IBuildResult object. Then next step is that I want to retrieve the attribute "Build Engine" from it:
Sample Code:Below sample code will do actions:
* Login RTC
* Retrieve build definition (IBuildDefinition)
* Retrieve build result of the build definition. (IBuildResult)
* Retrieve build request of the build result and use it to retrieve build engine. (IBuildEngine)
- package test;
-
- 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 com.ibm.team.build.client.ITeamBuildClient;
- import com.ibm.team.build.common.model.BuildState;
- import com.ibm.team.build.common.model.IBuildDefinition;
- import com.ibm.team.build.common.model.IBuildEngine;
- import com.ibm.team.build.common.model.IBuildEngineHandle;
- import com.ibm.team.build.common.model.IBuildRequest;
- import com.ibm.team.build.common.model.IBuildRequestHandle;
- import com.ibm.team.build.common.model.IBuildResult;
- import com.ibm.team.build.common.model.IBuildResultHandle;
- import com.ibm.team.build.common.model.query.IBaseBuildResultQueryModel.IBuildResultQueryModel;
- import com.ibm.team.process.client.IProcessClientService;
- import com.ibm.team.repository.client.IItemManager;
- import com.ibm.team.repository.client.ITeamRepository;
- import com.ibm.team.repository.client.ITeamRepository.ILoginHandler;
- import com.ibm.team.repository.client.ITeamRepository.ILoginHandler.ILoginInfo;
- import com.ibm.team.repository.client.TeamPlatform;
- import com.ibm.team.repository.common.TeamRepositoryException;
- import com.ibm.team.repository.common.UUID;
- import com.ibm.team.repository.common.model.FetchResult;
- import com.ibm.team.repository.common.query.IItemQuery;
- import com.ibm.team.repository.common.query.IItemQueryPage;
- import com.ibm.team.repository.common.query.ast.IPredicate;
- import com.ibm.team.repository.common.service.IQueryService;
- import com.ibm.team.workitem.client.IAuditableClient;
- import com.ibm.team.workitem.client.IWorkItemClient;
-
- public class RetrieveBuildEngine {
- public static IProgressMonitor MyProgressMonitor = new NullProgressMonitor();
- public static ITeamRepository TeamRepository = null;
- public static String BuildDefinitionID="xxx";
-
- 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 Start(String buildDftID) throws Exception
- {
- IProcessClientService processClient = (IProcessClientService) TeamRepository
- .getClientLibrary(IProcessClientService.class);
- IAuditableClient auditableClient = (IAuditableClient) TeamRepository
- .getClientLibrary(IAuditableClient.class);
- IWorkItemClient workItemClient = (IWorkItemClient) TeamRepository
- .getClientLibrary(IWorkItemClient.class);
- ITeamBuildClient buildClient = (ITeamBuildClient) TeamRepository.getClientLibrary(ITeamBuildClient.class);
-
-
-
- IBuildResultHandle handle = (IBuildResultHandle) IBuildResult.ITEM_TYPE
- .createItemHandle(UUID.valueOf(buildDftID), null);
- ArrayList handleList = new ArrayList();
- handleList.add(handle);
- FetchResult fetchResult = (FetchResult) TeamRepository.itemManager()
- .fetchCompleteItemsPermissionAware(handleList,
- IItemManager.REFRESH, null);
-
- IBuildDefinition definition = null;
- if (fetchResult.hasNotFoundItems()) {
- System.err.printf("\t[Error] Fail in retrieving build definition...");
- return;
- } else {
- for (Object result : fetchResult.getRetrievedItems()) {
-
- if (result instanceof IBuildResult) {
- IBuildResult buildResult = (IBuildResult) result;
-
- } else if (result instanceof IBuildDefinition) {
-
- definition = (IBuildDefinition) result;
- }
- }
- }
- if (definition == null) return;
-
-
- IBuildResultQueryModel buildResultQueryModel = IBuildResultQueryModel.ROOT;
- IItemQuery query = IItemQuery.FACTORY
- .newInstance(buildResultQueryModel);
-
- IPredicate predicate = (buildResultQueryModel.buildDefinition()
- ._eq(query.newItemHandleArg()))._and(
- buildResultQueryModel.personalBuild()._isFalse())._and(
- buildResultQueryModel.buildState()._eq(
- BuildState.COMPLETED.name()));
-
- query.filter(predicate);
- query.orderByDsc(buildResultQueryModel.buildStartTime());
-
- IItemQueryPage queryPage = buildClient.queryItems(query,
- new Object[] { definition.getItemId() },
- IQueryService.ITEM_QUERY_MAX_PAGE_SIZE, MyProgressMonitor);
-
-
- String[] properties = new String[] { IBuildResult.PROPERTY_LABEL,
- IBuildResult.PROPERTY_BUILD_STATUS,
- IBuildResult.PROPERTY_BUILD_REQUESTS,
- IBuildResult.PROPERTY_SUMMARY};
- List buildResults = TeamRepository.itemManager().fetchPartialItems(
- queryPage.getItemHandles(), IItemManager.DEFAULT,
- Arrays.asList(properties), MyProgressMonitor);
-
- for (Object result : buildResults) {
- IBuildResult buildResult = (IBuildResult) result;
-
- IBuildRequestHandle requestHandle = (IBuildRequestHandle) buildResult.getBuildRequests().get(0);
-
-
- IBuildRequest request = (IBuildRequest) TeamRepository.itemManager().fetchCompleteItem(requestHandle, IItemManager.DEFAULT,null);
-
- IBuildEngineHandle buildEngineHandler = request.getHandler();
- IBuildEngine engine = (IBuildEngine) TeamRepository.itemManager().fetchCompleteItem(buildEngineHandler, IItemManager.DEFAULT,null);
- System.out.printf("\t[Test] %s->%s\n", buildResult.getLabel(), engine.getId());
- }
- }
-
-
-
-
-
-
-
-
- public static void main(String[] args) throws Exception{
-
-
-
-
-
- String repositoryURI = "RTC Repository URL";
- String userId = "RTC account";
- String password = "RTC password";
-
- TeamPlatform.startup();
- try {
- IProgressMonitor monitor = new NullProgressMonitor();
- TeamRepository = login(repositoryURI, userId, password, monitor);
- Start(BuildDefinitionID);
- } catch (TeamRepositoryException e) {
- System.out.println("Unable to login: " + e.getMessage());
- } finally {
- TeamPlatform.shutdown();
- }
- }
-
- public static ITeamRepository login(String repositoryURI, String userId, String password, IProgressMonitor monitor) throwsTeamRepositoryException {
- ITeamRepository teamRepository = TeamPlatform.getTeamRepositoryService().getTeamRepository(repositoryURI);
- teamRepository.registerLoginHandler(new LoginHandler(userId, password));
- teamRepository.login(monitor);
- return teamRepository;
- }
- }