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

290 lines
9.2 KiB
Ruby
Raw Normal View History

2018-11-18 11:00:15 +05:30
# frozen_string_literal: true
2018-03-17 18:26:18 +05:30
class MergeRequestWidgetEntity < IssuableEntity
expose :state
2017-08-17 22:00:37 +05:30
expose :in_progress_merge_commit_sha
expose :merge_commit_sha
2018-10-15 14:42:47 +05:30
expose :short_merge_commit_sha
2017-08-17 22:00:37 +05:30
expose :merge_error
expose :merge_params
expose :merge_status
expose :merge_user_id
expose :merge_when_pipeline_succeeds
expose :source_branch
2019-03-02 22:35:43 +05:30
expose :source_branch_protected do |merge_request|
merge_request.source_project.present? && ProtectedBranch.protected?(merge_request.source_project, merge_request.source_branch)
end
2017-08-17 22:00:37 +05:30
expose :source_project_id
2018-11-18 11:00:15 +05:30
expose :source_project_full_path do |merge_request|
merge_request.source_project&.full_path
end
2018-11-08 19:23:39 +05:30
expose :squash
2017-08-17 22:00:37 +05:30
expose :target_branch
expose :target_project_id
2018-11-18 11:00:15 +05:30
expose :target_project_full_path do |merge_request|
merge_request.project&.full_path
end
2018-11-08 19:23:39 +05:30
expose :allow_collaboration
2017-08-17 22:00:37 +05:30
2018-03-17 18:26:18 +05:30
expose :should_be_rebased?, as: :should_be_rebased
expose :ff_only_enabled do |merge_request|
merge_request.project.merge_requests_ff_only_enabled
end
expose :metrics do |merge_request|
metrics = build_metrics(merge_request)
MergeRequestMetricsEntity.new(metrics).as_json
end
expose :rebase_commit_sha
expose :rebase_in_progress?, as: :rebase_in_progress
expose :can_push_to_source_branch do |merge_request|
presenter(merge_request).can_push_to_source_branch?
end
2018-03-27 19:54:05 +05:30
2018-03-17 18:26:18 +05:30
expose :rebase_path do |merge_request|
presenter(merge_request).rebase_path
end
2017-08-17 22:00:37 +05:30
# User entities
expose :merge_user, using: UserEntity
# Diff sha's
expose :diff_head_sha do |merge_request|
2018-03-27 19:54:05 +05:30
merge_request.diff_head_sha.presence
2017-08-17 22:00:37 +05:30
end
2019-02-02 18:00:53 +05:30
expose :actual_head_pipeline, with: PipelineDetailsEntity, as: :pipeline, if: -> (mr, _) { presenter(mr).can_read_pipeline? }
2019-03-02 22:35:43 +05:30
2018-12-13 13:39:08 +05:30
expose :merge_pipeline, with: PipelineDetailsEntity, if: ->(mr, _) { mr.merged? && can?(request.current_user, :read_pipeline, mr.target_project)}
2017-08-17 22:00:37 +05:30
2019-03-02 22:35:43 +05:30
expose :default_squash_commit_message
expose :default_merge_commit_message
expose :default_merge_commit_message_with_description do |merge_request|
merge_request.default_merge_commit_message(include_description: true)
end
expose :commits_without_merge_commits, using: MergeRequestWidgetCommitEntity do |merge_request|
merge_request.commits.without_merge_commits
end
expose :commits_count
2017-08-17 22:00:37 +05:30
# Booleans
2017-09-10 17:25:29 +05:30
expose :merge_ongoing?, as: :merge_ongoing
2017-08-17 22:00:37 +05:30
expose :work_in_progress?, as: :work_in_progress
expose :source_branch_exists?, as: :source_branch_exists
2018-03-17 18:26:18 +05:30
expose :mergeable_discussions_state?, as: :mergeable_discussions_state do |merge_request|
# This avoids calling MergeRequest#mergeable_discussions_state without
# considering the state of the MR first. If a MR isn't mergeable, we can
# safely short-circuit it.
if merge_request.mergeable_state?(skip_ci_check: true, skip_discussions_check: true)
merge_request.mergeable_discussions_state?
else
false
end
end
2017-08-17 22:00:37 +05:30
expose :branch_missing?, as: :branch_missing
expose :cannot_be_merged?, as: :has_conflicts
expose :can_be_merged?, as: :can_be_merged
2018-03-17 18:26:18 +05:30
expose :mergeable?, as: :mergeable
2017-08-17 22:00:37 +05:30
expose :remove_source_branch?, as: :remove_source_branch
expose :project_archived do |merge_request|
merge_request.project.archived?
end
expose :only_allow_merge_if_pipeline_succeeds do |merge_request|
merge_request.project.only_allow_merge_if_pipeline_succeeds?
end
# CI related
expose :has_ci?, as: :has_ci
expose :ci_status do |merge_request|
presenter(merge_request).ci_status
end
expose :issues_links do
expose :assign_to_closing do |merge_request|
presenter(merge_request).assign_to_closing_issues_link
end
expose :closing do |merge_request|
presenter(merge_request).closing_issues_links
end
expose :mentioned_but_not_closing do |merge_request|
presenter(merge_request).mentioned_issues_links
end
end
expose :source_branch_with_namespace_link do |merge_request|
presenter(merge_request).source_branch_with_namespace_link
end
expose :source_branch_path do |merge_request|
presenter(merge_request).source_branch_path
end
expose :current_user do
expose :can_remove_source_branch do |merge_request|
2018-11-08 19:23:39 +05:30
presenter(merge_request).can_remove_source_branch?
2017-08-17 22:00:37 +05:30
end
expose :can_revert_on_current_merge_request do |merge_request|
presenter(merge_request).can_revert_on_current_merge_request?
end
expose :can_cherry_pick_on_current_merge_request do |merge_request|
presenter(merge_request).can_cherry_pick_on_current_merge_request?
end
2018-03-27 19:54:05 +05:30
2018-11-08 19:23:39 +05:30
expose :can_create_note do |merge_request|
can?(request.current_user, :create_note, merge_request)
2018-03-27 19:54:05 +05:30
end
2018-11-18 11:00:15 +05:30
expose :can_create_issue do |merge_request|
can?(current_user, :create_issue, merge_request.project)
end
2018-11-08 19:23:39 +05:30
expose :can_update do |merge_request|
can?(request.current_user, :update_merge_request, merge_request)
2018-03-27 19:54:05 +05:30
end
2017-08-17 22:00:37 +05:30
end
# Paths
#
expose :target_branch_commits_path do |merge_request|
presenter(merge_request).target_branch_commits_path
end
2017-09-10 17:25:29 +05:30
expose :target_branch_tree_path do |merge_request|
presenter(merge_request).target_branch_tree_path
end
expose :new_blob_path do |merge_request|
2018-03-27 19:54:05 +05:30
if presenter(merge_request).can_push_to_source_branch?
project_new_blob_path(merge_request.source_project, merge_request.source_branch)
2017-09-10 17:25:29 +05:30
end
end
2017-08-17 22:00:37 +05:30
expose :conflict_resolution_path do |merge_request|
presenter(merge_request).conflict_resolution_path
end
expose :remove_wip_path do |merge_request|
presenter(merge_request).remove_wip_path
end
expose :cancel_merge_when_pipeline_succeeds_path do |merge_request|
presenter(merge_request).cancel_merge_when_pipeline_succeeds_path
end
expose :create_issue_to_resolve_discussions_path do |merge_request|
presenter(merge_request).create_issue_to_resolve_discussions_path
end
expose :merge_path do |merge_request|
presenter(merge_request).merge_path
end
expose :cherry_pick_in_fork_path do |merge_request|
presenter(merge_request).cherry_pick_in_fork_path
end
expose :revert_in_fork_path do |merge_request|
presenter(merge_request).revert_in_fork_path
end
expose :email_patches_path do |merge_request|
2017-09-10 17:25:29 +05:30
project_merge_request_path(merge_request.project, merge_request, format: :patch)
2017-08-17 22:00:37 +05:30
end
expose :plain_diff_path do |merge_request|
2017-09-10 17:25:29 +05:30
project_merge_request_path(merge_request.project, merge_request, format: :diff)
2017-08-17 22:00:37 +05:30
end
expose :status_path do |merge_request|
2017-09-10 17:25:29 +05:30
project_merge_request_path(merge_request.target_project, merge_request, format: :json)
2017-08-17 22:00:37 +05:30
end
expose :ci_environments_status_path do |merge_request|
2017-09-10 17:25:29 +05:30
ci_environments_status_project_merge_request_path(merge_request.project, merge_request)
2017-08-17 22:00:37 +05:30
end
expose :diverged_commits_count do |merge_request|
if merge_request.open? && merge_request.diverged_from_target_branch?
merge_request.diverged_commits_count
else
0
end
end
2018-03-27 19:54:05 +05:30
expose :create_note_path do |merge_request|
project_notes_path(merge_request.project, target_type: 'merge_request', target_id: merge_request.id)
end
2017-08-17 22:00:37 +05:30
expose :commit_change_content_path do |merge_request|
2017-09-10 17:25:29 +05:30
commit_change_content_project_merge_request_path(merge_request.project, merge_request)
2017-08-17 22:00:37 +05:30
end
2018-11-08 19:23:39 +05:30
expose :preview_note_path do |merge_request|
2018-12-05 23:21:45 +05:30
preview_markdown_path(merge_request.project, quick_actions_target_type: 'MergeRequest', quick_actions_target_id: merge_request.iid)
2018-11-08 19:23:39 +05:30
end
2018-10-15 14:42:47 +05:30
expose :merge_commit_path do |merge_request|
if merge_request.merge_commit_sha
project_commit_path(merge_request.project, merge_request.merge_commit_sha)
end
end
2018-11-18 11:00:15 +05:30
expose :test_reports_path do |merge_request|
if merge_request.has_test_reports?
test_reports_project_merge_request_path(merge_request.project, merge_request, format: :json)
end
end
2019-02-15 15:39:39 +05:30
expose :supports_suggestion?, as: :can_receive_suggestion
2019-03-02 22:35:43 +05:30
expose :conflicts_docs_path do |merge_request|
presenter(merge_request).conflicts_docs_path
end
2017-08-17 22:00:37 +05:30
private
delegate :current_user, to: :request
def presenter(merge_request)
@presenters ||= {}
2018-12-05 23:21:45 +05:30
@presenters[merge_request] ||= MergeRequestPresenter.new(merge_request, current_user: current_user) # rubocop: disable CodeReuse/Presenter
2017-08-17 22:00:37 +05:30
end
2018-03-17 18:26:18 +05:30
# Once SchedulePopulateMergeRequestMetricsWithEventsData fully runs,
# we can remove this method and just serialize MergeRequest#metrics
# instead. See https://gitlab.com/gitlab-org/gitlab-ce/issues/41587
def build_metrics(merge_request)
# There's no need to query and serialize metrics data for merge requests that are not
# merged or closed.
return unless merge_request.merged? || merge_request.closed?
return merge_request.metrics if merge_request.merged? && merge_request.metrics&.merged_by_id
return merge_request.metrics if merge_request.closed? && merge_request.metrics&.latest_closed_by_id
build_metrics_from_events(merge_request)
end
def build_metrics_from_events(merge_request)
closed_event = merge_request.closed_event
merge_event = merge_request.merge_event
MergeRequest::Metrics.new(latest_closed_at: closed_event&.updated_at,
latest_closed_by: closed_event&.author,
merged_at: merge_event&.updated_at,
merged_by: merge_event&.author)
end
2017-08-17 22:00:37 +05:30
end