2015年4月7日 星期二

[ RTC Client Lib ] SC - Download build result log by given IBuildResult object

Question 
As title, when I have a IBuildResult implemented object through querying, how do I download the log file from it? 
 

Sample Code 
Below is the code snippet of downloading build result log from given IBuildResult object: 
  1. /** 
  2. * Download build result log to directory [outputDir] with name [outname]. 
  3.  
  4. * @param buildClient: ITeamBuildClient object. 
  5. * @param buildResult: Target IBuildResult object 
  6. * @param outputDir: Output directory. 
  7. * @param outname: Output file name. 
  8. * @return True as the log exist and being downloaded; False otherwise. 
  9. * @throws Exception 
  10. */  
  11. public static boolean DownloadLog(ITeamBuildClient buildClient,   
  12.                                   IBuildResult buildResult,   
  13.                                   File outputDir,   
  14.                                   String outname) throws Exception    
  15. {  
  16.     System.out.printf("\t\tStatus=%s...\n", buildResult.getStatus());  
  17.     IBuildResultContribution[] ibrcl = buildClient  
  18.             .getBuildResultContributions(  
  19.                     buildResult,  
  20.                     IBuildResultContribution.LOG_EXTENDED_CONTRIBUTION_ID,  
  21.                     null);  
  22.     for (IBuildResultContribution ibrc : ibrcl) {  
  23.         // System.out.printf("\t\t%s (%s)\n", buildResult.getLabel(), buildResult.getStatus());  
  24.         String s = _getContent(ibrc.getExtendedContributionData(),  
  25.                                      TeamRepository.contentManager());  
  26.         System.out.printf("\t\t%s (%,d)...\n", ibrc.getLabel(), s.length());  
  27.         File outputLog = new File(outputDir, outname);  
  28.         BufferedWriter bw = new BufferedWriter(new FileWriter(  
  29.                 outputLog));  
  30.         bw.write(s);  
  31.         bw.flush();  
  32.         bw.close();                                                           
  33.         return true;  
  34.     }  
  35.     return false;  
  36. }  
