debian-mirror-gitlab/app/models/integrations/chat_message/push_message.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

121 lines
2.7 KiB
Ruby
Raw Normal View History

2021-06-08 01:23:25 +05:30
# frozen_string_literal: true
module Integrations
module ChatMessage
class PushMessage < BaseMessage
attr_reader :after
attr_reader :before
attr_reader :commits
attr_reader :ref
attr_reader :ref_type
def initialize(params)
super
@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?
return commit_messages if markdown
commit_message_attachments
end
def activity
{
title: humanized_action(short: true),
subtitle: "in #{project_link}",
text: compare_link,
image: user_avatar
}
end
private
def humanized_action(short: false)
action, ref_link, target_link = compose_action_details
2022-01-12 12:59:36 +05:30
text = [strip_markup(user_combined_name), action, ref_type, ref_link]
2021-06-08 01:23:25 +05:30
text << target_link unless short
text.join(' ')
end
def message
humanized_action
end
def format(string)
2021-09-04 01:27:46 +05:30
::Slack::Messenger::Util::LinkFormatter.format(string)
2021-06-08 01:23:25 +05:30
end
def commit_messages
commits.map { |commit| compose_commit_message(commit) }.join("\n\n")
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])
title = commit[:title]
url = commit[:url]
2022-01-12 12:59:36 +05:30
"#{link(id, url)}: #{strip_markup(title)} - #{strip_markup(author)}"
2021-06-08 01:23:25 +05:30
end
def new_branch?
Gitlab::Git.blank_ref?(before)
end
def removed_branch?
Gitlab::Git.blank_ref?(after)
end
def ref_url
if ref_type == 'tag'
"#{project_url}/-/tags/#{ref}"
else
"#{project_url}/commits/#{ref}"
end
end
def compare_url
"#{project_url}/compare/#{before}...#{after}"
end
def ref_link
2022-01-12 12:59:36 +05:30
link(ref, ref_url)
2021-06-08 01:23:25 +05:30
end
def project_link
2022-01-12 12:59:36 +05:30
link(project_name, project_url)
2021-06-08 01:23:25 +05:30
end
def compare_link
2022-01-12 12:59:36 +05:30
link('Compare changes', compare_url)
2021-06-08 01:23:25 +05:30
end
def compose_action_details
if new_branch?
['pushed new', ref_link, "to #{project_link}"]
elsif removed_branch?
['removed', ref, "from #{project_link}"]
else
['pushed to', ref_link, "of #{project_link} (#{compare_link})"]
end
end
def attachment_color
'#345'
end
end
end
end