Home > database >  Assign branch policies to newly created repository in new project
Assign branch policies to newly created repository in new project

Time:11-23

I am currently in the process of automating the creation of a repository within a project. So far I have been successful in creating the repository, creating a main/development branch and placing prerequisite files into the respective branches.

Now I am at the point where I need to assign a branch policy to the newly created repo, keep in mind that this is within a brand new repo in a brand new project. So far I have attempted the following methods but ran into issues:

  1. Attempted to assign branch policies via API call using the following documentation:

enter image description here

Calling the REST API via PowerShell:

$pat = '{PAT}'
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f "", $pat)))

$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Authorization", ("Basic {0}" -f $base64AuthInfo))
$headers.Add("Content-Type", "application/json")

$uri = "https://{instance}/{collection}/{project}/_apis/policy/configurations?api-version=6.0"

$body = '{
    "isEnabled": true,
    "isBlocking": true,
    "type": {
      "id": "fa4e907d-c16b-4a4c-9dfa-4916e5d171ab"
    },
    "settings": {
      "allowNoFastForward": true,
      "allowSquash": true,
      "scope": [
        {
          "repositoryId": "{repositoryId}",
          "refName": "refs/heads/main",
          "matchKind": "exact"
        }
      ]
    }
  }'

Invoke-RestMethod -Uri $uri -Headers $headers -Body $body -Method POST | ConvertTo-Json -Depth 10

Result:

enter image description here

  • Related