Get a list of BuildTypes that use any template via rest API

Answered

Hello,

I want to get a list of the buildtypes that are using some template via rest API.

For example. Get me the buildtypes where the template element exists.

An alternative that might be useful to me is something like: get usages of this template.

If there is an answer to that I would really like an example also.

Thank you in advance!

1
7 comments
Avatar
Fedor Rumyantsev

Hello!

Sure, this is achievable. In order to retrieve all build configurations using a template, you may use this endpoint:|

/app/rest/buildTypes/template:(templateFlag:true)

This will return all build configurations which are based on a template (essentially, the above locator revolves around fetching a list of all templates first (templateFlag:true), and then fetches every build configuration which is based on a template from the list).

To narrow the search, you may modify the above request:

/app/rest/buildTypes/template:(templateFlag:true,id:<templateID>)

The request will work similarly to above, but template list will be limited to the template with ID = <templateID>.

For your reference, here is our REST API documentation: https://www.jetbrains.com/help/teamcity/rest-api.html
Hope this helps; please let me know in case of any further questions!

0

Thank you for the answer!

I had already tried the above recommendations. The second one works fine. The first one does not work though.

It returns just one buildtype that uses some template. I want every buildtype that is based on any of the templates I have.

1
Avatar
Fedor Rumyantsev

Hello!

Apologies, I have tested the first endpoint on a narrow case. Unfortunately, it appears that the template locator is limited to a single value hence the issue - alternatively, you can request a full list of build configurations with template field exposed:

/app/rest/buildTypes?fields=buildType(id,name,projectId,template)

This approach will require you to manually filter out the entities with template entity exposed, though. Let me know if this works for you.

0

Yes. It works fine for me. Thank you very much! :-)

0

I'm also trying to use the REST API to find usages of a template. I hit the same problem that the template: locator only returns one item.
If I try your other suggestion of:

/app/rest/buildTypes?fields=buildType(id,name,projectId,template)

The template element also will only contain one item, when my build configurations might have several templates attached.

It's frustrating since this is an easy thing to query using the Web UI.
Also, templates are designed to be attached to multiple different build configurations, so I don't understand what use case there is for the template: locator only returning one item?

0

Hello Jenny!

The method with template locator will work as expected if used as below (e.g. will return multiple build types using the template in question):

/app/rest/buildTypes?locator=template:(templateFlag:true,id:<templateID>)

Note the difference in the calls; if locator parameter is passed "implicitly", e.g. as part of the path, then TeamCity will always respond with a single item. Apologies for the confusion my earlier response might have caused.

Speaking of the call which could return multiple templates (should the build configuration have them), indeed, template field will by default return first one. However, you can request all templates which configuration is based on by the below call:

/app/rest/buildTypes?fields=buildType(id,name,projectId,templates(buildType(id,name,projectName)))

The templates field here is a list of build configuration types (template internally is also a build configuration) which this configuration depends on. 
I hope this helps; please let me know if there is anything else I could assist with.

1

Thanks Fedor! Both your examples did exactly what I needed.

0

Please sign in to leave a comment.