From 043ee7d31bf6c699cb2033aba950c2d1b1ff3888 Mon Sep 17 00:00:00 2001 From: Don Syme Date: Thu, 26 Nov 2026 18:25:39 +0000 Subject: [PATCH 1/5] Run only affected test apps in pull request builds Re-wires the BuildOptimization test selection (#7392) that was dropped when test projects moved to Invoke-PerProjectTestRun, and computes PR changes from the PR merge commit so base-branch changes missing from a stale PR branch are no longer treated as PR changes. Non-PR events still run every test app. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- build/scripts/BuildOptimization.psm1 | 89 +++++++++++++++++- build/scripts/ParallelTestExecution.psm1 | 2 + .../scripts/tests/BuildOptimization.Test.ps1 | 94 +++++++++++++++++++ 3 files changed, 183 insertions(+), 2 deletions(-) diff --git a/build/scripts/BuildOptimization.psm1 b/build/scripts/BuildOptimization.psm1 index 2c5d5a53037..8331ce5a6ad 100644 --- a/build/scripts/BuildOptimization.psm1 +++ b/build/scripts/BuildOptimization.psm1 @@ -335,6 +335,54 @@ function Get-ChangedFilesForCI { } } +<# +.SYNOPSIS + Returns the files changed by a pull request, using the pull request merge commit. +.DESCRIPTION + For pull_request events GitHub checks out refs/pull/N/merge, whose first parent is the + base branch tip and whose second parent is the pull request head. Diffing the merge commit + against its first parent yields exactly the pull request's changes, unlike diffing the head + against a base branch commit, which also reports base-branch changes the pull request + branch does not contain yet. Needs only the first parent commit, so it works with shallow + checkouts. +.OUTPUTS + String array of changed file paths relative to the repo root, or $null when the checked-out + commit is not the pull request merge commit or the diff cannot be computed. +#> +function Get-PullRequestMergeChangedFiles { + [CmdletBinding()] + [OutputType([string[]])] + param() + + if ($env:GITHUB_EVENT_NAME -notmatch 'pull_request') { return $null } + if (-not $env:GITHUB_EVENT_PATH -or -not (Test-Path $env:GITHUB_EVENT_PATH)) { return $null } + $headSha = (Get-Content $env:GITHUB_EVENT_PATH -Raw | ConvertFrom-Json).pull_request.head.sha + if (-not $headSha) { return $null } + + $prevErrorAction = $ErrorActionPreference + $ErrorActionPreference = 'Continue' + try { + $parents = @((& git cat-file -p HEAD 2>$null) | Where-Object { $_ -match '^parent ' } | ForEach-Object { ($_ -split ' ')[1] }) + if ($parents.Count -ne 2 -or $parents[1] -ne $headSha) { + Write-Host "BUILD OPTIMIZATION: HEAD is not the merge commit for pull request head $headSha" + return $null + } + + & git cat-file -e "$($parents[0])^{commit}" 2>$null + if ($LASTEXITCODE -ne 0) { + & git fetch --no-tags --depth=1 origin $parents[0] 2>$null + } + + $files = @(& git -c core.quotePath=false diff --name-only $parents[0] HEAD -- 2>$null) + if ($LASTEXITCODE -ne 0 -or $files.Count -eq 0) { return $null } + Write-Host "BUILD OPTIMIZATION: Pull request merge commit changes $($files.Count) file(s)" + return [string[]]$files + } + finally { + $ErrorActionPreference = $prevErrorAction + } +} + <# .SYNOPSIS Checks whether any changed files match the fullBuildPatterns from AL-Go settings. @@ -413,7 +461,10 @@ function Get-AffectedAppNames { return [PSCustomObject]$runAll } - $changedFiles = Get-ChangedFilesForCI + $changedFiles = Get-PullRequestMergeChangedFiles + if (-not $changedFiles) { + $changedFiles = Get-ChangedFilesForCI -CompareFromMergeBase + } if (-not $changedFiles) { Write-Host "BUILD OPTIMIZATION: Running all tests - could not determine changed files" return [PSCustomObject]$runAll @@ -500,4 +551,38 @@ function Test-ShouldSkipTestApp { return $false } -Export-ModuleMember -Function Get-AppDependencyGraph, Get-AppForFile, Get-AffectedApps, Get-ChangedFilesForCI, Test-FullBuildPatternsMatch, Get-AffectedAppNames, Test-ShouldSkipTestApp +<# +.SYNOPSIS + Filters the test apps to run in a pull request build to those affected by the change. +.DESCRIPTION + Pull request builds run only test apps that depend, directly or transitively, on an app the + pull request changes. All other events (merge queue, push, schedule, workflow_dispatch) run + every test app, which is where full coverage is assured. Falls back to running every app + whenever the change set cannot be mapped safely (see Get-AffectedAppNames). +.PARAMETER AppNames + Test app names the job would otherwise run. +.PARAMETER BaseFolder + Root of the repository. +.OUTPUTS + String array of test app names to run. +#> +function Select-TestAppsToRun { + [CmdletBinding()] + [OutputType([string[]])] + param( + [Parameter(Mandatory)] + [AllowEmptyCollection()] + [string[]] $AppNames, + [Parameter(Mandatory)] + [string] $BaseFolder, + [string] $CacheFile = (Join-Path ([System.IO.Path]::GetTempPath()) 'BuildOptimization_AffectedApps.json') + ) + + if ($env:GITHUB_EVENT_NAME -ne 'pull_request') { return $AppNames } + + $selected = @($AppNames | Where-Object { -not (Test-ShouldSkipTestApp -AppName $_ -BaseFolder $BaseFolder -CacheFile $CacheFile) }) + Write-Host "BUILD OPTIMIZATION: Running $($selected.Count) of $($AppNames.Count) test app(s) affected by this pull request" + return [string[]]$selected +} + +Export-ModuleMember -Function Get-AppDependencyGraph, Get-AppForFile, Get-AffectedApps, Get-ChangedFilesForCI, Get-PullRequestMergeChangedFiles, Test-FullBuildPatternsMatch, Get-AffectedAppNames, Test-ShouldSkipTestApp, Select-TestAppsToRun diff --git a/build/scripts/ParallelTestExecution.psm1 b/build/scripts/ParallelTestExecution.psm1 index 0dc9bc1c985..7ef93a8c53e 100644 --- a/build/scripts/ParallelTestExecution.psm1 +++ b/build/scripts/ParallelTestExecution.psm1 @@ -13,6 +13,7 @@ if (-not (Get-Command Write-Log -ErrorAction SilentlyContinue)) { } } Import-Module (Join-Path $PSScriptRoot "ALAppBuild.psm1" -Resolve) +Import-Module (Join-Path $PSScriptRoot "BuildOptimization.psm1" -Resolve) <# .SYNOPSIS @@ -1557,6 +1558,7 @@ function Invoke-PerProjectTestRun { $installed = Get-InstalledTestAppNames -ContainerName $parameters.containerName -Tenant $parameters.tenant -Country $country $appNamesToTest = Get-AppNamesForBucket -InstalledTestAppNames $installed -TestType $testType -BucketNumber $bucketNumber + $appNamesToTest = @(Select-TestAppsToRun -AppNames $appNamesToTest -BaseFolder (Get-BaseFolder)) if ($appNamesToTest.Count -eq 0) { Write-Host "No test apps to run for testType '$testType' in this project." diff --git a/build/scripts/tests/BuildOptimization.Test.ps1 b/build/scripts/tests/BuildOptimization.Test.ps1 index 8ee671c9766..61f61aa1085 100644 --- a/build/scripts/tests/BuildOptimization.Test.ps1 +++ b/build/scripts/tests/BuildOptimization.Test.ps1 @@ -487,4 +487,98 @@ Describe "BuildOptimization" { } } } + + Context "Get-PullRequestMergeChangedFiles" { + BeforeAll { + $repo = Join-Path ([System.IO.Path]::GetTempPath()) "BuildOptimization_Merge_$([System.Guid]::NewGuid())" + New-Item -ItemType Directory -Path $repo | Out-Null + Push-Location $repo + try { + git init -q -b main + git config user.email 'ci@example.com' + git config user.name 'ci' + Set-Content -Path 'shared.txt' -Value 'base' + git add -A; git commit -q -m base + git checkout -q -b feature + Set-Content -Path 'feature.txt' -Value 'pr change' + git add -A; git commit -q -m feature + $script:prHead = (git rev-parse HEAD) + git checkout -q main + Set-Content -Path 'main-only.txt' -Value 'base branch change' + git add -A; git commit -q -m main + git merge -q --no-ff feature -m merge + } finally { + Pop-Location + } + $eventFile = Join-Path $repo 'event.json' + @{ pull_request = @{ head = @{ sha = $script:prHead } } } | ConvertTo-Json -Depth 4 | Set-Content $eventFile + } + AfterAll { + Remove-Item $repo -Recurse -Force -ErrorAction SilentlyContinue + } + + It "returns only the pull request's changes, not base-branch changes missing from the PR branch" { + $saved = @($env:GITHUB_EVENT_NAME, $env:GITHUB_EVENT_PATH) + Push-Location $repo + try { + $env:GITHUB_EVENT_NAME = 'pull_request' + $env:GITHUB_EVENT_PATH = $eventFile + Get-PullRequestMergeChangedFiles | Should -Be @('feature.txt') + } finally { + Pop-Location + $env:GITHUB_EVENT_NAME, $env:GITHUB_EVENT_PATH = $saved + } + } + + It "returns null when HEAD is not the pull request merge commit" { + $saved = @($env:GITHUB_EVENT_NAME, $env:GITHUB_EVENT_PATH) + Push-Location $repo + try { + $env:GITHUB_EVENT_NAME = 'pull_request' + $env:GITHUB_EVENT_PATH = $eventFile + git checkout -q HEAD~1 + Get-PullRequestMergeChangedFiles | Should -BeNullOrEmpty + } finally { + git checkout -q main + Pop-Location + $env:GITHUB_EVENT_NAME, $env:GITHUB_EVENT_PATH = $saved + } + } + } + + Context "Select-TestAppsToRun" { + BeforeAll { + $selectCache = Join-Path ([System.IO.Path]::GetTempPath()) "BuildOptimization_Select_$([System.Guid]::NewGuid()).json" + } + BeforeEach { + Remove-Item $selectCache -ErrorAction SilentlyContinue + } + AfterAll { + Remove-Item $selectCache -ErrorAction SilentlyContinue + } + + It "runs every test app outside pull request builds" { + $saved = $env:GITHUB_EVENT_NAME + try { + $env:GITHUB_EVENT_NAME = 'merge_group' + Mock -ModuleName BuildOptimization Test-ShouldSkipTestApp { return $true } + Select-TestAppsToRun -AppNames @('Shopify', 'E-Document Core') -BaseFolder $baseFolder -CacheFile $selectCache | Should -Be @('Shopify', 'E-Document Core') + } finally { + $env:GITHUB_EVENT_NAME = $saved + } + } + + It "runs only affected test apps in pull request builds" { + $saved = @($env:GITHUB_ACTIONS, $env:GITHUB_EVENT_NAME, $env:BUILD_OPTIMIZATION_DISABLED) + try { + $env:GITHUB_ACTIONS = 'true' + $env:GITHUB_EVENT_NAME = 'pull_request' + $env:BUILD_OPTIMIZATION_DISABLED = $null + Mock -ModuleName BuildOptimization Get-PullRequestMergeChangedFiles { return @('src/Apps/W1/EDocument/App/src/SomeFile.al') } + Select-TestAppsToRun -AppNames @('Shopify', 'E-Document Core') -BaseFolder $baseFolder -CacheFile $selectCache | Should -Be @('E-Document Core') + } finally { + $env:GITHUB_ACTIONS, $env:GITHUB_EVENT_NAME, $env:BUILD_OPTIMIZATION_DISABLED = $saved + } + } + } } From f05c581f1a63fd46d9a7749ab5471d41d179827c Mon Sep 17 00:00:00 2001 From: Don Syme Date: Thu, 26 Nov 2026 18:35:21 +0000 Subject: [PATCH 2/5] Isolate Test-ShouldSkipTestApp tests from the real pull request diff Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- build/scripts/tests/BuildOptimization.Test.ps1 | 2 ++ 1 file changed, 2 insertions(+) diff --git a/build/scripts/tests/BuildOptimization.Test.ps1 b/build/scripts/tests/BuildOptimization.Test.ps1 index 61f61aa1085..603c000a795 100644 --- a/build/scripts/tests/BuildOptimization.Test.ps1 +++ b/build/scripts/tests/BuildOptimization.Test.ps1 @@ -386,6 +386,8 @@ Describe "BuildOptimization" { } BeforeEach { Remove-Item $cacheFile -ErrorAction SilentlyContinue + # Tests drive change detection through Get-ChangedFilesForCI; do not read the real PR when running in a PR build + Mock -ModuleName BuildOptimization Get-PullRequestMergeChangedFiles { return $null } } AfterAll { Remove-Item $cacheFile -ErrorAction SilentlyContinue From c993d8ec763bca32deaffb9c9cd602eca0150965 Mon Sep 17 00:00:00 2001 From: Don Syme Date: Thu, 26 Nov 2026 18:37:53 +0000 Subject: [PATCH 3/5] Scope pull request builds to W1 and touched countries Same tree as microsoft/BCApps#14631 head 478ea1c0. --- .github/workflows/PullRequestHandler.yaml | 7 + build/scripts/PullRequestBuildScope.psm1 | 179 ++++++++++++++++++ .../tests/PullRequestBuildScope.Test.ps1 | 86 +++++++++ 3 files changed, 272 insertions(+) create mode 100644 build/scripts/PullRequestBuildScope.psm1 create mode 100644 build/scripts/tests/PullRequestBuildScope.Test.ps1 diff --git a/.github/workflows/PullRequestHandler.yaml b/.github/workflows/PullRequestHandler.yaml index 94b5a7f39fd..33bdf6d0dd5 100644 --- a/.github/workflows/PullRequestHandler.yaml +++ b/.github/workflows/PullRequestHandler.yaml @@ -60,6 +60,13 @@ jobs: lfs: true ref: ${{ github.event_name == 'pull_request' && github.sha || github.event_name == 'merge_group' && github.ref || format('refs/pull/{0}/merge', github.event.pull_request.number) }} + - name: Scope pull request build + if: github.event_name == 'pull_request' + shell: pwsh + run: | + Import-Module ./build/scripts/PullRequestBuildScope.psm1 -Force + Set-PullRequestBuildScope -BaseFolder $env:GITHUB_WORKSPACE + - name: Initialize the workflow id: init uses: microsoft/AL-Go/Actions/WorkflowInitialize@ea8d8a61e8d3968406c09635951edb7f0f95200a diff --git a/build/scripts/PullRequestBuildScope.psm1 b/build/scripts/PullRequestBuildScope.psm1 new file mode 100644 index 00000000000..3578ec5ed72 --- /dev/null +++ b/build/scripts/PullRequestBuildScope.psm1 @@ -0,0 +1,179 @@ +<# +.SYNOPSIS + Narrows the Pull Request Build matrix to the countries and test modes a pull request affects. +.DESCRIPTION + Every country project (Apps XX) also contains the W1 apps, so with AL-Go incremental builds any + W1 change rebuilds and retests all countries. Pull request builds instead run W1 plus the + countries whose own code the pull request changes. Full country coverage is provided by the + merge queue (all-country compilation) and scheduled full-matrix runs on official branches. + Runs in the Initialization job before AL-Go reads settings, and only rewrites files in that + job's workspace. +#> + +$ErrorActionPreference = "Stop" + +Import-Module (Join-Path $PSScriptRoot "BuildOptimization.psm1" -Resolve) + +$script:FullMatrixLabel = 'full-country-matrix' +$script:SentinelCountries = @('DE', 'US') +$script:RegionCountries = @{ APAC = @('AU', 'NZ'); DACH = @('AT', 'CH', 'DE'); NA = @('CA', 'MX', 'US') } +$script:InfrastructurePatterns = @( + 'build/*', + 'src/rulesets/*', + '.github/*.json', + '.github/actions/*', + '.github/workflows/PullRequestHandler.yaml', + '.github/workflows/_BuildALGoProject.yaml' +) + +<# +.SYNOPSIS + Determines which country projects a pull request needs, based on its changed files. +.PARAMETER ChangedFiles + Files changed by the pull request, relative to the repo root. +.PARAMETER Countries + All country codes that have country projects (excluding W1). +.OUTPUTS + PSCustomObject with Countries (string[]) and Reasons (string[]). +#> +function Get-PullRequestCountries { + [CmdletBinding()] + param( + [Parameter(Mandatory)] + [string[]] $ChangedFiles, + [Parameter(Mandatory)] + [string[]] $Countries + ) + + $selected = [System.Collections.Generic.SortedSet[string]]::new() + $reasons = [System.Collections.Generic.List[string]]::new() + + foreach ($file in $ChangedFiles) { + $path = $file.Replace('\', '/') + $codes = @() + if ($path -match '^src/(Apps|Layers)/([^/]+)/') { + $folder = $Matches[2].ToUpperInvariant() + if ($script:RegionCountries.ContainsKey($folder)) { $codes = $script:RegionCountries[$folder] } + elseif ($Countries -contains $folder) { $codes = @($folder) } + } + elseif ($path -match '^build/projects/(Test )?Apps ([A-Za-z]{2})/') { + $folder = $Matches[2].ToUpperInvariant() + if ($Countries -contains $folder) { $codes = @($folder) } + } + elseif ($script:InfrastructurePatterns | Where-Object { $path -like $_ }) { + $codes = $script:SentinelCountries + } + + foreach ($code in ($codes | Where-Object { $Countries -contains $_ })) { + if ($selected.Add($code)) { $reasons.Add("$code <- $path") } + } + } + + return [PSCustomObject]@{ Countries = [string[]]@($selected); Reasons = [string[]]@($reasons) } +} + +<# +.SYNOPSIS + Removes legacy test buckets when the pull request cannot affect any legacy test app. +.PARAMETER BuildModes + Build modes configured for a test project. +.PARAMETER Affected + Result of Get-AffectedAppNames (RunAll, AppNames). +.PARAMETER LegacyTestAppNames + App names listed in the LegacyTests-* buckets of TestConfiguration.json. +#> +function Get-PullRequestTestBuildModes { + [CmdletBinding()] + [OutputType([string[]])] + param( + [Parameter(Mandatory)] + [AllowEmptyCollection()] + [string[]] $BuildModes, + [Parameter(Mandatory)] + $Affected, + [Parameter(Mandatory)] + [string[]] $LegacyTestAppNames + ) + + if ($Affected.RunAll) { return $BuildModes } + if (@($Affected.AppNames | Where-Object { $LegacyTestAppNames -contains $_ }).Count -gt 0) { return $BuildModes } + return [string[]]@($BuildModes | Where-Object { $_ -notlike 'LegacyTestsBucket*' }) +} + +<# +.SYNOPSIS + Rewrites the Pull Request Build settings in the current workspace to scope a pull request build. +.DESCRIPTION + No-op for other events, when the pull request has the full-country-matrix label, or when the + pull request's changes cannot be determined (AL-Go then builds as before). +.PARAMETER BaseFolder + Root of the repository checkout. +#> +function Set-PullRequestBuildScope { + [CmdletBinding()] + param( + [Parameter(Mandatory)] + [string] $BaseFolder + ) + + if ($env:GITHUB_EVENT_NAME -ne 'pull_request') { return } + + $prEvent = Get-Content $env:GITHUB_EVENT_PATH -Raw | ConvertFrom-Json + if (@($prEvent.pull_request.labels | Where-Object { $_.name -eq $script:FullMatrixLabel }).Count -gt 0) { + Write-Host "PR BUILD SCOPE: '$($script:FullMatrixLabel)' label present - building every country" + return + } + + $changedFiles = Get-PullRequestMergeChangedFiles + if (-not $changedFiles) { + Write-Host "PR BUILD SCOPE: Could not determine pull request changes - building every country" + return + } + + $projectsFolder = Join-Path $BaseFolder 'build/projects' + $projectNames = @(Get-ChildItem -Path $projectsFolder -Directory | Where-Object { Test-Path (Join-Path $_.FullName '.AL-Go/settings.json') } | Select-Object -ExpandProperty Name) + $countries = @($projectNames | Where-Object { $_ -match '^Apps ([A-Z]{2})$' -and $Matches[1] -ne 'W1' } | ForEach-Object { $_.Substring(5) }) + + $scope = Get-PullRequestCountries -ChangedFiles $changedFiles -Countries $countries + $projects = @($projectNames | Where-Object { + -not ($_ -match '^(Test )?Apps ([A-Z]{2})$' -and $Matches[2] -ne 'W1' -and $scope.Countries -notcontains $Matches[2]) + } | ForEach-Object { Join-Path 'build' (Join-Path 'projects' $_) }) + + $settingsFile = Join-Path $BaseFolder '.github/Pull Request Build.settings.json' + $settings = Get-Content $settingsFile -Raw | ConvertFrom-Json + $conditional = @($settings.conditionalSettings | Where-Object { $_.triggers -notcontains 'pull_request' }) + $conditional += [PSCustomObject]@{ triggers = @('pull_request'); settings = [PSCustomObject]@{ projects = $projects } } + $settings.conditionalSettings = $conditional + $settings | ConvertTo-Json -Depth 10 | Set-Content -Path $settingsFile -Encoding UTF8 + + $affected = Get-AffectedAppNames -BaseFolder $BaseFolder + $testConfig = Get-Content (Join-Path $BaseFolder 'build/scripts/TestConfiguration.json') -Raw | ConvertFrom-Json + $legacyApps = @($testConfig.PSObject.Properties | Where-Object { $_.Name -like 'LegacyTests-*' } | ForEach-Object { $_.Value }) + $droppedModes = @() + foreach ($project in @($projectNames | Where-Object { $_ -like 'Test Apps *' })) { + $projectSettingsFile = Join-Path $projectsFolder "$project/.AL-Go/settings.json" + $projectSettings = Get-Content $projectSettingsFile -Raw | ConvertFrom-Json + if (-not $projectSettings.buildModes) { continue } + $modes = Get-PullRequestTestBuildModes -BuildModes $projectSettings.buildModes -Affected $affected -LegacyTestAppNames $legacyApps + if ($modes.Count -ne @($projectSettings.buildModes).Count) { + $droppedModes += @($projectSettings.buildModes | Where-Object { $modes -notcontains $_ }) + $projectSettings.buildModes = $modes + $projectSettings | ConvertTo-Json -Depth 20 | Set-Content -Path $projectSettingsFile -Encoding UTF8 + } + } + + $countryText = if ($scope.Countries.Count) { $scope.Countries -join ', ' } else { 'none' } + $modeText = if ($droppedModes.Count) { ($droppedModes | Select-Object -Unique) -join ', ' } else { 'none' } + $summary = @( + "### Pull request build scope", + "", + "- Countries built besides W1: $countryText", + "- Test modes skipped (no affected legacy test app): $modeText", + "- Add the ``$($script:FullMatrixLabel)`` label and re-run to build every country.", + "" + ) + @($scope.Reasons | ForEach-Object { " - $_" }) + $summary | ForEach-Object { Write-Host "PR BUILD SCOPE: $_" } + if ($env:GITHUB_STEP_SUMMARY) { $summary | Add-Content -Path $env:GITHUB_STEP_SUMMARY -Encoding UTF8 } +} + +Export-ModuleMember -Function Get-PullRequestCountries, Get-PullRequestTestBuildModes, Set-PullRequestBuildScope diff --git a/build/scripts/tests/PullRequestBuildScope.Test.ps1 b/build/scripts/tests/PullRequestBuildScope.Test.ps1 new file mode 100644 index 00000000000..bca85fef5b6 --- /dev/null +++ b/build/scripts/tests/PullRequestBuildScope.Test.ps1 @@ -0,0 +1,86 @@ +Describe "PullRequestBuildScope" { + BeforeAll { + Import-Module "$PSScriptRoot\..\PullRequestBuildScope.psm1" -Force + $countries = @('AT', 'AU', 'CA', 'CH', 'CZ', 'DE', 'FR', 'MX', 'NZ', 'US') + } + + Context "Get-PullRequestCountries" { + It "builds no extra country for W1, System Application and disabled-test changes" { + $files = @('src/Apps/W1/Shopify/App/src/Foo.al', 'src/Layers/W1/BaseApp/Bar.al', 'src/System Application/App/x.al', 'src/DisabledTests/Foo/x.json') + (Get-PullRequestCountries -ChangedFiles $files -Countries $countries).Countries | Should -BeNullOrEmpty + } + + It "maps country and region folders to their countries" { + $files = @('src/Layers/CZ/BaseApp/x.al', 'src/Apps/NA/Foo/app/x.al', 'build/projects/Test Apps FR/.AL-Go/settings.json') + (Get-PullRequestCountries -ChangedFiles $files -Countries $countries).Countries | Should -Be @('CA', 'CZ', 'FR', 'MX', 'US') + } + + It "builds sentinel countries for build infrastructure changes" { + (Get-PullRequestCountries -ChangedFiles @('build/scripts/RunTestsInBcContainer.ps1') -Countries $countries).Countries | Should -Be @('DE', 'US') + } + } + + Context "Get-PullRequestTestBuildModes" { + BeforeAll { + $modes = @('IntegrationTests', 'UncategorizedTests', 'LegacyTestsBucket1', 'LegacyTestsBucket2') + } + + It "drops legacy buckets when no legacy test app is affected" { + $affected = [PSCustomObject]@{ RunAll = $false; AppNames = @('Shopify', 'Shopify Connector Test') } + Get-PullRequestTestBuildModes -BuildModes $modes -Affected $affected -LegacyTestAppNames @('Tests-ERM') | Should -Be @('IntegrationTests', 'UncategorizedTests') + } + + It "keeps legacy buckets when a legacy test app is affected or everything must run" { + $affected = [PSCustomObject]@{ RunAll = $false; AppNames = @('Tests-ERM') } + Get-PullRequestTestBuildModes -BuildModes $modes -Affected $affected -LegacyTestAppNames @('Tests-ERM') | Should -Be $modes + Get-PullRequestTestBuildModes -BuildModes $modes -Affected ([PSCustomObject]@{ RunAll = $true; AppNames = @() }) -LegacyTestAppNames @('Tests-ERM') | Should -Be $modes + } + } + + Context "Set-PullRequestBuildScope" { + BeforeEach { + $root = Join-Path ([System.IO.Path]::GetTempPath()) "PullRequestBuildScope_$([System.Guid]::NewGuid())" + foreach ($project in @('Apps W1', 'Test Apps W1', 'Apps CZ', 'Test Apps CZ', 'Apps DE', 'Test Apps DE', 'System Application Modules')) { + $folder = Join-Path $root "build/projects/$project/.AL-Go" + New-Item -ItemType Directory -Path $folder -Force | Out-Null + $projectSettings = @{ projectName = $project } + if ($project -like 'Test Apps *') { $projectSettings.buildModes = @('IntegrationTests', 'LegacyTestsBucket1') } + $projectSettings | ConvertTo-Json | Set-Content (Join-Path $folder 'settings.json') + } + New-Item -ItemType Directory -Path (Join-Path $root '.github') -Force | Out-Null + '{ "conditionalSettings": [ { "triggers": [ "merge_group" ], "settings": { "projects": [ "build/projects/Apps W1" ] } } ] }' | Set-Content (Join-Path $root '.github/Pull Request Build.settings.json') + New-Item -ItemType Directory -Path (Join-Path $root 'build/scripts') -Force | Out-Null + '{ "LegacyTests-Bucket1": [ "Tests-ERM" ] }' | Set-Content (Join-Path $root 'build/scripts/TestConfiguration.json') + + $eventFile = Join-Path $root 'event.json' + $saved = @($env:GITHUB_EVENT_NAME, $env:GITHUB_EVENT_PATH, $env:GITHUB_STEP_SUMMARY) + $env:GITHUB_EVENT_NAME = 'pull_request' + $env:GITHUB_EVENT_PATH = $eventFile + $env:GITHUB_STEP_SUMMARY = $null + Mock -ModuleName PullRequestBuildScope Get-PullRequestMergeChangedFiles { return @('src/Apps/CZ/Foo/app/x.al') } + Mock -ModuleName PullRequestBuildScope Get-AffectedAppNames { return [PSCustomObject]@{ RunAll = $false; AppNames = @('Foo CZ') } } + } + AfterEach { + $env:GITHUB_EVENT_NAME, $env:GITHUB_EVENT_PATH, $env:GITHUB_STEP_SUMMARY = $saved + Remove-Item $root -Recurse -Force -ErrorAction SilentlyContinue + } + + It "limits pull request projects to W1 and touched countries and drops unaffected legacy modes" { + @{ pull_request = @{ labels = @() } } | ConvertTo-Json -Depth 4 | Set-Content $eventFile + Set-PullRequestBuildScope -BaseFolder $root + + $settings = Get-Content (Join-Path $root '.github/Pull Request Build.settings.json') -Raw | ConvertFrom-Json + ($settings.conditionalSettings | Where-Object { $_.triggers -contains 'merge_group' }).settings.projects | Should -Be @('build/projects/Apps W1') + $projects = @(($settings.conditionalSettings | Where-Object { $_.triggers -contains 'pull_request' }).settings.projects | ForEach-Object { $_.Replace('\', '/') }) + $projects | Sort-Object | Should -Be @('build/projects/Apps CZ', 'build/projects/Apps W1', 'build/projects/System Application Modules', 'build/projects/Test Apps CZ', 'build/projects/Test Apps W1') + (Get-Content (Join-Path $root 'build/projects/Test Apps CZ/.AL-Go/settings.json') -Raw | ConvertFrom-Json).buildModes | Should -Be @('IntegrationTests') + } + + It "leaves settings untouched when the full-country-matrix label is present" { + @{ pull_request = @{ labels = @(@{ name = 'full-country-matrix' }) } } | ConvertTo-Json -Depth 4 | Set-Content $eventFile + $before = Get-Content (Join-Path $root '.github/Pull Request Build.settings.json') -Raw + Set-PullRequestBuildScope -BaseFolder $root + Get-Content (Join-Path $root '.github/Pull Request Build.settings.json') -Raw | Should -Be $before + } + } +} From e2520544f496e63c09821035b45f770f6b19b991 Mon Sep 17 00:00:00 2001 From: Don Syme Date: Thu, 26 Nov 2026 18:31:34 +0000 Subject: [PATCH 4/5] CI research: ignore CI stack files when classifying PR changes (do not merge) --- build/scripts/BuildOptimization.psm1 | 8 ++++++++ build/scripts/ResearchIgnorePaths.txt | 3 +++ 2 files changed, 11 insertions(+) create mode 100644 build/scripts/ResearchIgnorePaths.txt diff --git a/build/scripts/BuildOptimization.psm1 b/build/scripts/BuildOptimization.psm1 index 8331ce5a6ad..d888e0b75a0 100644 --- a/build/scripts/BuildOptimization.psm1 +++ b/build/scripts/BuildOptimization.psm1 @@ -375,6 +375,14 @@ function Get-PullRequestMergeChangedFiles { $files = @(& git -c core.quotePath=false diff --name-only $parents[0] HEAD -- 2>$null) if ($LASTEXITCODE -ne 0 -or $files.Count -eq 0) { return $null } + # RESEARCH ONLY (do not merge): hide the CI stack's own files so probe changes measure a single PR class + $ignoreFile = Join-Path $PSScriptRoot 'ResearchIgnorePaths.txt' + if (Test-Path $ignoreFile) { + $ignore = @(Get-Content $ignoreFile | Where-Object { $_ -and -not $_.StartsWith('#') }) + $files = @($files | Where-Object { $f = $_; -not ($ignore | Where-Object { $f -like $_ }) }) + Write-Host "BUILD OPTIMIZATION: RESEARCH ignoring CI stack files, $($files.Count) probe file(s) remain: $($files -join ', ')" + if ($files.Count -eq 0) { return $null } + } Write-Host "BUILD OPTIMIZATION: Pull request merge commit changes $($files.Count) file(s)" return [string[]]$files } diff --git a/build/scripts/ResearchIgnorePaths.txt b/build/scripts/ResearchIgnorePaths.txt new file mode 100644 index 00000000000..2dcb1e70069 --- /dev/null +++ b/build/scripts/ResearchIgnorePaths.txt @@ -0,0 +1,3 @@ +# RESEARCH ONLY: paths changed by the CI stack itself +build/* +.github/* From 0dd26d981eb42062d188bc4971f3b797a4841eca Mon Sep 17 00:00:00 2001 From: Don Syme Date: Thu, 26 Nov 2026 18:37:54 +0000 Subject: [PATCH 5/5] CI research probe: W1 base-layer change (BaseApp) --- src/Layers/W1/BaseApp/ApplicationSystemConstants.Codeunit.al | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Layers/W1/BaseApp/ApplicationSystemConstants.Codeunit.al b/src/Layers/W1/BaseApp/ApplicationSystemConstants.Codeunit.al index 48b70eec903..da2810ba414 100644 --- a/src/Layers/W1/BaseApp/ApplicationSystemConstants.Codeunit.al +++ b/src/Layers/W1/BaseApp/ApplicationSystemConstants.Codeunit.al @@ -89,3 +89,5 @@ codeunit 9015 "Application System Constants" end; } + +// CI research probe: W1 base-layer change (BaseApp) (do not merge)