Skip to content

Commit e793f4a

Browse files
Properly count the number of repos after a github scan resume (#625)
1 parent 10f4d02 commit e793f4a

File tree

2 files changed

+64
-5
lines changed

2 files changed

+64
-5
lines changed

pkg/sources/github/github.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ func (s *Source) scan(ctx context.Context, installationClient *github.Client, ch
370370
defer s.jobSem.Release(1)
371371
defer wg.Done()
372372

373-
s.setProgressCompleteWithRepo(i+progressIndexOffset, repoURL)
373+
s.setProgressCompleteWithRepo(i, progressIndexOffset, repoURL)
374374
// Ensure the repo is removed from the resume info after being scanned.
375375
defer func(s *Source) {
376376
s.resumeInfoMutex.Lock()
@@ -794,7 +794,7 @@ func (s *Source) normalizeRepos(ctx context.Context, apiClient *github.Client) {
794794
}
795795

796796
// setProgressCompleteWithRepo calls the s.SetProgressComplete after safely setting up the encoded resume info string.
797-
func (s *Source) setProgressCompleteWithRepo(index int, repoURL string) {
797+
func (s *Source) setProgressCompleteWithRepo(index int, offset int, repoURL string) {
798798
s.resumeInfoMutex.Lock()
799799
defer s.resumeInfoMutex.Unlock()
800800

@@ -805,5 +805,5 @@ func (s *Source) setProgressCompleteWithRepo(index int, repoURL string) {
805805
// Make the resume info string from the slice.
806806
encodedResumeInfo := sources.EncodeResumeInfo(s.resumeInfoSlice)
807807

808-
s.SetProgressComplete(index, len(s.repos), fmt.Sprintf("Repo: %s", repoURL), encodedResumeInfo)
808+
s.SetProgressComplete(index+offset, len(s.repos)+offset, fmt.Sprintf("Repo: %s", repoURL), encodedResumeInfo)
809809
}

pkg/sources/github/github_test.go

+61-2
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ func TestEnumerateWithApp(t *testing.T) {
340340
}
341341

342342
// This only tests the resume info slice portion of setProgressCompleteWithRepo.
343-
func Test_setProgressCompleteWithRepo(t *testing.T) {
343+
func Test_setProgressCompleteWithRepo_resumeInfo(t *testing.T) {
344344
tests := []struct {
345345
startingResumeInfoSlice []string
346346
repoURL string
@@ -367,9 +367,68 @@ func Test_setProgressCompleteWithRepo(t *testing.T) {
367367

368368
for _, tt := range tests {
369369
s.resumeInfoSlice = tt.startingResumeInfoSlice
370-
s.setProgressCompleteWithRepo(0, tt.repoURL)
370+
s.setProgressCompleteWithRepo(0, 0, tt.repoURL)
371371
if !reflect.DeepEqual(s.resumeInfoSlice, tt.wantResumeInfoSlice) {
372372
t.Errorf("s.setProgressCompleteWithRepo() got: %v, want: %v", s.resumeInfoSlice, tt.wantResumeInfoSlice)
373373
}
374374
}
375375
}
376+
377+
func Test_setProgressCompleteWithRepo_Progress(t *testing.T) {
378+
repos := []string{"a", "b", "c", "d", "e"}
379+
tests := map[string]struct {
380+
repos []string
381+
index int
382+
offset int
383+
wantPercentComplete int64
384+
wantSectionsCompleted int32
385+
wantSectionsRemaining int32
386+
}{
387+
"starting from the beginning, no offset": {
388+
repos: repos,
389+
index: 0,
390+
offset: 0,
391+
wantPercentComplete: 0,
392+
wantSectionsCompleted: 0,
393+
wantSectionsRemaining: 5,
394+
},
395+
"resume from the third, offset 2": {
396+
repos: repos[2:],
397+
index: 0,
398+
offset: 2,
399+
wantPercentComplete: 40,
400+
wantSectionsCompleted: 2,
401+
wantSectionsRemaining: 5,
402+
},
403+
"resume from the third, on last repo, offset 2": {
404+
repos: repos[2:],
405+
index: 2,
406+
offset: 2,
407+
wantPercentComplete: 80,
408+
wantSectionsCompleted: 4,
409+
wantSectionsRemaining: 5,
410+
},
411+
}
412+
413+
logger := logrus.New()
414+
logger.Out = io.Discard
415+
416+
for _, tt := range tests {
417+
s := &Source{
418+
repos: tt.repos,
419+
log: logger.WithField("no", "output"),
420+
}
421+
422+
s.setProgressCompleteWithRepo(tt.index, tt.offset, "")
423+
gotProgress := s.GetProgress()
424+
if gotProgress.PercentComplete != tt.wantPercentComplete {
425+
t.Errorf("s.setProgressCompleteWithRepo() PercentComplete got: %v want: %v", gotProgress.PercentComplete, tt.wantPercentComplete)
426+
}
427+
if gotProgress.SectionsCompleted != tt.wantSectionsCompleted {
428+
t.Errorf("s.setProgressCompleteWithRepo() PercentComplete got: %v want: %v", gotProgress.SectionsCompleted, tt.wantSectionsCompleted)
429+
}
430+
if gotProgress.SectionsRemaining != tt.wantSectionsRemaining {
431+
t.Errorf("s.setProgressCompleteWithRepo() PercentComplete got: %v want: %v", gotProgress.SectionsRemaining, tt.wantSectionsRemaining)
432+
}
433+
}
434+
}

0 commit comments

Comments
 (0)