Skip to content

Commit ed35ddc

Browse files
Copilotmazhelez
andcommitted
Fix repository type detection to use GitHub template repository API
Co-authored-by: mazhelez <[email protected]>
1 parent 990ed65 commit ed35ddc

File tree

3 files changed

+234
-117
lines changed

3 files changed

+234
-117
lines changed

Actions/CheckForUpdates/CheckForUpdates.ps1

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -264,17 +264,28 @@ foreach($checkfile in $checkfiles) {
264264
$allowCustomJobsInEndRepos = $repoSettings.allowCustomJobsInEndRepos
265265
}
266266

267-
if ($repoSettings.templateUrl) {
268-
# Extract repository reference from templateUrl (e.g., "microsoft/AL-Go-PTE" from "https://github.com/microsoft/AL-Go-PTE@main")
269-
$templateRepoUrl = $repoSettings.templateUrl.Split('@')[0]
270-
$templateRepoReference = $templateRepoUrl.Split('/')[-2..-1] -join '/'
271-
# Final repository is one where templateUrl doesn't point to standard AL-Go repositories
272-
$standardAlGoRepos = @('microsoft/AL-Go-PTE', 'microsoft/AL-Go-AppSource', 'microsoft/AL-Go')
273-
$isFinalRepository = $templateRepoReference -notin $standardAlGoRepos
267+
# Check if current repository is a GitHub template repository
268+
# Template repositories should always allow custom jobs
269+
# Final repositories (non-template) should respect the allowCustomJobsInEndRepos setting
270+
try {
271+
$headers = @{
272+
"Accept" = "application/vnd.github+json"
273+
"Authorization" = "Bearer $token"
274+
"X-GitHub-Api-Version" = "2022-11-28"
275+
}
276+
$repoInfo = (InvokeWebRequest -Headers $headers -Uri "$($env:GITHUB_API_URL)/repos/$($env:GITHUB_REPOSITORY)").Content | ConvertFrom-Json
277+
$isTemplateRepository = $repoInfo.is_template
278+
279+
# Final repository is one that is NOT marked as a template repository
280+
$isFinalRepository = -not $isTemplateRepository
281+
}
282+
catch {
283+
Write-Host "Warning: Could not determine if repository is a template. Assuming it's a final repository."
284+
$isFinalRepository = $true
274285
}
275286

276287
if ($isFinalRepository -and -not $allowCustomJobsInEndRepos) {
277-
Write-Host "Skipping custom jobs from current repository (final repository using template: $($repoSettings.templateUrl), allowCustomJobsInEndRepos: $allowCustomJobsInEndRepos): $dstFile"
288+
Write-Host "Skipping custom jobs from current repository (final repository, not marked as template, allowCustomJobsInEndRepos: $allowCustomJobsInEndRepos): $dstFile"
278289
}
279290
else {
280291
Write-Host "Apply customizations from current repository: $dstFile"

Tests/CheckForUpdates.Action.Test.ps1

Lines changed: 95 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -144,88 +144,125 @@ Describe "CheckForUpdates Action Tests" {
144144
}
145145
}
146146

147-
It 'Test final repository detection logic' {
148-
# Test the repository detection logic used to determine if custom jobs should be applied
147+
It 'Test template repository detection logic' {
148+
# Test the repository detection logic using GitHub API to determine if repository is a template
149149
$actionName = "CheckForUpdates"
150150
$scriptRoot = Join-Path $PSScriptRoot "..\Actions\$actionName" -Resolve
151151
. (Join-Path -Path $scriptRoot -ChildPath "CheckForUpdates.HelperFunctions.ps1")
152+
Import-Module (Join-Path $scriptRoot "..\Github-Helper.psm1") -DisableNameChecking -Force
152153

153154
# Mock environment variables
154155
$env:GITHUB_REPOSITORY = "testowner/testrepo"
156+
$env:GITHUB_API_URL = "https://api.github.com"
155157

156-
# Test scenario 1: Final repository using template
157-
$repoSettings = @{
158-
templateUrl = "https://github.com/testowner/template-repo@main"
159-
}
158+
# Mock InvokeWebRequest for template repository
159+
Mock InvokeWebRequest -MockWith {
160+
return @{
161+
Content = '{"is_template": true}'
162+
}
163+
} -ParameterFilter { $uri -eq "https://api.github.com/repos/testowner/testrepo" }
160164

161-
# Extract repository reference logic (mirroring the actual code)
162-
$currentRepoReference = $env:GITHUB_REPOSITORY
165+
# Test scenario 1: Template repository (GitHub template)
166+
$token = "mock-token"
163167
$isFinalRepository = $false
164168

165-
if ($repoSettings.templateUrl) {
166-
$templateRepoUrl = $repoSettings.templateUrl.Split('@')[0]
167-
$templateRepoReference = $templateRepoUrl.Split('/')[-2..-1] -join '/'
168-
$isFinalRepository = $templateRepoReference -ne $currentRepoReference
169+
try {
170+
$headers = @{
171+
"Accept" = "application/vnd.github+json"
172+
"Authorization" = "Bearer $token"
173+
"X-GitHub-Api-Version" = "2022-11-28"
174+
}
175+
$repoInfo = (InvokeWebRequest -Headers $headers -Uri "$($env:GITHUB_API_URL)/repos/$($env:GITHUB_REPOSITORY)").Content | ConvertFrom-Json
176+
$isTemplateRepository = $repoInfo.is_template
177+
$isFinalRepository = -not $isTemplateRepository
178+
}
179+
catch {
180+
$isFinalRepository = $true
169181
}
170182

171-
$isFinalRepository | Should -Be $true -Because "Repository using template should be detected as final repository"
183+
$isFinalRepository | Should -Be $false -Because "GitHub template repository should not be detected as final repository"
172184

173-
# Test scenario 2: Template repository (no templateUrl)
174-
$repoSettings = @{}
185+
# Mock InvokeWebRequest for final repository
186+
Mock InvokeWebRequest -MockWith {
187+
return @{
188+
Content = '{"is_template": false}'
189+
}
190+
} -ParameterFilter { $uri -eq "https://api.github.com/repos/testowner/testrepo" }
175191

192+
# Test scenario 2: Final repository (not a GitHub template)
176193
$isFinalRepository = $false
177-
if ($repoSettings.templateUrl) {
178-
$templateRepoUrl = $repoSettings.templateUrl.Split('@')[0]
179-
$templateRepoReference = $templateRepoUrl.Split('/')[-2..-1] -join '/'
180-
$isFinalRepository = $templateRepoReference -ne $currentRepoReference
194+
195+
try {
196+
$repoInfo = (InvokeWebRequest -Headers $headers -Uri "$($env:GITHUB_API_URL)/repos/$($env:GITHUB_REPOSITORY)").Content | ConvertFrom-Json
197+
$isTemplateRepository = $repoInfo.is_template
198+
$isFinalRepository = -not $isTemplateRepository
199+
}
200+
catch {
201+
$isFinalRepository = $true
181202
}
182203

183-
$isFinalRepository | Should -Be $false -Because "Repository without templateUrl should not be detected as final repository"
204+
$isFinalRepository | Should -Be $true -Because "Non-template repository should be detected as final repository"
184205

185-
# Test scenario 3: Repository referencing itself (edge case)
186-
$repoSettings = @{
187-
templateUrl = "https://github.com/testowner/testrepo@main"
188-
}
206+
# Test scenario 3: API error (fallback behavior)
207+
Mock InvokeWebRequest -MockWith {
208+
throw "API Error"
209+
} -ParameterFilter { $uri -eq "https://api.github.com/repos/testowner/testrepo" }
189210

190211
$isFinalRepository = $false
191-
if ($repoSettings.templateUrl) {
192-
$templateRepoUrl = $repoSettings.templateUrl.Split('@')[0]
193-
$templateRepoReference = $templateRepoUrl.Split('/')[-2..-1] -join '/'
194-
$isFinalRepository = $templateRepoReference -ne $currentRepoReference
212+
213+
try {
214+
$repoInfo = (InvokeWebRequest -Headers $headers -Uri "$($env:GITHUB_API_URL)/repos/$($env:GITHUB_REPOSITORY)").Content | ConvertFrom-Json
215+
$isTemplateRepository = $repoInfo.is_template
216+
$isFinalRepository = -not $isTemplateRepository
217+
}
218+
catch {
219+
$isFinalRepository = $true
195220
}
196221

197-
$isFinalRepository | Should -Be $false -Because "Repository referencing itself should not be detected as final repository"
222+
$isFinalRepository | Should -Be $true -Because "When API fails, repository should be assumed to be final repository"
198223
}
199224

200225
It 'Test allowCustomJobsInEndRepos setting behavior' {
201-
# Test the allowCustomJobsInEndRepos setting logic used to determine if custom jobs should be applied
226+
# Test the allowCustomJobsInEndRepos setting logic for template vs final repositories
202227
$env:GITHUB_REPOSITORY = "testowner/final-repo"
228+
$env:GITHUB_API_URL = "https://api.github.com"
229+
Import-Module (Join-Path $PSScriptRoot "..\Actions\CheckForUpdates\..\Github-Helper.psm1") -DisableNameChecking -Force
203230

204-
# Test scenario 1: Final repository with allowCustomJobsInEndRepos = false (default)
205-
$repoSettings = @{
206-
templateUrl = "https://github.com/testowner/template-repo@main"
207-
}
231+
# Test scenario 1: Final repository (not template) with allowCustomJobsInEndRepos = false (default)
232+
Mock InvokeWebRequest -MockWith {
233+
return @{
234+
Content = '{"is_template": false}'
235+
}
236+
} -ParameterFilter { $uri -eq "https://api.github.com/repos/testowner/final-repo" }
208237

209-
$currentRepoReference = $env:GITHUB_REPOSITORY
238+
$repoSettings = @{}
239+
$token = "mock-token"
210240
$isFinalRepository = $false
211241
$allowCustomJobsInEndRepos = $false
212242

213243
if ($repoSettings.ContainsKey('allowCustomJobsInEndRepos')) {
214244
$allowCustomJobsInEndRepos = $repoSettings.allowCustomJobsInEndRepos
215245
}
216246

217-
if ($repoSettings.templateUrl) {
218-
$templateRepoUrl = $repoSettings.templateUrl.Split('@')[0]
219-
$templateRepoReference = $templateRepoUrl.Split('/')[-2..-1] -join '/'
220-
$isFinalRepository = $templateRepoReference -ne $currentRepoReference
247+
try {
248+
$headers = @{
249+
"Accept" = "application/vnd.github+json"
250+
"Authorization" = "Bearer $token"
251+
"X-GitHub-Api-Version" = "2022-11-28"
252+
}
253+
$repoInfo = (InvokeWebRequest -Headers $headers -Uri "$($env:GITHUB_API_URL)/repos/$($env:GITHUB_REPOSITORY)").Content | ConvertFrom-Json
254+
$isTemplateRepository = $repoInfo.is_template
255+
$isFinalRepository = -not $isTemplateRepository
256+
}
257+
catch {
258+
$isFinalRepository = $true
221259
}
222260

223261
$shouldSkipCustomJobs = $isFinalRepository -and -not $allowCustomJobsInEndRepos
224262
$shouldSkipCustomJobs | Should -Be $true -Because "Final repository with allowCustomJobsInEndRepos = false should skip custom jobs"
225263

226264
# Test scenario 2: Final repository with allowCustomJobsInEndRepos = true
227265
$repoSettings = @{
228-
templateUrl = "https://github.com/testowner/template-repo@main"
229266
allowCustomJobsInEndRepos = $true
230267
}
231268

@@ -238,15 +275,29 @@ Describe "CheckForUpdates Action Tests" {
238275
$shouldSkipCustomJobs | Should -Be $false -Because "Final repository with allowCustomJobsInEndRepos = true should NOT skip custom jobs"
239276

240277
# Test scenario 3: Template repository (should always apply custom jobs regardless of setting)
278+
Mock InvokeWebRequest -MockWith {
279+
return @{
280+
Content = '{"is_template": true}'
281+
}
282+
} -ParameterFilter { $uri -eq "https://api.github.com/repos/testowner/final-repo" }
283+
241284
$repoSettings = @{
242285
allowCustomJobsInEndRepos = $false
243286
}
244287

288+
$allowCustomJobsInEndRepos = $false
289+
if ($repoSettings.ContainsKey('allowCustomJobsInEndRepos')) {
290+
$allowCustomJobsInEndRepos = $repoSettings.allowCustomJobsInEndRepos
291+
}
292+
245293
$isFinalRepository = $false
246-
if ($repoSettings.templateUrl) {
247-
$templateRepoUrl = $repoSettings.templateUrl.Split('@')[0]
248-
$templateRepoReference = $templateRepoUrl.Split('/')[-2..-1] -join '/'
249-
$isFinalRepository = $templateRepoReference -ne $currentRepoReference
294+
try {
295+
$repoInfo = (InvokeWebRequest -Headers $headers -Uri "$($env:GITHUB_API_URL)/repos/$($env:GITHUB_REPOSITORY)").Content | ConvertFrom-Json
296+
$isTemplateRepository = $repoInfo.is_template
297+
$isFinalRepository = -not $isTemplateRepository
298+
}
299+
catch {
300+
$isFinalRepository = $true
250301
}
251302

252303
$shouldSkipCustomJobs = $isFinalRepository -and -not $allowCustomJobsInEndRepos

0 commit comments

Comments
 (0)