entitlements-github-plugin 1.2.11 → 1.2.12

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: b3e49b53f427002ef007b515fb1796dc67912d81f507b2c4c9db5eca1350c675
4
- data.tar.gz: c2f886127ad631f4a5b16361eb59fced9862bbb7374ee116bb1333f5a294ce7c
3
+ metadata.gz: a4eb4d9c385dbf924adbf07ebfe1389428497434a0e1dd147e7b8a7cffa7839c
4
+ data.tar.gz: 10ef2ed121b482f03478a77e53d38726bbf2accb847808075307d122d600cd43
5
5
  SHA512:
6
- metadata.gz: a6d7730c2b953cdaa18f84a5338da18a0992d44977504a3840eec71afbe4a4a7dd11394e9770136a23e5f18f4ed75594105306674292af77a8675291a45124d7
7
- data.tar.gz: d4b14db75424d7c1a1e324e594cf3836f89c89559191835b8cdd96b9aaf0a2abb7aa98b42ec19e967c7dd708b848f84a4aaad5cb5074f7e6ce554ccb741cfb99
6
+ metadata.gz: ab73d55574df9272669eb5eba24fc2ae46d209d18435ba8f0cbee6c78fa8ea7a05aad755074b63bc203878fa97d3ecc33fa17d9776c70a2b9e024821adc308fb
7
+ data.tar.gz: efe310b6df99749f04a8464c6273557a2a85f5d91b9695c98e76cf704aa9f0ec24a29101cfafe40ac7cedca0558ce41be3456f6b193c8ac25d7482e2ba98d335
@@ -355,12 +355,20 @@ module Entitlements
355
355
  # future if this gets more widely used, consider one of the graphql client
356
356
  # gems, such as https://github.com/github/graphql-client.)
357
357
  #
358
+ # When ENTITLEMENTS_PARALLEL_ORG_MEMBERS is set, .ghe.com teams are read over REST instead,
359
+ # with member pages fetched in parallel.
360
+ #
358
361
  # team_slug - Identifier of the team to retrieve.
359
362
  #
360
363
  # Returns a data structure with team data.
361
364
  Contract String => { members: C::ArrayOf[String], team_id: Integer, parent_team_name: C::Or[String, nil],
362
365
  roles: C::HashOf[String => String] }
363
366
  def graphql_team_data(team_slug)
367
+ if ENV["ENTITLEMENTS_PARALLEL_ORG_MEMBERS"] && graphql_uri.host.end_with?(".ghe.com")
368
+ rest_data = rest_team_data_in_parallel(team_slug)
369
+ return rest_data if rest_data
370
+ end
371
+
364
372
  cursor = nil
365
373
  team_id = nil
366
374
  parent_team_name = nil
@@ -424,6 +432,46 @@ module Entitlements
424
432
  { members: result, team_id:, parent_team_name:, roles: }
425
433
  end
426
434
 
435
+ # Read the members of a team over REST, fetching member pages in parallel. The REST member
436
+ # list also includes members of child teams, so teams with child teams are left to GraphQL.
437
+ #
438
+ # team_slug - Identifier of the team to retrieve.
439
+ #
440
+ # Returns the same data structure as graphql_team_data, or nil if the team has child teams.
441
+ Contract String => C::Maybe[{ members: C::ArrayOf[String], team_id: Integer, parent_team_name: C::Or[String, nil],
442
+ roles: C::HashOf[String => String] }]
443
+ def rest_team_data_in_parallel(team_slug)
444
+ team_path = "orgs/#{org}/teams/#{team_slug}"
445
+
446
+ begin
447
+ team = Retryable.with_context(:default, not: [Octokit::NotFound]) do
448
+ octokit.get(team_path)
449
+ end
450
+ rescue Octokit::NotFound
451
+ raise TeamNotFound, "Requested team #{team_slug} does not exist in #{org}!"
452
+ end
453
+
454
+ child_teams = Retryable.with_context(:default) do
455
+ octokit.get("#{team_path}/teams", per_page: 1)
456
+ end
457
+ if child_teams.any?
458
+ Entitlements.logger.debug "Team #{org}/#{team_slug} has child teams, reading it with GraphQL"
459
+ return nil
460
+ end
461
+
462
+ members = rest_pages_in_parallel(->(client, options) { client.paginate("#{team_path}/members", options) })
463
+ maintainers = rest_pages_in_parallel(lambda do |client, options|
464
+ client.paginate("#{team_path}/members", { role: "maintainer", **options })
465
+ end)
466
+
467
+ logins = members.map { |member| member[:login].downcase }.uniq
468
+ maintainer_logins = maintainers.map { |member| member[:login].downcase }.to_set
469
+ roles = logins.to_h { |login| [login, maintainer_logins.include?(login) ? "maintainer" : "member"] }
470
+ parent_team_name = team[:parent] && team[:parent][:slug]
471
+
472
+ { members: logins, team_id: team[:id], parent_team_name:, roles: }
473
+ end
474
+
427
475
  # Ensure that the given team ID actually matches up to the team slug on GitHub. This is in place
