Skip to content

Instantly share code, notes, and snippets.

@rfennell
Created November 12, 2021 12:40
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rfennell/def3ae3e7303db66f3eda3d2eb4a2475 to your computer and use it in GitHub Desktop.
Save rfennell/def3ae3e7303db66f3eda3d2eb4a2475 to your computer and use it in GitHub Desktop.
Set Azure DevOps All Repositories Branch policies
param
(
[parameter(Mandatory=$true,HelpMessage="The target Azure DevOps Instance")]
$org,
[parameter(Mandatory=$true,HelpMessage="The new project name")]
$projectName,
[parameter(Mandatory=$true,HelpMessage="A PAT with access to target org")]
$pat
)
$ErrorActionPreference = "continue"
# Update the branch policies for all repos in the project
# We could use the command `az repos` to set policies https://docs.microsoft.com/en-us/cli/azure/repos?view=azure-cli-latest
# az repos policy approver-count update --project <projectname> --blocking true --enabled true --branch main --repository-id <guid> --minimum-approver-count w --reset-on-source-push true --creator-vote-counts false --allow-downvotes false
# However, this command requires you set the policies for specific repos in the project
# Using the REST API we are able to passing the 'null' value for the repo to set the policies for all repos in the project
# The JSON payload can be discovered by viewing the setting of the policy in the Azure portal with the browser development tools loaded
write-host "Set the branch policies for all repos" -ForegroundColor Green
$jsonBodies = @{}
$jsonBodies.Add("Require reviewer", '{"type":{"id":"fa4e907d-c16b-4a4c-9dfa-4906e5d171dd"},"revision":1,"isDeleted":false,"isBlocking":true,"isEnabled":true,"settings":{"allowDownvotes":false,"blockLastPusherVote":false,"creatorVoteCounts":false,"requireVoteOnLastIteration":false,"resetOnSourcePush":true,"resetRejectionsOnSourcePush":true,"minimumApproverCount":2,"scope":[{"repositoryId":null,"refName":"refs/heads/main","matchKind":"Exact"}]}}')
$jsonBodies.Add("Linked work items", '{"type":{"id":"40e92b44-2fe1-4dd6-b3d8-74a9c21d0c6e"},"revision":1,"isDeleted":false,"isBlocking":true,"isEnabled":true,"settings":{"scope":[{"repositoryId":null,"refName":"refs/heads/main","matchKind":"Exact"}]}}')
$jsonBodies.Add("Check comments", '{"type":{"id":"c6a1889d-b943-4856-b76f-9e46bb6b0df2"},"revision":1,"isDeleted":false,"isBlocking":true,"isEnabled":true,"settings":{"scope":[{"repositoryId":null,"refName":"refs/heads/main","matchKind":"Exact"}]}}')
# Setup the REST call details used for all the branch policey update API calls
$headers = @{ Accept="application/json" }
$headers["Accept-Charset"] = "utf-8"
$encodedPat = [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes(":$pat"))
$headers["Authorization"] = "Basic $encodedPat"
$uri = "$fullOrgUrl/$projectName/_apis/policy/Configurations?api-version=6.1-preview.1"
$jsonBodies.getEnumerator() | foreach {
write-host " - Set the branch policy - $($_.Key)" -ForegroundColor Green
$p1 = Invoke-RestMethod $uri -Method "POST" -Headers $headers -ContentType "application/json" -Body ([System.Text.Encoding]::UTF8.GetBytes($_.Value))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment