Tuesday 7 April 2020

Copy Azure Git Repo Branch Policies from One Branch to Another

Some teams when they practice Agile sprints keep a branch for the life of the sprint. In this case they do branch to develop features, make pull requests to sprint branch and release from the sprint branch at the end of the sprint. It is important to protect the sprint branch from incoming pull requests with policies. Since, there is no out of the box way to copy over the policies defined in one branch to another, each sprint the policies need to be created for the new sprint branch, which makes it a cumbersome process. In order to make it possible to copy set of policies defined in a branch to another, in Azure Git repos, now you can use the script made available here.

The script does below described actions.

The script takes the parameters listed below.

  • $AzureDevOpsPAT: Personal Access Token having access to the code read, write and manage
  • $OrganizationName: Azure DevOps organization name
  • $teamProjectName: Team project name which contains the repository
  • $repositoryName: Name of the Azure Git repository
  • $fromBranch: Branch with the policies already setup
  • $toBranch: Branch the policies should be copied to

As the first step an authentication header is created using the personal access token (PAT) to allow access to the REST API.

In the next step it reads the repository details to obtain the repository id.

Then the policies configured in the from branch is retrieved.

Next each policy is read and removed the information of the from branch. The target branch is set and the policy is applied to the target branch.

This would be a handy tool to just copy over the policies from one Azure Git repo branch to another branch in the same repo by executing the script as shown below.
.\PolicyCopy.ps1 -AzureDevOpsPAT 'yourPAT' -OrganizationName 'xamariners' -teamProjectName 'yourAzureDevOpsOrganization' -repositoryName 'YourAzureDevOpsGitRepo' -fromBranch 'from' -toBranch 'version/0.0.1999_policycopy'

No comments:

Popular Posts