Commit 8a6787cc authored by Marc Shaw's avatar Marc Shaw

Use the approvals.size for the total approvals

This method will give us the total number of both, required
and optional approvers.

Issue: gitlab.com/gitlab-org/gitlab/-/issues/209786
Merge Request: gitlab.com/gitlab-org/gitlab/-/merge_requests/32886
parent 657e290a
...@@ -167,10 +167,9 @@ class ApprovalState ...@@ -167,10 +167,9 @@ class ApprovalState
end end
end end
def optional_approvals_count # This is the required + optional approval count
strong_memoize(:optional_approvals_count) do def total_approvals_count
wrapped_approval_rules.sum { |rule| [0, rule.approved_approvers.count - rule.approvals_required].max } approvals.size
end
end end
private private
......
...@@ -17,6 +17,7 @@ module Approvable ...@@ -17,6 +17,7 @@ module Approvable
authors_can_approve? authors_can_approve?
committers_can_approve? committers_can_approve?
approvers_overwritten? approvers_overwritten?
total_approvals_count
}.freeze }.freeze
delegate(*FORWARDABLE_METHODS, to: :approval_state) delegate(*FORWARDABLE_METHODS, to: :approval_state)
......
...@@ -1638,50 +1638,16 @@ describe ApprovalState do ...@@ -1638,50 +1638,16 @@ describe ApprovalState do
end end
end end
describe '#optional_approvals_count' do describe '#total_approvals_count' do
context 'when there are no optional approvals' do let(:rule) { create_rule(approvals_required: 1, rule_type: :any_approver, users: [approver1]) }
let(:rule) { create_rule(approvals_required: 5) }
before do
approve_rules([rule])
end
it 'returns 0' do
expect(subject.optional_approvals_count).to eq(0)
end
end
context 'when there are no approvals' do
let(:rule) { create_rule(approvals_required: 5) }
it 'returns 0' do
expect(subject.optional_approvals_count).to eq(0)
end
end
context 'when there are only optional approvals' do
let(:rule) { create_rule(approvals_required: 0) }
before do before do
create(:approval, merge_request: merge_request, user: rule.users.first) create(:approval, merge_request: merge_request, user: rule.users.first)
create(:approval, merge_request: merge_request, user: approver2)
end end
it 'returns the number of approvals' do it 'returns the total number of approvals (required + optional)' do
expect(subject.optional_approvals_count).to eq(1) expect(subject.total_approvals_count).to eq(2)
end
end
context 'when there are both required and optional approvals' do
let(:rule) { create_rule(approvals_required: 1, rule_type: :any_approver, users: [approver1, approver2]) }
before do
create(:approval, merge_request: merge_request, user: rule.users.first)
create(:approval, merge_request: merge_request, user: rule.users.second)
end
it 'returns the number of optional approvals' do
expect(subject.optional_approvals_count).to eq(1)
end
end end
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