Skip to content

Commit ab12625

Browse files
committed
Support ignoring 404s when user is not found in GitHub instance
1 parent 6f5348f commit ab12625

File tree

2 files changed

+75
-2
lines changed

2 files changed

+75
-2
lines changed

lib/entitlements/backend/github_team/service.rb

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -437,8 +437,16 @@ def add_user_to_team(user:, team:, role: "member")
437437
end
438438
Entitlements.logger.debug "#{identifier} add_user_to_team(user=#{user}, org=#{org}, team_id=#{team.team_id}, role=#{role})"
439439
validate_team_id_and_slug!(team.team_id, team.team_name)
440-
result = octokit.add_team_membership(team.team_id, user, role:)
441-
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
442450
end
443451

444452
# Remove user from team.

spec/unit/entitlements/backend/github_team/service_spec.rb

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -580,6 +580,71 @@
580580
result = subject.send(:add_user_to_team, user: "blackmanx", team:)
581581
expect(result).to eq(false)
582582
end
583+
584+
context "ignore_not_found is false" do
585+
it "raises when user is not found" do
586+
expect(subject).to receive(:validate_team_id_and_slug!).with(1001, "russian-blues").and_return(true)
587+
expect(subject).to receive(:org_members).and_return(Set.new(%w[blackmanx]))
588+
589+
add_membership_response = {
590+
"url" => "https://github.fake/api/v3/teams/1001/memberships/blackmanx",
591+
"role" => "member",
592+
"state" => "active"
593+
}
594+
595+
stub_request(:put, "https://github.fake/api/v3/teams/1001/memberships/blackmanx")
596+
.to_return(
597+
status: 404,
598+
headers: {
599+
"Content-Type" => "application/json"
600+
},
601+
body: JSON.generate({
602+
"message" => "Not Found",
603+
"documentation_url" => "https://docs.github.com/rest"
604+
})
605+
)
606+
607+
expect { subject.send(:add_user_to_team, user: "blackmanx", team:) }.to raise_error(Octokit::NotFound)
608+
end
609+
end
610+
611+
context "ignore_not_found is true" do
612+
let(:subject) do
613+
described_class.new(
614+
addr: "https://github.fake/api/v3",
615+
org: "kittensinc",
616+
token: "GoPackGo",
617+
ou: "ou=kittensinc,ou=GitHub,dc=github,dc=fake",
618+
ignore_not_found: true
619+
)
620+
end
621+
622+
it "ignores 404s" do
623+
expect(subject).to receive(:validate_team_id_and_slug!).with(1001, "russian-blues").and_return(true)
624+
expect(subject).to receive(:org_members).and_return(Set.new(%w[blackmanx]))
625+
626+
add_membership_response = {
627+
"url" => "https://github.fake/api/v3/teams/1001/memberships/blackmanx",
628+
"role" => "member",
629+
"state" => "active"
630+
}
631+
632+
stub_request(:put, "https://github.fake/api/v3/teams/1001/memberships/blackmanx")
633+
.to_return(
634+
status: 404,
635+
headers: {
636+
"Content-type" => "application/json"
637+
},
638+
body: JSON.generate({
639+
"message" => "Not Found",
640+
"documentation_url" => "https://docs.github.com/rest"
641+
})
642+
)
643+
644+
result = subject.send(:add_user_to_team, user: "blackmanx", team:)
645+
expect(result).to eq(false)
646+
end
647+
end
583648
end
584649

585650
describe "#remove_user_from_team" do

0 commit comments

Comments
 (0)