
class Repository {
    [string] $Id = ""
    [string] $Name = ""
    [string] $DefaultBranch = ""
    [string] $IsDisabled = ""
    [string] $WebUrl = ""
    [string] $LastPRDate = ""
    [string] $LastPRName = ""
    [string] $LastPRUrl = ""
}

$fileName =  "c:\temp\2023-05-03 repositories.csv" 

Write-Host "========================================================================================================================================================================"
Write-Host "Creating repository overview."
Write-Host "========================================================================================================================================================================"

$repos = az repos list  --organization "https://dev.azure.com/effectory/" --project "survey software" | ConvertFrom-Json | Select-Object

[Repository[]]$Result = @()

foreach ($repo in $repos) 
{
    [Repository] $repository = [Repository]::new()
    $repository.Id = $repo.id
    $repository.Name = $repo.name
    $repository.DefaultBranch = $repo.defaultBranch
    $repository.IsDisabled = $repo.isDisabled
    $repository.WebUrl = $repo.webUrl

    $lastPr = az repos pr list --project "survey software" --repository $repo.name --organization "https://dev.azure.com/effectory/" --status all --top 1 | ConvertFrom-Json | Select-Object

    if ($lastPr) {
    $repository.LastPRDate = $lastPr.creationDate
    $repository.LastPRName = $lastPr.title
    $repository.LastPRUrl = $lastPr.url
    }

    $Result += $repository 
}

$Result | Export-Csv -Path $fileName -Append -NoTypeInformation

Write-Host "========================================================================================================================================================================"
Write-Host "Done."

# az repos pr list --project "survey software" --repository "ProjectCenter" --organization "https://dev.azure.com/effectory/" --status all --top 1