I got the following VSTS-related question last week from a customer:

We are using pull request builds heavily to validate changes to our code. Our branching strategy is simplistic, focusing on frequent commits to a high quality master branch, with various policies (like pull request builds) to secure the branch.

This branching model works well for us, but our internal audit requirement proscribes that all builds are kept for 500 days, unless if another policy applies. Our internal stakeholders do not need us to keep pull request builds, and we noticed that we can use the branch filter “refs/pull/*” to configure a separate policy for them.

What we are trying to do now, is configure our pull request policy across all projects and definitions in our VSTS account. Is there a way to automate this, or do we need to configure the filter in each and every build definition?

Find a script sample here ->

What does this script do?

  • Get all projects in an account
  • Get all build definitions in that project
  • Get the full build definition details (includes retention options) for each definition
  • In the retention settings, check if the branch filter “refs/pull/*” is already there
    • If it is, update it with the policy set in the script
    • If it is not, create it and add it to the other filters
  • Update the build definition

Does this work in TFS as well?

Yes, but you would need to add some code to handle multiple collections.
The URIs used would have to have the collection name added to them too.

Versions of TFS before api version 4.0 (2017/2015) may have a slightly different syntax in their JSON responses and requests.