Skip to content

Commit 03740a8

Browse files
authored
Merge pull request #288 from github/422
fix(github_org): handle 422 in a nicer way
2 parents fb284c6 + c0b6a6a commit 03740a8

4 files changed

Lines changed: 27 additions & 2 deletions

File tree

‎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 (1.2.1)
4+
entitlements-github-plugin (1.2.2)
55
contracts (~> 0.17.0)
66
faraday (~> 2.0)
77
faraday-retry (~> 2.0)

‎lib/entitlements/backend/github_org/service.rb‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ def add_user_to_organization(user, role)
5454

5555
Entitlements.logger.warn "User #{user} not found in GitHub instance #{identifier}, ignoring."
5656
return false
57+
rescue Octokit::UnprocessableEntity => e
58+
Entitlements.logger.warn "Unprocessable entity when adding #{user} to organization #{org} with role #{role}: #{e.message}. This likely means the user blocked the invite, so we're ignoring them, but someone should check it."
59+
return false
5760
end
5861

5962
# Happy path

‎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 = "1.2.1"
5+
VERSION = "1.2.2"
66
end
77
end

‎spec/unit/entitlements/backend/github_org/service_spec.rb‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,28 @@
201201
expect(result).to eq(false)
202202
end
203203
end
204+
205+
context "when the API returns an unprocessable entity error" do
206+
it "warns and returns false" do
207+
expect(logger).to receive(:debug).with("github.fake add_user_to_organization(user=bob, org=kittensinc, role=admin)")
208+
expect(logger).to receive(:debug).with("Setting up GitHub API connection to https://github.fake/api/v3/")
209+
expect(logger).to receive(:warn).with(/Unprocessable entity when adding bob to organization kittensinc with role admin.*likely means the user blocked the invite/)
210+
211+
stub_request(:put, "https://github.fake/api/v3/orgs/kittensinc/memberships/bob").to_return(
212+
status: 422,
213+
headers: {
214+
"Content-type" => "application/json"
215+
},
216+
body: JSON.generate({
217+
"message" => "Unprocessable Entity",
218+
"documentation_url" => "https://docs.github.com/rest"
219+
})
220+
)
221+
222+
result = subject.send(:add_user_to_organization, "bob", "admin")
223+
expect(result).to eq(false)
224+
end
225+
end
204226
end
205227
end
206228

0 commit comments

Comments
 (0)