2018-11-18 11:00:15 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-10-12 21:52:04 +05:30
|
|
|
class MergeRequestWidgetEntity < Grape::Entity
|
|
|
|
include RequestAwareEntity
|
|
|
|
|
2020-10-24 23:57:45 +05:30
|
|
|
SUGGEST_PIPELINE = 'suggest_pipeline'
|
|
|
|
|
2020-01-01 13:55:28 +05:30
|
|
|
expose :id
|
|
|
|
expose :iid
|
|
|
|
|
2018-11-18 11:00:15 +05:30
|
|
|
expose :source_project_full_path do |merge_request|
|
|
|
|
merge_request.source_project&.full_path
|
|
|
|
end
|
2019-10-12 21:52:04 +05:30
|
|
|
|
2018-11-18 11:00:15 +05:30
|
|
|
expose :target_project_full_path do |merge_request|
|
|
|
|
merge_request.project&.full_path
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2020-10-24 23:57:45 +05:30
|
|
|
expose :can_create_pipeline_in_target_project do |merge_request|
|
|
|
|
can?(current_user, :create_pipeline, merge_request.target_project)
|
|
|
|
end
|
|
|
|
|
2019-10-12 21:52:04 +05:30
|
|
|
expose :email_patches_path do |merge_request|
|
|
|
|
project_merge_request_path(merge_request.project, merge_request, format: :patch)
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
|
2019-10-12 21:52:04 +05:30
|
|
|
expose :plain_diff_path do |merge_request|
|
|
|
|
project_merge_request_path(merge_request.project, merge_request, format: :diff)
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
|
2019-10-12 21:52:04 +05:30
|
|
|
expose :merge_request_basic_path do |merge_request|
|
|
|
|
project_merge_request_path(merge_request.target_project, merge_request, serializer: :basic, format: :json)
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
2018-03-27 19:54:05 +05:30
|
|
|
|
2019-10-12 21:52:04 +05:30
|
|
|
expose :merge_request_widget_path do |merge_request|
|
|
|
|
widget_project_json_merge_request_path(merge_request.target_project, merge_request, format: :json)
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2019-10-12 21:52:04 +05:30
|
|
|
expose :merge_request_cached_widget_path do |merge_request|
|
|
|
|
cached_widget_project_json_merge_request_path(merge_request.target_project, merge_request, format: :json)
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
2019-10-12 21:52:04 +05:30
|
|
|
expose :commit_change_content_path do |merge_request|
|
|
|
|
commit_change_content_project_merge_request_path(merge_request.project, merge_request)
|
2019-03-02 22:35:43 +05:30
|
|
|
end
|
|
|
|
|
2019-10-12 21:52:04 +05:30
|
|
|
expose :conflicts_docs_path do |merge_request|
|
|
|
|
help_page_path('user/project/merge_requests/resolve_conflicts.md')
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
2019-10-12 21:52:04 +05:30
|
|
|
expose :merge_request_pipelines_docs_path do |merge_request|
|
|
|
|
help_page_path('ci/merge_request_pipelines/index.md')
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
2019-10-12 21:52:04 +05:30
|
|
|
expose :ci_environments_status_path do |merge_request|
|
|
|
|
ci_environments_status_project_merge_request_path(merge_request.project, merge_request)
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
expose :merge_request_add_ci_config_path, if: ->(mr, _) { can_add_ci_config_path?(mr) } do |merge_request|
|
|
|
|
project_new_blob_path(
|
|
|
|
merge_request.source_project,
|
|
|
|
merge_request.source_branch,
|
|
|
|
file_name: '.gitlab-ci.yml',
|
2020-04-08 14:13:33 +05:30
|
|
|
commit_message: s_("CommitMessage|Add %{file_name}") % { file_name: Gitlab::FileDetector::PATTERNS[:gitlab_ci] },
|
2020-11-24 15:15:51 +05:30
|
|
|
mr_path: merge_request_path(merge_request),
|
2020-04-08 14:13:33 +05:30
|
|
|
suggest_gitlab_ci_yml: true
|
2020-03-13 15:44:24 +05:30
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2020-10-24 23:57:45 +05:30
|
|
|
expose :user_callouts_path, if: -> (*) { Feature.enabled?(:suggest_pipeline) } do |merge_request|
|
|
|
|
user_callouts_path
|
|
|
|
end
|
|
|
|
|
|
|
|
expose :suggest_pipeline_feature_id, if: -> (*) { Feature.enabled?(:suggest_pipeline) } do |merge_request|
|
|
|
|
SUGGEST_PIPELINE
|
|
|
|
end
|
|
|
|
|
|
|
|
expose :is_dismissed_suggest_pipeline, if: -> (*) { Feature.enabled?(:suggest_pipeline) } do |merge_request|
|
|
|
|
current_user && current_user.dismissed_callout?(feature_name: SUGGEST_PIPELINE)
|
|
|
|
end
|
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
expose :human_access do |merge_request|
|
|
|
|
merge_request.project.team.human_max_access(current_user&.id)
|
|
|
|
end
|
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
expose :new_project_pipeline_path do |merge_request|
|
|
|
|
new_project_pipeline_path(merge_request.project)
|
|
|
|
end
|
|
|
|
|
2019-09-04 21:01:54 +05:30
|
|
|
# Rendering and redacting Markdown can be expensive. These links are
|
|
|
|
# just nice to have in the merge request widget, so only
|
|
|
|
# include them if they are explicitly requested on first load.
|
|
|
|
expose :issues_links, if: -> (_, opts) { opts[:issues_links] } do
|
2017-08-17 22:00:37 +05:30
|
|
|
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
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
expose :blob_path do
|
|
|
|
expose :head_path, if: -> (mr, _) { mr.source_branch_sha } do |merge_request|
|
|
|
|
project_blob_path(merge_request.project, merge_request.source_branch_sha)
|
|
|
|
end
|
|
|
|
|
|
|
|
expose :base_path, if: -> (mr, _) { mr.diff_base_sha } do |merge_request|
|
|
|
|
project_blob_path(merge_request.project, merge_request.diff_base_sha)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
expose :codeclimate, if: -> (mr, _) { head_pipeline_downloadable_path_for_report_type(:codequality) } do
|
|
|
|
expose :head_path do |merge_request|
|
|
|
|
head_pipeline_downloadable_path_for_report_type(:codequality)
|
|
|
|
end
|
|
|
|
|
|
|
|
expose :base_path do |merge_request|
|
|
|
|
base_pipeline_downloadable_path_for_report_type(:codequality)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
private
|
|
|
|
|
2019-12-04 20:38:33 +05:30
|
|
|
delegate :current_user, to: :request
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
def presenter(merge_request)
|
|
|
|
@presenters ||= {}
|
2019-12-04 20:38:33 +05:30
|
|
|
@presenters[merge_request] ||= MergeRequestPresenter.new(merge_request, current_user: current_user) # rubocop: disable CodeReuse/Presenter
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
2020-03-13 15:44:24 +05:30
|
|
|
|
|
|
|
def can_add_ci_config_path?(merge_request)
|
2020-07-28 23:09:34 +05:30
|
|
|
merge_request.open? &&
|
|
|
|
merge_request.source_branch_exists? &&
|
|
|
|
merge_request.source_project&.uses_default_ci_config? &&
|
2020-04-22 19:07:51 +05:30
|
|
|
!merge_request.source_project.has_ci? &&
|
2020-10-24 23:57:45 +05:30
|
|
|
merge_request.commits_count > 0 &&
|
2020-04-08 14:13:33 +05:30
|
|
|
can?(current_user, :read_build, merge_request.source_project) &&
|
|
|
|
can?(current_user, :create_pipeline, merge_request.source_project)
|
2020-03-13 15:44:24 +05:30
|
|
|
end
|
2020-07-28 23:09:34 +05:30
|
|
|
|
|
|
|
def head_pipeline_downloadable_path_for_report_type(file_type)
|
|
|
|
object.head_pipeline&.present(current_user: current_user)
|
|
|
|
&.downloadable_path_for_report_type(file_type)
|
|
|
|
end
|
|
|
|
|
|
|
|
def base_pipeline_downloadable_path_for_report_type(file_type)
|
|
|
|
object.base_pipeline&.present(current_user: current_user)
|
|
|
|
&.downloadable_path_for_report_type(file_type)
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
2019-12-04 20:38:33 +05:30
|
|
|
|
|
|
|
MergeRequestWidgetEntity.prepend_if_ee('EE::MergeRequestWidgetEntity')
|