Commit 6054912f authored by Mark Chao's avatar Mark Chao

Check for code owner rules even when no regular rules present

fallback_approvals_required should be checked in conjunction with
code owner rules (if any)
parent fe8aa11a
...@@ -43,11 +43,9 @@ class ApprovalState ...@@ -43,11 +43,9 @@ class ApprovalState
def approval_needed? def approval_needed?
return false unless project.feature_available?(:merge_request_approvers) return false unless project.feature_available?(:merge_request_approvers)
if regular_rules.empty? result = wrapped_approval_rules.any? { |rule| rule.approvals_required > 0 }
fallback_approvals_required > 0 result ||= fallback_approvals_required > 0 if regular_rules.empty?
else result
wrapped_approval_rules.any? { |rule| rule.approvals_required > 0 }
end
end end
def fallback_approvals_required def fallback_approvals_required
...@@ -56,11 +54,9 @@ class ApprovalState ...@@ -56,11 +54,9 @@ class ApprovalState
def approved? def approved?
strong_memoize(:approved) do strong_memoize(:approved) do
if regular_rules.empty? result = wrapped_approval_rules.all?(&:approved?)
approvals.size >= fallback_approvals_required result &&= approvals.size >= fallback_approvals_required if regular_rules.empty?
else result
wrapped_approval_rules.all?(&:approved?)
end
end end
end end
...@@ -70,11 +66,9 @@ class ApprovalState ...@@ -70,11 +66,9 @@ class ApprovalState
def approvals_required def approvals_required
strong_memoize(:approvals_required) do strong_memoize(:approvals_required) do
if regular_rules.empty? result = wrapped_approval_rules.sum(&:approvals_required)
[project.approvals_before_merge, merge_request.approvals_before_merge || 0].max result = [result, fallback_approvals_required].max if regular_rules.empty?
else result
wrapped_approval_rules.sum(&:approvals_required)
end
end end
end end
...@@ -82,11 +76,9 @@ class ApprovalState ...@@ -82,11 +76,9 @@ class ApprovalState
# considered approved. # considered approved.
def approvals_left def approvals_left
strong_memoize(:approvals_left) do strong_memoize(:approvals_left) do
if regular_rules.empty? result = wrapped_approval_rules.sum(&:approvals_left)
[fallback_approvals_required - approved_approvers.size, 0].max result = [result, fallback_approvals_required - approved_approvers.size].max if regular_rules.empty?
else result
wrapped_approval_rules.sum(&:approvals_left)
end
end end
end end
......
...@@ -127,64 +127,74 @@ describe ApprovalState do ...@@ -127,64 +127,74 @@ describe ApprovalState do
end end
describe '#approved?' do describe '#approved?' do
context 'when no rules' do shared_examples_for 'when rules are present' do
before do context 'when all rules are approved' do
project.update(approvals_before_merge: 1) before do
end subject.wrapped_approval_rules.each do |rule|
create(:approval, merge_request: merge_request, user: rule.users.first)
end
end
context 'when overall_approvals_required is not met' do it 'returns true' do
it 'returns false' do expect(subject.approved?).to eq(true)
expect(subject.wrapped_approval_rules.size).to eq(0)
expect(subject.approved?).to eq(false)
end end
end end
context 'when overall_approvals_required is met' do context 'when some rules are not approved' do
it 'returns true' do before do
create(:approval, merge_request: merge_request) allow(subject.wrapped_approval_rules.first).to receive(:approved?).and_return(false)
end
expect(subject.wrapped_approval_rules.size).to eq(0) it 'returns false' do
expect(subject.approved?).to eq(true) expect(subject.approved?).to eq(false)
end end
end end
end end
context 'when rules are present' do shared_examples_for 'checking fallback_approvals_required' do
before do before do
2.times { create_rule(users: [create(:user)]) } project.update(approvals_before_merge: 1)
subject.wrapped_approval_rules.each do |rule|
allow(rule).to receive(:approved?).and_return(true)
end
end end
context 'when all rules are approved' do context 'when it is not met' do
before do it 'returns false' do
subject.wrapped_approval_rules.each do |rule| expect(subject.approved?).to eq(false)
create(:approval, merge_request: merge_request, user: rule.users.first)
end
end end
end
context 'when it is met' do
it 'returns true' do it 'returns true' do
create(:approval, merge_request: merge_request)
expect(subject.approved?).to eq(true) expect(subject.approved?).to eq(true)
end end
end
end
context 'when overall_approvals_required is not met' do context 'when no rules' do
before do it_behaves_like 'checking fallback_approvals_required'
project.update(approvals_before_merge: 3) end
end
it 'returns true as overall approvals_required is ignored' do context 'when only code owner rules present' do
expect(subject.approved?).to eq(true) before do
end 2.times { create_rule(users: [create(:user)], code_owner: true) }
end
end end
context 'when some rules are not approved' do it_behaves_like 'when rules are present'
before do it_behaves_like 'checking fallback_approvals_required'
allow(subject.wrapped_approval_rules.first).to receive(:approved?).and_return(false) end
end
it 'returns false' do context 'when regular rules present' do
expect(subject.approved?).to eq(false) before do
end project.update(approvals_before_merge: 999)
2.times { create_rule(users: [create(:user)]) }
end end
it_behaves_like 'when rules are present'
end end
end end
...@@ -673,64 +683,74 @@ describe ApprovalState do ...@@ -673,64 +683,74 @@ describe ApprovalState do
end end
describe '#approved?' do describe '#approved?' do
context 'when no rules' do shared_examples_for 'when rules are present' do
before do context 'when all rules are approved' do
project.update(approvals_before_merge: 1) before do
end subject.wrapped_approval_rules.each do |rule|
create(:approval, merge_request: merge_request, user: rule.users.first)
end
end
context 'when overall_approvals_required is not met' do it 'returns true' do
it 'returns false' do expect(subject.approved?).to eq(true)
expect(subject.wrapped_approval_rules.size).to eq(0)
expect(subject.approved?).to eq(false)
end end
end end
context 'when overall_approvals_required is met' do context 'when some rules are not approved' do
it 'returns true' do before do
create(:approval, merge_request: merge_request) allow(subject.wrapped_approval_rules.first).to receive(:approved?).and_return(false)
end
expect(subject.wrapped_approval_rules.size).to eq(0) it 'returns false' do
expect(subject.approved?).to eq(true) expect(subject.approved?).to eq(false)
end end
end end
end end
context 'when rules are present' do shared_examples_for 'checking fallback_approvals_required' do
before do before do
2.times { create_rule(users: [create(:user)]) } project.update(approvals_before_merge: 1)
subject.wrapped_approval_rules.each do |rule|
allow(rule).to receive(:approved?).and_return(true)
end
end end
context 'when all rules are approved' do context 'when it is not met' do
before do it 'returns false' do
subject.wrapped_approval_rules.each do |rule| expect(subject.approved?).to eq(false)
create(:approval, merge_request: merge_request, user: rule.users.first)
end
end end
end
context 'when it is met' do
it 'returns true' do it 'returns true' do
create(:approval, merge_request: merge_request)
expect(subject.approved?).to eq(true) expect(subject.approved?).to eq(true)
end end
end
end
context 'when overall_approvals_required is not met' do context 'when no rules' do
before do it_behaves_like 'checking fallback_approvals_required'
project.update(approvals_before_merge: 3) end
end
it 'returns true as overall approvals_required is ignored' do context 'when only code owner rules present' do
expect(subject.approved?).to eq(true) before do
end 2.times { create_rule(users: [create(:user)], code_owner: true) }
end
end end
context 'when some rules are not approved' do it_behaves_like 'when rules are present'
before do it_behaves_like 'checking fallback_approvals_required'
allow(subject.wrapped_approval_rules.first).to receive(:approved?).and_return(false) end
end
it 'returns false' do context 'when regular rules present' do
expect(subject.approved?).to eq(false) before do
end project.update(approvals_before_merge: 999)
2.times { create_rule(users: [create(:user)]) }
end end
it_behaves_like 'when rules are present'
end end
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