程式扎記: [ RTC Client Lib ] SC - How to retrieve attribute "Build Engine" from IBuildResult object

標籤

2015年3月30日 星期一

[ RTC Client Lib ] SC - How to retrieve attribute "Build Engine" from IBuildResult object

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)

  1. package test;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.Arrays;  
  5. import java.util.List;  
  6.   
  7. import org.eclipse.core.runtime.IProgressMonitor;  
  8. import org.eclipse.core.runtime.NullProgressMonitor;  
  9.   
  10. import com.ibm.team.build.client.ITeamBuildClient;  
  11. import com.ibm.team.build.common.model.BuildState;  
  12. import com.ibm.team.build.common.model.IBuildDefinition;  
  13. import com.ibm.team.build.common.model.IBuildEngine;  
  14. import com.ibm.team.build.common.model.IBuildEngineHandle;  
  15. import com.ibm.team.build.common.model.IBuildRequest;  
  16. import com.ibm.team.build.common.model.IBuildRequestHandle;  
  17. import com.ibm.team.build.common.model.IBuildResult;  
  18. import com.ibm.team.build.common.model.IBuildResultHandle;  
  19. import com.ibm.team.build.common.model.query.IBaseBuildResultQueryModel.IBuildResultQueryModel;  
  20. import com.ibm.team.process.client.IProcessClientService;  
  21. import com.ibm.team.repository.client.IItemManager;  
  22. import com.ibm.team.repository.client.ITeamRepository;  
  23. import com.ibm.team.repository.client.ITeamRepository.ILoginHandler;  
  24. import com.ibm.team.repository.client.ITeamRepository.ILoginHandler.ILoginInfo;  
  25. import com.ibm.team.repository.client.TeamPlatform;  
  26. import com.ibm.team.repository.common.TeamRepositoryException;  
  27. import com.ibm.team.repository.common.UUID;  
  28. import com.ibm.team.repository.common.model.FetchResult;  
  29. import com.ibm.team.repository.common.query.IItemQuery;  
  30. import com.ibm.team.repository.common.query.IItemQueryPage;  
  31. import com.ibm.team.repository.common.query.ast.IPredicate;  
  32. import com.ibm.team.repository.common.service.IQueryService;  
  33. import com.ibm.team.workitem.client.IAuditableClient;  
  34. import com.ibm.team.workitem.client.IWorkItemClient;  
  35.   
  36. public class RetrieveBuildEngine {  
  37.     public static IProgressMonitor MyProgressMonitor = new NullProgressMonitor();  
  38.     public static ITeamRepository TeamRepository = null;  
  39.     public static String BuildDefinitionID="xxx";  
  40.       
  41.     private static class LoginHandler implements ILoginHandler, ILoginInfo {  
  42.   
  43.         private String fUserId;  
  44.         private String fPassword;  
  45.   
  46.         private LoginHandler(String userId, String password) {  
  47.             fUserId = userId;  
  48.             fPassword = password;  
  49.         }  
  50.   
  51.         public String getUserId() {  
  52.             return fUserId;  
  53.         }  
  54.   
  55.         public String getPassword() {  
  56.             return fPassword;  
  57.         }  
  58.   
  59.         public ILoginInfo challenge(ITeamRepository repository) {  
  60.             return this;  
  61.         }  
  62.     }  
  63.       
  64.     public static void Start(String buildDftID) throws Exception  
  65.     {  
  66.         IProcessClientService processClient = (IProcessClientService) TeamRepository  
  67.                 .getClientLibrary(IProcessClientService.class);  
  68.         IAuditableClient auditableClient = (IAuditableClient) TeamRepository  
  69.                 .getClientLibrary(IAuditableClient.class);  
  70.         IWorkItemClient workItemClient = (IWorkItemClient) TeamRepository  
  71.                 .getClientLibrary(IWorkItemClient.class);  
  72.         ITeamBuildClient buildClient = (ITeamBuildClient) TeamRepository.getClientLibrary(ITeamBuildClient.class);  
  73.         //IRepositoryItemService itemService = getService(IRepositoryItemService.class);  
  74.           
  75.         // 1) Fetch Build Definition  
  76.         IBuildResultHandle handle = (IBuildResultHandle) IBuildResult.ITEM_TYPE  
  77.                 .createItemHandle(UUID.valueOf(buildDftID), null);  
  78.         ArrayList handleList = new ArrayList();  
  79.         handleList.add(handle);  
  80.         FetchResult fetchResult = (FetchResult) TeamRepository.itemManager()  
  81.                 .fetchCompleteItemsPermissionAware(handleList,  
  82.                         IItemManager.REFRESH, null);  
  83.           
  84.         IBuildDefinition definition = null;  
  85.         if (fetchResult.hasNotFoundItems()) {  
  86.             System.err.printf("\t[Error] Fail in retrieving build definition...");  
  87.             return;  
  88.         } else {  
  89.             for (Object result : fetchResult.getRetrievedItems()) {  
  90.                 // System.out.printf("\t[Test] Retrieved item:\n%s\n", result);  
  91.                 if (result instanceof IBuildResult) {  
  92.                     IBuildResult buildResult = (IBuildResult) result;  
  93.                     //System.out.printf("\t\tBuild Result=%s\n", buildResult);  
  94.                 } else if (result instanceof IBuildDefinition) {  
  95.                     //System.out.printf("\t\tBuild Definition=%s\n", result);  
  96.                     definition = (IBuildDefinition) result;  
  97.                 }  
  98.             }  
  99.         }  
  100.         if (definition == nullreturn;  
  101.           
  102.         // 2) Query Build Result          
  103.         IBuildResultQueryModel buildResultQueryModel = IBuildResultQueryModel.ROOT;  
  104.         IItemQuery query = IItemQuery.FACTORY  
  105.                 .newInstance(buildResultQueryModel);  
  106.           
  107.         IPredicate predicate = (buildResultQueryModel.buildDefinition()  
  108.                 ._eq(query.newItemHandleArg()))._and(  
  109.                 buildResultQueryModel.personalBuild()._isFalse())._and(  
  110.                 buildResultQueryModel.buildState()._eq(  
  111.                         BuildState.COMPLETED.name()));  
  112.   
  113.         query.filter(predicate);  
  114.         query.orderByDsc(buildResultQueryModel.buildStartTime());  
  115.   
  116.         IItemQueryPage queryPage = buildClient.queryItems(query,  
  117.                 new Object[] { definition.getItemId() },  
  118.                 IQueryService.ITEM_QUERY_MAX_PAGE_SIZE, MyProgressMonitor);  
  119.   
  120.         // print out the labels of the retrieved builds  
  121.         String[] properties = new String[] { IBuildResult.PROPERTY_LABEL,  
  122.                                                          IBuildResult.PROPERTY_BUILD_STATUS,  
  123.                                                          IBuildResult.PROPERTY_BUILD_REQUESTS,  
  124.                                                          IBuildResult.PROPERTY_SUMMARY};  
  125.         List buildResults = TeamRepository.itemManager().fetchPartialItems(  
  126.                 queryPage.getItemHandles(), IItemManager.DEFAULT,  
  127.                 Arrays.asList(properties), MyProgressMonitor);  
  128.           
  129.         for (Object result : buildResults) {  
  130.             IBuildResult buildResult = (IBuildResult) result;  
  131.             //get the build request handle  
  132.             IBuildRequestHandle requestHandle = (IBuildRequestHandle) buildResult.getBuildRequests().get(0);  
  133.             //get the build request details  
  134.             //IBuildRequest request = (IBuildRequest) itemService.fetchItem(requestHandle, IBuildRequest.PROPERTIES_COMPLETE);  
  135.             IBuildRequest request = (IBuildRequest) TeamRepository.itemManager().fetchCompleteItem(requestHandle, IItemManager.DEFAULT,null);  
  136.             //get the build engine for the build request  
  137.             IBuildEngineHandle  buildEngineHandler = request.getHandler();  
  138.             IBuildEngine engine = (IBuildEngine) TeamRepository.itemManager().fetchCompleteItem(buildEngineHandler, IItemManager.DEFAULT,null);  
  139.             System.out.printf("\t[Test] %s->%s\n", buildResult.getLabel(), engine.getId());  
  140.         }  
  141.     }  
  142.       
  143.     /** 
  144.      * How to retrieve attribute "Build Engine" from IBuildResult object 
  145.      * https://jazz.net/forum/questions/176905/how-to-retrieve-attribute-build-engine-from-ibuildresult-object 
  146.      * https://jazz.net/forum/questions/27120/retrieving-the-build-id 
  147.      * @param args 
  148.      * @throws Exception 
  149.      */  
  150.     public static void main(String[] args) throws Exception{  
  151.         /*if (args.length != 4) { 
  152.             System.out.println("Usage: RequestBuild "); 
  153.             System.exit(1); 
  154.         }*/  
  155.   
  156.         String repositoryURI = "RTC Repository URL";  
  157.         String userId = "RTC account";  
  158.         String password = "RTC password";         
  159.   
  160.         TeamPlatform.startup();  
  161.         try {  
  162.             IProgressMonitor monitor = new NullProgressMonitor();  
  163.             TeamRepository = login(repositoryURI, userId, password, monitor);  
  164.             Start(BuildDefinitionID);  
  165.         } catch (TeamRepositoryException e) {  
  166.             System.out.println("Unable to login: " + e.getMessage());  
  167.         } finally {  
  168.             TeamPlatform.shutdown();  
  169.         }  
  170.     }  
  171.       
  172.     public static ITeamRepository login(String repositoryURI, String userId, String password, IProgressMonitor monitor) throwsTeamRepositoryException {  
  173.         ITeamRepository teamRepository = TeamPlatform.getTeamRepositoryService().getTeamRepository(repositoryURI);  
  174.         teamRepository.registerLoginHandler(new LoginHandler(userId, password));  
  175.         teamRepository.login(monitor);  
  176.         return teamRepository;  
  177.     }  
  178. }  


沒有留言:

張貼留言

網誌存檔

關於我自己

我的相片
Where there is a will, there is a way!