Skip to content
21 changes: 19 additions & 2 deletions Actions/CheckForUpdates/CheckForUpdates.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -254,8 +254,25 @@

if ($dstFileExists) {
if ($type -eq 'workflow') {
Write-Host "Apply customizations from current repository: $dstFile"
[Yaml]::ApplyCustomizations([ref] $srcContent, $dstFile)
# Determine if current repository is a final repository (has templateUrl pointing to another repo)
# Final repositories should not have custom jobs applied to prevent persistence of removed template jobs
$currentRepoReference = $env:GITHUB_REPOSITORY
$isFinalRepository = $false

Check notice

Code scanning / PSScriptAnalyzer

Line has trailing whitespace Note

Line has trailing whitespace
if ($repoSettings.templateUrl) {
# Extract repository reference from templateUrl (e.g., "microsoft/AL-Go-PTE" from "https://github.com/microsoft/AL-Go-PTE@main")
$templateRepoUrl = $repoSettings.templateUrl.Split('@')[0]
$templateRepoReference = $templateRepoUrl.Split('/')[-2..-1] -join '/'
$isFinalRepository = $templateRepoReference -ne $currentRepoReference
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't correct. Final repo is a repo where the template URL doesn't point to microsoft/AL-Go-PTE or microsoft/AL-Go-AppSource or microsoft/AL-Go.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, that's also not correct.

I don't think there's a mechanism currently to differentiate between final and template repos.
Maybe the template repos should be marked as template? (as in GitHub template repo).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed the final repository detection logic. The logic now correctly identifies final repositories as those where templateUrl doesn't point to the standard AL-Go repositories (microsoft/AL-Go-PTE, microsoft/AL-Go-AppSource, microsoft/AL-Go). Also removed the unused variable and added comprehensive tests. Commit 990ed65.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed the repository detection logic to use GitHub's template repository API instead of templateUrl analysis. The new approach checks the is_template property from the GitHub API to properly distinguish between template and final repositories. This correctly addresses the issue that there was no reliable mechanism to differentiate repository types. Template repositories (marked as GitHub templates) always allow custom jobs, while final repositories respect the allowCustomJobsInEndRepos setting. Implemented in commit ed35ddc.

}

Check notice

Code scanning / PSScriptAnalyzer

Line has trailing whitespace Note

Line has trailing whitespace
if ($isFinalRepository) {
Write-Host "Skipping custom jobs from current repository (final repository using template: $($repoSettings.templateUrl)): $dstFile"
}
else {
Write-Host "Apply customizations from current repository: $dstFile"
[Yaml]::ApplyCustomizations([ref] $srcContent, $dstFile)
}
}

# file exists, compare and add to $updateFiles if different
Expand Down
69 changes: 61 additions & 8 deletions Tests/CheckForUpdates.Action.Test.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -134,14 +134,67 @@
($yaml.content -join "`r`n") | Should -be ($customizedYaml.content -join "`r`n")
}

