Hello all!
I am currently working on a script that will be triggered by a pipeline in ADO. I am doing my testing in PowerShell ISE. The script will get todays date and the first day of a sprint and if they match, create a new release branch release/v1.0.1. I have been able to do this successfully but the next part is to create another release branch on the next sprint (v1.0.2). I am currently having issues with doing so. The branch creation was a bit finicky to begin with, but I was able to get that taken care of.
My test code without parameters is as follows:
# Parameters for Azure DevOps
Param(
[string]$collectionurl = "",
[string]$organizationUrl = "", # Azure DevOps organization URL
[string]$project = "", # Your Azure DevOps project
[string]$team = "", # Your team name in Azure DevOps
[string]$repoid = "", # Your repository ID
[string]$newObjectId = "",
[string]$user = "",
[string]$token = ""
)
# Function to get the most recent release branch and determine the next version
function Get-NextReleaseBranchVersion {
# Use your custom $uri for filtering branches in the release path
$uri = "$organizationUrl/$project/_apis/git/repositories/$repoName/refs?filter=heads/&filterContains=release/v&api-version=6.0"
try {
# Fetch all branches that contain 'release/v' in the name
$branchesResponse = Invoke-RestMethod -Uri $uri -Method Get -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}
Write-Host "Debug: Total Branches Retrieved - Count: $($branchesResponse.value.Count)"
Filter branches that match the "release/v*" pattern
$releaseBranches = $branchesResponse.value Where-Object { $_.name -match '^refs/heads/release/v\d+\.\d+\.\d+$' }
Write-Host Debug: Release Branches After Filtering - Count: $($releaseBranches.Count)
if ($releaseBranches.Count -eq 0) {
# If no release branches are found, start with v1.0.1
Write-Host "No existing release branches found. Creating the first release branch."
return "v1.0.1"
} else {
# Extract version numbers, sort them, and determine the next version
$latestBranch = $releaseBranches Sort-Object { [version]($_.name -replace 'refs/heads/release/v', '') } -Descending Select-Object -First 1
Write-Host "Debug: Latest Release Branch Found - $($latestBranch.name)"
Store the version part for use outside of the function
$latestVersion = $latestBranch.name -replace '^refs/heads/release/v', ''
$versionParts = $latestVersion.Split('.')
$versionParts[2] = [int]$versionParts[2] + 1 # Increment the patch number
If patch number exceeds 9, reset to 0 and increment the minor version
if ($versionParts[2] -gt 9) {
$versionParts[2] = 0
$versionParts[1] = [int]$versionParts[1] + 1
}
Construct the new version string
return v$($versionParts[0]).$($versionParts[1]).$($versionParts[2])
}
} catch {
Write-Host Failed to retrieve release branches. Error: $_
return $null
}
}
Use the new version to create the branch
$newVersion = Get-NextReleaseBranchVersion
if ($null -ne $newVersion) {
# Construct the new branch name
$Newbranch_name = "release/$newVersion"
Write-Host "Creating new release branch: $Newbranch_name"
Get the latest commit ID from the most recent release branch or feature branch if no releases exist
if ($newVersion -eq v1.0.1) {
# Use the commit ID from the feature branch for the first release branch
$baseBranch = feature
} else {
# Use the most recent release branch for subsequent releases
$baseBranch = release/v$latestVersion
}
Fetch commit ID using the correctly set base branch
$baseBranchUri = $organizationUrl/$project/_apis/git/repositories/$repoName/commits?searchCriteria.itemVersion.version=$baseBranch$top=1api-version=6.0
$baseBranchResponse = Invoke-RestMethod -Uri $baseBranchUri -Method Get -Headers @{Authorization=(Basic {0} -f $base64AuthInfo)}
if ($baseBranchResponse.value.Count -gt 0) {
$newObjectId = $baseBranchResponse.value[0].com>tId
Create the JSON payload for the request
function CreateJsonBody {
$value = @
[
{
name: refs/heads/$Newbranch_name,
oldObjectId: 0000000000000000000000000000000000000000,
newObjectId: $newObjectId
}
]
"@
return $value
}
$json = CreateJsonBody
Endpoint for creating a new branch
$NewBranchUri = $organizationUrl/$project/_apis/git/repositories/$repoName/refs?api-version=6.0
$response = Invoke-RestMethod -Uri $NewBranchUri -Method POST -Body $json -ContentType application/json -Headers @{Authorization=(Basic {0} -f $base64AuthInfo)}
if ($response -ne $null) {
Write-Host "New branch '$Newbranch_name' created successfully."
} else {
Write-Host "Branch creation failed."
}
} else {
Write-Host "Failed to retrieve the latest commit ID from the base branch."
}
} else {
Write-Host "Could not determine the next release branch version."
}
This results in the following error:
Invoke-RestMethod : {“$id”:“1”,“innerException”:null,“message”:“TF401019: The Git repository with name or identifier 1 does not exist or you do not have permissions for the operation you are attempting.”
I added some further code error explanation as well that says Failed to retrieve the latest commit ID from the base branch.
Any help here is appreciated!