Skip to content

Commit 87c4a6e

Browse files
authored
Merge pull request #5043 from github/global-graphql-batch-prefetch
Prefetch all collection items globally before tests
2 parents c02dcfa + d4fbdbc commit 87c4a6e

File tree

3 files changed

+49
-4
lines changed

3 files changed

+49
-4
lines changed

test/collections_test.rb

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@
140140
if ENV["SKIP_COLLECTION_API_CHECKS"]
141141
skip "Skipping collection API checks (rename detection handled by collections-renames)"
142142
end
143+
prefetch_all_collection_items!
143144

144145
errors = []
145146
repos_to_check = []
@@ -155,10 +156,6 @@
155156
end
156157
end
157158

158-
cache_repos_exist_check!(repos_to_check)
159-
cache_users_exist_check!(users_to_check)
160-
cache_orgs_exist_check!(users_not_found_from(users_to_check))
161-
162159
repos_to_check.each do |repo|
163160
repo_result = client.repository(repo)
164161
current_name_with_owner = repo_result&.full_name

test/collections_test_helper.rb

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,3 +118,42 @@ def annotate_collection_item_error(collection, string, error_message)
118118
def possible_image_file_names_for_collection(collection)
119119
COLLECTION_IMAGE_EXTENSIONS.map { |ext| "#{collection}#{ext}" }
120120
end
121+
122+
GRAPHQL_BATCH_SIZE = 100
123+
124+
def prefetch_all_collection_items!
125+
return if NewOctokit.global_prefetch_done?
126+
127+
repos, users = collect_all_collection_items
128+
prefetch_repos!(repos)
129+
prefetch_users!(users)
130+
131+
NewOctokit.global_prefetch_done!
132+
end
133+
134+
def collect_all_collection_items
135+
all_items = collections.flat_map { |c| items_for_collection(c) || [] }
136+
137+
repos = all_items.select { |item| item.match?(USERNAME_AND_REPO_REGEX) }.uniq
138+
users = all_items
139+
.select { |item| item.match?(USERNAME_REGEX) && !item.match?(USERNAME_AND_REPO_REGEX) }
140+
.uniq
141+
142+
[repos, users]
143+
end
144+
145+
def prefetch_repos!(repos)
146+
repos.each_slice(GRAPHQL_BATCH_SIZE) do |batch|
147+
cache_repos_exist_check!(batch)
148+
end
149+
end
150+
151+
def prefetch_users!(users)
152+
users.each_slice(GRAPHQL_BATCH_SIZE) do |batch|
153+
cache_users_exist_check!(batch)
154+
end
155+
156+
users_not_found_from(users).each_slice(GRAPHQL_BATCH_SIZE) do |batch|
157+
cache_orgs_exist_check!(batch)
158+
end
159+
end

test/test_helper.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ class NewOctokit < Octokit::Client
5959
@@repo_request_count = 0 unless defined? @@repo_request_count
6060
@@user_request_count = 0 unless defined? @@user_request_count
6161
@@messages = [] unless defined? @@messages
62+
@@global_prefetch_done = false unless defined? @@global_prefetch_done
6263

6364
def repos
6465
@@repos
@@ -122,6 +123,14 @@ def self.messages
122123
@@messages
123124
end
124125

126+
def self.global_prefetch_done?
127+
@@global_prefetch_done
128+
end
129+
130+
def self.global_prefetch_done!
131+
@@global_prefetch_done = true
132+
end
133+
125134
# rubocop:enable Style/ClassVars
126135
end
127136

0 commit comments

Comments
 (0)