Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Azure DevOps Rest API to create a branch from a specific branch

I'm looking for an Azure DevOps Rest API to create a new branch from an existing branch.

like image 624
irudne Avatar asked Dec 17 '22 14:12

irudne


2 Answers

Azure DevOps Rest API to create a branch from a specific branch

Konteks pointed out the correct REST API.

We could use the Initial commit (Create a new branch) to create a branch, but if you want to create a branch from a specific branch, we need modify the Request Body.

POST https://dev.azure.com/fabrikam/_apis/git/repositories/{repositoryId}/pushes?api-version=5.1

First, we need use the REST API Repositories - List to get the repositoryId.

GET https://dev.azure.com/{organization}/{project}/_apis/git/repositories?api-version=4.1

Then, use the REST API Refs - List with filter=<BranchName> to get the oldObjectId for your specific branch:

GET https://dev.azure.com/{organization}/{project}/_apis/git/repositories/{repositoryId}/refs?filter=heads/master&api-version=5.1

Now, we could use the REST API Repositories - List with following request body to create a new branch from a specific branch:

{
  "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"
          }
        }
      ]
    }
  ]
}

Result from postman:

enter image description here

This is my test powershell scripts:

$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

Hope this helps.

like image 73
Leo Liu-MSFT Avatar answered Jan 17 '23 10:01

Leo Liu-MSFT


I captured the requests created by the AzureDevOps web UI and found out that creating a branch from a specific branch is quite simple task for the 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
like image 30
Miroslav Ježík Avatar answered Jan 17 '23 11:01

Miroslav Ježík