Skip to content

Commit

Permalink
Support ignoring 404s when user is not found in GitHub instance
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonmacgowan committed Dec 14, 2023
1 parent 6f5348f commit ab12625
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 2 deletions.
12 changes: 10 additions & 2 deletions lib/entitlements/backend/github_team/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -437,8 +437,16 @@ def add_user_to_team(user:, team:, role: "member")
end
Entitlements.logger.debug "#{identifier} add_user_to_team(user=#{user}, org=#{org}, team_id=#{team.team_id}, role=#{role})"
validate_team_id_and_slug!(team.team_id, team.team_name)
result = octokit.add_team_membership(team.team_id, user, role:)
result[:state] == "active" || result[:state] == "pending"

begin
result = octokit.add_team_membership(team.team_id, user, role:)
result[:state] == "active" || result[:state] == "pending"
rescue Octokit::NotFound => e
raise e unless ignore_not_found

Entitlements.logger.warn "User #{user} not found in GitHub instance #{identifier}, ignoring."
false
end
end

# Remove user from team.
Expand Down
65 changes: 65 additions & 0 deletions spec/unit/entitlements/backend/github_team/service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,71 @@
result = subject.send(:add_user_to_team, user: "blackmanx", team:)
expect(result).to eq(false)
end

context "ignore_not_found is false" do
it "raises when user is not found" do
expect(subject).to receive(:validate_team_id_and_slug!).with(1001, "russian-blues").and_return(true)
expect(subject).to receive(:org_members).and_return(Set.new(%w[blackmanx]))

add_membership_response = {
"url" => "https://github.fake/api/v3/teams/1001/memberships/blackmanx",
"role" => "member",
"state" => "active"
}

stub_request(:put, "https://github.fake/api/v3/teams/1001/memberships/blackmanx")
.to_return(
status: 404,
headers: {
"Content-Type" => "application/json"
},
body: JSON.generate({
"message" => "Not Found",
"documentation_url" => "https://docs.github.com/rest"
})
)

expect { subject.send(:add_user_to_team, user: "blackmanx", team:) }.to raise_error(Octokit::NotFound)
end
end

context "ignore_not_found is true" do
let(:subject) do
described_class.new(
addr: "https://github.fake/api/v3",
org: "kittensinc",
token: "GoPackGo",
ou: "ou=kittensinc,ou=GitHub,dc=github,dc=fake",
ignore_not_found: true
)
end

it "ignores 404s" do
expect(subject).to receive(:validate_team_id_and_slug!).with(1001, "russian-blues").and_return(true)
expect(subject).to receive(:org_members).and_return(Set.new(%w[blackmanx]))

add_membership_response = {
"url" => "https://github.fake/api/v3/teams/1001/memberships/blackmanx",
"role" => "member",
"state" => "active"
}

stub_request(:put, "https://github.fake/api/v3/teams/1001/memberships/blackmanx")
.to_return(
status: 404,
headers: {
"Content-type" => "application/json"
},
body: JSON.generate({
"message" => "Not Found",
"documentation_url" => "https://docs.github.com/rest"
})
)

result = subject.send(:add_user_to_team, user: "blackmanx", team:)
expect(result).to eq(false)
end
end
end

describe "#remove_user_from_team" do
Expand Down

0 comments on commit ab12625

Please sign in to comment.