428
476
  # because we are relying on something in graphql that we shouldn't be, until the attribute we need
429
477
  # is added as a first class citizen. Once that happens, this can be removed.
@@ -330,24 +330,34 @@ module Entitlements
330
330
  result
331
331
  end
332
332
 
333
- # Fetch the first page of organization members for a role, then the remaining pages
334
- # using up to REST_THREADS threads.
333
+ # Fetch all organization members for a role, fetching pages in parallel.
335
334
  #
336
335
  # role - String with the role ("admin" or "member")
337
336
  #
338
337
  # Returns Array of member resources.
339
338
  Contract String => C::ArrayOf[C::Any]
340
339
  def organization_members_in_parallel(role)
340
+ rest_pages_in_parallel(->(client, options) { client.organization_members(org, { role:, **options }) })
341
+ end
342
+
343
+ # Fetch the first page of a paginated REST list, then the remaining pages
344
+ # using up to REST_THREADS threads.
345
+ #
346
+ # fetch_page - Proc called with an Octokit client and a Hash of options, returning the response
347
+ #
348
+ # Returns Array of resources.
349
+ Contract Proc => C::ArrayOf[C::Any]
350
+ def rest_pages_in_parallel(fetch_page)
341
351
  client = new_octokit(auto_paginate: false)
342
352
  first_page = Retryable.with_context(:default) do
343
- client.organization_members(org, { role:, page: 1 })
353
+ fetch_page.call(client, { page: 1 })
344
354
  end
345
355
 
346
356
  rels = client.last_response.rels
347
357
  # GitHub can omit rel="last", so fall back to fetching one page at a time.
348
358
  if rels[:next] && !rels[:last]
349
359
  return Retryable.with_context(:default) do
350
- octokit.organization_members(org, { role: })
360
+ fetch_page.call(octokit, {})
351
361
  end
352
362
  end
353
363
 
@@ -360,13 +370,13 @@ module Entitlements
360
370
  Thread.new do
361
371
  Thread.current.report_on_exception = false
362
372
  thread_client = new_octokit(auto_paginate: false)
363
- members = []
373
+ responses = {}
364
374
  while (page = pages.pop)
365
- members.concat(Retryable.with_context(:default) do
366
- thread_client.organization_members(org, { role:, page: })
367
- end)
375
+ responses[page] = Retryable.with_context(:default) do
376
+ fetch_page.call(thread_client, { page: })
377
+ end
368
378
  end
369
- members
379
+ responses
370
380
  rescue StandardError
371
381
  # Stop the other threads from fetching more pages.
372
382
  pages.clear
@@ -374,7 +384,9 @@ module Entitlements
374
384
  end
375
385
  end
376
386
 
377
- first_page + threads.flat_map(&:value)
387
+ # Threads pick up pages in any order, so put the pages back in order.
388
+ responses = threads.map(&:value).reduce({}, :merge)
389
+ first_page + responses.sort.flat_map { |_page, resources| resources }
378
390
  end
379
391
 
380
392
  # Query GraphQL API to get a list of pending members for the organization.
data/lib/version.rb CHANGED
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Entitlements
4
4
  module Version
5
- VERSION = "1.2.11"
5
+ VERSION = "1.2.12"
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.11
4
+ version: 1.2.12
5
5
  platform: ruby
6
6
  authors:
7
7
  - GitHub, Inc. Security Ops