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:
- /**
- * Download build result log to directory [outputDir] with name [outname].
- *
- * @param buildClient: ITeamBuildClient object.
- * @param buildResult: Target IBuildResult object
- * @param outputDir: Output directory.
- * @param outname: Output file name.
- * @return True as the log exist and being downloaded; False otherwise.
- * @throws Exception
- */
- public static boolean DownloadLog(ITeamBuildClient buildClient,
- IBuildResult buildResult,
- File outputDir,
- String outname) throws Exception
- {
- System.out.printf("\t\tStatus=%s...\n", buildResult.getStatus());
- IBuildResultContribution[] ibrcl = buildClient
- .getBuildResultContributions(
- buildResult,
- IBuildResultContribution.LOG_EXTENDED_CONTRIBUTION_ID,
- null);
- for (IBuildResultContribution ibrc : ibrcl) {
- // System.out.printf("\t\t%s (%s)\n", buildResult.getLabel(), buildResult.getStatus());
- String s = _getContent(ibrc.getExtendedContributionData(),
- TeamRepository.contentManager());
- System.out.printf("\t\t%s (%,d)...\n", ibrc.getLabel(), s.length());
- File outputLog = new File(outputDir, outname);
- BufferedWriter bw = new BufferedWriter(new FileWriter(
- outputLog));
- bw.write(s);
- bw.flush();
- bw.close();
- return true;
- }
- return false;
- }
- package test.pub;
- import java.io.BufferedWriter;
- import java.io.ByteArrayOutputStream;
- import java.io.File;
- import java.io.FileWriter;
- import java.io.OutputStream;
- 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.IBuildDefinitionHandle;
- import com.ibm.team.build.common.model.IBuildResult;
- import com.ibm.team.build.common.model.IBuildResultContribution;
- 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.IContentManager;
- 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.IContent;
- 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 DownloadLogFromBuildResult {
- public static IProgressMonitor MyProgressMonitor = new NullProgressMonitor();
- public static ITeamRepository TeamRepository = null;
- public static String repositoryURI = "...";
- public static String userId = "...";
- public static String password = "...";
- private static String _getContent(IContent ic, IContentManager icm)
- {
- // make an output stream for the content manager
- OutputStream os = new ByteArrayOutputStream();
- // get the content of the content object.
- try
- {
- if (ic != null)
- {
- icm.retrieveContent(ic, os, null);
- return os.toString();
- }
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- return null;
- }
- /**
- * Download build result log to directory [outputDir] with name [outname].
- *
- * @param buildClient: ITeamBuildClient object.
- * @param buildResult: Target IBuildResult object
- * @param outputDir: Output directory.
- * @param outname: Output file name.
- * @return True as the log exist and being downloaded; False otherwise.
- * @throws Exception
- */
- public static boolean DownloadLog(ITeamBuildClient buildClient,
- IBuildResult buildResult,
- File outputDir,
- String outname) throws Exception
- {
- System.out.printf("\t\tStatus=%s...\n", buildResult.getStatus());
- IBuildResultContribution[] ibrcl = buildClient
- .getBuildResultContributions(
- buildResult,
- IBuildResultContribution.LOG_EXTENDED_CONTRIBUTION_ID,
- null);
- for (IBuildResultContribution ibrc : ibrcl) {
- // System.out.printf("\t\t%s (%s)\n", buildResult.getLabel(), buildResult.getStatus());
- String s = _getContent(ibrc.getExtendedContributionData(),
- TeamRepository.contentManager());
- System.out.printf("\t\t%s (%,d)...\n", ibrc.getLabel(), s.length());
- File outputLog = new File(outputDir, outname);
- BufferedWriter bw = new BufferedWriter(new FileWriter(
- outputLog));
- bw.write(s);
- bw.flush();
- bw.close();
- return true;
- }
- return false;
- }
- public static boolean Start(String buildDefinitionID, String buildRstLabel, File outputDir, String outname) 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);
- IBuildDefinitionHandle handle = (IBuildDefinitionHandle) IBuildDefinition.ITEM_TYPE
- .createItemHandle(UUID.valueOf(buildDefinitionID), 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 false;
- } else {
- for (Object result : fetchResult.getRetrievedItems()) {
- // System.out.printf("\t[Test] Retrieved item:\n%s\n", result);
- if (result instanceof IBuildDefinition) {
- //System.out.printf("\t\tBuild Definition=%s\n", result);
- definition = (IBuildDefinition) result;
- }
- }
- }
- if (definition == null)
- return false;
- 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;
- // buildResult.getStatus().equals(BuildStatus.ERROR)
- if (buildResult.getLabel().startsWith(buildRstLabel)) {
- return DownloadLog(buildClient, buildResult, outputDir, outname);
- }
- }
- System.err.printf("\t[Warn] No matching build result with given label='%s'!\n", buildRstLabel);
- return false;
- }
- public static void main(String[] args) throws Exception{
- String buildDefinitionID = "xxx";
- String buildRstLabel="xxx";
- File outputDir=new File("output");
- String outputLogname="buildRst.log";
- TeamPlatform.startup();
- try {
- Login();
- Start(buildDefinitionID, buildRstLabel, outputDir, outputLogname);
- } catch (TeamRepositoryException e) {
- System.out.println("Unable to login: " + e.getMessage());
- } finally {
- TeamPlatform.shutdown();
- }
- }
- 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 Login() throws TeamRepositoryException {
- ITeamRepository teamRepository = TeamPlatform.getTeamRepositoryService().getTeamRepository(repositoryURI);
- teamRepository.registerLoginHandler(new LoginHandler(userId, password));
- teamRepository.login(MyProgressMonitor);
- TeamRepository = teamRepository;
- }
- }
沒有留言:
張貼留言