debian-mirror-gitlab/app/serializers/merge_request_user_entity.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

45 lines
1.5 KiB
Ruby
Raw Normal View History

2018-11-18 11:00:15 +05:30
# frozen_string_literal: true
2021-02-22 17:27:13 +05:30
class MergeRequestUserEntity < ::API::Entities::UserBasic
2021-03-11 19:13:27 +05:30
include UserStatusTooltip
include RequestAwareEntity
2021-04-17 20:07:23 +05:30
def self.satisfies(*methods)
->(_, options) { methods.all? { |m| options[:merge_request].try(m) } }
end
2021-02-22 17:27:13 +05:30
expose :can_merge do |reviewer, options|
options[:merge_request]&.can_be_merged_by?(reviewer)
2018-11-08 19:23:39 +05:30
end
2021-03-11 19:13:27 +05:30
expose :can_update_merge_request do |reviewer, options|
request.current_user&.can?(:update_merge_request, options[:merge_request])
end
2021-12-11 22:18:48 +05:30
expose :reviewed, if: satisfies(:present?, :allows_reviewers?) do |user, options|
find_reviewer_or_assignee(user, options)&.reviewed?
end
2021-03-11 19:13:27 +05:30
2022-07-16 23:28:13 +05:30
expose :attention_requested, if: ->(_, options) { options[:merge_request].present? && options[:merge_request].allows_reviewers? && request.current_user&.mr_attention_requests_enabled? } do |user, options|
2021-12-11 22:18:48 +05:30
find_reviewer_or_assignee(user, options)&.attention_requested?
2021-03-11 19:13:27 +05:30
end
2021-04-17 20:07:23 +05:30
expose :approved, if: satisfies(:present?) do |user, options|
# This approach is preferred over MergeRequest#approved_by? since this
# makes one query per merge request, whereas #approved_by? makes one per user
options[:merge_request].approvals.any? { |app| app.user_id == user.id }
end
2021-12-11 22:18:48 +05:30
private
def find_reviewer_or_assignee(user, options)
if options[:type] == :reviewers
options[:merge_request].find_reviewer(user)
else
options[:merge_request].find_assignee(user)
end
end
2018-11-08 19:23:39 +05:30
end
2021-02-22 17:27:13 +05:30
2021-06-08 01:23:25 +05:30
MergeRequestUserEntity.prepend_mod_with('MergeRequestUserEntity')