entitlements-github-plugin 1.2.9 → 1.2.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/entitlements/backend/github_org/controller.rb +3 -0
- data/lib/entitlements/service/github.rb +96 -12
- data/lib/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: b3e49b53f427002ef007b515fb1796dc67912d81f507b2c4c9db5eca1350c675
|
|
4
|
+
data.tar.gz: c2f886127ad631f4a5b16361eb59fced9862bbb7374ee116bb1333f5a294ce7c
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: a6d7730c2b953cdaa18f84a5338da18a0992d44977504a3840eec71afbe4a4a7dd11394e9770136a23e5f18f4ed75594105306674292af77a8675291a45124d7
|
|
7
|
+
data.tar.gz: d4b14db75424d7c1a1e324e594cf3836f89c89559191835b8cdd96b9aaf0a2abb7aa98b42ec19e967c7dd708b848f84a4aaad5cb5074f7e6ce554ccb741cfb99
|
|
@@ -140,8 +140,11 @@ module Entitlements
|
|
|
140
140
|
end
|
|
141
141
|
end
|
|
142
142
|
|
|
143
|
+
# Set ENTITLEMENTS_PREFETCH_PENDING_MEMBERS to also fetch pending members here, where
|
|
144
|
+
# orgs can run in parallel, instead of during the serial calculation phase.
|
|
143
145
|
def prefetch
|
|
144
146
|
existing_groups
|
|
147
|
+
provider.pending_members if ENV["ENTITLEMENTS_PREFETCH_PENDING_MEMBERS"]
|
|
145
148
|
end
|
|
146
149
|
|
|
147
150
|
private
|
|
@@ -21,6 +21,9 @@ module Entitlements
|
|
|
21
21
|
MAX_GRAPHQL_RETRIES = 3
|
|
22
22
|
WAIT_BETWEEN_GRAPHQL_RETRIES = 1
|
|
23
23
|
|
|
24
|
+
# Threads used to fetch REST pages in parallel
|
|
25
|
+
REST_THREADS = 8
|
|
26
|
+
|
|
24
27
|
attr_reader :addr, :org, :token, :ou, :ignore_not_found
|
|
25
28
|
|
|
26
29
|
# Constructor.
|
|
@@ -169,17 +172,25 @@ module Entitlements
|
|
|
169
172
|
# Returns an Octokit client object.
|
|
170
173
|
Contract C::None => Octokit::Client
|
|
171
174
|
def octokit
|
|
172
|
-
@octokit ||=
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
175
|
+
@octokit ||= new_octokit(auto_paginate: true)
|
|
176
|
+
end
|
|
177
|
+
|
|
178
|
+
# Build a new octokit client.
|
|
179
|
+
#
|
|
180
|
+
# auto_paginate - Boolean to follow pagination automatically
|
|
181
|
+
#
|
|
182
|
+
# Returns an Octokit client object.
|
|
183
|
+
Contract C::KeywordArgs[auto_paginate: C::Bool] => Octokit::Client
|
|
184
|
+
def new_octokit(auto_paginate:)
|
|
185
|
+
client = Octokit::Client.new(access_token: token)
|
|
186
|
+
middleware = Octokit::Default.middleware.dup
|
|
187
|
+
middleware.adapter :net_http_persistent
|
|
188
|
+
client.middleware = middleware
|
|
189
|
+
client.api_endpoint = addr if addr
|
|
190
|
+
client.auto_paginate = auto_paginate
|
|
191
|
+
client.per_page = 100
|
|
192
|
+
Entitlements.logger.debug "Setting up GitHub API connection to #{client.api_endpoint}"
|
|
193
|
+
client
|
|
183
194
|
end
|
|
184
195
|
|
|
185
196
|
# Get data from the predictive updates cache if it's available and valid, or else get it
|
|
@@ -204,13 +215,18 @@ module Entitlements
|
|
|
204
215
|
end
|
|
205
216
|
|
|
206
217
|
# Use GraphQL in GitHub Actions, where entitlements-caches can answer it, and REST elsewhere.
|
|
218
|
+
# .ghe.com orgs are not served by entitlements-caches, so they always use REST.
|
|
219
|
+
# Set ENTITLEMENTS_PARALLEL_ORG_MEMBERS to fetch REST pages in parallel.
|
|
207
220
|
#
|
|
208
221
|
# Takes no parameters.
|
|
209
222
|
#
|
|
210
223
|
# Returns Hash of { "username" => "ROLE" } where "ROLE" is from GraphQL Enum.
|
|
211
224
|
Contract C::None => C::HashOf[String => String]
|
|
212
225
|
def members_and_roles_from_api
|
|
213
|
-
ENV["GITHUB_RUN_ID"] ?
|
|
226
|
+
return members_and_roles_from_graphql if ENV["GITHUB_RUN_ID"] && !graphql_uri.host.end_with?(".ghe.com")
|
|
227
|
+
return members_and_roles_from_rest_in_parallel if ENV["ENTITLEMENTS_PARALLEL_ORG_MEMBERS"]
|
|
228
|
+
|
|
229
|
+
members_and_roles_from_rest
|
|
214
230
|
end
|
|
215
231
|
|
|
216
232
|
# Query GraphQL API to get a list of members and their roles.
|
|
@@ -293,6 +309,74 @@ module Entitlements
|
|
|
293
309
|
result
|
|
294
310
|
end
|
|
295
311
|
|
|
312
|
+
# Same as members_and_roles_from_rest, but fetches pages in parallel.
|
|
313
|
+
#
|
|
314
|
+
# Takes no parameters.
|
|
315
|
+
#
|
|
316
|
+
# Returns Hash of { "username" => "ROLE" } where "ROLE" is ADMIN or MEMBER
|
|
317
|
+
Contract C::None => C::HashOf[String => String]
|
|
318
|
+
def members_and_roles_from_rest_in_parallel
|
|
319
|
+
Entitlements.logger.debug "Loading organization members and roles for #{org} in parallel"
|
|
320
|
+
result = {}
|
|
321
|
+
|
|
322
|
+
organization_members_in_parallel("admin").each do |member|
|
|
323
|
+
result[member[:login].downcase] = "ADMIN"
|
|
324
|
+
end
|
|
325
|
+
|
|
326
|
+
organization_members_in_parallel("member").each do |member|
|
|
327
|
+
result[member[:login].downcase] = "MEMBER"
|
|
328
|
+
end
|
|
329
|
+
|
|
330
|
+
result
|
|
331
|
+
end
|
|
332
|
+
|
|
333
|
+
# Fetch the first page of organization members for a role, then the remaining pages
|
|
334
|
+
# using up to REST_THREADS threads.
|
|
335
|
+
#
|
|
336
|
+
# role - String with the role ("admin" or "member")
|
|
337
|
+
#
|
|
338
|
+
# Returns Array of member resources.
|
|
339
|
+
Contract String => C::ArrayOf[C::Any]
|
|
340
|
+
def organization_members_in_parallel(role)
|
|
341
|
+
client = new_octokit(auto_paginate: false)
|
|
342
|
+
first_page = Retryable.with_context(:default) do
|
|
343
|
+
client.organization_members(org, { role:, page: 1 })
|
|
344
|
+
end
|
|
345
|
+
|
|
346
|
+
rels = client.last_response.rels
|
|
347
|
+
# GitHub can omit rel="last", so fall back to fetching one page at a time.
|
|
348
|
+
if rels[:next] && !rels[:last]
|
|
349
|
+
return Retryable.with_context(:default) do
|
|
350
|
+
octokit.organization_members(org, { role: })
|
|
351
|
+
end
|
|
352
|
+
end
|
|
353
|
+
|
|
354
|
+
last_page = rels[:last] ? URI.decode_www_form(URI(rels[:last].href).query).to_h.fetch("page").to_i : 1
|
|
355
|
+
pages = Queue.new
|
|
356
|
+
(2..last_page).each { |page| pages << page }
|
|
357
|
+
pages.close
|
|
358
|
+
|
|
359
|
+
threads = Array.new([REST_THREADS, last_page - 1].min) do
|
|
360
|
+
Thread.new do
|
|
361
|
+
Thread.current.report_on_exception = false
|
|
362
|
+
thread_client = new_octokit(auto_paginate: false)
|
|
363
|
+
members = []
|
|
364
|
+
while (page = pages.pop)
|
|
365
|
+
members.concat(Retryable.with_context(:default) do
|
|
366
|
+
thread_client.organization_members(org, { role:, page: })
|
|
367
|
+
end)
|
|
368
|
+
end
|
|
369
|
+
members
|
|
370
|
+
rescue StandardError
|
|
371
|
+
# Stop the other threads from fetching more pages.
|
|
372
|
+
pages.clear
|
|
373
|
+
raise
|
|
374
|
+
end
|
|
375
|
+
end
|
|
376
|
+
|
|
377
|
+
first_page + threads.flat_map(&:value)
|
|
378
|
+
end
|
|
379
|
+
|
|
296
380
|
# Query GraphQL API to get a list of pending members for the organization.
|
|
297
381
|
#
|
|
298
382
|
# Takes no parameters.
|
data/lib/version.rb
CHANGED