It 'Test that Update AL-Go System Files uses fixes runs-on' {
. (Join-Path $scriptRoot "yamlclass.ps1")

$updateYamlFile = Join-Path $scriptRoot "..\..\Templates\Per Tenant Extension\.github\workflows\UpdateGitHubGoSystemFiles.yaml"
$updateYaml = [Yaml]::Load($updateYamlFile)
$updateYaml.content | Where-Object { $_ -like '*runs-on:*' } | ForEach-Object {
$_.Trim() | Should -Be 'runs-on: windows-latest' -Because "Expected 'runs-on: windows-latest', in order to hardcode runner to windows-latest, but got $_"
}
It 'Test that Update AL-Go System Files uses fixes runs-on' {
. (Join-Path $scriptRoot "yamlclass.ps1")

$updateYamlFile = Join-Path $scriptRoot "..\..\Templates\Per Tenant Extension\.github\workflows\UpdateGitHubGoSystemFiles.yaml"
$updateYaml = [Yaml]::Load($updateYamlFile)
$updateYaml.content | Where-Object { $_ -like '*runs-on:*' } | ForEach-Object {
$_.Trim() | Should -Be 'runs-on: windows-latest' -Because "Expected 'runs-on: windows-latest', in order to hardcode runner to windows-latest, but got $_"
}
}

It 'Test final repository detection logic' {
# Test the repository detection logic used to determine if custom jobs should be applied
$actionName = "CheckForUpdates"
$scriptRoot = Join-Path $PSScriptRoot "..\Actions\$actionName" -Resolve
. (Join-Path -Path $scriptRoot -ChildPath "CheckForUpdates.HelperFunctions.ps1")

Check notice

Code scanning / PSScriptAnalyzer

Line has trailing whitespace Note

Line has trailing whitespace
# Mock environment variables
$env:GITHUB_REPOSITORY = "testowner/testrepo"

Check notice

Code scanning / PSScriptAnalyzer

Line has trailing whitespace Note

Line has trailing whitespace
# Test scenario 1: Final repository using template
$repoSettings = @{
templateUrl = "https://github.com/testowner/template-repo@main"
}

Check notice

Code scanning / PSScriptAnalyzer

Line has trailing whitespace Note

Line has trailing whitespace
# Extract repository reference logic (mirroring the actual code)
$currentRepoReference = $env:GITHUB_REPOSITORY
$isFinalRepository = $false

Check notice

Code scanning / PSScriptAnalyzer

Line has trailing whitespace Note

Line has trailing whitespace
if ($repoSettings.templateUrl) {
$templateRepoUrl = $repoSettings.templateUrl.Split('@')[0]
$templateRepoReference = $templateRepoUrl.Split('/')[-2..-1] -join '/'
$isFinalRepository = $templateRepoReference -ne $currentRepoReference
}

Check notice

Code scanning / PSScriptAnalyzer

Line has trailing whitespace Note

Line has trailing whitespace
$isFinalRepository | Should -Be $true -Because "Repository using template should be detected as final repository"

Check notice

Code scanning / PSScriptAnalyzer

Line has trailing whitespace Note

Line has trailing whitespace
# Test scenario 2: Template repository (no templateUrl)
$repoSettings = @{}

Check notice

Code scanning / PSScriptAnalyzer

Line has trailing whitespace Note

Line has trailing whitespace
$isFinalRepository = $false
if ($repoSettings.templateUrl) {
$templateRepoUrl = $repoSettings.templateUrl.Split('@')[0]
$templateRepoReference = $templateRepoUrl.Split('/')[-2..-1] -join '/'
$isFinalRepository = $templateRepoReference -ne $currentRepoReference
}

Check notice

Code scanning / PSScriptAnalyzer

Line has trailing whitespace Note

Line has trailing whitespace
$isFinalRepository | Should -Be $false -Because "Repository without templateUrl should not be detected as final repository"

Check notice

Code scanning / PSScriptAnalyzer

Line has trailing whitespace Note

Line has trailing whitespace
# Test scenario 3: Repository referencing itself (edge case)
$repoSettings = @{
templateUrl = "https://github.com/testowner/testrepo@main"
}

Check notice

Code scanning / PSScriptAnalyzer

Line has trailing whitespace Note

Line has trailing whitespace
$isFinalRepository = $false
if ($repoSettings.templateUrl) {
$templateRepoUrl = $repoSettings.templateUrl.Split('@')[0]
$templateRepoReference = $templateRepoUrl.Split('/')[-2..-1] -join '/'
$isFinalRepository = $templateRepoReference -ne $currentRepoReference
}

Check notice

Code scanning / PSScriptAnalyzer

Line has trailing whitespace Note

Line has trailing whitespace
$isFinalRepository | Should -Be $false -Because "Repository referencing itself should not be detected as final repository"
}
}

Expand Down
240 changes: 240 additions & 0 deletions Tests/CustomJobRemoval.Test.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,240 @@
Get-Module TestActionsHelper | Remove-Module -Force
Import-Module (Join-Path $PSScriptRoot 'TestActionsHelper.psm1')
$errorActionPreference = "Stop"; $ProgressPreference = "SilentlyContinue"; Set-StrictMode -Version 2.0

Describe "Custom Job Removal Tests" {
BeforeAll {
$actionName = "CheckForUpdates"
$scriptRoot = Join-Path $PSScriptRoot "..\Actions\$actionName" -Resolve
Import-Module (Join-Path $scriptRoot "..\Github-Helper.psm1") -DisableNameChecking -Force
. (Join-Path -Path $scriptRoot -ChildPath "CheckForUpdates.HelperFunctions.ps1")
. (Join-Path -Path $scriptRoot -ChildPath "yamlclass.ps1")

Check notice

Code scanning / PSScriptAnalyzer

Line has trailing whitespace Note

Line has trailing whitespace
# Create temporary directory for test files
$testDir = Join-Path $PSScriptRoot "temp_custom_job_test"
if (Test-Path $testDir) {
Remove-Item -Path $testDir -Recurse -Force
}
New-Item -Path $testDir -ItemType Directory -Force | Out-Null
}

AfterAll {
# Clean up test directory
$testDir = Join-Path $PSScriptRoot "temp_custom_job_test"
if (Test-Path $testDir) {
Remove-Item -Path $testDir -Recurse -Force
}
}

It 'Custom jobs should not be applied from final repositories' {
$testDir = Join-Path $PSScriptRoot "temp_custom_job_test"

Check notice

Code scanning / PSScriptAnalyzer

Line has trailing whitespace Note

Line has trailing whitespace
# Create a mock template CICD workflow (base workflow)
$templateWorkflow = @(
"name: 'CI/CD'",
"on:",
" workflow_dispatch:",
"jobs:",
" Initialization:",
" runs-on: [ windows-latest ]",
" steps:",
" - name: Initialize",
" run: echo 'Initializing'",
" Build:",
" needs: [ Initialization ]",
" runs-on: [ windows-latest ]",
" steps:",
" - name: Build",
" run: echo 'Building'",
" PostProcess:",
" needs: [ Initialization, Build ]",
" runs-on: [ windows-latest ]",
" steps:",
" - name: PostProcess",
" run: echo 'PostProcessing'"
)

Check notice

Code scanning / PSScriptAnalyzer

Line has trailing whitespace Note

Line has trailing whitespace
# Create a final repository workflow with custom jobs (simulating a repository that uses a template)
$finalRepoWorkflow = @(
"name: 'CI/CD'",
"on:",
" workflow_dispatch:",
"jobs:",
" Initialization:",
" runs-on: [ windows-latest ]",
" steps:",
" - name: Initialize",
" run: echo 'Initializing'",
" CustomJob-ShouldNotPersist:",
" needs: [ Initialization ]",
" runs-on: [ windows-latest ]",
" steps:",
" - name: Custom Step",
" run: echo 'This custom job should not persist'",
" Build:",
" needs: [ Initialization, CustomJob-ShouldNotPersist ]",
" runs-on: [ windows-latest ]",
" steps:",
" - name: Build",
" run: echo 'Building'",
" PostProcess:",
" needs: [ Initialization, Build ]",
" runs-on: [ windows-latest ]",
" steps:",
" - name: PostProcess",
" run: echo 'PostProcessing'"
)

Check notice

Code scanning / PSScriptAnalyzer

Line has trailing whitespace Note

Line has trailing whitespace
# Save test files
$templateFile = Join-Path $testDir "template_cicd.yaml"
$finalRepoFile = Join-Path $testDir "final_repo_cicd.yaml"

Check notice

Code scanning / PSScriptAnalyzer

Line has trailing whitespace Note

Line has trailing whitespace
$templateWorkflow -join "`n" | Set-Content -Path $templateFile -Encoding UTF8
$finalRepoWorkflow -join "`n" | Set-Content -Path $finalRepoFile -Encoding UTF8

Check notice

Code scanning / PSScriptAnalyzer

Line has trailing whitespace Note

Line has trailing whitespace
# Mock environment and repo settings for final repository
$env:GITHUB_REPOSITORY = "testowner/final-repo"
$repoSettings = @{
templateUrl = "https://github.com/testowner/template-repo@main"
}

Check notice

Code scanning / PSScriptAnalyzer

Line has trailing whitespace Note

Line has trailing whitespace
# Simulate the logic from CheckForUpdates.ps1
$srcContent = Get-Content -Path $templateFile -Raw
$dstFileExists = Test-Path -Path $finalRepoFile
$type = 'workflow'

Check notice

Code scanning / PSScriptAnalyzer

Line has trailing whitespace Note

Line has trailing whitespace
# Apply the final repository detection logic
$currentRepoReference = $env:GITHUB_REPOSITORY
$isFinalRepository = $false

Check notice

Code scanning / PSScriptAnalyzer

Line has trailing whitespace Note

Line has trailing whitespace
if ($repoSettings.templateUrl) {
$templateRepoUrl = $repoSettings.templateUrl.Split('@')[0]
$templateRepoReference = $templateRepoUrl.Split('/')[-2..-1] -join '/'
$isFinalRepository = $templateRepoReference -ne $currentRepoReference
}

Check notice

Code scanning / PSScriptAnalyzer

Line has trailing whitespace Note

Line has trailing whitespace
# Test that final repository is correctly detected
$isFinalRepository | Should -Be $true

Check notice

Code scanning / PSScriptAnalyzer

Line has trailing whitespace Note

Line has trailing whitespace
# Apply customizations based on repository type
if ($dstFileExists -and $type -eq 'workflow') {
if (-not $isFinalRepository) {
[Yaml]::ApplyCustomizations([ref] $srcContent, $finalRepoFile)
}
}

Check notice

Code scanning / PSScriptAnalyzer

Line has trailing whitespace Note

Line has trailing whitespace
# Verify that custom jobs were NOT applied (srcContent should not contain CustomJob-ShouldNotPersist)
$srcContent | Should -Not -Match "CustomJob-ShouldNotPersist"
$srcContent | Should -Not -Match "This custom job should not persist"

Check notice

Code scanning / PSScriptAnalyzer

Line has trailing whitespace Note

Line has trailing whitespace
# Verify that the base template structure is preserved
$srcContent | Should -Match "Initialization:"
$srcContent | Should -Match "Build:"
$srcContent | Should -Match "PostProcess:"
}

It 'Custom jobs should be applied from template repositories' {
$testDir = Join-Path $PSScriptRoot "temp_custom_job_test"

Check notice

Code scanning / PSScriptAnalyzer

Line has trailing whitespace Note

Line has trailing whitespace
# Create a mock template CICD workflow (base workflow)
$templateWorkflow = @(
"name: 'CI/CD'",
"on:",
" workflow_dispatch:",
"jobs:",
" Initialization:",
" runs-on: [ windows-latest ]",
" steps:",
" - name: Initialize",
" run: echo 'Initializing'",
" Build:",
" needs: [ Initialization ]",
" runs-on: [ windows-latest ]",
" steps:",
" - name: Build",
" run: echo 'Building'",
" PostProcess:",
" needs: [ Initialization, Build ]",
" runs-on: [ windows-latest ]",
" steps:",
" - name: PostProcess",
" run: echo 'PostProcessing'"
)

Check notice

Code scanning / PSScriptAnalyzer

Line has trailing whitespace Note

Line has trailing whitespace
# Create a template repository workflow with custom jobs
$templateRepoWorkflow = @(
"name: 'CI/CD'",
"on:",
" workflow_dispatch:",
"jobs:",
" Initialization:",
" runs-on: [ windows-latest ]",
" steps:",
" - name: Initialize",
" run: echo 'Initializing'",
" CustomJob-Template:",
" needs: [ Initialization ]",
" runs-on: [ windows-latest ]",
" steps:",
" - name: Template Custom Step",
" run: echo 'This is a template custom job'",
" Build:",
" needs: [ Initialization, CustomJob-Template ]",
" runs-on: [ windows-latest ]",
" steps:",
" - name: Build",
" run: echo 'Building'",
" PostProcess:",
" needs: [ Initialization, Build ]",
" runs-on: [ windows-latest ]",
" steps:",
" - name: PostProcess",
" run: echo 'PostProcessing'"
)

Check notice

Code scanning / PSScriptAnalyzer

Line has trailing whitespace Note

Line has trailing whitespace
# Save test files
$templateFile = Join-Path $testDir "template_cicd2.yaml"
$templateRepoFile = Join-Path $testDir "template_repo_cicd.yaml"

Check notice

Code scanning / PSScriptAnalyzer

Line has trailing whitespace Note

Line has trailing whitespace
$templateWorkflow -join "`n" | Set-Content -Path $templateFile -Encoding UTF8
$templateRepoWorkflow -join "`n" | Set-Content -Path $templateRepoFile -Encoding UTF8

Check notice

Code scanning / PSScriptAnalyzer

Line has trailing whitespace Note

Line has trailing whitespace
# Mock environment and repo settings for template repository (no templateUrl)
$env:GITHUB_REPOSITORY = "testowner/template-repo"
$repoSettings = @{} # No templateUrl - this is a template repository

Check notice

Code scanning / PSScriptAnalyzer

Line has trailing whitespace Note

Line has trailing whitespace
# Simulate the logic from CheckForUpdates.ps1
$srcContent = Get-Content -Path $templateFile -Raw
$dstFileExists = Test-Path -Path $templateRepoFile
$type = 'workflow'

Check notice

Code scanning / PSScriptAnalyzer

Line has trailing whitespace Note

Line has trailing whitespace
# Apply the final repository detection logic
$currentRepoReference = $env:GITHUB_REPOSITORY
$isFinalRepository = $false

Check notice

Code scanning / PSScriptAnalyzer

Line has trailing whitespace Note

Line has trailing whitespace
if ($repoSettings.templateUrl) {
$templateRepoUrl = $repoSettings.templateUrl.Split('@')[0]
$templateRepoReference = $templateRepoUrl.Split('/')[-2..-1] -join '/'
$isFinalRepository = $templateRepoReference -ne $currentRepoReference
}

Check notice

Code scanning / PSScriptAnalyzer

Line has trailing whitespace Note

Line has trailing whitespace
# Test that template repository is correctly detected
$isFinalRepository | Should -Be $false

Check notice

Code scanning / PSScriptAnalyzer

Line has trailing whitespace Note

Line has trailing whitespace
# Apply customizations based on repository type
if ($dstFileExists -and $type -eq 'workflow') {
if (-not $isFinalRepository) {
[Yaml]::ApplyCustomizations([ref] $srcContent, $templateRepoFile)
}
}

Check notice

Code scanning / PSScriptAnalyzer

Line has trailing whitespace Note

Line has trailing whitespace
# Verify that custom jobs WERE applied (srcContent should contain CustomJob-Template)
$srcContent | Should -Match "CustomJob-Template"
$srcContent | Should -Match "This is a template custom job"

Check notice

Code scanning / PSScriptAnalyzer

Line has trailing whitespace Note

Line has trailing whitespace
# Verify that the base template structure is preserved
$srcContent | Should -Match "Initialization:"
$srcContent | Should -Match "Build:"
$srcContent | Should -Match "PostProcess:"
}
}
Loading