debian-mirror-gitlab/app/presenters/ci/pipeline_presenter.rb

176 lines
6 KiB
Ruby
Raw Normal View History

2018-11-18 11:00:15 +05:30
# frozen_string_literal: true
2017-08-17 22:00:37 +05:30
module Ci
class PipelinePresenter < Gitlab::View::Presenter::Delegated
2018-10-15 14:42:47 +05:30
include Gitlab::Utils::StrongMemoize
2022-04-04 11:22:00 +05:30
delegator_override_with Gitlab::Utils::StrongMemoize # This module inclusion is expected. See https://gitlab.com/gitlab-org/gitlab/-/issues/352884.
2021-11-18 22:05:49 +05:30
2019-02-15 15:39:39 +05:30
# We use a class method here instead of a constant, allowing EE to redefine
# the returned `Hash` more easily.
def self.failure_reasons
2021-06-08 01:23:25 +05:30
{ unknown_failure: 'The reason for the pipeline failure is unknown.',
config_error: 'The pipeline failed due to an error on the CI/CD configuration file.',
external_validation_failure: 'The external pipeline validation failed.',
user_not_verified: 'The pipeline failed due to the user not being verified',
size_limit_exceeded: 'The pipeline size limit was exceeded.',
job_activity_limit_exceeded: 'The pipeline job activity limit was exceeded.',
deployments_limit_exceeded: 'The pipeline deployments limit was exceeded.',
2022-07-23 23:45:48 +05:30
project_deleted: 'The project associated with this pipeline was deleted.' }
2019-02-15 15:39:39 +05:30
end
2018-03-17 18:26:18 +05:30
2021-11-18 22:05:49 +05:30
presents ::Ci::Pipeline, as: :pipeline
2017-08-17 22:00:37 +05:30
2021-11-18 22:05:49 +05:30
delegator_override :failed_builds
2018-10-15 14:42:47 +05:30
def failed_builds
return [] unless can?(current_user, :read_build, pipeline)
strong_memoize(:failed_builds) do
pipeline.builds.latest.failed
end
end
2021-11-18 22:05:49 +05:30
delegator_override :failure_reason
2018-03-17 18:26:18 +05:30
def failure_reason
return unless pipeline.failure_reason?
2019-02-15 15:39:39 +05:30
self.class.failure_reasons[pipeline.failure_reason.to_sym] ||
2018-03-17 18:26:18 +05:30
pipeline.failure_reason
end
2017-08-17 22:00:37 +05:30
def status_title
if auto_canceled?
"Pipeline is redundant and is auto-canceled by Pipeline ##{auto_canceled_by_id}"
end
end
2019-07-07 11:18:12 +05:30
2020-04-22 19:07:51 +05:30
def localized_names
{
merge_train: s_('Pipeline|Merge train pipeline'),
merged_result: s_('Pipeline|Merged result pipeline'),
2022-10-11 01:57:18 +05:30
detached: s_('Pipeline|Merge request pipeline')
2020-04-22 19:07:51 +05:30
}.freeze
end
2019-12-04 20:38:33 +05:30
2023-01-13 00:05:48 +05:30
def event_type_name
2019-12-04 20:38:33 +05:30
# 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-22 19:07:51 +05:30
localized_names.fetch(pipeline.merge_request_event_type, s_('Pipeline|Pipeline'))
2019-12-04 20:38:33 +05:30
end
2022-01-26 12:08:38 +05:30
delegator_override :coverage
def coverage
return unless pipeline.coverage.present?
'%.2f' % pipeline.coverage
end
2019-07-07 11:18:12 +05:30
def ref_text
if pipeline.detached_merge_request_pipeline?
2019-12-21 20:55:43 +05:30
_("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
}
2021-04-17 20:07:23 +05:30
elsif pipeline.merged_result_pipeline?
2019-12-21 20:55:43 +05:30
_("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-07-07 11:18:12 +05:30
elsif pipeline.ref
2019-12-21 20:55:43 +05:30
_("for %{ref}").html_safe % { ref: plain_ref_name }
end
end
2020-03-13 15:44:24 +05:30
def all_related_merge_request_text(limit: nil)
2019-12-21 20:55:43 +05:30
if all_related_merge_requests.none?
2020-03-13 15:44:24 +05:30
_("No related merge requests found.")
2019-12-21 20:55:43 +05:30
else
2022-11-25 23:54:43 +05:30
(_("%{count} related %{pluralized_subject}: %{links}") % {
2019-12-21 20:55:43 +05:30
count: all_related_merge_requests.count,
2020-03-13 15:44:24 +05:30
pluralized_subject: n_('merge request', 'merge requests', all_related_merge_requests.count),
links: all_related_merge_request_links(limit: limit).join(', ')
2019-12-21 20:55:43 +05:30
}).html_safe
2019-07-07 11:18:12 +05:30
end
end
2020-03-13 15:44:24 +05:30
def has_many_merge_requests?
all_related_merge_requests.count > 1
end
2019-07-07 11:18:12 +05:30
def link_to_pipeline_ref
2022-01-26 12:08:38 +05:30
ApplicationController.helpers.link_to(pipeline.ref,
2019-07-07 11:18:12 +05:30
project_commits_path(pipeline.project, pipeline.ref),
class: "ref-name")
end
def link_to_merge_request
return unless merge_request_presenter
2022-01-26 12:08:38 +05:30
ApplicationController.helpers.link_to(merge_request_presenter.to_reference,
2019-07-07 11:18:12 +05:30
project_merge_request_path(merge_request_presenter.project, merge_request_presenter),
class: 'mr-iid')
end
def link_to_merge_request_source_branch
merge_request_presenter&.source_branch_link
end
def link_to_merge_request_target_branch
merge_request_presenter&.target_branch_link
end
2020-07-28 23:09:34 +05:30
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-07-07 11:18:12 +05:30
private
2019-12-21 20:55:43 +05:30
def plain_ref_name
2022-01-26 12:08:38 +05:30
ApplicationController.helpers.content_tag(:span, pipeline.ref, class: 'ref-name')
2019-12-21 20:55:43 +05:30
end
2019-07-07 11:18:12 +05:30
def merge_request_presenter
2019-12-21 20:55:43 +05:30
strong_memoize(:merge_request_presenter) do
2020-03-13 15:44:24 +05:30
if pipeline.merge_request?
2019-12-21 20:55:43 +05:30
pipeline.merge_request.present(current_user: current_user)
end
end
end
2019-07-07 11:18:12 +05:30
2020-03-13 15:44:24 +05:30
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-12-21 20:55:43 +05:30
mr_path = project_merge_request_path(merge_request.project, merge_request)
2022-01-26 12:08:38 +05:30
ApplicationController.helpers.link_to "#{merge_request.to_reference} #{merge_request.title}", mr_path, class: 'mr-iid'
2019-12-21 20:55:43 +05:30
end
end
def all_related_merge_requests
strong_memoize(:all_related_merge_requests) do
2020-03-07 23:17:34 +05:30
if pipeline.ref && can?(current_user, :read_merge_request, pipeline.project)
pipeline.all_merge_requests_by_recency.to_a
else
[]
end
2019-12-21 20:55:43 +05:30
end
2019-07-07 11:18:12 +05:30
end
2017-08-17 22:00:37 +05:30
end
end
2019-12-04 20:38:33 +05:30
2021-06-08 01:23:25 +05:30
Ci::PipelinePresenter.prepend_mod_with('Ci::PipelinePresenter')