Jazz Forum Welcome to the Jazz Community Forum Connect and collaborate with IBM Engineering experts and users

How to get the build result with Java API?

Hi, I use the Java API to request the build, and then I want to wait the build result to output if the build pass or fail. 
I use the following code, but do not know how to get the build result, any one can help?

ITeamBuildClient buildClient = (ITeamBuildClient) teamRepository
.getClientLibrary(ITeamBuildClient.class);
IBuildDefinitionHandle buildDefinition = buildClient.getBuildDefinition(
buildDefinitionId, monitor);
ITeamBuildRequestClient requestClient = (ITeamBuildRequestClient) teamRepository
.getClientLibrary(ITeamBuildRequestClient.class);
IBuildRequest buildRequest = requestClient.requestBuild(
buildDefinition, true, monitor);

0 votes


Accepted answer

Permanent link
 Using IBuildRequest's getBuildResult() will give you the IBuildResultHandle.  You can then fetch the corresponding IBuildResult using teamRepository.itemManager().fetch...
Be sure to specify IItemManager.REFRESH for the flags arg, so it gets the latest state from the server rather than using the cached, last-read item.  If you only want to check the state, then it would be faster to do a partial fetch, specifying to fetch just the state and/or status properties.
e.g.
IBuildResultHandle buildResultHandle = buildRequest.getBuildResult();
IBuildResult buildResult = (IBuildResult) teamRepository.itemManager().fetchPartialItem(buildResultHandle, IItemManager.REFRESH,
                Arrays.asList(new String[] { IBuildResult.PROPERTY_STATE, IBuildResult.PROPERTY_STATUS })
To avoid swamping the server, you should probably have some kind of pause between retries.  For example, the waitForTeamBuild Ant task waits 30 secs between retries, by default.

Jia Jia Li selected this answer as the correct answer

0 votes

Comments

 Hi, Nick

Thanks for your information very much. Here is my sample code to request the build ,and then wait for the result to do something, could you please help to review it? Thanks!


 TeamPlatform.startup();

            ........

                  ITeamBuildClient buildClient = (ITeamBuildClient) teamRepository
.getClientLibrary(ITeamBuildClient.class);
IBuildDefinitionHandle buildDefinition = buildClient.getBuildDefinition(
buildDefinitionId, monitor);
ITeamBuildRequestClient requestClient = (ITeamBuildRequestClient) teamRepository
.getClientLibrary(ITeamBuildRequestClient.class);
IBuildRequest buildRequest = requestClient.requestBuild(
buildDefinition, true, monitor);

IBuildResult buildResult = (IBuildResult) teamRepository.itemManager().fetchCompleteItem(buildRequest.getBuildResult(), IItemManager.REFRESH, null);

                        System.out.println("wait the build completed........");
while(buildResult.getState().toString()!="COMPLETED"){
                                Thread.sleep(1000*30);

    buildResult = (IBuildResult) teamRepository.itemManager().fetchCompleteItem(buildRequest.getBuildResult(), IItemManager.REFRESH, null);
                  }

            ........

finally{
TeamPlatform.shutdown();

Hi, Nick I use while to get the buildresult state, waiting for his completed and do something else. I attach my code in the following, could you please to check if there is anything wrong?

That looks good. A couple of suggestions:

  1. Pass the monitor to the fetch calls, and check isCanceled() on it if you want to allow cancelation. Normally operations throw OperationCanceledException in this case, and client code catches it at some reasonable top level, e.g. the UI action's job doing the work.
  2. You can compare enum's directly, without having to convert to string. e.g. buildResult.getState() != BuildState.COMPLETED

1 vote

Jia, you might find some inspiration in https://jazz.net/library/article/807 if you didn't already read the article. Would you mind if I kooe the link to this code for future usage?

Thanks Ralph. I've added a pointer to your article in the Build FAQ: https://jazz.net/wiki/bin/view/Main/BuildFAQ#Is_there_a_programmatic_API_for

Hi, Nick, thanks for your suggestion very much! I will modify it.

Also Ralph, thanks for your always help very much! Your article is very useful!

showing 5 of 6 show 1 more comments

3 other answers

Permanent link

Refer to this wiki page.

https://jazz.net/wiki/bin/view/Main/BuildJavaProgrammingExamples
Section: "Querying for Builds with a Given Tag".

Additionally, use buildResult.getState().toString() function to obtain state of build, e.g. like "COMPLETED". For full list of enum of build status, take a look at "Enum BuildState" in com.ibm.team.build.common.model package.

Hope this helps !



0 votes


Permanent link
 Hi,

I am writing the server side operation participant for the Request build. I need to get the latest build label which is run. 
ENV: RTC 4.0.1

I have written the code, but it is going on the infinite loop, in the while condition. 
Please let me know the issue in the code below.

IRepositoryItemService repositoryItemService = getService(IRepositoryItemService.class);
ITeamBuildService teamBuildService = getService(ITeamBuildService.class);
try {
if(operation.getOperationData() instanceof BuildRequestParams){
BuildRequestParams buildRequestParams = (BuildRequestParams)operation.getOperationData();
IBuildDefinitionHandle buildDefinitionHandle = buildRequestParams.getBuildDefinition();
IBuildResult[] buildResults = teamBuildService.getBuildResultsForBuildDefinition(buildDefinitionHandle, null);
if(buildResults != null && buildResults.length > 0){
IBuildResult latestBuildResult = buildResults[buildResults.length - 1];
latestBuildResult = (IBuildResult)repositoryItemService.fetchItem(latestBuildResult.getItemHandle(), IRepositoryItemService.COMPLETE);
while(latestBuildResult.getState()!= BuildState.COMPLETED){
Thread.currentThread();
Thread.sleep(1000);
latestBuildResult = (IBuildResult)repositoryItemService.fetchItem(latestBuildResult.getItemHandle(), IRepositoryItemService.COMPLETE);
}
System.out.println("Build Label ::: "+latestBuildResult.getLabel());
}
}

This is running into an infinite loop and the latestBuildResult.getState() is always NOT_STARTED.
I am not able to get the refreshed state of the build result.

Please help



0 votes


Permanent link
 Hi Pancha,

I see two issues in the code:
1) in the while loop, it keeps fetching the same item.  You'd need to iterate through the items in the array instead.

2) the results from getBuildResultsForBuildDefinition are not in any particular order (it depends on the DB), so starting from the end does not necessarily return the latest build.  I suggest using com.ibm.team.build.internal.common.ITeamBuildService.getQueryPageForBuildResultsForBuildDefinitions(IBuildDefinitionHandle[], String[]).  This lets you pass the desired build state(s) too, e.g. pass new String[] { BuildState.COMPLETED.name() } for the second arg.  The results are ordered by build start time, most recent first.  So the latest build would be at index 0.


0 votes

Your answer

Register or log in to post your answer.

Dashboards and work items are no longer publicly available, so some links may be invalid. We now provide similar information through other means. Learn more here.

Search context
Follow this question

By Email: 

Once you sign in you will be able to subscribe for any updates here.

By RSS:

Answers
Answers and Comments
Question details
× 10,941
× 562

Question asked: Sep 04 '12, 2:05 p.m.

Question was seen: 7,548 times

Last updated: Mar 18 '13, 9:36 a.m.

Confirmation Cancel Confirm