-
Notifications
You must be signed in to change notification settings - Fork 169
Fix custom jobs persisting in final repositories when removed from templates #1830
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
3c7d0ba
7e72f93
01b2812
f371a41
d487821
e7a40d0
990ed65
ed35ddc
6fe9a7b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 noticeCode 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 noticeCode 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 noticeCode 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 noticeCode 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 noticeCode 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 noticeCode 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 noticeCode 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 noticeCode scanning / PSScriptAnalyzer Line has trailing whitespace Note
Line has trailing whitespace
|
||
| # Apply the final repository detection logic | ||
| $currentRepoReference = $env:GITHUB_REPOSITORY | ||
| $isFinalRepository = $false | ||
|
|
||
Check noticeCode 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 noticeCode scanning / PSScriptAnalyzer Line has trailing whitespace Note
Line has trailing whitespace
|
||
| # Test that final repository is correctly detected | ||
| $isFinalRepository | Should -Be $true | ||
|
|
||
Check noticeCode 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 noticeCode 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 noticeCode 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 noticeCode 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 noticeCode 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 noticeCode 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 noticeCode 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 noticeCode 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 noticeCode 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 noticeCode scanning / PSScriptAnalyzer Line has trailing whitespace Note
Line has trailing whitespace
|
||
| # Apply the final repository detection logic | ||
| $currentRepoReference = $env:GITHUB_REPOSITORY | ||
| $isFinalRepository = $false | ||
|
|
||
Check noticeCode 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 noticeCode scanning / PSScriptAnalyzer Line has trailing whitespace Note
Line has trailing whitespace
|
||
| # Test that template repository is correctly detected | ||
| $isFinalRepository | Should -Be $false | ||
|
|
||
Check noticeCode 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 noticeCode 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 noticeCode 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:" | ||
| } | ||
| } | ||
There was a problem hiding this comment.
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-PTEormicrosoft/AL-Go-AppSourceormicrosoft/AL-Go.There was a problem hiding this comment.
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).
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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_templateproperty 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 theallowCustomJobsInEndRepossetting. Implemented in commit ed35ddc.