diff options
author | Mark Chao <mchao@gitlab.com> | 2018-09-26 11:47:24 +0800 |
---|---|---|
committer | Mark Chao <mchao@gitlab.com> | 2018-10-01 18:12:46 +0800 |
commit | c50fc090a5cc44304b14b0509c7e111d342b3bdc (patch) | |
tree | 6ec68a11f2939e7597884b9c63ba659f8e99cd7c | |
parent | 6e6aa12ebeeaca235406202e690a5b740e17152b (diff) | |
download | gitlab-ce-c50fc090a5cc44304b14b0509c7e111d342b3bdc.tar.gz |
Add Project#members_among to obtain only authorized users of the projectce-5382-approvers-from-code-owners
-rw-r--r-- | app/models/project.rb | 12 | ||||
-rw-r--r-- | spec/models/project_spec.rb | 43 |
2 files changed, 55 insertions, 0 deletions
diff --git a/app/models/project.rb b/app/models/project.rb index 503fbc30768..59f088156c7 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -1365,6 +1365,18 @@ class Project < ActiveRecord::Base end end + # Filters `users` to return only authorized users of the project + def members_among(users) + if users.is_a?(ActiveRecord::Relation) && !users.loaded? + authorized_users.merge(users) + else + return [] if users.empty? + + user_ids = authorized_users.where(users: { id: users.map(&:id) }).pluck(:id) + users.select { |user| user_ids.include?(user.id) } + end + end + def default_branch @default_branch ||= repository.root_ref if repository.exists? end diff --git a/spec/models/project_spec.rb b/spec/models/project_spec.rb index afc9ea1917e..d15ba89efb5 100644 --- a/spec/models/project_spec.rb +++ b/spec/models/project_spec.rb @@ -3995,6 +3995,49 @@ describe Project do end end + context '#members_among' do + let(:users) { create_list(:user, 3) } + set(:group) { create(:group) } + set(:project) { create(:project, namespace: group) } + + before do + project.add_guest(users.first) + project.group.add_maintainer(users.last) + end + + context 'when users is an Array' do + it 'returns project members among the users' do + expect(project.members_among(users)).to eq([users.first, users.last]) + end + + it 'maintains input order' do + expect(project.members_among(users.reverse)).to eq([users.last, users.first]) + end + + it 'returns empty array if users is empty' do + result = project.members_among([]) + + expect(result).to be_empty + end + end + + context 'when users is a relation' do + it 'returns project members among the users' do + result = project.members_among(User.where(id: users.map(&:id))) + + expect(result).to be_a(ActiveRecord::Relation) + expect(result).to eq([users.first, users.last]) + end + + it 'returns empty relation if users is empty' do + result = project.members_among(User.none) + + expect(result).to be_a(ActiveRecord::Relation) + expect(result).to be_empty + end + end + end + def rugged_config Gitlab::GitalyClient::StorageSettings.allow_disk_access do project.repository.rugged.config |