DevOpsHandling/Get-SucessfulBuildList.ps1

<#
 .Synopsis
  Gets all successful builds existing in the build pipeline for the project
 .Description
  Gets all successful buils existing in the build pipeline for the project
 .Parameter devOpsOrganization
  Name of the DevOps Organization that holds the project
 .Parameter devOpsProjectName
  Name of the project in DevOps
 .Parameter repositoryName
  Name of the DevOps repository
 .Parameter devOpsToken
  PAT for DevOps
 .Parameter BranchName
  Name of the branch the builds should be retrieved from
  .Example
  $builds = Get-SucessfulBuildList -devOpsOrganization "test" -devOpsProjectName "test project" -repositoryName "test repo" -devOpsToken "00000000-0000-0000-0000-000000000000"
#>

function Get-SucessfulBuildList {
    Param (
        [Parameter(Mandatory=$true)]
        [string] $devOpsOrganization,
        [Parameter(Mandatory=$true)]
        [string] $devOpsProjectName,
        [Parameter(Mandatory=$true)]
        [string]$repositoryName,
        [Parameter(Mandatory=$true)]
        [string] $devOpsToken,
        [Parameter(Mandatory=$false)]
        [string] $branchName
    )

    try {
        if ($null -eq $branchName) {
            $builds = (Invoke-AzureDevOpsApi -url ('https://dev.azure.com/{0}/{1}/_apis/build/builds/?repositoryId={2}&repositoryType=TfsGit&resultFilter=succeeded&queryOrder=finishTimeDescending' -f $devOpsOrganization, $devOpsProjectName, (Get-RepositoryId -devOpsOrganization $devOpsOrganization -devOpsProjectName $devOpsProjectName -repositoryName $repositoryName -devOpsToken $devOpsToken)) -devOpsToken $devOpsToken).value
        }
        else {
            $builds = (Invoke-AzureDevOpsApi -url ('https://dev.azure.com/{0}/{1}/_apis/build/builds/?repositoryId={2}&repositoryType=TfsGit&resultFilter=succeeded&queryOrder=finishTimeDescending&branchName={3}' -f $devOpsOrganization, $devOpsProjectName, (Get-RepositoryId -devOpsOrganization $devOpsOrganization -devOpsProjectName $devOpsProjectName -repositoryName $repositoryName -devOpsToken $devOpsToken), $branchName) -devOpsToken $devOpsToken).value
        }

        return $builds
    }
    catch {
        return @()
    }
}