Last active
August 16, 2023 10:36
Revisions
-
manoj-choudhari-git revised this gist
Aug 16, 2023 . 1 changed file with 5 additions and 5 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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, $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, $version) { ## ----------------------------------------------------------------- ## Get ID of the base branch ## ----------------------------------------------------------------- $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=$version" $body = ConvertTo-Json @( @{ name = "refs/heads/$newBranch" -
manoj-choudhari-git created this gist
Aug 16, 2023 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 }