Commit b0d0889c authored by Kerri Miller's avatar Kerri Miller

Merge branch '293032-return-either-project-or-mr-approval-rules-in-users-request' into 'master'

Return either project or MR approval rules in users request

See merge request gitlab-org/gitlab!50139
parents ea6bfaa8 afa6d080
......@@ -17,6 +17,7 @@ module Approvable
committers_can_approve?
approvers_overwritten?
total_approvals_count
wrapped_approval_rules
}.freeze
delegate(*FORWARDABLE_METHODS, to: :approval_state)
......
......@@ -275,7 +275,7 @@ module EE
end
def applicable_approval_rules_for_user(user_id)
approval_rules.applicable_to_branch(target_branch).select do |rule|
wrapped_approval_rules.select do |rule|
rule.approvers.pluck(:id).include?(user_id)
end
end
......
......@@ -7,6 +7,10 @@ RSpec.describe UserSerializer do
let_it_be(:user2) { create(:user) }
let_it_be(:merge_request) { create(:merge_request) }
let_it_be(:project) { merge_request.project }
let_it_be(:protected_branch) { create(:protected_branch, project: project, name: merge_request.target_branch) }
let_it_be(:approval_project_rule) do
create(:approval_project_rule, name: 'Project Rule', project: project, users: [user1], protected_branches: [protected_branch])
end
let(:serializer) { described_class.new(options) }
......@@ -33,20 +37,41 @@ RSpec.describe UserSerializer do
context 'with approval_rules' do
let(:options) { super().merge(approval_rules: 'true') }
let!(:approval_merge_request_rule) do
create(:approval_merge_request_rule, name: 'Merge Request Rule', merge_request: merge_request, users: [user1])
context 'with merge request approval rules' do
let!(:approval_merge_request_rule) do
create(:approval_merge_request_rule, name: 'Merge Request Rule', merge_request: merge_request, users: [user1])
end
let!(:approval_code_owner_rule) do
create(:code_owner_rule, name: 'Code Owner Rule', merge_request: merge_request, users: [user1])
end
it 'returns users with merge request approval rules' do
serialized_user1, serialized_user2 = serializer.represent([user1, user2], project: project).as_json
expect(serialized_user1).to include(
'id' => user1.id,
'applicable_approval_rules' => [
{ 'id' => approval_merge_request_rule.id, 'name' => 'Merge Request Rule', 'rule_type' => 'regular' },
{ 'id' => approval_code_owner_rule.id, 'name' => 'Code Owner Rule', 'rule_type' => 'code_owner' }
]
)
expect(serialized_user2).to include('id' => user2.id, 'applicable_approval_rules' => [])
end
end
it 'returns users with applicable_approval_rules' do
serialized_user1, serialized_user2 = serializer.represent([user1, user2], project: project).as_json
context 'without merge request approval rules' do
it 'returns users with project approval rules' do
serialized_user1, serialized_user2 = serializer.represent([user1, user2], project: project).as_json
expect(serialized_user1).to include(
'id' => user1.id,
'applicable_approval_rules' => [
{ 'id' => approval_merge_request_rule.id, 'name' => 'Merge Request Rule', 'rule_type' => 'regular' }
]
)
expect(serialized_user2).to include('id' => user2.id, 'applicable_approval_rules' => [])
expect(serialized_user1).to include(
'id' => user1.id,
'applicable_approval_rules' => [
{ 'id' => approval_project_rule.id, 'name' => 'Project Rule', 'rule_type' => 'regular' }
]
)
expect(serialized_user2).to include('id' => user2.id, 'applicable_approval_rules' => [])
end
end
end
end
......@@ -61,11 +86,6 @@ RSpec.describe UserSerializer do
context 'with approval_rules' do
let(:options) { super().merge(approval_rules: 'true') }
let!(:protected_branch) { create(:protected_branch, project: project, name: 'my_branch') }
let!(:approval_project_rule) do
create(:approval_project_rule, name: 'Project Rule', project: project, users: [user1], protected_branches: [protected_branch])
end
it 'returns users with applicable_approval_rules' do
serialized_user1, serialized_user2 = serializer.represent([user1, user2], project: project).as_json
......@@ -79,7 +99,7 @@ RSpec.describe UserSerializer do
end
context 'with target_branch' do
let(:options) { super().merge(target_branch: 'my_branch') }
let(:options) { super().merge(target_branch: protected_branch.name) }
it 'returns users with applicable_approval_rules' do
serialized_user1, serialized_user2 = serializer.represent([user1, user2], project: project).as_json
......
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