2016-11-25 14:36:37 -05:00
module ChatMessage
2016-07-29 12:59:54 -04:00
class PipelineMessage < BaseMessage
2016-11-16 09:52:37 -05:00
attr_reader :ref_type , :ref , :status , :project_name , :project_url ,
2016-07-29 12:59:54 -04:00
:user_name , :duration , :pipeline_id
def initialize ( data )
2016-08-01 04:06:57 -04:00
pipeline_attributes = data [ :object_attributes ]
@ref_type = pipeline_attributes [ :tag ] ? 'tag' : 'branch'
@ref = pipeline_attributes [ :ref ]
@status = pipeline_attributes [ :status ]
@duration = pipeline_attributes [ :duration ]
@pipeline_id = pipeline_attributes [ :id ]
2016-07-29 12:59:54 -04:00
@project_name = data [ :project ] [ :path_with_namespace ]
@project_url = data [ :project ] [ :web_url ]
2016-12-13 06:58:26 -05:00
@user_name = ( data [ :user ] && data [ :user ] [ :name ] ) || 'API'
2016-07-29 12:59:54 -04:00
end
def pretext
''
end
def fallback
format ( message )
end
def attachments
[ { text : format ( message ) , color : attachment_color } ]
end
private
def message
" #{ project_link } : Pipeline #{ pipeline_link } of #{ branch_link } #{ ref_type } by #{ user_name } #{ humanized_status } in #{ duration } #{ 'second' . pluralize ( duration ) } "
end
def format ( string )
Slack :: Notifier :: LinkFormatter . format ( string )
end
def humanized_status
case status
when 'success'
'passed'
else
status
end
end
def attachment_color
if status == 'success'
'good'
else
'danger'
end
end
def branch_url
" #{ project_url } /commits/ #{ ref } "
end
def branch_link
" [ #{ ref } ]( #{ branch_url } ) "
end
def project_link
" [ #{ project_name } ]( #{ project_url } ) "
end
def pipeline_url
" #{ project_url } /pipelines/ #{ pipeline_id } "
end
def pipeline_link
2016-11-16 09:52:37 -05:00
" [ # #{ pipeline_id } ]( #{ pipeline_url } ) "
2016-07-29 12:59:54 -04:00
end
end
end