2017-08-17 22:00:37 +05:30
|
|
|
module ChatMessage
|
2016-09-29 09:46:39 +05:30
|
|
|
class PipelineMessage < BaseMessage
|
2017-08-17 22:00:37 +05:30
|
|
|
attr_reader :ref_type
|
|
|
|
attr_reader :ref
|
|
|
|
attr_reader :status
|
|
|
|
attr_reader :duration
|
|
|
|
attr_reader :pipeline_id
|
2016-09-29 09:46:39 +05:30
|
|
|
|
|
|
|
def initialize(data)
|
2017-08-17 22:00:37 +05:30
|
|
|
super
|
|
|
|
|
|
|
|
@user_name = data.dig(:user, :name) || 'API'
|
|
|
|
|
2016-09-29 09:46:39 +05:30
|
|
|
pipeline_attributes = data[:object_attributes]
|
|
|
|
@ref_type = pipeline_attributes[:tag] ? 'tag' : 'branch'
|
|
|
|
@ref = pipeline_attributes[:ref]
|
|
|
|
@status = pipeline_attributes[:status]
|
2017-08-17 22:00:37 +05:30
|
|
|
@duration = pipeline_attributes[:duration].to_i
|
2016-09-29 09:46:39 +05:30
|
|
|
@pipeline_id = pipeline_attributes[:id]
|
|
|
|
end
|
|
|
|
|
|
|
|
def pretext
|
|
|
|
''
|
|
|
|
end
|
|
|
|
|
|
|
|
def fallback
|
|
|
|
format(message)
|
|
|
|
end
|
|
|
|
|
|
|
|
def attachments
|
2017-08-17 22:00:37 +05:30
|
|
|
return message if markdown
|
|
|
|
|
2016-09-29 09:46:39 +05:30
|
|
|
[{ text: format(message), color: attachment_color }]
|
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
def activity
|
|
|
|
{
|
2017-09-10 17:25:29 +05:30
|
|
|
title: "Pipeline #{pipeline_link} of #{ref_type} #{branch_link} by #{user_name} #{humanized_status}",
|
2017-08-17 22:00:37 +05:30
|
|
|
subtitle: "in #{project_link}",
|
|
|
|
text: "in #{pretty_duration(duration)}",
|
|
|
|
image: user_avatar || ''
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2016-09-29 09:46:39 +05:30
|
|
|
private
|
|
|
|
|
|
|
|
def message
|
2017-09-10 17:25:29 +05:30
|
|
|
"#{project_link}: Pipeline #{pipeline_link} of #{ref_type} #{branch_link} by #{user_name} #{humanized_status} in #{pretty_duration(duration)}"
|
2016-09-29 09:46:39 +05:30
|
|
|
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
|
2017-08-17 22:00:37 +05:30
|
|
|
"[##{pipeline_id}](#{pipeline_url})"
|
2016-09-29 09:46:39 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|