Commit 333c6edc authored by Mayra Cabrera's avatar Mayra Cabrera

Merge branch 'fix/bots-for-group-ee-license' into 'master'

Exempt bot users for groups from license seat usage

See merge request gitlab-org/gitlab!79101
parents fc572400 9184bcd0
......@@ -578,7 +578,7 @@ module EE
def billed_user_ids_excluding_guests
strong_memoize(:billed_user_ids_excluding_guests) do
group_member_user_ids = billed_group_members.non_guests.distinct.pluck(:user_id)
group_member_user_ids = billed_group_users(non_guests: true).distinct.pluck(:id)
project_member_user_ids = billed_project_users(non_guests: true).distinct.pluck(:id)
shared_group_user_ids = billed_shared_non_guests_group_members.non_guests.distinct.pluck(:user_id)
shared_project_user_ids = billed_invited_non_guests_group_to_project_members.non_guests.distinct.pluck(:user_id)
......@@ -595,7 +595,7 @@ module EE
def billed_user_ids_including_guests
strong_memoize(:billed_user_ids_including_guests) do
group_member_user_ids = billed_group_members.distinct.pluck(:user_id)
group_member_user_ids = billed_group_users.distinct.pluck(:id)
project_member_user_ids = billed_project_users.distinct.pluck(:id)
shared_group_user_ids = billed_shared_group_members.distinct.pluck(:user_id)
shared_project_user_ids = billed_invited_group_to_project_members.distinct.pluck(:user_id)
......@@ -611,10 +611,16 @@ module EE
end
# Members belonging directly to Group or its subgroups
def billed_group_members
::GroupMember.active_without_invites_and_requests.where(
def billed_group_users(non_guests: false)
members = ::GroupMember.active_without_invites_and_requests.where(
source_id: self_and_descendants
)
members = members.non_guests if non_guests
user_ids = members.distinct.select(:user_id)
::User.without_project_bot.where(id: user_ids)
end
# Members belonging directly to Projects within Group or Projects within subgroups
......
......@@ -913,7 +913,7 @@ RSpec.describe Namespace do
end
end
shared_context 'project bot users' do
shared_context 'bot user for project' do
let(:project_bot) { create(:user, :project_bot) }
before do
......@@ -921,6 +921,14 @@ RSpec.describe Namespace do
end
end
shared_context 'bot user for group' do
let(:group_bot) { create(:user, :project_bot) }
before do
group.add_maintainer(group_bot)
end
end
describe '#billed_user_ids', :saas do
context 'with a user namespace' do
let(:user) { create(:user) }
......@@ -989,13 +997,20 @@ RSpec.describe Namespace do
expect(billed_user_ids[:shared_project_user_ids]).to match_array([])
end
context 'with project bot users' do
include_context 'project bot users'
context 'with bot users for project' do
include_context 'bot user for project'
it { expect(billed_user_ids[:user_ids]).not_to include(project_bot.id) }
it { expect(billed_user_ids[:project_member_user_ids]).not_to include(project_bot.id) }
end
context 'with bot users for group' do
include_context 'bot user for group'
it { expect(billed_user_ids[:user_ids]).not_to include(group_bot.id) }
it { expect(billed_user_ids[:group_member_user_ids]).not_to include(group_bot.id) }
end
context 'when group is invited to the project' do
let(:invited_group) { create(:group) }
let(:invited_group_developer) { create(:user) }
......@@ -1122,13 +1137,20 @@ RSpec.describe Namespace do
expect(billed_user_ids[:project_member_user_ids]).to match_array([developer.id, project_guest.id, project_developer.id])
end
context 'with project bot users' do
include_context 'project bot users'
context 'with bot users for project' do
include_context 'bot user for project'
it { expect(billed_user_ids[:user_ids]).not_to include(project_bot.id) }
it { expect(billed_user_ids[:project_member_user_ids]).not_to include(project_bot.id) }
end
context 'with bot users for group' do
include_context 'bot user for group'
it { expect(billed_user_ids[:user_ids]).not_to include(group_bot.id) }
it { expect(billed_user_ids[:group_member_user_ids]).not_to include(group_bot.id) }
end
context 'when group is invited to the project' do
let(:invited_group) { create(:group) }
let(:invited_group_developer) { create(:user) }
......@@ -1232,10 +1254,11 @@ RSpec.describe Namespace do
expect(group.billable_members_count).to eq(2)
end
context 'with project bot users' do
include_context 'project bot users'
context 'with bot users for project and group' do
include_context 'bot user for project'
include_context 'bot user for group'
it 'does not include project bot users in the count' do
it 'does not include bot users in the count' do
expect(group.billable_members_count).to eq(2)
end
end
......@@ -1297,10 +1320,11 @@ RSpec.describe Namespace do
expect(group.billable_members_count).to eq(4)
end
context 'with project bot users' do
include_context 'project bot users'
context 'with bot users for project and group' do
include_context 'bot user for project'
include_context 'bot user for group'
it 'does not include project bot users in the count' do
it 'does not include bot users in the count' do
expect(group.billable_members_count).to eq(4)
end
end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment