Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix destroying of team member not working #7000

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions app/commands/github/team_member/destroy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,16 @@ class Github::TeamMember::Destroy
initialize_with :user_id, :team_name

def call
::Github::TeamMember.where(user_id:, team_name:).destroy_all.tap do
User::UpdateMaintainer.(user) if user
end
return unless team_member

team_member.destroy
User::UpdateMaintainer.(user) if user
end

private
memoize
def team_member = ::Github::TeamMember.find_by(user_id:, team_name:)

memoize
def user = User.find_by(uid: user_id)
end
2 changes: 1 addition & 1 deletion app/commands/github/team_member/sync_members.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def add_team_members!

def delete_team_members!
Github::TeamMember.find_each do |team_member|
next if org_team_members[team_member.team_name].include?(team_member.user_id)
next if org_team_members[team_member.team_name].to_a.include?(team_member.user_id)

Github::TeamMember::Destroy.(team_member.user_id, team_member.team_name)
end
Expand Down
9 changes: 3 additions & 6 deletions app/models/github/team_member.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
class Github::TeamMember < ApplicationRecord
has_one :user,
foreign_key: :uid,
primary_key: :user_id,
class_name: "User",
inverse_of: :github_team_memberships,
dependent: :destroy
belongs_to :user,
primary_key: :uid,
inverse_of: :github_team_memberships
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is right. Looking at the schema, there is a user_id column. So the primary key is the user_id column on this table.

