用于从特定分支创建分支的 Azure DevOps Rest API

iru*_*dne 3 azure azure-devops azure-devops-rest-api

我正在寻找 Azure DevOps Rest API 来从现有分支创建新分支。

小智 11

我捕获了 AzureDevOps Web UI 创建的请求,发现从特定分支创建分支对于 REST api 来说是非常简单的任务。

$repository = "repositoryName"
$newBranch = "newBranchName"
$baseBranch = "baseBranchName"

$organization = "organizationName"
$project = "projectName"
$pat = "personalAccessToken"

$base64AuthInfo = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$pat"))

$headers = @{ Authorization = "Basic $base64AuthInfo" }

# Get ID of the base branch
$url = "https://dev.azure.com/$organization/$project/_apis/git/repositories/$repository/refs?filter=heads/$baseBranch&api-version=5.1"
$baseBranchResponse = Invoke-RestMethod -Uri $url -ContentType "application/json" -headers $headers -Method GET

# Create a new branch
$url = "https://dev.azure.com/$organization/$project/_apis/git/repositories/$repository/refs?api-version=5.1"
$body = ConvertTo-Json @(
@{
    name = "refs/heads/$newBranch"
    newObjectId = $baseBranchResponse.value.objectId
    oldObjectId = "0000000000000000000000000000000000000000"
})

$response = Invoke-RestMethod -Uri $url -ContentType "application/json" -Body $body -headers $headers -Method POST
Run Code Online (Sandbox Code Playgroud)


Leo*_*SFT 5

用于从特定分支创建分支的 Azure DevOps Rest API

Konteks 指出了正确的 REST API。

我们可以使用Initial commit(创建一个新分支)来创建一个分支,但是如果你想从一个特定的分支创建一个分支,我们需要修改Request Body。

POST https://dev.azure.com/fabrikam/_apis/git/repositories/{repositoryId}/pushes?api-version=5.1
Run Code Online (Sandbox Code Playgroud)

首先,我们需要使用 REST API Repositories-List来获取repositoryId.

GET https://dev.azure.com/{organization}/{project}/_apis/git/repositories?api-version=4.1
Run Code Online (Sandbox Code Playgroud)

然后,使用 REST API Refs - List withfilter=<BranchName>来获取oldObjectId您的特定分支:

GET https://dev.azure.com/{organization}/{project}/_apis/git/repositories/{repositoryId}/refs?filter=heads/master&api-version=5.1
Run Code Online (Sandbox Code Playgroud)

现在,我们可以使用Repositories - List带有以下请求正文的 REST API从特定分支创建一个新分支

{
  "refUpdates": [
    {
      "name": "refs/heads/<DefineYourNewBranchName>",
      "oldObjectId": "<oldObjectId we get from above REST API>"
    }
  ],
  "commits": [
    {
      "comment": "Initial commit.",
      "changes": [
        {
          "changeType": "add",
          "item": {
            "path": "/readme111.md"
          },
          "newContent": {
            "content": "My first file!",
            "contentType": "rawtext"
          }
        }
      ]
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)

邮递员的结果:

在此处输入图片说明

这是我的测试 powershell 脚本:

$connectionToken="<PAT Here>"
$base64AuthInfo= [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($connectionToken)"))

$headers = @{ Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN" }

$url= "https://dev.azure.com/<Organizationname>/<ProjectName>/_apis/git/repositories/<repositoryId>/pushes?api-version=5.1"


$body =@"
{
  "refUpdates": [
    {
      "name": "refs/heads/Test228",
      "oldObjectId": "a57f0c34f8ec7330bdc37e7631f7be3cc3ea3956"
    }
  ],
  "commits": [
    {
      "comment": "Initial commit.",
      "changes": [
        {
          "changeType": "add",
          "item": {
            "path": "/readme111.md"
          },
          "newContent": {
            "content": "My first file!",
            "contentType": "rawtext"
          }
        }
      ]
    }
  ]
}
"@


Write-Host "$url"
$response= Invoke-RestMethod -Uri $url -ContentType "application/json-patch+json" -Body $body -headers @{authorization = "Basic $base64AuthInfo"} -Method POST
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助。