Files
Cloud-20Engineering/Powershell/Lists/DevOps/Repositories.ps1
Jurjen Ladenius a226ca97ac added documetation
2025-11-03 08:12:01 +01:00

167 lines
7.9 KiB
PowerShell

<#
.SYNOPSIS
Exports Azure DevOps repository information along with last completed pull request details to a CSV file.
.DESCRIPTION
This script retrieves all repositories from an Azure DevOps project and collects detailed information
about each repository including basic metadata and information about the most recent completed pull request.
For active repositories, it identifies the last completed PR and captures details about the author,
reviewers, and other relevant information. The data is exported to a timestamped CSV file for analysis.
.PARAMETER Organization
The Azure DevOps organization name. Defaults to "effectory" if not specified.
.PARAMETER Project
The Azure DevOps project name. Defaults to "survey software" if not specified.
.EXAMPLE
.\Repositories.ps1
Exports repository information using the default organization and project settings.
.EXAMPLE
.\Repositories.ps1 -Organization "myorg" -Project "myproject"
Exports repository information for a specific organization and project.
.OUTPUTS
Creates a timestamped CSV file in the current directory with the format: "yyyy-MM-dd HHmm repositories.csv"
The CSV contains the following columns:
- Id: Repository unique identifier
- Name: Repository name
- DefaultBranch: Repository default branch (e.g., main, master)
- IsDisabled: Boolean indicating if the repository is disabled
- WebUrl: Repository web URL in Azure DevOps
- LastPRDate: Creation date of the most recent completed pull request
- LastPRName: Title of the most recent completed pull request
- LastPRCreatedBy: Author of the most recent completed pull request
- LastPRReviewers: Comma-separated list of reviewers for the most recent completed PR
- LastPRUrl: Direct URL to the most recent completed pull request
.NOTES
Author: Cloud Engineering Team
Created: 2025
Requires: PowerShell 5.1 or later, Azure CLI installed and authenticated
Dependencies: Azure CLI (az) must be installed and user must be authenticated
Prerequisites:
- Install Azure CLI: https://docs.microsoft.com/en-us/cli/azure/install-azure-cli
- Authenticate: az login
- Set default subscription if needed: az account set --subscription "subscription-name"
The script processes all repositories in the specified project, including disabled ones.
For disabled repositories, pull request information will be empty as they cannot be accessed.
Only completed pull requests are considered when determining the "last" PR.
.LINK
https://docs.microsoft.com/en-us/cli/azure/repos
#>
param(
[Parameter(Mandatory=$false, HelpMessage="Azure DevOps organization name")]
[string]$Organization = "effectory",
[Parameter(Mandatory=$false, HelpMessage="Azure DevOps project name")]
[string]$Project = "survey software"
)
# Define a class to structure repository information
class Repository {
[string] $Id = "" # Repository unique identifier
[string] $Name = "" # Repository display name
[string] $DefaultBranch = "" # Repository default branch (e.g., main, master)
[string] $IsDisabled = "" # Whether the repository is disabled (True/False)
[string] $WebUrl = "" # Repository web URL in Azure DevOps
[string] $LastPRDate = "" # Creation date of most recent completed PR
[string] $LastPRName = "" # Title of most recent completed PR
[string] $LastPRCreatedBy = "" # Author of most recent completed PR
[string] $LastPRReviewers = "" # Comma-separated list of reviewers
[string] $LastPRUrl = "" # Direct URL to most recent completed PR
}
# Generate timestamped filename for the output CSV
[string] $date = Get-Date -Format "yyyy-MM-dd HHmm"
$fileName = ".\$date repositories.csv"
# Display script execution banner
Write-Host "========================================================================================================================================================================"
Write-Host "Creating repository overview for Organization: $Organization, Project: $Project"
Write-Host "Output file: $fileName"
Write-Host "Note: This script requires Azure CLI to be installed and authenticated (az login)"
Write-Host "========================================================================================================================================================================"
# Retrieve all repositories from the Azure DevOps project
Write-Host "Fetching repositories from project '$Project'..." -ForegroundColor Yellow
$repos = az repos list --organization "https://dev.azure.com/$Organization/" --project "$Project" | ConvertFrom-Json | Select-Object
Write-Host "Found $($repos.Count) repositories" -ForegroundColor Green
# Initialize array to store repository information
[Repository[]]$Result = @()
# Process each repository to collect information and last PR details
foreach ($repo in $repos)
{
Write-Host "Processing repository: $($repo.name)" -ForegroundColor Cyan
# Create new repository object and populate basic information
[Repository] $repository = [Repository]::new()
$repository.Id = $repo.id
$repository.Name = $repo.name
$repository.DefaultBranch = $repo.defaultBranch
$repository.IsDisabled = $repo.isDisabled
$repository.WebUrl = $repo.webUrl
# Only attempt to get pull request information for active repositories
if ($true -ne $repo.isDisabled)
{
Write-Host " Fetching last completed pull request..." -ForegroundColor Gray
# Get the most recent completed pull request (top 1, sorted by most recent)
$lastPr = az repos pr list --project "$Project" --repository $repo.name --organization "https://dev.azure.com/$Organization/" --status completed --top 1 | ConvertFrom-Json | Select-Object
# If a completed PR exists, capture its details
if ($lastPr)
{
$repository.LastPRDate = $lastPr.creationDate
$repository.LastPRName = $lastPr.title
$repository.LastPRUrl = $lastPr.url
$repository.LastPRCreatedBy = $lastPr.createdBy.displayName
# Join all reviewer names into a comma-separated string
$repository.LastPRReviewers = $lastPr.reviewers | join-string -property displayName -Separator ','
Write-Host " Last PR: $($lastPr.title) ($(Get-Date $lastPr.creationDate -Format 'yyyy-MM-dd'))" -ForegroundColor Gray
}
else
{
Write-Host " No completed pull requests found" -ForegroundColor Gray
}
}
else
{
Write-Host " Repository is disabled - skipping pull request analysis" -ForegroundColor Yellow
}
# Add repository to results array
$Result += $repository
}
# Export results to CSV file
$Result | Export-Csv -Path $fileName -NoTypeInformation
# Display completion summary
Write-Host "========================================================================================================================================================================"
Write-Host "Export completed successfully!" -ForegroundColor Green
Write-Host "Total repositories processed: $($Result.Count)" -ForegroundColor Yellow
# Count repositories with and without recent PRs
$reposWithPRs = ($Result | Where-Object { $_.LastPRDate -ne "" }).Count
$activeRepos = ($Result | Where-Object { $_.IsDisabled -ne "True" }).Count
$disabledRepos = ($Result | Where-Object { $_.IsDisabled -eq "True" }).Count
Write-Host "Active repositories: $activeRepos" -ForegroundColor Yellow
Write-Host "Disabled repositories: $disabledRepos" -ForegroundColor Yellow
Write-Host "Repositories with completed PRs: $reposWithPRs" -ForegroundColor Yellow
Write-Host "Output file: $fileName" -ForegroundColor Yellow
Write-Host "========================================================================================================================================================================"