It's all about the answers!

Ask a question

How to get the build result with Java API?

Jia Jia Li (8058157192) | asked Sep 04 '12, 2:05 p.m.
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
IBuildDefinitionHandle buildDefinition = buildClient.getBuildDefinition(
buildDefinitionId, monitor);
ITeamBuildRequestClient requestClient = (ITeamBuildRequestClient) teamRepository
IBuildRequest buildRequest = requestClient.requestBuild(
buildDefinition, true, monitor);

Accepted answer

permanent link
Nick Edgar (6.5k711) | answered Sep 18 '12, 2:47 p.m.
 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.
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

Jia Jia Li commented Sep 18 '12, 6:58 p.m. | edited Sep 25 '12, 9:50 a.m.

 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!



                  ITeamBuildClient buildClient = (ITeamBuildClient) teamRepository
IBuildDefinitionHandle buildDefinition = buildClient.getBuildDefinition(
buildDefinitionId, monitor);
ITeamBuildRequestClient requestClient = (ITeamBuildRequestClient) teamRepository
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........");

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



Jia Jia Li commented Sep 18 '12, 6:59 p.m.

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?

Nick Edgar commented Sep 19 '12, 9:41 a.m. | edited Sep 19 '12, 9:42 a.m.

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

Ralph Schoon commented Sep 19 '12, 11:00 a.m.

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

Nick Edgar commented Sep 19 '12, 11:45 a.m.

Thanks Ralph. I've added a pointer to your article in the Build FAQ:

Jia Jia Li commented Sep 25 '12, 5:29 a.m.

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
Takehiko Amano (1.3k3741) | answered Sep 06 '12, 10:34 p.m.
edited Sep 06 '12, 10:35 p.m.

Refer to this wiki page.
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 package.

Hope this helps !

permanent link
Pancha Gyaneswari Yelika (45811) | answered Mar 18 '13, 8:03 a.m.

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){
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

permanent link
Nick Edgar (6.5k711) | answered Mar 18 '13, 9:36 a.m.
 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[], String[]).  This lets you pass the desired build state(s) too, e.g. pass new String[] { } for the second arg.  The results are ordered by build start time, most recent first.  So the latest build would be at index 0.

Your answer

Register or 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.