Find, verify, and analyze leaked credentials
Go credentialsdevsecopsdynamic-analysisprecommitscanningsecretsecret-managementsecretssecuritysecurity-toolstrufflehogverification
Properly count the number of repos after a github scan resume (#625)
trufflesteeevee793f4a5e692
diff --git a/pkg/sources/github/github.go b/pkg/sources/github/github.goindex e1b6c046f..50fb0b09a 100644--- a/pkg/sources/github/github.go+++ b/pkg/sources/github/github.go@@ -370,7 +370,7 @@ func (s *Source) scan(ctx context.Context, installationClient *github.Client, chdefer s.jobSem.Release(1)defer wg.Done()- s.setProgressCompleteWithRepo(i+progressIndexOffset, repoURL)+ s.setProgressCompleteWithRepo(i, progressIndexOffset, repoURL)// Ensure the repo is removed from the resume info after being scanned.defer func(s *Source) {s.resumeInfoMutex.Lock()@@ -794,7 +794,7 @@ func (s *Source) normalizeRepos(ctx context.Context, apiClient *github.Client) {}// setProgressCompleteWithRepo calls the s.SetProgressComplete after safely setting up the encoded resume info string.-func (s *Source) setProgressCompleteWithRepo(index int, repoURL string) {+func (s *Source) setProgressCompleteWithRepo(index int, offset int, repoURL string) {s.resumeInfoMutex.Lock()defer s.resumeInfoMutex.Unlock()@@ -805,5 +805,5 @@ func (s *Source) setProgressCompleteWithRepo(index int, repoURL string) {// Make the resume info string from the slice.encodedResumeInfo := sources.EncodeResumeInfo(s.resumeInfoSlice)- s.SetProgressComplete(index, len(s.repos), fmt.Sprintf("Repo: %s", repoURL), encodedResumeInfo)+ s.SetProgressComplete(index+offset, len(s.repos)+offset, fmt.Sprintf("Repo: %s", repoURL), encodedResumeInfo)}diff --git a/pkg/sources/github/github_test.go b/pkg/sources/github/github_test.goindex f658e2be4..7488e311e 100644--- a/pkg/sources/github/github_test.go+++ b/pkg/sources/github/github_test.go@@ -340,7 +340,7 @@ func TestEnumerateWithApp(t *testing.T) {}// This only tests the resume info slice portion of setProgressCompleteWithRepo.-func Test_setProgressCompleteWithRepo(t *testing.T) {+func Test_setProgressCompleteWithRepo_resumeInfo(t *testing.T) {tests := []struct {startingResumeInfoSlice []stringrepoURL string@@ -367,9 +367,68 @@ func Test_setProgressCompleteWithRepo(t *testing.T) {for _, tt := range tests {s.resumeInfoSlice = tt.startingResumeInfoSlice- s.setProgressCompleteWithRepo(0, tt.repoURL)+ s.setProgressCompleteWithRepo(0, 0, tt.repoURL)if !reflect.DeepEqual(s.resumeInfoSlice, tt.wantResumeInfoSlice) {t.Errorf("s.setProgressCompleteWithRepo() got: %v, want: %v", s.resumeInfoSlice, tt.wantResumeInfoSlice)}}}++func Test_setProgressCompleteWithRepo_Progress(t *testing.T) {+ repos := []string{"a", "b", "c", "d", "e"}+ tests := map[string]struct {+ repos []string+ index int+ offset int+ wantPercentComplete int64+ wantSectionsCompleted int32+ wantSectionsRemaining int32+ }{+ "starting from the beginning, no offset": {+ repos: repos,+ index: 0,+ offset: 0,+ wantPercentComplete: 0,+ wantSectionsCompleted: 0,+ wantSectionsRemaining: 5,+ },+ "resume from the third, offset 2": {+ repos: repos[2:],+ index: 0,+ offset: 2,+ wantPercentComplete: 40,+ wantSectionsCompleted: 2,+ wantSectionsRemaining: 5,+ },+ "resume from the third, on last repo, offset 2": {+ repos: repos[2:],+ index: 2,+ offset: 2,+ wantPercentComplete: 80,+ wantSectionsCompleted: 4,+ wantSectionsRemaining: 5,+ },+ }++ logger := logrus.New()+ logger.Out = io.Discard++ for _, tt := range tests {+ s := &Source{+ repos: tt.repos,+ log: logger.WithField("no", "output"),+ }++ s.setProgressCompleteWithRepo(tt.index, tt.offset, "")+ gotProgress := s.GetProgress()+ if gotProgress.PercentComplete != tt.wantPercentComplete {+ t.Errorf("s.setProgressCompleteWithRepo() PercentComplete got: %v want: %v", gotProgress.PercentComplete, tt.wantPercentComplete)+ }+ if gotProgress.SectionsCompleted != tt.wantSectionsCompleted {+ t.Errorf("s.setProgressCompleteWithRepo() PercentComplete got: %v want: %v", gotProgress.SectionsCompleted, tt.wantSectionsCompleted)+ }+ if gotProgress.SectionsRemaining != tt.wantSectionsRemaining {+ t.Errorf("s.setProgressCompleteWithRepo() PercentComplete got: %v want: %v", gotProgress.SectionsRemaining, tt.wantSectionsRemaining)+ }+ }+}