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
|
|
|
|
{
|
2018-12-23 12:14:25 +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
|
|
|
|
|
2018-12-23 12:14:25 +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
|
2018-12-23 12:14:25 +05:30
|
|
|
humanized_action
|
2015-04-26 12:48:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def format(string)
|
|
|
|
Slack::Notifier::LinkFormatter.format(string)
|
|
|
|
end
|
|
|
|
|
|
|
|
def commit_messages
|
2017-08-17 22:00:37 +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])
|
|
|
|
message = commit[:message]
|
|
|
|
url = commit[:url]
|
|
|
|
|
|
|
|
"[#{id}](#{url}): #{message} - #{author}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def new_branch?
|
|
|
|
Gitlab::Git.blank_ref?(before)
|
|
|
|
end
|
|
|
|
|
|
|
|
def removed_branch?
|
|
|
|
Gitlab::Git.blank_ref?(after)
|
|
|
|
end
|
|
|
|
|
|
|
|
def branch_url
|
|
|
|
"#{project_url}/commits/#{ref}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def compare_url
|
|
|
|
"#{project_url}/compare/#{before}...#{after}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def branch_link
|
|
|
|
"[#{ref}](#{branch_url})"
|
|
|
|
end
|
|
|
|
|
|
|
|
def project_link
|
|
|
|
"[#{project_name}](#{project_url})"
|
|
|
|
end
|
|
|
|
|
|
|
|
def compare_link
|
|
|
|
"[Compare changes](#{compare_url})"
|
|
|
|
end
|
|
|
|
|
2018-12-23 12:14:25 +05:30
|
|
|
def compose_action_details
|
|
|
|
if new_branch?
|
|
|
|
['pushed new', branch_link, "to #{project_link}"]
|
|
|
|
elsif removed_branch?
|
|
|
|
['removed', ref, "from #{project_link}"]
|
|
|
|
else
|
|
|
|
['pushed to', branch_link, "of #{project_link} (#{compare_link})"]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-04-26 12:48:37 +05:30
|
|
|
def attachment_color
|
|
|
|
'#345'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|