@@ -35,6 +35,23 @@ def sync(implementation, role)
3535
3636 private
3737
38+ Contract String , String => C ::HashOf [ Symbol , C ::Any ]
39+ def add_user_to_role ( user , role )
40+ if role == "security_manager"
41+ octokit . add_role_to_user ( user , role )
42+
43+ # This is a hack to get around the fact that the GitHub API
44+ # has two different concepts of organization roles,
45+ # and the one we want to use is not present in organization memberships.
46+ #
47+ # If we get here, we know that the user is already member of the organization,
48+ # and we know that the user has been successfully granted the role.
49+ { user :, role :, state : "active" }
50+ else
51+ octokit . update_organization_membership ( org , user :, role :)
52+ end
53+ end
54+
3855 # Upsert a user with a role to the organization.
3956 #
4057 # user: A String with the (GitHub) username of the person to add or modify.
@@ -46,10 +63,21 @@ def add_user_to_organization(user, role)
4663 Entitlements . logger . debug "#{ identifier } add_user_to_organization(user=#{ user } , org=#{ org } , role=#{ role } )"
4764
4865 begin
49- new_membership = octokit . update_organization_membership ( org , user : , role : )
66+ new_membership = add_role_to_user ( user , role )
5067 rescue Octokit ::NotFound => e
5168 raise e unless ignore_not_found
5269
70+ Entitlements . logger . warn "User #{ user } not found in GitHub instance #{ identifier } , ignoring."
71+ return false
72+ rescue Octokit ::UnprocessableEntity => e
73+ # Two conditions can cause this:
74+ # - If the role is not enabled, we'll get a 422.
75+ # - If the user is not a member of the organization, we'll get a 422.
76+
77+ # We'll loop this under ignore_not_found
78+ # since this affects the case where we want to add a user to security_manager role
79+ raise e unless ignore_not_found
80+
5381 Entitlements . logger . warn "User #{ user } not found in GitHub instance #{ identifier } , ignoring."
5482 return false
5583 end
0 commit comments