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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f2b0df98e7f69e4f2189cf5a2e4edf7308faf18c8c307263c21bbf03e6e25e32
4
- data.tar.gz: 411e9f69958ffc3685613befb69bce451a1ff18e4b087782ef143e9e7ce989a2
3
+ metadata.gz: b3e49b53f427002ef007b515fb1796dc67912d81f507b2c4c9db5eca1350c675
4
+ data.tar.gz: c2f886127ad631f4a5b16361eb59fced9862bbb7374ee116bb1333f5a294ce7c
5
5
  SHA512:
6
- metadata.gz: ed27dd9f70d393b25a3cc8b0f3b5bac2c013d5aafe2030e423ee0a2d40476e2f90e81f61e5e46a6848bddc524b14da987f7dec9eed1ab8b4ee24ed2d2aa7312e
7
- data.tar.gz: 344da39e0b7af4638393a4ffe21ee15ebbe70b90172edc0b8b674d93b08e92035b53fea1f20062953de7f6249c873ad0280205074d877c324cb18febf7373fe6
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 ||= begin
173
- client = Octokit::Client.new(access_token: token)
174
- middleware = Octokit::Default.middleware.dup
175
- middleware.adapter :net_http_persistent
176
- client.middleware = middleware
177
- client.api_endpoint = addr if addr
178
- client.auto_paginate = true
179
- client.per_page = 100
180
- Entitlements.logger.debug "Setting up GitHub API connection to #{client.api_endpoint}"
181
- client
182
- end
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"] ? members_and_roles_from_graphql : members_and_roles_from_rest
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
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Entitlements
4
4
  module Version
5
- VERSION = "1.2.9"
5
+ VERSION = "1.2.11"
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: entitlements-github-plugin
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.9
4
+ version: 1.2.11
5
5
  platform: ruby
6
6
  authors:
7
7
  - GitHub, Inc. Security Ops