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
end
end
def optional_approvals_count
strong_memoize(:optional_approvals_count) do
wrapped_approval_rules.sum { |rule| [0, rule.approved_approvers.count - rule.approvals_required].max }
end
# This is the required + optional approval count
def total_approvals_count
approvals.size
end
private
......
......@@ -17,6 +17,7 @@ module Approvable
authors_can_approve?
committers_can_approve?
approvers_overwritten?
total_approvals_count
}.freeze
delegate(*FORWARDABLE_METHODS, to: :approval_state)
......
......@@ -1638,50 +1638,16 @@ describe ApprovalState do
end
end
describe '#optional_approvals_count' do
context 'when there are no optional approvals' do
let(:rule) { create_rule(approvals_required: 5) }
describe '#total_approvals_count' do
let(:rule) { create_rule(approvals_required: 1, rule_type: :any_approver, users: [approver1]) }
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
create(:approval, merge_request: merge_request, user: rule.users.first)
end
it 'returns the number of approvals' do
expect(subject.optional_approvals_count).to eq(1)
end
before do
create(:approval, merge_request: merge_request, user: rule.users.first)
create(:approval, merge_request: merge_request, user: approver2)
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
it 'returns the total number of approvals (required + optional)' do
expect(subject.total_approvals_count).to eq(2)
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