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:
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:
- 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);
- //IRepositoryItemService itemService = getService(IRepositoryItemService.class);
- // 1) Fetch Build Definition
- 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()) {
- // System.out.printf("\t[Test] Retrieved item:\n%s\n", result);
- if (result instanceof IBuildResult) {
- IBuildResult buildResult = (IBuildResult) result;
- //System.out.printf("\t\tBuild Result=%s\n", buildResult);
- } else if (result instanceof IBuildDefinition) {
- //System.out.printf("\t\tBuild Definition=%s\n", result);
- definition = (IBuildDefinition) result;
- }
- }
- }
- if (definition == null) return;
- // 2) Query Build Result
- 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);
- // print out the labels of the retrieved builds
- 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;
- //get the build request handle
- IBuildRequestHandle requestHandle = (IBuildRequestHandle) buildResult.getBuildRequests().get(0);
- //get the build request details
- //IBuildRequest request = (IBuildRequest) itemService.fetchItem(requestHandle, IBuildRequest.PROPERTIES_COMPLETE);
- IBuildRequest request = (IBuildRequest) TeamRepository.itemManager().fetchCompleteItem(requestHandle, IItemManager.DEFAULT,null);
- //get the build engine for the build request
- 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());
- }
- }
- /**
- * How to retrieve attribute "Build Engine" from IBuildResult object
- * https://jazz.net/forum/questions/176905/how-to-retrieve-attribute-build-engine-from-ibuildresult-object
- * https://jazz.net/forum/questions/27120/retrieving-the-build-id
- * @param args
- * @throws Exception
- */
- public static void main(String[] args) throws Exception{
- /*if (args.length != 4) {
- System.out.println("Usage: RequestBuild
"); - System.exit(1);
- }*/
- 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;
- }
- }
沒有留言:
張貼留言