简体   繁体   中英

Assign branch policies to newly created repository in new project

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:

https://learn.microsoft.com/en-us/rest/api/azure/devops/policy/configurations/create?view=azure-devops-rest-6.0&tabs=HTTP#merge-strategy-policy

When I attempt to post to the URL given for any of these policies I see the error message as follows: Invoke-RestMethod: {"count":1,"value":{"Message":"The requested resource does not support http method 'POST'."}}

  1. Attempted to update the branch policies via Azure CLI, I installed the Azure CLI and acquired a personal access token. I see there are commands to accomplish getting a list of policies within a branch or editing them. However, when I try to remotely sign into our instance of DevOps I see this error:

Azure DevOps CLI is not supported for Azure DevOps server

I think this one may be a versioning issue since the output is telling me it's not supported. Perhaps I'm not implementing this correctly but the syntax is straight forward in what it's asking for so I don't think it's a syntax issue.

  1. Last attempt was to create a branch with some standard policies on it within the new project. Then using the Update Ref API call I would use the ID of that already created branch to try and create a new branch mimicking the exact configuration of the old one. Here's the documentation I used to try this out:

https://learn.microsoft.com/en-us/rest/api/azure/devops/git/refs/update-refs?view=azure-devops-rest-6.0&tabs=HTTP

The post method for this one expects a relatively small payload; just the name, oldObjectID and newObjectID. However the issue I ran into here is that the old branch is out of scope while trying to create a new branch in a different repo. Even though the branch exists in a repo in the same project it still can't find the ID of the old branch and returns an unsuccessful error in the output: unresolvableToCommit

I have attempted calling the Azure DevOps REST API " Configurations - Create " via Postman and PowerShell script to set merge strategy policy for a new branch in a new git repository on Azure DevOps Server 2020, and the REST API can work well as expected.

Below is a sample I attempted on my side.

Calling the REST API via Postman:

在此处输入图像描述

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:

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM