Skip to content

Commit 94f8e48

Browse files
Merge pull request #28 from github/jasonmacgowan/ignore_not_found
Support ignoring 404s when user is not found in GitHub instance
2 parents 62f8827 + 7f71f60 commit 94f8e48

File tree

17 files changed

+204
-55
lines changed

17 files changed

+204
-55
lines changed

Gemfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
entitlements-github-plugin (0.4.4)
4+
entitlements-github-plugin (0.5.0)
55
contracts (~> 0.17.0)
66
faraday (~> 2.0)
77
faraday-retry (~> 2.0)

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ Any plugins defined in `lib/entitlements-and-plugins` will be loaded and used at
5656
dir: github.com/github/org
5757
org: github
5858
token: <%= ENV["GITHUB_ORG_TOKEN"] %>
59+
ignore_not_found: false # optional argument to ignore users who are not found in the GitHub instance
5960
type: "github_org"
6061
```
6162
@@ -72,6 +73,7 @@ Any plugins defined in `lib/entitlements-and-plugins` will be loaded and used at
7273
dir: github.com/github/teams
7374
org: github
7475
token: <%= ENV["GITHUB_ORG_TOKEN"] %>
76+
ignore_not_found: false # optional argument to ignore users who are not found in the GitHub instance
7577
type: "github_team"
7678
```
7779

lib/entitlements/backend/github_org/controller.rb

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -120,12 +120,13 @@ def apply(action)
120120
Contract String, C::HashOf[String => C::Any] => nil
121121
def validate_config!(key, data)
122122
spec = COMMON_GROUP_CONFIG.merge({
123-
"base" => { required: true, type: String },
124-
"addr" => { required: false, type: String },
125-
"org" => { required: true, type: String },
126-
"token" => { required: true, type: String },
127-
"features" => { required: false, type: Array },
128-
"ignore" => { required: false, type: Array }
123+
"base" => { required: true, type: String },
124+
"addr" => { required: false, type: String },
125+
"org" => { required: true, type: String },
126+
"token" => { required: true, type: String },
127+
"features" => { required: false, type: Array },
128+
"ignore" => { required: false, type: Array },
129+
"ignore_not_found" => { required: false, type: [FalseClass, TrueClass] },
129130
})
130131
text = "GitHub organization group #{key.inspect}"
131132
Entitlements::Util::Util.validate_attr!(spec, data, text)

lib/entitlements/backend/github_org/provider.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ def initialize(config:)
2525
org: config.fetch("org"),
2626
addr: config.fetch("addr", nil),
2727
token: config.fetch("token"),
28-
ou: config.fetch("base")
28+
ou: config.fetch("base"),
29+
ignore_not_found: config.fetch("ignore_not_found", false)
2930
)
3031
@role_cache = {}
3132
end

lib/entitlements/backend/github_org/service.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,15 @@ def sync(implementation, role)
4444
Contract String, String => C::Bool
4545
def add_user_to_organization(user, role)
4646
Entitlements.logger.debug "#{identifier} add_user_to_organization(user=#{user}, org=#{org}, role=#{role})"
47-
new_membership = octokit.update_organization_membership(org, user:, role:)
47+
48+
begin
49+
new_membership = octokit.update_organization_membership(org, user:, role:)
50+
rescue Octokit::NotFound => e
51+
raise e unless ignore_not_found
52+
53+
Entitlements.logger.warn "User #{user} not found in GitHub instance #{identifier}, ignoring."
54+
return false
55+
end
4856

4957
# Happy path
5058
if new_membership[:role] == role

lib/entitlements/backend/github_team/controller.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,8 @@ def validate_config!(key, data)
110110
"base" => { required: true, type: String },
111111
"addr" => { required: false, type: String },
112112
"org" => { required: true, type: String },
113-
"token" => { required: true, type: String }
113+
"token" => { required: true, type: String },
114+
"ignore_not_found" => { required: false, type: [FalseClass, TrueClass] },
114115
})
115116
text = "GitHub group #{key.inspect}"
116117
Entitlements::Util::Util.validate_attr!(spec, data, text)

lib/entitlements/backend/github_team/provider.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ def initialize(config:)
2323
org: config.fetch("org"),
2424
addr: config.fetch("addr", nil),
2525
token: config.fetch("token"),
26-
ou: config.fetch("base")
26+
ou: config.fetch("base"),
27+
ignore_not_found: config.fetch("ignore_not_found", false)
2728
)
2829

2930
@github_team_cache = {}

lib/entitlements/backend/github_team/service.rb

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,10 @@ class TeamNotFound < RuntimeError; end
2828
addr: C::Maybe[String],
2929
org: String,
3030
token: String,
31-
ou: String
31+
ou: String,
32+
ignore_not_found: C::Bool,
3233
] => C::Any
33-
def initialize(addr: nil, org:, token:, ou:)
34+
def initialize(addr: nil, org:, token:, ou:, ignore_not_found: false)
3435
super
3536
Entitlements.cache[:github_team_members] ||= {}
3637
Entitlements.cache[:github_team_members][org] ||= {}
@@ -436,8 +437,16 @@ def add_user_to_team(user:, team:, role: "member")
436437
end
437438
Entitlements.logger.debug "#{identifier} add_user_to_team(user=#{user}, org=#{org}, team_id=#{team.team_id}, role=#{role})"
438439
validate_team_id_and_slug!(team.team_id, team.team_name)
439-
result = octokit.add_team_membership(team.team_id, user, role:)
440-
result[:state] == "active" || result[:state] == "pending"
440+
441+
begin
442+
result = octokit.add_team_membership(team.team_id, user, role:)
443+
result[:state] == "active" || result[:state] == "pending"
444+
rescue Octokit::NotFound => e
445+
raise e unless ignore_not_found
446+
447+
Entitlements.logger.warn "User #{user} not found in GitHub instance #{identifier}, ignoring."
448+
false
449+
end
441450
end
442451

443452
# Remove user from team.

lib/entitlements/service/github.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class GitHub
1717
MAX_GRAPHQL_RETRIES = 3
1818
WAIT_BETWEEN_GRAPHQL_RETRIES = 1
1919

20-
attr_reader :addr, :org, :token, :ou
20+
attr_reader :addr, :org, :token, :ou, :ignore_not_found
2121

2222
# Constructor.
2323
#
@@ -31,14 +31,16 @@ class GitHub
3131
addr: C::Maybe[String],
3232
org: String,
3333
token: String,
34-
ou: String
34+
ou: String,
35+
ignore_not_found: C::Bool,
3536
] => C::Any
36-
def initialize(addr: nil, org:, token:, ou:)
37+
def initialize(addr: nil, org:, token:, ou:, ignore_not_found: false)
3738
# Save some parameters for the connection but don't actually connect yet.
3839
@addr = addr
3940
@org = org
4041
@token = token
4142
@ou = ou
43+
@ignore_not_found = ignore_not_found
4244

4345
# This is a global cache across all invocations of this object. GitHub membership
4446
# need to be obtained only one time per organization, but might be used multiple times.

lib/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

33
module Entitlements
44
module Version
5-
VERSION = "0.4.4"
5+
VERSION = "0.5.0"
66
end
77
end

0 commit comments

Comments
 (0)