Commit 657e290a authored by Marc Shaw's avatar Marc Shaw

Create optional_approvals_count method

Issue: gitlab.com/gitlab-org/gitlab/-/issues/209786
Merge Request: gitlab.com/gitlab-org/gitlab/-/merge_requests/32886
parent 9b9570f7
......@@ -167,6 +167,12 @@ 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
end
private
attr_reader :target_branch
......
......@@ -36,7 +36,7 @@ class ApprovalWrappedRule
filter_approvers(@approval_rule.approvers)
end
# @return [Array<User>] all approvers related to this rule
# @return [Array<User>] of users who have approved the merge request
#
# This is dynamically calculated unless it is persisted as `approved_approvers`.
#
......
......@@ -1637,4 +1637,51 @@ describe ApprovalState do
end
end
end
describe '#optional_approvals_count' do
context 'when there are no optional approvals' do
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
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
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
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