Skip to content
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

Fix Github enumerateWithToken test failure #2880

Merged
merged 1 commit into from
May 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 16 additions & 52 deletions pkg/sources/github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -545,19 +545,8 @@ func (s *Source) enumerateWithToken(ctx context.Context, apiEndpoint, token stri
}
s.apiClient = ghClient

// TODO: this should support scanning users too

specificScope := false

if len(s.repos) > 0 {
specificScope = true
}

var (
ghUser *github.User
)

ctx.Logger().V(1).Info("Enumerating with token", "endpoint", apiEndpoint)
var ghUser *github.User
for {
ghUser, _, err = s.apiClient.Users.Get(ctx, "")
if s.handleRateLimit(err) {
Expand All @@ -569,71 +558,46 @@ func (s *Source) enumerateWithToken(ctx context.Context, apiEndpoint, token stri
break
}

if s.orgsCache.Count() > 0 {
specificScope = true
for _, org := range s.orgsCache.Keys() {
orgCtx := context.WithValue(ctx, "account", org)
userType, err := s.getReposByOrgOrUser(ctx, org)
if err != nil {
orgCtx.Logger().Error(err, "error fetching repos for org or user")
continue
}

if userType == organization && s.conn.ScanUsers {
if err := s.addMembersByOrg(ctx, org); err != nil {
orgCtx.Logger().Error(err, "Unable to add members by org")
}
}
}
}

// If no scope was provided, enumerate them.
specificScope := len(s.repos) > 0 || s.orgsCache.Count() > 0
if !specificScope {
// Enumerate the user's orgs and repos if none were specified.
if err := s.getReposByUser(ctx, ghUser.GetLogin()); err != nil {
s.log.Error(err, "error fetching repos by user")
s.log.Error(err, "Unable to fetch repos for the current user", "user", ghUser.GetLogin())
}
if err := s.addUserGistsToCache(ctx, ghUser.GetLogin()); err != nil {
s.log.Error(err, "Unable to fetch gists for the current user", "user", ghUser.GetLogin())
}

isGHE := !strings.EqualFold(apiEndpoint, cloudEndpoint)
if isGHE {
s.addAllVisibleOrgs(ctx)
} else {
// Scan for orgs is default with a token. GitHub App enumerates the repositories
// that were assigned to it in GitHub App settings.
// Scan for orgs is default with a token.
// GitHub App enumerates the repos that were assigned to it in GitHub App settings.
s.addOrgsByUser(ctx, ghUser.GetLogin())
}
}

if len(s.orgsCache.Keys()) > 0 {
for _, org := range s.orgsCache.Keys() {
orgCtx := context.WithValue(ctx, "account", org)
userType, err := s.getReposByOrgOrUser(ctx, org)
if err != nil {
orgCtx.Logger().Error(err, "error fetching repos for org or user")
orgCtx.Logger().Error(err, "Unable to fetch repos for org or user")
continue
}

if userType == organization && s.conn.ScanUsers {
if err := s.addMembersByOrg(ctx, org); err != nil {
orgCtx.Logger().Error(err, "Unable to add members by org for org")
orgCtx.Logger().Error(err, "Unable to add members for org")
}
}
}

if err := s.getReposByUser(ctx, ghUser.GetLogin()); err != nil {
s.log.Error(err, "error fetching repos for the current user", "user", ghUser.GetLogin())
}

// If we enabled ScanUsers above, we've already added the gists for the current user and users from the orgs.
// So if we don't have ScanUsers enabled, add the user gists as normal.
if err := s.addUserGistsToCache(ctx, ghUser.GetLogin()); err != nil {
s.log.Error(err, "error fetching gists for the current user", "user", ghUser.GetLogin())
if s.conn.ScanUsers && len(s.memberCache) > 0 {
s.log.Info("Fetching repos for org members", "org_count", s.orgsCache.Count(), "member_count", len(s.memberCache))
s.addReposForMembers(ctx)
}

return nil
}

if s.conn.ScanUsers {
s.log.Info("Adding repos", "orgs", s.orgsCache.Count(), "members", len(s.memberCache))
s.addReposForMembers(ctx)
return nil
}

return nil
Expand Down
3 changes: 2 additions & 1 deletion pkg/sources/github/github_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -400,14 +400,15 @@ func TestEnumerateWithToken(t *testing.T) {

gock.New("https://api.github.com").
Get("/users/super-secret-user/repos").
MatchParam("per_page", "100").
Reply(200).
JSON([]map[string]string{{"clone_url": "https://github.com/super-secret-user/super-secret-repo.git", "full_name": "super-secret-user/super-secret-repo"}})

gock.New("https://api.github.com").
Get("/user/orgs").
MatchParam("per_page", "100").
Reply(200).
JSON([]map[string]string{{"clone_url": "https://github.com/super-secret-repo.git", "full_name": "super-secret-repo"}})
JSON(`[]`)

gock.New("https://api.github.com").
Get("/users/super-secret-user/gists").
Expand Down
Loading