Skip to content

Commit 2df575e

Browse files
committed
Batch GitHub team GraphQL reads
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: bff1e95b-3949-4ae8-b3a5-737fd278a977
1 parent 3298437 commit 2df575e

7 files changed

Lines changed: 510 additions & 157 deletions

File tree

lib/entitlements/backend/github_team/controller.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,8 @@ def initialize(group_name, config = nil)
2626

2727
def prefetch
2828
teams = Entitlements::Data::Groups::Calculated.read_all(group_name, config)
29-
teams.each do |team_slug|
30-
entitlement_group = Entitlements::Data::Groups::Calculated.read(team_slug)
31-
provider.read(entitlement_group)
32-
end
29+
entitlement_groups = teams.map { |team_slug| Entitlements::Data::Groups::Calculated.read(team_slug) }
30+
provider.prefetch(entitlement_groups)
3331
end
3432

3533
# Calculation routines.

lib/entitlements/backend/github_team/provider.rb

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,20 @@ def initialize(config:)
3030
@github_team_cache = {}
3131
end
3232

33+
# Populate the provider cache for a collection of desired teams.
34+
#
35+
# entitlement_groups - Array of Entitlements::Models::Group objects.
36+
#
37+
# Returns nothing.
38+
Contract C::ArrayOf[Entitlements::Models::Group] => nil
39+
def prefetch(entitlement_groups)
40+
github.read_teams(entitlement_groups).each do |team_name, github_team|
41+
log_loaded_team(github_team) if github_team
42+
@github_team_cache[team_name] = github_team
43+
end
44+
nil
45+
end
46+
3347
# Read in a specific GitHub.com Team and enumerate its members. Results are cached
3448
# for future runs.
3549
#
@@ -39,15 +53,12 @@ def initialize(config:)
3953
Contract Entitlements::Models::Group => C::Maybe[Entitlements::Models::Group]
4054
def read(entitlement_group)
4155
slug = Entitlements::Util::Util.any_to_cn(entitlement_group.cn.downcase)
42-
return @github_team_cache[slug] if @github_team_cache[slug]
56+
return @github_team_cache[slug] if @github_team_cache.key?(slug)
4357

4458
github_team = github.read_team(entitlement_group)
4559

46-
# We should not cache a team which does not exist
47-
return nil if github_team.nil?
48-
49-
Entitlements.logger.debug "Loaded #{github_team.team_dn} (id=#{github_team.team_id}) with #{github_team.member_strings.count} member(s)"
50-
@github_team_cache[github_team.team_name] = github_team
60+
log_loaded_team(github_team) if github_team
61+
@github_team_cache[slug] = github_team
5162
end
5263

5364
# Dry run of committing changes. Returns a list of users added or removed.
@@ -151,6 +162,12 @@ def auto_generate_ignored_users(entitlement_group)
151162

152163
private
153164

165+
Contract Entitlements::Backend::GitHubTeam::Models::Team => nil
166+
def log_loaded_team(github_team)
167+
Entitlements.logger.debug "Loaded #{github_team.team_dn} (id=#{github_team.team_id}) with #{github_team.member_strings.count} member(s)"
168+
nil
169+
end
170+
154171
# Construct an Entitlements::Models::Group for a new group and team
155172
#
156173
# group - An Entitlements::Models::Group object representing the defined group

0 commit comments

Comments
 (0)