Below is the full sample code to download build result log by given build definition id and build result label: 
  1. package test.pub;  
  2.   
  3. import java.io.BufferedWriter;  
  4. import java.io.ByteArrayOutputStream;  
  5. import java.io.File;  
  6. import java.io.FileWriter;  
  7. import java.io.OutputStream;  
  8. import java.util.ArrayList;  
  9. import java.util.Arrays;  
  10. import java.util.List;  
  11.   
  12. import org.eclipse.core.runtime.IProgressMonitor;  
  13. import org.eclipse.core.runtime.NullProgressMonitor;  
  14.   
  15. import com.ibm.team.build.client.ITeamBuildClient;  
  16. import com.ibm.team.build.common.model.BuildState;  
  17. import com.ibm.team.build.common.model.IBuildDefinition;  
  18. import com.ibm.team.build.common.model.IBuildDefinitionHandle;  
  19. import com.ibm.team.build.common.model.IBuildResult;  
  20. import com.ibm.team.build.common.model.IBuildResultContribution;  
  21. import com.ibm.team.build.common.model.IBuildResultHandle;  
  22. import com.ibm.team.build.common.model.query.IBaseBuildResultQueryModel.IBuildResultQueryModel;  
  23. import com.ibm.team.process.client.IProcessClientService;  
  24. import com.ibm.team.repository.client.IContentManager;  
  25. import com.ibm.team.repository.client.IItemManager;  
  26. import com.ibm.team.repository.client.ITeamRepository;  
  27. import com.ibm.team.repository.client.ITeamRepository.ILoginHandler;  
  28. import com.ibm.team.repository.client.ITeamRepository.ILoginHandler.ILoginInfo;  
  29. import com.ibm.team.repository.client.TeamPlatform;  
  30. import com.ibm.team.repository.common.IContent;  
  31. import com.ibm.team.repository.common.TeamRepositoryException;  
  32. import com.ibm.team.repository.common.UUID;  
  33. import com.ibm.team.repository.common.model.FetchResult;  
  34. import com.ibm.team.repository.common.query.IItemQuery;  
  35. import com.ibm.team.repository.common.query.IItemQueryPage;  
  36. import com.ibm.team.repository.common.query.ast.IPredicate;  
  37. import com.ibm.team.repository.common.service.IQueryService;  
  38. import com.ibm.team.workitem.client.IAuditableClient;  
  39. import com.ibm.team.workitem.client.IWorkItemClient;  
  40.   
  41. public class DownloadLogFromBuildResult {  
  42.     public static IProgressMonitor MyProgressMonitor = new NullProgressMonitor();  
  43.     public static ITeamRepository TeamRepository = null;  
  44.       
  45.     public static String repositoryURI = "...";  
  46.     public static String userId = "...";  
  47.     public static String password = "...";  
  48.       
  49.     private static String _getContent(IContent ic, IContentManager icm)   
  50.     {   
  51.         // make an output stream for the content manager   
  52.         OutputStream os = new ByteArrayOutputStream();   
  53.         // get the content of the content object.   
  54.         try   
  55.         {   
  56.             if (ic != null)   
  57.             {   
  58.                 icm.retrieveContent(ic, os, null);   
  59.                 return os.toString();   
  60.             }   
  61.         }   
  62.         catch (Exception e)   
  63.         {   
  64.             e.printStackTrace();   
  65.         }   
  66.         return null;   
  67.     }   
  68.       
  69.     /** 
  70.      * Download build result log to directory [outputDir] with name [outname]. 
  71.      *  
  72.      * @param buildClient: ITeamBuildClient object. 
  73.      * @param buildResult: Target IBuildResult object 
  74.      * @param outputDir: Output directory. 
  75.      * @param outname: Output file name. 
  76.      * @return True as the log exist and being downloaded; False otherwise. 
  77.      * @throws Exception 
  78.      */  
  79.     public static boolean DownloadLog(ITeamBuildClient buildClient,   
  80.                                       IBuildResult buildResult,   
  81.                                       File outputDir,   
  82.                                       String outname) throws Exception    
  83.     {  
  84.         System.out.printf("\t\tStatus=%s...\n", buildResult.getStatus());  
  85.         IBuildResultContribution[] ibrcl = buildClient  
  86.                 .getBuildResultContributions(  
  87.                         buildResult,  
  88.                         IBuildResultContribution.LOG_EXTENDED_CONTRIBUTION_ID,  
  89.                         null);  
  90.         for (IBuildResultContribution ibrc : ibrcl) {  
  91.             // System.out.printf("\t\t%s (%s)\n", buildResult.getLabel(), buildResult.getStatus());  
  92.             String s = _getContent(ibrc.getExtendedContributionData(),  
  93.                                          TeamRepository.contentManager());  
  94.             System.out.printf("\t\t%s (%,d)...\n", ibrc.getLabel(), s.length());  
  95.             File outputLog = new File(outputDir, outname);  
  96.             BufferedWriter bw = new BufferedWriter(new FileWriter(  
  97.                     outputLog));  
  98.             bw.write(s);  
  99.             bw.flush();  
  100.             bw.close();                                                           
  101.             return true;  
  102.         }  
  103.         return false;  
  104.     }  
  105.       
  106.     public static boolean Start(String buildDefinitionID, String buildRstLabel, File outputDir, String outname) throws Exception {  
  107.         IProcessClientService processClient = (IProcessClientService) TeamRepository  
  108.                 .getClientLibrary(IProcessClientService.class);  
  109.         IAuditableClient auditableClient = (IAuditableClient) TeamRepository  
  110.                 .getClientLibrary(IAuditableClient.class);  
  111.         IWorkItemClient workItemClient = (IWorkItemClient) TeamRepository  
  112.                 .getClientLibrary(IWorkItemClient.class);  
  113.         ITeamBuildClient buildClient = (ITeamBuildClient) TeamRepository.getClientLibrary(ITeamBuildClient.class);  
  114.           
  115.         IBuildDefinitionHandle handle = (IBuildDefinitionHandle) IBuildDefinition.ITEM_TYPE  
  116.                 .createItemHandle(UUID.valueOf(buildDefinitionID), null);  
  117.         ArrayList handleList = new ArrayList();  
  118.         handleList.add(handle);  
  119.         FetchResult fetchResult = (FetchResult) TeamRepository.itemManager()  
  120.                 .fetchCompleteItemsPermissionAware(handleList,  
  121.                         IItemManager.REFRESH, null);  
  122.   
  123.         IBuildDefinition definition = null;  
  124.         if (fetchResult.hasNotFoundItems()) {  
  125.             System.err.printf("\t[Error] Fail in retrieving build definition...");  
  126.             return false;  
  127.         } else {  
  128.             for (Object result : fetchResult.getRetrievedItems()) {  
  129.                 // System.out.printf("\t[Test] Retrieved item:\n%s\n", result);  
  130.                 if (result instanceof IBuildDefinition) {  
  131.                     //System.out.printf("\t\tBuild Definition=%s\n", result);  
  132.                     definition = (IBuildDefinition) result;  
  133.                 }  
  134.             }  
  135.         }  
  136.   
  137.         if (definition == null)  
  138.             return false;  
  139.           
  140.         IBuildResultQueryModel buildResultQueryModel = IBuildResultQueryModel.ROOT;  
  141.         IItemQuery query = IItemQuery.FACTORY  
  142.                 .newInstance(buildResultQueryModel);  
  143.           
  144.         IPredicate predicate = (buildResultQueryModel.buildDefinition()  
  145.                 ._eq(query.newItemHandleArg()))._and(  
  146.                 buildResultQueryModel.personalBuild()._isFalse())._and(  
  147.                 buildResultQueryModel.buildState()._eq(  
  148.                         BuildState.COMPLETED.name()));  
  149.   
  150.         query.filter(predicate);  
  151.         query.orderByDsc(buildResultQueryModel.buildStartTime());  
  152.   
  153.         IItemQueryPage queryPage = buildClient.queryItems(query,  
  154.                 new Object[] { definition.getItemId() },  
  155.                 IQueryService.ITEM_QUERY_MAX_PAGE_SIZE, MyProgressMonitor);  
  156.   
  157.         // print out the labels of the retrieved builds  
  158.         String[] properties = new String[] { IBuildResult.PROPERTY_LABEL,  
  159.                                                          IBuildResult.PROPERTY_BUILD_STATUS,  
  160.                                                          IBuildResult.PROPERTY_BUILD_REQUESTS,  
  161.                                                          IBuildResult.PROPERTY_SUMMARY};  
  162.         List buildResults = TeamRepository.itemManager().fetchPartialItems(  
  163.                 queryPage.getItemHandles(), IItemManager.DEFAULT,  
  164.                 Arrays.asList(properties), MyProgressMonitor);  
  165.         for (Object result : buildResults) {  
  166.             IBuildResult buildResult = (IBuildResult) result;             
  167.   
  168.             // buildResult.getStatus().equals(BuildStatus.ERROR)  
  169.             if (buildResult.getLabel().startsWith(buildRstLabel)) {   
  170.                 return DownloadLog(buildClient, buildResult, outputDir, outname);             
  171.             }  
  172.         }  
  173.         System.err.printf("\t[Warn] No matching build result with given label='%s'!\n", buildRstLabel);  
  174.         return false;  
  175.     }  
  176.   
  177.     public static void main(String[] args) throws Exception{  
  178.         String  buildDefinitionID = "xxx";  
  179.         String  buildRstLabel="xxx";  
  180.         File    outputDir=new File("output");  
  181.         String  outputLogname="buildRst.log";  
  182.         TeamPlatform.startup();  
  183.         try {             
  184.             Login();  
  185.             Start(buildDefinitionID, buildRstLabel, outputDir, outputLogname);  
  186.         } catch (TeamRepositoryException e) {  
  187.             System.out.println("Unable to login: " + e.getMessage());  
  188.         } finally {  
  189.             TeamPlatform.shutdown();  
  190.         }  
  191.     }  
  192.   
  193.     private static class LoginHandler implements ILoginHandler, ILoginInfo {  
  194.   
  195.         private String fUserId;  
  196.         private String fPassword;  
  197.   
  198.         private LoginHandler(String userId, String password) {  
  199.             fUserId = userId;  
  200.             fPassword = password;  
  201.         }  
  202.   
  203.         public String getUserId() {  
  204.             return fUserId;  
  205.         }  
  206.   
  207.         public String getPassword() {  
  208.             return fPassword;  
  209.         }  
  210.   
  211.         public ILoginInfo challenge(ITeamRepository repository) {  
  212.             return this;  
  213.         }  
  214.     }  
  215.       
  216.     public static void Login() throws TeamRepositoryException {  
  217.         ITeamRepository teamRepository = TeamPlatform.getTeamRepositoryService().getTeamRepository(repositoryURI);  
  218.         teamRepository.registerLoginHandler(new LoginHandler(userId, password));  
  219.         teamRepository.login(MyProgressMonitor);  
  220.         TeamRepository = teamRepository;  
  221.     }  
  222. }  

沒有留言:

張貼留言

[Git 常見問題] error: The following untracked working tree files would be overwritten by merge

  Source From  Here 方案1: // x -----删除忽略文件已经对 git 来说不识别的文件 // d -----删除未被添加到 git 的路径中的文件 // f -----强制运行 #   git clean -d -fx 方案2: 今天在服务器上  gi...