2018-11-20 20:47:30 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
module ChatMessage
|
2015-04-26 12:48:37 +05:30
|
|
|
class PushMessage < BaseMessage
|
|
|
|
attr_reader :after
|
|
|
|
attr_reader :before
|
|
|
|
attr_reader :commits
|
|
|
|
attr_reader :ref
|
|
|
|
attr_reader :ref_type
|
|
|
|
|
|
|
|
def initialize(params)
|
2017-08-17 22:00:37 +05:30
|
|
|
super
|
|
|
|
|
2015-04-26 12:48:37 +05:30
|
|
|
@after = params[:after]
|
|
|
|
@before = params[:before]
|
|
|
|
@commits = params.fetch(:commits, [])
|
|
|
|
@ref_type = Gitlab::Git.tag_ref?(params[:ref]) ? 'tag' : 'branch'
|
|
|
|
@ref = Gitlab::Git.ref_name(params[:ref])
|
|
|
|
end
|
|
|
|
|
|
|
|
def attachments
|
|
|
|
return [] if new_branch? || removed_branch?
|
2017-08-17 22:00:37 +05:30
|
|
|
return commit_messages if markdown
|
2015-04-26 12:48:37 +05:30
|
|
|
|
|
|
|
commit_message_attachments
|
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
def activity
|
|
|
|
{
|
2019-02-15 15:39:39 +05:30
|
|
|
title: humanized_action(short: true),
|
2017-08-17 22:00:37 +05:30
|
|
|
subtitle: "in #{project_link}",
|
|
|
|
text: compare_link,
|
|
|
|
image: user_avatar
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2015-04-26 12:48:37 +05:30
|
|
|
private
|
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
def humanized_action(short: false)
|
|
|
|
action, ref_link, target_link = compose_action_details
|
|
|
|
text = [user_combined_name, action, ref_type, ref_link]
|
|
|
|
text << target_link unless short
|
|
|
|
text.join(' ')
|
|
|
|
end
|
|
|
|
|
2015-04-26 12:48:37 +05:30
|
|
|
def message
|
2019-02-15 15:39:39 +05:30
|
|
|
humanized_action
|
2015-04-26 12:48:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def format(string)
|
2020-04-08 14:13:33 +05:30
|
|
|
Slack::Messenger::Util::LinkFormatter.format(string)
|
2015-04-26 12:48:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def commit_messages
|
2020-04-08 14:13:33 +05:30
|
|
|
commits.map { |commit| compose_commit_message(commit) }.join("\n\n")
|
2015-04-26 12:48:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def commit_message_attachments
|
|
|
|
[{ text: format(commit_messages), color: attachment_color }]
|
|
|
|
end
|
|
|
|
|
|
|
|
def compose_commit_message(commit)
|
|
|
|
author = commit[:author][:name]
|
|
|
|
id = Commit.truncate_sha(commit[:id])
|
2020-04-08 14:13:33 +05:30
|
|
|
title = commit[:title]
|
2019-12-04 20:38:33 +05:30
|
|
|
|
2015-04-26 12:48:37 +05:30
|
|
|
url = commit[:url]
|
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
"[#{id}](#{url}): #{title} - #{author}"
|
2015-04-26 12:48:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def new_branch?
|
|
|
|
Gitlab::Git.blank_ref?(before)
|
|
|
|
end
|
|
|
|
|
|
|
|
def removed_branch?
|
|
|
|
Gitlab::Git.blank_ref?(after)
|
|
|
|
end
|
|
|
|
|
2019-12-26 22:10:19 +05:30
|
|
|
def ref_url
|
|
|
|
if ref_type == 'tag'
|
|
|
|
"#{project_url}/-/tags/#{ref}"
|
|
|
|
else
|
|
|
|
"#{project_url}/commits/#{ref}"
|
|
|
|
end
|
2015-04-26 12:48:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def compare_url
|
|
|
|
"#{project_url}/compare/#{before}...#{after}"
|
|
|
|
end
|
|
|
|
|
2019-12-26 22:10:19 +05:30
|
|
|
def ref_link
|
|
|
|
"[#{ref}](#{ref_url})"
|
2015-04-26 12:48:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def project_link
|
|
|
|
"[#{project_name}](#{project_url})"
|
|
|
|
end
|
|
|
|
|
|
|
|
def compare_link
|
|
|
|
"[Compare changes](#{compare_url})"
|
|
|
|
end
|
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
def compose_action_details
|
|
|
|
if new_branch?
|
2019-12-26 22:10:19 +05:30
|
|
|
['pushed new', ref_link, "to #{project_link}"]
|
2019-02-15 15:39:39 +05:30
|
|
|
elsif removed_branch?
|
|
|
|
['removed', ref, "from #{project_link}"]
|
|
|
|
else
|
2019-12-26 22:10:19 +05:30
|
|
|
['pushed to', ref_link, "of #{project_link} (#{compare_link})"]
|
2019-02-15 15:39:39 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-04-26 12:48:37 +05:30
|
|
|
def attachment_color
|
|
|
|
'#345'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|