Skip to content

Commit 2b01218

Browse files
committed
ensure all calls out through octokit have retry logic configured for unhandled exception classes
1 parent e47fe14 commit 2b01218

File tree

4 files changed

+41
-12
lines changed

4 files changed

+41
-12
lines changed

lib/entitlements/backend/github_org.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,5 @@ class DuplicateUserError < RuntimeError; end
2424
require_relative "github_org/controller"
2525
require_relative "github_org/provider"
2626
require_relative "github_org/service"
27+
require_relative "../config/retry"
28+
Retry.setup!

lib/entitlements/backend/github_org/service.rb

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,9 @@ def add_user_to_organization(user, role)
4646
Entitlements.logger.debug "#{identifier} add_user_to_organization(user=#{user}, org=#{org}, role=#{role})"
4747

4848
begin
49-
new_membership = octokit.update_organization_membership(org, user:, role:)
49+
new_membership = Retryable.with_context(:default, not: [Octokit::NotFound]) do
50+
octokit.update_organization_membership(org, user:, role:)
51+
end
5052
rescue Octokit::NotFound => e
5153
raise e unless ignore_not_found
5254

@@ -78,7 +80,10 @@ def add_user_to_organization(user, role)
7880
Contract String => C::Bool
7981
def remove_user_from_organization(user)
8082
Entitlements.logger.debug "#{identifier} remove_user_from_organization(user=#{user}, org=#{org})"
81-
result = octokit.remove_organization_membership(org, user:)
83+
84+
result = Retryable.with_context(:default) do
85+
octokit.remove_organization_membership(org, user:)
86+
end
8287

8388
# If we removed the user, remove them from the cache of members, so that any GitHub team
8489
# operations in this organization will ignore this user.

lib/entitlements/backend/github_team.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,5 @@
44
require_relative "github_team/models/team"
55
require_relative "github_team/provider"
66
require_relative "github_team/service"
7+
require_relative "../config/retry"
8+
Retry.setup!

lib/entitlements/backend/github_team/service.rb

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -279,10 +279,12 @@ def create_team(entitlement_group:)
279279
team_options[:parent_team_id] = parent_team_data[:team_id]
280280
rescue TeamNotFound
281281
# if the parent team does not exist, create it (think `mkdir -p` logic here)
282-
result = octokit.create_team(
283-
org,
284-
{ name: entitlement_metadata["parent_team_name"], repo_names: [], privacy: "closed" }
285-
)
282+
result = Retryable.with_context(:default, not: [Octokit::UnprocessableEntity]) do
283+
octokit.create_team(
284+
org,
285+
{ name: entitlement_metadata["parent_team_name"], repo_names: [], privacy: "closed" }
286+
)
287+
end
286288

287289
Entitlements.logger.debug "created parent team #{entitlement_metadata["parent_team_name"]} with id #{result[:id]}"
288290

@@ -296,7 +298,11 @@ def create_team(entitlement_group:)
296298
end
297299

298300
Entitlements.logger.debug "create_team(team=#{team_name})"
299-
result = octokit.create_team(org, team_options)
301+
302+
result = Retryable.with_context(:default, not: [Octokit::UnprocessableEntity]) do
303+
octokit.create_team(org, team_options)
304+
end
305+
300306
Entitlements.logger.debug "created team #{team_name} with id #{result[:id]}"
301307
true
302308
rescue Octokit::UnprocessableEntity => e
@@ -317,7 +323,10 @@ def update_team(team:, metadata: {})
317323
Entitlements.logger.debug "update_team(team=#{team.team_name})"
318324
options = { name: team.team_name, repo_names: [], privacy: "closed",
319325
parent_team_id: metadata[:parent_team_id] }
320-
octokit.update_team(team.team_id, options)
326+
Retryable.with_context(:default, not: [Octokit::UnprocessableEntity]) do
327+
octokit.update_team(team.team_id, options)
328+
end
329+
321330
true
322331
rescue Octokit::UnprocessableEntity => e
323332
Entitlements.logger.debug "update_team(team=#{team.team_name}) ERROR - #{e.message}"
@@ -334,7 +343,9 @@ def update_team(team:, metadata: {})
334343
team_name: String
335344
] => Sawyer::Resource
336345
def team_by_name(org_name:, team_name:)
337-
octokit.team_by_name(org_name, team_name)
346+
Retryable.with_context(:default) do
347+
octokit.team_by_name(org_name, team_name)
348+
end
338349
end
339350

340351
private
@@ -426,7 +437,10 @@ def validate_team_id_and_slug!(team_id, team_slug)
426437
@validation_cache ||= {}
427438
@validation_cache[team_id] ||= begin
428439
Entitlements.logger.debug "validate_team_id_and_slug!(#{team_id}, #{team_slug.inspect})"
429-
team_data = octokit.team(team_id)
440+
team_data = Retryable.with_context(:default) do
441+
octokit.team(team_id)
442+
end
443+
430444
team_data[:slug]
431445
end
432446
return if @validation_cache[team_id] == team_slug
@@ -457,7 +471,10 @@ def add_user_to_team(user:, team:, role: "member")
457471
validate_team_id_and_slug!(team.team_id, team.team_name)
458472

459473
begin
460-
result = octokit.add_team_membership(team.team_id, user, role:)
474+
result = Retryable.with_context(:default, not: [Octokit::UnprocessableEntity, Octokit::NotFound]) do
475+
octokit.add_team_membership(team.team_id, user, role:)
476+
end
477+
461478
result[:state] == "active" || result[:state] == "pending"
462479
rescue Octokit::UnprocessableEntity => e
463480
raise e unless ignore_not_found && e.message =~ /Enterprise Managed Users must be part of the organization to be assigned to the team/
@@ -487,7 +504,10 @@ def remove_user_from_team(user:, team:)
487504

488505
Entitlements.logger.debug "#{identifier} remove_user_from_team(user=#{user}, org=#{org}, team_id=#{team.team_id})"
489506
validate_team_id_and_slug!(team.team_id, team.team_name)
490-
octokit.remove_team_membership(team.team_id, user)
507+
508+
Retryable.with_context(:default) do
509+
octokit.remove_team_membership(team.team_id, user)
510+
end
491511
end
492512
end
493513
end

0 commit comments

Comments
 (0)