Commit b647ee79 authored by Sam Bingner's avatar Sam Bingner Committed by Arturo Herrero

Much cleaner Discord notification outputs

parent 8f51affe
...@@ -3,7 +3,8 @@ ...@@ -3,7 +3,8 @@
require "discordrb/webhooks" require "discordrb/webhooks"
class DiscordService < ChatNotificationService class DiscordService < ChatNotificationService
LINK_REGEX = /(\[[^\]]*\]\()([^\)]*)\)/.freeze ATTACHMENT_REGEX = /: (?<entry>.*?)\n - (?<name>.*)\n*/.freeze
def title def title
s_("DiscordService|Discord Notifications") s_("DiscordService|Discord Notifications")
end end
...@@ -34,10 +35,10 @@ class DiscordService < ChatNotificationService ...@@ -34,10 +35,10 @@ class DiscordService < ChatNotificationService
# No-op. # No-op.
end end
def self.supported_events # def self.supported_events
%w[push issue confidential_issue merge_request note confidential_note tag_push # %w[push issue confidential_issue merge_request note confidential_note tag_push
pipeline wiki_page] # pipeline wiki_page]
end # end
def default_fields def default_fields
[ [
...@@ -53,11 +54,24 @@ class DiscordService < ChatNotificationService ...@@ -53,11 +54,24 @@ class DiscordService < ChatNotificationService
client = Discordrb::Webhooks::Client.new(url: webhook) client = Discordrb::Webhooks::Client.new(url: webhook)
client.execute do |builder| client.execute do |builder|
builder.content = (message.pretext + "\n\n" + message.attachments.to_s).gsub(LINK_REGEX, "\\1<\\2>)") builder.add_embed do |embed|
embed.author = Discordrb::Webhooks::EmbedAuthor.new(name: message.user_name, icon_url: message.user_avatar)
embed.description = (message.pretext + "\n" + wrap_array(message.attachments).join("\n")).gsub(ATTACHMENT_REGEX, " \\k<entry> - \\k<name>\n")
end
end end
end end
def custom_data(data) def custom_data(data)
super(data).merge(markdown: true) super(data).merge(markdown: true)
end end
def wrap_array object
if object.nil?
[]
elsif object.respond_to?(:to_ary)
object.to_ary || [object]
else
[object]
end
end
end end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment