Configuring standard policies for all repositories in Azure Repos
By default you can only configure policies on specific branches in Azure Repos. If you're using a strict naming pattern for your branches (Release Flow or GitHub Flow), you may want to set a policy for all future Release Branches, or all Feature branches.
A couple of weeks ago I blogged about setting collection level permissions on Azure Repos. That sparked questions whether the same was possible on Branch Policies in the comments, twitter and the Azure DevOps Club slack channel.
By default you can only configure policies on specific branches in Azure Repos. You access the policies through the Branch's [...] menu and set the policy from there. But if you're using a strict naming pattern for your branches (e.g. when using Release Flow or GitHub Flow), you may want to set a policy for all future Release Branches, or all Feature branches.
It would be nice if you could write these policies into law, that way you don't have to set them for every future branch.
Let's start with the bad news: the policy API is specific to a Project. Because of that you can't set the policies for all Git Repositories in an account, but you can specify the policy for all repositories in a Project.
If you look at the request that's generated when saving a Branch Policy, you can see the UI sending a POST request to the /{Project Guid}/api/policy/Configurations
REST API when creating a new policy. That request contains the scope for each policy:
As you can see, the policy has a Scope. You can have multiple active policies and each can have its own scope. The UI will always create a specific scope that contains the repositoryId
and the exact branch name.
But if you look at the docs for this API, you'll find that this is not the only option available. The widest scope you can create has no repository scope at all and applies to all repositories in that project:
But there are other cool options as well. You can configure a policy for all branches with a specific prefix by setting the matchKind
from exact
to prefix
.
Unfortunately, it looks like this API exists at the Project level only. One can't set the policy for all future projects. But, think about it, that makes sense. You can't predict all the future group names, Build Definition IDs and such for projects that don't exist yet. But it's less restricted than the UI would let you believe.
To figure out how each of the policies is specified, configure one branch the way you want ant then open /{Project Guid}/_apis/policy/Configurations/
on your account. you'll be treated with the JSON for your current configuration:
{
"count": 1,
"value": [
{
"isEnabled": true,
"isBlocking": true,
"settings": {
"useSquashMerge": false,
"scope": [
{
"refName": "refs/heads/master",
"matchKind": "Exact",
"repositoryId": "7317f685-3e85-41d6-8e20-10d2319262a7"
}
]
}
}
]
}
Find out all you need to know about policy types by querying them from your account as well, my account returns these:
The configuration for each policy is a bit of a mystery. I tend to configure a policy through the UI, then retrieve the configured policy to see what the JSON looks like.
Now that you understand the underlying concepts, guids and things, you can use the raw REST requests from PowerShell or... You could use the new Azure CLI for Azure DevOps:
az extension add --name "azure-devops"
az login
az repos policy create --org {your org} --project {your project name or guid} --config "path/to/config/file"
For reference:
Leave a comment.