2018-07-24 10:00:56 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-03-20 22:05:52 +00:00
|
|
|
module Ci
|
2017-03-28 17:47:11 +00:00
|
|
|
class PipelinePresenter < Gitlab::View::Presenter::Delegated
|
2018-04-21 22:30:37 +00:00
|
|
|
include Gitlab::Utils::StrongMemoize
|
2019-03-06 08:34:38 +00:00
|
|
|
include ActionView::Helpers::UrlHelper
|
2018-04-21 22:30:37 +00:00
|
|
|
|
2018-11-13 16:29:14 +00:00
|
|
|
# We use a class method here instead of a constant, allowing EE to redefine
|
|
|
|
# the returned `Hash` more easily.
|
|
|
|
def self.failure_reasons
|
2019-12-14 18:07:40 +00:00
|
|
|
{ unknown_failure: 'Unknown pipeline failure!',
|
|
|
|
config_error: 'CI/CD YAML configuration error!',
|
2020-11-27 18:09:52 +00:00
|
|
|
external_validation_failure: 'External pipeline validation failed!',
|
|
|
|
deployments_limit_exceeded: 'Pipeline deployments limit exceeded!' }
|
2018-11-13 16:29:14 +00:00
|
|
|
end
|
2017-10-03 11:18:59 +00:00
|
|
|
|
|
|
|
presents :pipeline
|
2017-09-29 09:14:00 +00:00
|
|
|
|
2018-04-21 22:30:37 +00:00
|
|
|
def failed_builds
|
|
|
|
return [] unless can?(current_user, :read_build, pipeline)
|
|
|
|
|
|
|
|
strong_memoize(:failed_builds) do
|
|
|
|
pipeline.builds.latest.failed
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-09-29 09:14:00 +00:00
|
|
|
def failure_reason
|
|
|
|
return unless pipeline.failure_reason?
|
|
|
|
|
2018-11-13 16:29:14 +00:00
|
|
|
self.class.failure_reasons[pipeline.failure_reason.to_sym] ||
|
2017-09-29 09:14:00 +00:00
|
|
|
pipeline.failure_reason
|
|
|
|
end
|
2017-03-20 22:05:52 +00:00
|
|
|
|
|
|
|
def status_title
|
2017-04-06 13:32:56 +00:00
|
|
|
if auto_canceled?
|
|
|
|
"Pipeline is redundant and is auto-canceled by Pipeline ##{auto_canceled_by_id}"
|
|
|
|
end
|
2017-03-20 22:05:52 +00:00
|
|
|
end
|
2019-03-06 08:34:38 +00:00
|
|
|
|
2020-04-21 15:21:10 +00:00
|
|
|
def localized_names
|
|
|
|
{
|
|
|
|
merge_train: s_('Pipeline|Merge train pipeline'),
|
|
|
|
merged_result: s_('Pipeline|Merged result pipeline'),
|
|
|
|
detached: s_('Pipeline|Detached merge request pipeline')
|
|
|
|
}.freeze
|
|
|
|
end
|
2019-08-28 07:51:26 +00:00
|
|
|
|
|
|
|
def name
|
|
|
|
# Currently, `merge_request_event_type` is the only source to name pipelines
|
|
|
|
# but this could be extended with the other types in the future.
|
2020-04-21 15:21:10 +00:00
|
|
|
localized_names.fetch(pipeline.merge_request_event_type, s_('Pipeline|Pipeline'))
|
2019-08-28 07:51:26 +00:00
|
|
|
end
|
|
|
|
|
2019-03-06 08:34:38 +00:00
|
|
|
def ref_text
|
|
|
|
if pipeline.detached_merge_request_pipeline?
|
2019-10-08 09:06:09 +00:00
|
|
|
_("for %{link_to_merge_request} with %{link_to_merge_request_source_branch}")
|
|
|
|
.html_safe % {
|
|
|
|
link_to_merge_request: link_to_merge_request,
|
|
|
|
link_to_merge_request_source_branch: link_to_merge_request_source_branch
|
|
|
|
}
|
2019-03-06 08:34:38 +00:00
|
|
|
elsif pipeline.merge_request_pipeline?
|
2019-10-08 09:06:09 +00:00
|
|
|
_("for %{link_to_merge_request} with %{link_to_merge_request_source_branch} into %{link_to_merge_request_target_branch}")
|
|
|
|
.html_safe % {
|
|
|
|
link_to_merge_request: link_to_merge_request,
|
|
|
|
link_to_merge_request_source_branch: link_to_merge_request_source_branch,
|
|
|
|
link_to_merge_request_target_branch: link_to_merge_request_target_branch
|
|
|
|
}
|
|
|
|
elsif pipeline.ref && pipeline.ref_exists?
|
|
|
|
_("for %{link_to_pipeline_ref}")
|
|
|
|
.html_safe % { link_to_pipeline_ref: link_to_pipeline_ref }
|
2019-03-06 08:34:38 +00:00
|
|
|
elsif pipeline.ref
|
2019-10-08 09:06:09 +00:00
|
|
|
_("for %{ref}").html_safe % { ref: plain_ref_name }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-01-30 09:08:55 +00:00
|
|
|
def all_related_merge_request_text(limit: nil)
|
2019-10-08 09:06:09 +00:00
|
|
|
if all_related_merge_requests.none?
|
2020-01-30 09:08:55 +00:00
|
|
|
_("No related merge requests found.")
|
2019-10-08 09:06:09 +00:00
|
|
|
else
|
|
|
|
_("%{count} related %{pluralized_subject}: %{links}" % {
|
|
|
|
count: all_related_merge_requests.count,
|
2020-01-30 09:08:55 +00:00
|
|
|
pluralized_subject: n_('merge request', 'merge requests', all_related_merge_requests.count),
|
|
|
|
links: all_related_merge_request_links(limit: limit).join(', ')
|
2019-10-08 09:06:09 +00:00
|
|
|
}).html_safe
|
2019-03-06 08:34:38 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-01-30 09:08:55 +00:00
|
|
|
def has_many_merge_requests?
|
|
|
|
all_related_merge_requests.count > 1
|
|
|
|
end
|
|
|
|
|
2019-03-06 08:34:38 +00:00
|
|
|
def link_to_pipeline_ref
|
|
|
|
link_to(pipeline.ref,
|
|
|
|
project_commits_path(pipeline.project, pipeline.ref),
|
|
|
|
class: "ref-name")
|
|
|
|
end
|
|
|
|
|
|
|
|
def link_to_merge_request
|
|
|
|
return unless merge_request_presenter
|
|
|
|
|
|
|
|
link_to(merge_request_presenter.to_reference,
|
|
|
|
project_merge_request_path(merge_request_presenter.project, merge_request_presenter),
|
|
|
|
class: 'mr-iid')
|
|
|
|
end
|
|
|
|
|
|
|
|
def link_to_merge_request_source_branch
|
2019-04-29 07:29:54 +00:00
|
|
|
merge_request_presenter&.source_branch_link
|
2019-03-06 08:34:38 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def link_to_merge_request_target_branch
|
2019-04-29 07:29:54 +00:00
|
|
|
merge_request_presenter&.target_branch_link
|
2019-03-06 08:34:38 +00:00
|
|
|
end
|
|
|
|
|
2020-07-17 06:09:11 +00:00
|
|
|
def downloadable_path_for_report_type(file_type)
|
|
|
|
if (job_artifact = batch_lookup_report_artifact_for_file_type(file_type)) &&
|
|
|
|
can?(current_user, :read_build, job_artifact.job)
|
|
|
|
download_project_job_artifacts_path(
|
|
|
|
job_artifact.project,
|
|
|
|
job_artifact.job,
|
|
|
|
file_type: file_type,
|
|
|
|
proxy: true)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-03-06 08:34:38 +00:00
|
|
|
private
|
|
|
|
|
2019-10-08 09:06:09 +00:00
|
|
|
def plain_ref_name
|
|
|
|
content_tag(:span, pipeline.ref, class: 'ref-name')
|
|
|
|
end
|
|
|
|
|
2019-03-06 08:34:38 +00:00
|
|
|
def merge_request_presenter
|
2019-10-08 09:06:09 +00:00
|
|
|
strong_memoize(:merge_request_presenter) do
|
2020-02-13 12:08:49 +00:00
|
|
|
if pipeline.merge_request?
|
2019-10-08 09:06:09 +00:00
|
|
|
pipeline.merge_request.present(current_user: current_user)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2019-03-06 08:34:38 +00:00
|
|
|
|
2020-01-30 09:08:55 +00:00
|
|
|
def all_related_merge_request_links(limit: nil)
|
|
|
|
limit ||= all_related_merge_requests.count
|
|
|
|
|
|
|
|
all_related_merge_requests.first(limit).map do |merge_request|
|
2019-10-08 09:06:09 +00:00
|
|
|
mr_path = project_merge_request_path(merge_request.project, merge_request)
|
|
|
|
|
|
|
|
link_to "#{merge_request.to_reference} #{merge_request.title}", mr_path, class: 'mr-iid'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def all_related_merge_requests
|
|
|
|
strong_memoize(:all_related_merge_requests) do
|
2020-03-04 21:07:54 +00:00
|
|
|
if pipeline.ref && can?(current_user, :read_merge_request, pipeline.project)
|
|
|
|
pipeline.all_merge_requests_by_recency.to_a
|
|
|
|
else
|
|
|
|
[]
|
|
|
|
end
|
2019-10-08 09:06:09 +00:00
|
|
|
end
|
2019-03-06 08:34:38 +00:00
|
|
|
end
|
2017-03-20 22:05:52 +00:00
|
|
|
end
|
|
|
|
end
|
2019-09-13 13:26:31 +00:00
|
|
|
|
|
|
|
Ci::PipelinePresenter.prepend_if_ee('EE::Ci::PipelinePresenter')
|