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
2019-07-07 11:18:12 +05:30
include ActionView :: Helpers :: UrlHelper
2018-10-15 14:42:47 +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
{ config_error : 'CI/CD YAML configuration error!' }
end
2018-03-17 18:26:18 +05:30
2017-08-17 22:00:37 +05:30
presents :pipeline
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
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
def ref_text
if pipeline . detached_merge_request_pipeline?
_ ( " 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 }
elsif pipeline . merge_request_pipeline?
_ ( " 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
if pipeline . ref_exists?
_ ( " for %{link_to_pipeline_ref} " ) . html_safe % { link_to_pipeline_ref : link_to_pipeline_ref }
else
2019-09-04 21:01:54 +05:30
_ ( " for %{ref} " ) . html_safe % { ref : content_tag ( :span , pipeline . ref , class : 'ref-name' ) }
2019-07-07 11:18:12 +05:30
end
end
end
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
merge_request_presenter & . source_branch_link
end
def link_to_merge_request_target_branch
merge_request_presenter & . target_branch_link
end
private
def merge_request_presenter
return unless pipeline . triggered_by_merge_request?
@merge_request_presenter || = pipeline . merge_request . present ( current_user : current_user )
end
2017-08-17 22:00:37 +05:30
end
end