2018-08-10 02:45:01 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-11-25 14:36:37 -05:00
|
|
|
module ChatMessage
|
2016-07-29 12:59:54 -04:00
|
|
|
class PipelineMessage < BaseMessage
|
2019-07-25 10:26:29 -04:00
|
|
|
MAX_VISIBLE_JOBS = 10
|
|
|
|
|
|
|
|
attr_reader :user
|
2017-04-03 13:16:24 -04:00
|
|
|
attr_reader :ref_type
|
|
|
|
attr_reader :ref
|
|
|
|
attr_reader :status
|
2019-07-25 10:26:29 -04:00
|
|
|
attr_reader :detailed_status
|
2017-04-03 13:16:24 -04:00
|
|
|
attr_reader :duration
|
2019-07-25 10:26:29 -04:00
|
|
|
attr_reader :finished_at
|
2017-04-03 13:16:24 -04:00
|
|
|
attr_reader :pipeline_id
|
2019-07-25 10:26:29 -04:00
|
|
|
attr_reader :failed_stages
|
|
|
|
attr_reader :failed_jobs
|
|
|
|
|
|
|
|
attr_reader :project
|
|
|
|
attr_reader :commit
|
|
|
|
attr_reader :committer
|
|
|
|
attr_reader :pipeline
|
2016-07-29 12:59:54 -04:00
|
|
|
|
|
|
|
def initialize(data)
|
2017-04-04 11:10:21 -04:00
|
|
|
super
|
|
|
|
|
2019-07-25 10:26:29 -04:00
|
|
|
@user = data[:user]
|
2017-10-06 16:00:50 -04:00
|
|
|
@user_name = data.dig(:user, :username) || 'API'
|
2017-04-04 11:10:21 -04:00
|
|
|
|
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]
|
2019-07-25 10:26:29 -04:00
|
|
|
@detailed_status = pipeline_attributes[:detailed_status]
|
2017-05-03 03:25:13 -04:00
|
|
|
@duration = pipeline_attributes[:duration].to_i
|
2019-07-25 10:26:29 -04:00
|
|
|
@finished_at = pipeline_attributes[:finished_at] ? Time.parse(pipeline_attributes[:finished_at]).to_i : nil
|
2016-08-01 04:06:57 -04:00
|
|
|
@pipeline_id = pipeline_attributes[:id]
|
2020-04-13 17:09:38 -04:00
|
|
|
|
|
|
|
# Get list of jobs that have actually failed (after exhausting all retries)
|
|
|
|
@failed_jobs = actually_failed_jobs(Array(data[:builds]))
|
2019-07-25 10:26:29 -04:00
|
|
|
@failed_stages = @failed_jobs.map { |j| j[:stage] }.uniq
|
|
|
|
|
|
|
|
@project = Project.find(data[:project][:id])
|
|
|
|
@commit = project.commit_by(oid: data[:commit][:id])
|
|
|
|
@committer = commit.committer
|
|
|
|
@pipeline = Ci::Pipeline.find(pipeline_id)
|
2016-07-29 12:59:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def pretext
|
|
|
|
''
|
|
|
|
end
|
|
|
|
|
2017-04-04 11:10:21 -04:00
|
|
|
def attachments
|
|
|
|
return message if markdown
|
|
|
|
|
2019-07-25 10:26:29 -04:00
|
|
|
[{
|
|
|
|
fallback: format(message),
|
|
|
|
color: attachment_color,
|
|
|
|
author_name: user_combined_name,
|
|
|
|
author_icon: user_avatar,
|
|
|
|
author_link: author_url,
|
|
|
|
title: s_("ChatMessage|Pipeline #%{pipeline_id} %{humanized_status} in %{duration}") %
|
|
|
|
{
|
|
|
|
pipeline_id: pipeline_id,
|
|
|
|
humanized_status: humanized_status,
|
|
|
|
duration: pretty_duration(duration)
|
|
|
|
},
|
|
|
|
title_link: pipeline_url,
|
|
|
|
fields: attachments_fields,
|
|
|
|
footer: project.name,
|
2019-08-13 15:04:56 -04:00
|
|
|
footer_icon: project.avatar_url(only_path: false),
|
2019-07-25 10:26:29 -04:00
|
|
|
ts: finished_at
|
|
|
|
}]
|
2017-04-03 13:16:24 -04:00
|
|
|
end
|
|
|
|
|
2017-04-04 11:10:21 -04:00
|
|
|
def activity
|
|
|
|
{
|
2019-10-22 08:06:20 -04:00
|
|
|
title: s_("ChatMessage|Pipeline %{pipeline_link} of %{ref_type} %{ref_link} by %{user_combined_name} %{humanized_status}") %
|
2019-07-25 10:26:29 -04:00
|
|
|
{
|
|
|
|
pipeline_link: pipeline_link,
|
|
|
|
ref_type: ref_type,
|
2019-10-22 08:06:20 -04:00
|
|
|
ref_link: ref_link,
|
2019-07-25 10:26:29 -04:00
|
|
|
user_combined_name: user_combined_name,
|
|
|
|
humanized_status: humanized_status
|
|
|
|
},
|
|
|
|
subtitle: s_("ChatMessage|in %{project_link}") % { project_link: project_link },
|
|
|
|
text: s_("ChatMessage|in %{duration}") % { duration: pretty_duration(duration) },
|
2017-04-04 11:10:21 -04:00
|
|
|
image: user_avatar || ''
|
|
|
|
}
|
2016-07-29 12:59:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2020-04-13 17:09:38 -04:00
|
|
|
def actually_failed_jobs(builds)
|
|
|
|
succeeded_job_names = builds.map { |b| b[:name] if b[:status] == 'success' }.compact.uniq
|
|
|
|
|
|
|
|
failed_jobs = builds.select do |build|
|
|
|
|
# Select jobs which doesn't have a successful retry
|
|
|
|
build[:status] == 'failed' && !succeeded_job_names.include?(build[:name])
|
|
|
|
end
|
|
|
|
|
|
|
|
failed_jobs.uniq { |job| job[:name] }.reverse
|
|
|
|
end
|
|
|
|
|
2019-07-25 10:26:29 -04:00
|
|
|
def failed_stages_field
|
|
|
|
{
|
|
|
|
title: s_("ChatMessage|Failed stage").pluralize(failed_stages.length),
|
2020-02-25 16:09:23 -05:00
|
|
|
value: Slack::Messenger::Util::LinkFormatter.format(failed_stages_links),
|
2019-07-25 10:26:29 -04:00
|
|
|
short: true
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def failed_jobs_field
|
|
|
|
{
|
|
|
|
title: s_("ChatMessage|Failed job").pluralize(failed_jobs.length),
|
2020-02-25 16:09:23 -05:00
|
|
|
value: Slack::Messenger::Util::LinkFormatter.format(failed_jobs_links),
|
2019-07-25 10:26:29 -04:00
|
|
|
short: true
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def yaml_error_field
|
|
|
|
{
|
|
|
|
title: s_("ChatMessage|Invalid CI config YAML file"),
|
|
|
|
value: pipeline.yaml_errors,
|
|
|
|
short: false
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def attachments_fields
|
|
|
|
fields = [
|
|
|
|
{
|
|
|
|
title: ref_type == "tag" ? s_("ChatMessage|Tag") : s_("ChatMessage|Branch"),
|
2020-02-25 16:09:23 -05:00
|
|
|
value: Slack::Messenger::Util::LinkFormatter.format(ref_link),
|
2019-07-25 10:26:29 -04:00
|
|
|
short: true
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: s_("ChatMessage|Commit"),
|
2020-02-25 16:09:23 -05:00
|
|
|
value: Slack::Messenger::Util::LinkFormatter.format(commit_link),
|
2019-07-25 10:26:29 -04:00
|
|
|
short: true
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
|
|
|
fields << failed_stages_field if failed_stages.any?
|
|
|
|
fields << failed_jobs_field if failed_jobs.any?
|
|
|
|
fields << yaml_error_field if pipeline.has_yaml_errors?
|
|
|
|
|
|
|
|
fields
|
|
|
|
end
|
|
|
|
|
2016-07-29 12:59:54 -04:00
|
|
|
def message
|
2019-10-22 08:06:20 -04:00
|
|
|
s_("ChatMessage|%{project_link}: Pipeline %{pipeline_link} of %{ref_type} %{ref_link} by %{user_combined_name} %{humanized_status} in %{duration}") %
|
2019-07-25 10:26:29 -04:00
|
|
|
{
|
|
|
|
project_link: project_link,
|
|
|
|
pipeline_link: pipeline_link,
|
|
|
|
ref_type: ref_type,
|
2019-10-22 08:06:20 -04:00
|
|
|
ref_link: ref_link,
|
2019-07-25 10:26:29 -04:00
|
|
|
user_combined_name: user_combined_name,
|
|
|
|
humanized_status: humanized_status,
|
|
|
|
duration: pretty_duration(duration)
|
|
|
|
}
|
2016-07-29 12:59:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def humanized_status
|
2020-04-22 02:09:42 -04:00
|
|
|
case status
|
|
|
|
when 'success'
|
|
|
|
detailed_status == "passed with warnings" ? s_("ChatMessage|has passed with warnings") : s_("ChatMessage|has passed")
|
|
|
|
when 'failed'
|
|
|
|
s_("ChatMessage|has failed")
|
2016-07-29 12:59:54 -04:00
|
|
|
else
|
2020-04-22 02:09:42 -04:00
|
|
|
status
|
2016-07-29 12:59:54 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def attachment_color
|
2020-04-22 02:09:42 -04:00
|
|
|
case status
|
|
|
|
when 'success'
|
|
|
|
detailed_status == 'passed with warnings' ? 'warning' : 'good'
|
2016-07-29 12:59:54 -04:00
|
|
|
else
|
2020-04-22 02:09:42 -04:00
|
|
|
'danger'
|
2016-07-29 12:59:54 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-10-22 08:06:20 -04:00
|
|
|
def ref_url
|
|
|
|
if ref_type == 'tag'
|
|
|
|
"#{project_url}/-/tags/#{ref}"
|
|
|
|
else
|
|
|
|
"#{project_url}/commits/#{ref}"
|
|
|
|
end
|
2016-07-29 12:59:54 -04:00
|
|
|
end
|
|
|
|
|
2019-10-22 08:06:20 -04:00
|
|
|
def ref_link
|
|
|
|
"[#{ref}](#{ref_url})"
|
2016-07-29 12:59:54 -04:00
|
|
|
end
|
|
|
|
|
2019-07-25 10:26:29 -04:00
|
|
|
def project_url
|
|
|
|
project.web_url
|
|
|
|
end
|
|
|
|
|
2016-07-29 12:59:54 -04:00
|
|
|
def project_link
|
2019-07-25 10:26:29 -04:00
|
|
|
"[#{project.name}](#{project_url})"
|
|
|
|
end
|
|
|
|
|
|
|
|
def pipeline_failed_jobs_url
|
|
|
|
"#{project_url}/pipelines/#{pipeline_id}/failures"
|
2016-07-29 12:59:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def pipeline_url
|
2020-04-22 02:09:42 -04:00
|
|
|
if failed_jobs.any?
|
2019-07-25 10:26:29 -04:00
|
|
|
pipeline_failed_jobs_url
|
|
|
|
else
|
|
|
|
"#{project_url}/pipelines/#{pipeline_id}"
|
|
|
|
end
|
2016-07-29 12:59:54 -04:00
|
|
|
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
|
2019-07-25 10:26:29 -04:00
|
|
|
|
|
|
|
def job_url(job)
|
|
|
|
"#{project_url}/-/jobs/#{job[:id]}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def job_link(job)
|
|
|
|
"[#{job[:name]}](#{job_url(job)})"
|
|
|
|
end
|
|
|
|
|
|
|
|
def failed_jobs_links
|
|
|
|
failed = failed_jobs.slice(0, MAX_VISIBLE_JOBS)
|
|
|
|
truncated = failed_jobs.slice(MAX_VISIBLE_JOBS, failed_jobs.size)
|
|
|
|
|
|
|
|
failed_links = failed.map { |job| job_link(job) }
|
|
|
|
|
|
|
|
unless truncated.blank?
|
|
|
|
failed_links << s_("ChatMessage|and [%{count} more](%{pipeline_failed_jobs_url})") % {
|
|
|
|
count: truncated.size,
|
|
|
|
pipeline_failed_jobs_url: pipeline_failed_jobs_url
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
failed_links.join(I18n.translate(:'support.array.words_connector'))
|
|
|
|
end
|
|
|
|
|
|
|
|
def stage_link(stage)
|
|
|
|
# All stages link to the pipeline page
|
|
|
|
"[#{stage}](#{pipeline_url})"
|
|
|
|
end
|
|
|
|
|
|
|
|
def failed_stages_links
|
|
|
|
failed_stages.map { |s| stage_link(s) }.join(I18n.translate(:'support.array.words_connector'))
|
|
|
|
end
|
|
|
|
|
|
|
|
def commit_url
|
|
|
|
Gitlab::UrlBuilder.build(commit)
|
|
|
|
end
|
|
|
|
|
|
|
|
def commit_link
|
|
|
|
"[#{commit.title}](#{commit_url})"
|
|
|
|
end
|
|
|
|
|
|
|
|
def author_url
|
|
|
|
return unless user && committer
|
|
|
|
|
|
|
|
Gitlab::UrlBuilder.build(committer)
|
|
|
|
end
|
2016-07-29 12:59:54 -04:00
|
|
|
end
|
|
|
|
end
|