Skip to content

Instantly share code, notes, and snippets.

@manoj-choudhari-git
Last active August 16, 2023 10:36

Revisions

  1. manoj-choudhari-git revised this gist Aug 16, 2023. 1 changed file with 5 additions and 5 deletions.
    10 changes: 5 additions & 5 deletions azure-devops-create-branch.ps1
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@

    $devopsApiVersion = "7.0"
    $newBranch = "sprint/userStoryNumber";
    $baseBranch = "master";
    $organization = "contoso";
    @@ -8,7 +8,7 @@ $repository = "contosowebapp";
    ## -----------------------------------------------------------------
    ## Create Branch
    ## -----------------------------------------------------------------
    createNewBranch($organization, $project, $repository, $baseBranch, $newBranch, $headers);
    createNewBranch($organization, $project, $repository, $baseBranch, $newBranch, $headers, $devopsApiVersion);

    ## -----------------------------------------------------------------
    ## Generate Auth Header
    @@ -20,19 +20,19 @@ $headers = @{ Authorization = "Basic $base64AuthInfo" }
    ## -----------------------------------------------------------------
    ## Function - To Generate New Branch From Given Base Branch
    ## -----------------------------------------------------------------
    function createNewBranch($organization, $project, $repository, $baseBranch, $newBranch, $headers)
    function createNewBranch($organization, $project, $repository, $baseBranch, $newBranch, $headers, $version)
    {

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

    ## -----------------------------------------------------------------
    ## Create a new branch
    ## -----------------------------------------------------------------
    $createNewBranchUrl = "https://dev.azure.com/$organization/$project/_apis/git/repositories/$repository/refs?api-version=5.1"
    $createNewBranchUrl = "https://dev.azure.com/$organization/$project/_apis/git/repositories/$repository/refs?api-version=$version"
    $body = ConvertTo-Json @(
    @{
    name = "refs/heads/$newBranch"
  2. manoj-choudhari-git created this gist Aug 16, 2023.
    48 changes: 48 additions & 0 deletions azure-devops-create-branch.ps1
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,48 @@

    $newBranch = "sprint/userStoryNumber";
    $baseBranch = "master";
    $organization = "contoso";
    $project = "contoso";
    $repository = "contosowebapp";

    ## -----------------------------------------------------------------
    ## Create Branch
    ## -----------------------------------------------------------------
    createNewBranch($organization, $project, $repository, $baseBranch, $newBranch, $headers);

    ## -----------------------------------------------------------------
    ## Generate Auth Header
    ## -----------------------------------------------------------------
    $pat = "<<your-personal-access-token>>"
    $base64AuthInfo = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$pat"))
    $headers = @{ Authorization = "Basic $base64AuthInfo" }

    ## -----------------------------------------------------------------
    ## Function - To Generate New Branch From Given Base Branch
    ## -----------------------------------------------------------------
    function createNewBranch($organization, $project, $repository, $baseBranch, $newBranch, $headers)
    {

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

    ## -----------------------------------------------------------------
    ## Create a new branch
    ## -----------------------------------------------------------------
    $createNewBranchUrl = "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 $createNewBranchUrl -ContentType "application/json" -Body $body -headers $headers -Method POST

    Write-Host $response.value
    }