Skip to content

Commit eceb10f

Browse files
authored
Add GHORG_FETCH_ALL check to no-clean mode processing (gabrie30#594)
The handleNoCleanMode function now only performs fetch-all operations if GHORG_FETCH_ALL is set to true, improving control over repository processing. Added tests to verify behavior when fetch-all is enabled and disabled.
1 parent f05578e commit eceb10f

File tree

2 files changed

+114
-21
lines changed

2 files changed

+114
-21
lines changed

cmd/repository_processor.go

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -353,31 +353,34 @@ func (rp *RepositoryProcessor) handleBackupMode(repo *scm.Repo) bool {
353353

354354
// handleNoCleanMode processes repositories in no-clean mode
355355
func (rp *RepositoryProcessor) handleNoCleanMode(repo *scm.Repo) bool {
356-
// Temporarily restore credentials for fetch-all to work with private repos
357-
err := rp.git.SetOriginWithCredentials(*repo)
358-
if err != nil {
359-
rp.addError(fmt.Sprintf("Problem trying to set remote with credentials: %s Error: %v", repo.URL, err))
360-
return false
361-
}
356+
// Fetch all if enabled
357+
if os.Getenv("GHORG_FETCH_ALL") == "true" {
358+
// Temporarily restore credentials for fetch-all to work with private repos
359+
err := rp.git.SetOriginWithCredentials(*repo)
360+
if err != nil {
361+
rp.addError(fmt.Sprintf("Problem trying to set remote with credentials: %s Error: %v", repo.URL, err))
362+
return false
363+
}
362364

363-
err = rp.git.FetchAll(*repo)
364-
fetchErr := err // Store fetch error for later reporting
365+
err = rp.git.FetchAll(*repo)
366+
fetchErr := err // Store fetch error for later reporting
365367

366-
// Always strip credentials again for security, even if fetch failed
367-
err = rp.git.SetOrigin(*repo)
368-
if err != nil {
369-
rp.addError(fmt.Sprintf("Problem trying to reset remote after fetch: %s Error: %v", repo.URL, err))
370-
return false
371-
}
368+
// Always strip credentials again for security, even if fetch failed
369+
err = rp.git.SetOrigin(*repo)
370+
if err != nil {
371+
rp.addError(fmt.Sprintf("Problem trying to reset remote after fetch: %s Error: %v", repo.URL, err))
372+
return false
373+
}
372374

373-
if fetchErr != nil && repo.IsWiki {
374-
rp.addInfo(fmt.Sprintf("Wiki may be enabled but there was no content to clone on: %s Error: %v", repo.URL, fetchErr))
375-
return false
376-
}
375+
if fetchErr != nil && repo.IsWiki {
376+
rp.addInfo(fmt.Sprintf("Wiki may be enabled but there was no content to clone on: %s Error: %v", repo.URL, fetchErr))
377+
return false
378+
}
377379

378-
if fetchErr != nil {
379-
rp.addError(fmt.Sprintf("Could not fetch remotes: %s Error: %v", repo.URL, fetchErr))
380-
return false
380+
if fetchErr != nil {
381+
rp.addError(fmt.Sprintf("Could not fetch remotes: %s Error: %v", repo.URL, fetchErr))
382+
return false
383+
}
381384
}
382385

383386
return true

cmd/repository_processor_test.go

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,96 @@ func TestRepositoryProcessor_ProcessRepository_NoCleanMode(t *testing.T) {
325325
}
326326
}
327327

328+
func TestRepositoryProcessor_ProcessRepository_NoCleanModeWithFetchAllDisabled(t *testing.T) {
329+
defer UnsetEnv("GHORG_")()
330+
os.Setenv("GHORG_NO_CLEAN", "true")
331+
os.Setenv("GHORG_FETCH_ALL", "false")
332+
333+
// Set up temporary directory with existing repo
334+
dir, err := os.MkdirTemp("", "ghorg_test_no_clean_fetch_all_disabled")
335+
if err != nil {
336+
t.Fatal(err)
337+
}
338+
defer os.RemoveAll(dir)
339+
340+
outputDirAbsolutePath = dir
341+
342+
// Create existing repo directory
343+
repoDir := filepath.Join(dir, "test-repo")
344+
err = os.MkdirAll(repoDir, 0755)
345+
if err != nil {
346+
t.Fatal(err)
347+
}
348+
349+
mockGit := NewExtendedMockGit()
350+
processor := NewRepositoryProcessor(mockGit)
351+
352+
repo := scm.Repo{
353+
Name: "test-repo",
354+
URL: "https://github.com/org/test-repo",
355+
CloneBranch: "main",
356+
HostPath: repoDir,
357+
}
358+
359+
repoNameWithCollisions := make(map[string]bool)
360+
processor.ProcessRepository(&repo, repoNameWithCollisions, false, "test-repo", 0)
361+
362+
stats := processor.GetStats()
363+
// In no-clean mode with fetch-all disabled, we should still process successfully
364+
if stats.PulledCount != 1 {
365+
t.Errorf("Expected pulled count to be 1, got %d", stats.PulledCount)
366+
}
367+
// Should not have any errors since fetch-all is skipped when disabled
368+
if len(stats.CloneErrors) != 0 {
369+
t.Errorf("Expected no errors when FETCH_ALL is disabled, got %d errors: %v", len(stats.CloneErrors), stats.CloneErrors)
370+
}
371+
}
372+
373+
func TestRepositoryProcessor_ProcessRepository_NoCleanModeWithFetchAllEnabled(t *testing.T) {
374+
defer UnsetEnv("GHORG_")()
375+
os.Setenv("GHORG_NO_CLEAN", "true")
376+
os.Setenv("GHORG_FETCH_ALL", "true")
377+
378+
// Set up temporary directory with existing repo
379+
dir, err := os.MkdirTemp("", "ghorg_test_no_clean_fetch_all_enabled")
380+
if err != nil {
381+
t.Fatal(err)
382+
}
383+
defer os.RemoveAll(dir)
384+
385+
outputDirAbsolutePath = dir
386+
387+
// Create existing repo directory
388+
repoDir := filepath.Join(dir, "test-repo")
389+
err = os.MkdirAll(repoDir, 0755)
390+
if err != nil {
391+
t.Fatal(err)
392+
}
393+
394+
mockGit := NewExtendedMockGit()
395+
processor := NewRepositoryProcessor(mockGit)
396+
397+
repo := scm.Repo{
398+
Name: "test-repo",
399+
URL: "https://github.com/org/test-repo",
400+
CloneBranch: "main",
401+
HostPath: repoDir,
402+
}
403+
404+
repoNameWithCollisions := make(map[string]bool)
405+
processor.ProcessRepository(&repo, repoNameWithCollisions, false, "test-repo", 0)
406+
407+
stats := processor.GetStats()
408+
// In no-clean mode with fetch-all enabled, we should still process successfully
409+
if stats.PulledCount != 1 {
410+
t.Errorf("Expected pulled count to be 1, got %d", stats.PulledCount)
411+
}
412+
// Should not have any errors since fetch-all is enabled and mocked
413+
if len(stats.CloneErrors) != 0 {
414+
t.Errorf("Expected no errors when FETCH_ALL is enabled, got %d errors: %v", len(stats.CloneErrors), stats.CloneErrors)
415+
}
416+
}
417+
328418
func TestRepositoryProcessor_ProcessRepository_NameCollisions(t *testing.T) {
329419
defer UnsetEnv("GHORG_")()
330420

0 commit comments

Comments
 (0)