Last active
January 25, 2024 13:59
-
-
Save LockTar/b70cc40f4895cb0e2e724cfe0f9faf87 to your computer and use it in GitHub Desktop.
Copy all Azure DevOps branch policies from one branch to another using Azure CLI
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Description: Copies branch policies from one branch to another branch | |
# Run the script in the root of the git repository | |
[CmdletBinding()] | |
param( | |
[Parameter(Mandatory = $true)] | |
[string] $RepositoryName, | |
[Parameter(Mandatory = $true)] | |
[string] $FromBranch, | |
[Parameter(Mandatory = $true)] | |
[string] $ToBranch, | |
[Parameter(Mandatory = $true)] | |
[ValidateNotNullOrEmpty()] | |
[ValidateSet('exact', 'prefix')] | |
[string] $ToBranchMatchType | |
) | |
$ErrorActionPreference = 'Stop'; | |
# Get id of the repo | |
$repositoryId = az repos list --query "[?name=='$RepositoryName'].id | [0]" | |
$repositoryId | |
# Get all policies from branch | |
#az repos policy list --query "[?contains(settings.scope[].repositoryId,'$repositoryId')] | [?contains(settings.scope[].refName, 'refs/heads/$FromBranch')]" | |
# List all policies in table format with relevant information | |
#az repos policy list --query "[?contains(settings.scope[].repositoryId,'$repositoryId')] | [?contains(settings.scope[].refName, 'refs/heads/$FromBranch')] | [].{Type:type.displayName, BuildDefinitionId:settings.buildDefinitionId, Name:settings.displayName, Branch:settings.scope[0].refName}" --output table | |
$policies = az repos policy list --query "[?contains(settings.scope[].repositoryId,'$repositoryId')] | [?contains(settings.scope[].refName, 'refs/heads/$FromBranch')]" | ConvertFrom-Json | |
foreach ($policy in $policies) { | |
# See documentation on how to create a branch policy https://learn.microsoft.com/en-us/azure/devops/repos/git/branch-policies?view=azure-devops&tabs=azure-devops-cli | |
Write-Host "Create policy of the type '$($policy.type.displayName)'" | |
$completeToBranch = "refs/heads/$ToBranch" | |
switch ($policy.type.displayName) { | |
'Build' { | |
Write-Host "Create policy '$($policy.settings.displayName)'" | |
# Join path filter with ; as separator | |
$pathFilter = $null | |
if ($null -ne $policy.settings.filenamePatterns) { | |
$pathFilter = $policy.settings.filenamePatterns -join ';' | |
} | |
# Create branch build policy | |
if ($null -eq $pathFilter) { | |
az repos policy build create ` | |
--repository-id $repositoryId ` | |
--branch $completeToBranch ` | |
--branch-match-type $ToBranchMatchType ` | |
--enabled $policy.isEnabled ` | |
--blocking $policy.isBlocking ` | |
--queue-on-source-update-only $policy.settings.queueOnSourceUpdateOnly ` | |
--manual-queue-only $policy.settings.manualQueueOnly ` | |
--valid-duration $policy.settings.validDuration ` | |
--build-definition-id $policy.settings.buildDefinitionId ` | |
--display-name $policy.settings.displayName | |
} | |
else { | |
az repos policy build create ` | |
--repository-id $repositoryId ` | |
--branch $completeToBranch ` | |
--branch-match-type $ToBranchMatchType ` | |
--enabled $policy.isEnabled ` | |
--blocking $policy.isBlocking ` | |
--queue-on-source-update-only $policy.settings.queueOnSourceUpdateOnly ` | |
--manual-queue-only $policy.settings.manualQueueOnly ` | |
--valid-duration $policy.settings.validDuration ` | |
--build-definition-id $policy.settings.buildDefinitionId ` | |
--display-name $policy.settings.displayName ` | |
--path-filter $pathFilter | |
} | |
} | |
'Minimum number of reviewers' { | |
#Known bug: https://github.com/Azure/azure-devops-cli-extension/issues/1091 | |
#--block-lastPusherVote $policy.settings.blockLastPusherVote ` | |
az repos policy approver-count create ` | |
--repository-id $repositoryId ` | |
--branch $completeToBranch ` | |
--branch-match-type $ToBranchMatchType ` | |
--enabled $policy.isEnabled ` | |
--blocking $policy.settings.blockLastPusherVote ` | |
--allow-downvotes $policy.settings.allowDownvotes ` | |
--creator-vote-counts $policy.settings.creatorVoteCounts ` | |
--minimum-approver-count $policy.settings.minimumApproverCount ` | |
--reset-on-source-push $policy.settings.resetOnSourcePush | |
} | |
'Work item linking' { | |
az repos policy work-item-linking create ` | |
--repository-id $repositoryId ` | |
--branch $completeToBranch ` | |
--branch-match-type $ToBranchMatchType ` | |
--enabled $policy.isEnabled ` | |
--blocking $policy.isBlocking | |
} | |
'Comment requirements' { | |
az repos policy comment-required create ` | |
--repository-id $repositoryId ` | |
--branch $completeToBranch ` | |
--branch-match-type $ToBranchMatchType ` | |
--enabled $policy.isEnabled ` | |
--blocking $policy.isBlocking | |
} | |
'Require a merge strategy' { | |
$allowNoFastForward = $false | |
if ($null -ne $policy.settings.allowNoFastForward) { $allowNoFastForward = $policy.settings.allowNoFastForward } | |
$allowRebase = $false | |
if ($null -ne $policy.settings.allowRebase) { $allowRebase = $policy.settings.allowRebase } | |
$allowRebaseMerge = $false | |
if ($null -ne $policy.settings.allowRebaseMerge) { $allowRebaseMerge = $policy.settings.allowRebaseMerge } | |
$allowSquash = $false | |
if ($null -ne $policy.settings.allowSquash) { $allowSquash = $policy.settings.allowSquash } | |
az repos policy merge-strategy create ` | |
--repository-id $repositoryId ` | |
--branch $completeToBranch ` | |
--branch-match-type $ToBranchMatchType ` | |
--enabled $policy.isEnabled ` | |
--blocking $policy.isBlocking ` | |
--allow-no-fast-forward $allowNoFastForward ` | |
--allow-rebase $allowRebase ` | |
--allow-rebase-merge $allowRebaseMerge ` | |
--allow-squash $allowSquash | |
} | |
Default { | |
Write-Host "Policy type '$($policy.type.displayName)' is not supported" | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment