REST API Get list of build numbers

How can I get list of build numbers in plain text or from json output with filter? 
Request example: 
http://my_teamcity/httpAuth/app/rest/builds/?locator=buildType:<my_buildType>,branch:<my_branch>,count:10,status:SUCCESS&fields=build:number 

Output xml: 
<builds> 
<build number="1.9.0-SNAPSHOT-b1682"/> 
<build number="1.9.0-SNAPSHOT-b1679"/> 
<build number="1.9.0-SNAPSHOT-b1675"/> 
<build number="1.9.0-SNAPSHOT-b1451"/> 
</builds>

Output json: 
{"build":[{"number":"1.9.0-SNAPSHOT-b1682"},{"number":"1.9.0-SNAPSHOT-b1679"},{"number":"1.9.0-SNAPSHOT-b1675"},{"number":"1.9.0-SNAPSHOT-b1451"}]}

But I need: 
1.9.0-SNAPSHOT-b1682 
1.9.0-SNAPSHOT-b1679 
1.9.0-SNAPSHOT-b1675 
1.9.0-SNAPSHOT-b1451 
or 
[{"number":"1.9.0-SNAPSHOT-b1682"},{"number":"1.9.0-SNAPSHOT-b1679"},{"number":"1.9.0-SNAPSHOT-b1675"},{"number":"1.9.0-SNAPSHOT-b1451"}]

Denis Lapuente answered me in my request:

I'm afraid that the REST API will just return the objects as you have experienced. You will need to handle the objects yourself to turn it into the output you need.

But maybe anyone can give me a specific solution. I need this list to create a drop-down options menu in Rundeck job (or, maybe, in Jenkins).

0
1 comment
Avatar
Permanently deleted user

Hello AShutov

What I believe that Denis is getting at is that you need to process the results of the REST query to get what you want. With out knowing how you would like process the data (programming language, operating system, other applications where the result is to go, etc.), it is hard to give you a specific example.

For instance, since I work in a MS Windows / .NET shop I would write a C# console application using the RestSharp and Newtonsoft.Json libraries to request your query and then parse the JSON result into the data structure with a for each loop to emit the build numbers on each line.  If I was writing a Unix script I would use curl or wget for the request and pipe the output though sed or awk to get the second result. I am willing to bet that you would like nether technology. Here is a code snipped from my experiment rig with .Net 

var client = new RestClient
{
   BaseUrl = new Uri(server),
   Authenticator = new HttpBasicAuthenticator(user, password)
};
response =client.Execute(new RestRequest( $"{BaseApiString}/builds/?locator=buildType::<my_buildType>,count:10&fields=build(number,id)"));
var json = JToken.Parse(response.Content);
var buildNumbers = json["build"].Select(build => build["number"].ToString());
Logger.Debug(string.Join("\n",buildNumbers));

Hope this helps.

 

--

Chris

 

0

Please sign in to leave a comment.