Public/Get-ChangedALFiles.ps1
|
function Get-ChangedALFiles { <# .SYNOPSIS Get list of changed AL files using native git .DESCRIPTION Detects changed .al files in the current branch compared to a base branch. Auto-detects PR context in Azure DevOps pipelines. .PARAMETER BaseBranch Base branch for comparison (default: origin/master). In PR builds, auto-detects from SYSTEM_PULLREQUEST_TARGETBRANCH environment variable. .EXAMPLE Get-ChangedALFiles Returns all changed .al files compared to origin/master .EXAMPLE Get-ChangedALFiles -BaseBranch "origin/development" Compare against specific branch .OUTPUTS System.String[] - Array of file paths relative to repository root .NOTES Author: waldo Version: 1.0.0 #> [CmdletBinding()] param( [Parameter(Mandatory = $false)] [string]$BaseBranch = "origin/master" ) begin { Write-Verbose "Starting $($MyInvocation.MyCommand.Name)" } process { try { # Auto-detect base branch in PR context if ($env:SYSTEM_PULLREQUEST_TARGETBRANCH) { $branchName = $env:SYSTEM_PULLREQUEST_TARGETBRANCH -replace '^refs/heads/', '' $BaseBranch = "origin/$branchName" Write-Host "PR detected - comparing against $BaseBranch" } Write-Verbose "Getting changed AL files using git diff against $BaseBranch" # Ensure we have latest remote refs Write-Verbose "Fetching latest from origin..." git fetch origin --quiet 2>&1 | Out-Null # In PR builds, use BUILD_SOURCEVERSION (the merge commit) if available # Otherwise use HEAD $compareRef = "HEAD" if ($env:BUILD_SOURCEVERSION -and $env:BUILD_SOURCEVERSION -ne '') { $compareRef = $env:BUILD_SOURCEVERSION } # Debug: Show current state Write-Host "Current HEAD: $(git rev-parse HEAD 2>&1)" Write-Host "Compare ref: $compareRef" Write-Host "Compare commit: $(git rev-parse $compareRef 2>&1)" Write-Host "Base branch: $BaseBranch" Write-Host "Base commit: $(git rev-parse $BaseBranch 2>&1)" Write-Host "" # Get list of changed .al files # Use two-dot syntax for merge commits: BASE..HEAD shows commits reachable from HEAD but not BASE Write-Host "Running git diff: $BaseBranch..$compareRef" $gitOutput = git diff --name-only --diff-filter=ACMR "$BaseBranch..$compareRef" 2>&1 # Check if git command failed if ($LASTEXITCODE -ne 0) { Write-Error "Git diff failed with exit code $LASTEXITCODE" Write-Error "Git output: $gitOutput" throw "Git diff command failed" } $changedFiles = $gitOutput | Where-Object { $_ -match '\.al$' } if (-not $changedFiles) { Write-Host "No AL files changed" Write-Host "" Write-Host "Debug: All changed files:" $allFiles = git diff --name-only "$BaseBranch..$compareRef" 2>&1 if ($allFiles) { $allFiles | ForEach-Object { Write-Host " $_" } } else { Write-Host " (no files changed)" } Write-Host "" Write-Host "✅ No AL files changed - skipping review" return @() } $changedFiles = @($changedFiles) # Ensure array Write-Host "✅ Found $($changedFiles.Count) changed AL file(s):" foreach ($file in $changedFiles) { Write-Host " - $file" } return $changedFiles } catch { Write-Error "Error in $($MyInvocation.MyCommand.Name): $_" throw } } end { Write-Verbose "Completed $($MyInvocation.MyCommand.Name)" } } |