We can remove the user_id column, in which case we need to remove that column and add a uid column instead. But I feel like we should just be referencing a normal user_id instinctively.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would be better, as user_id is currently actually the uid (it's named user ID in github).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The original idea was to decouple the table as we might want to query non-DB users, but that hasn't really happend. I'll do a separate PR to fix this. I'll also check the organization members.

end
3 changes: 3 additions & 0 deletions test/commands/github/team_member/create_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ class Github::TeamMember::CreateTest < ActiveSupport::TestCase
test "creates team member" do
user_id = '137131'
team_name = 'fsharp'
create(:user, uid: user_id)

team_name_member = Github::TeamMember::Create.(user_id, team_name)

Expand All @@ -30,6 +31,8 @@ class Github::TeamMember::CreateTest < ActiveSupport::TestCase
user_id = '137131'
team_name = 'fsharp'

create(:user, uid: user_id)

assert_idempotent_command do
Github::TeamMember::Create.(user_id, team_name)
end
Expand Down
20 changes: 19 additions & 1 deletion test/commands/github/team_member/destroy_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class Github::TeamMember::DestroyTest < ActiveSupport::TestCase
user_id = '137131'
team_name = 'fsharp'

create(:user, uid: user_id)
create(:github_team_member, team_name:, user_id:)

# Sanity check
Expand All @@ -15,13 +16,29 @@ class Github::TeamMember::DestroyTest < ActiveSupport::TestCase
refute Github::TeamMember.where(user_id:, team_name:).exists?
end

test "update maintainer role" do
test "does not remove user" do
user_id = '137131'
team_name = 'fsharp'

user = create(:user, uid: user_id)
create(:github_team_member, team_name:, user_id:)

# Sanity check
assert Github::TeamMember.where(user_id:, team_name:).exists?

Github::TeamMember::Destroy.(user_id, team_name)

refute Github::TeamMember.where(user_id:, team_name:).exists?
assert User.where(id: user.id).exists?
end

test "update maintainer role" do
user_id = '137131'
team_name = 'fsharp'

user = create(:user, uid: user_id)
create(:github_team_member, team_name:, user_id:)

User::UpdateMaintainer.expects(:call).with(user).once

Github::TeamMember::Destroy.(user_id, team_name)
Expand All @@ -30,6 +47,7 @@ class Github::TeamMember::DestroyTest < ActiveSupport::TestCase
test "idempotent" do
user_id = '137131'
team_name = 'fsharp'
create(:user, uid: user_id)

assert_idempotent_command do
Github::TeamMember::Destroy.(user_id, team_name)
Expand Down
26 changes: 26 additions & 0 deletions test/commands/github/team_member/sync_members_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ class Github::TeamMember::SyncMembersTest < ActiveSupport::TestCase
team_members = { 'ruby' => [12_412, 82_462], 'fsharp' => [12_412, 56_653] }
Github::Organization.any_instance.stubs(:team_members).returns(team_members)

create(:user, uid: '12412')
create(:user, uid: '82462')
create(:user, uid: '56653')
Github::TeamMember::Destroy.expects(:call).never

Github::TeamMember::SyncMembers.()

assert ::Github::TeamMember.where(team_name: 'ruby', user_id: '12412').exists?
Expand All @@ -17,6 +22,7 @@ class Github::TeamMember::SyncMembersTest < ActiveSupport::TestCase
team_members = { 'ruby' => [12_412] }
Github::Organization.any_instance.stubs(:team_members).returns(team_members)

create(:user, uid: '12412')
create :github_team_member, team_name: 'ruby', user_id: '12412'

Github::TeamMember::SyncMembers.()
Expand All @@ -29,6 +35,9 @@ class Github::TeamMember::SyncMembersTest < ActiveSupport::TestCase
team_members = { 'ruby' => [82_462], 'fsharp' => [12_412] }
Github::Organization.any_instance.stubs(:team_members).returns(team_members)

create(:user, uid: '56653')
create(:user, uid: '82462')
create(:user, uid: '12412')
create :github_team_member, team_name: 'ruby', user_id: '56653'

Github::TeamMember::SyncMembers.()
Expand All @@ -38,4 +47,21 @@ class Github::TeamMember::SyncMembersTest < ActiveSupport::TestCase
assert ::Github::TeamMember.where(team_name: 'ruby', user_id: '82462').exists?
assert ::Github::TeamMember.where(team_name: 'fsharp', user_id: '12412').exists?
end

test "delete when group does not exist" do
team_members = { 'ruby' => [82_462], 'fsharp' => [12_412] }
Github::Organization.any_instance.stubs(:team_members).returns(team_members)

create(:user, uid: '12412')
create(:user, uid: '82462')
create(:user, uid: '56653')
create :github_team_member, team_name: 'prolog', user_id: '56653'

Github::TeamMember::SyncMembers.()

assert_equal 2, ::Github::TeamMember.count
refute ::Github::TeamMember.where(team_name: 'prolog', user_id: '56653').exists?
assert ::Github::TeamMember.where(team_name: 'ruby', user_id: '82462').exists?
assert ::Github::TeamMember.where(team_name: 'fsharp', user_id: '12412').exists?
end
end
3 changes: 3 additions & 0 deletions test/commands/webhooks/process_membership_update_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ class Webhooks::ProcessMembershipUpdateTest < ActiveSupport::TestCase
team_name = 'team11'
org = 'exercism'

create(:user, uid: user_id)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you also need a provider here?


Github::Organization.any_instance.stubs(:name).returns(org)

Webhooks::ProcessMembershipUpdate.('added', user_id, team_name, org)
Expand All @@ -17,6 +19,7 @@ class Webhooks::ProcessMembershipUpdateTest < ActiveSupport::TestCase
user_id = 12_348_521
team_name = 'team11'
org = 'exercism'
create(:user, uid: user_id)
create(:github_team_member, user_id:, team_name:)

Github::Organization.any_instance.stubs(:name).returns(org)
Expand Down
2 changes: 1 addition & 1 deletion test/factories/github/team_members.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
FactoryBot.define do
factory :github_team_member, class: 'Github::TeamMember' do
user_id { SecureRandom.hex }
user { create(:user, uid: SecureRandom.hex) }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
user { create(:user, uid: SecureRandom.hex) }
user

I'm not sure why you need to specify uidhere. I think just a normal user is fine.

team_name { SecureRandom.hex }
end
end
10 changes: 6 additions & 4 deletions test/models/github/team_member_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

class Github::TeamMemberTest < ActiveSupport::TestCase
test "user" do
team_member = create :github_team_member
assert_nil team_member.user
uid = "123"
team_name = "ruby"
user = create(:user, uid:)

user = create :user, uid: team_member.user_id
assert_equal user, team_member.reload.user
team_member = create(:github_team_member, user_id: uid, team_name:)
assert_equal user, team_member.user
assert_includes user.github_team_memberships, team_member
end
end
Loading