gitlab-org--gitlab-foss/app/models/project_services/discord_service.rb

62 lines
1.4 KiB
Ruby
Raw Normal View History

2018-10-30 11:23:20 +00:00
# frozen_string_literal: true
require "discordrb/webhooks"
class DiscordService < ChatNotificationService
def title
"Discord Notifications"
end
def description
"Receive event notifications in Discord"
end
def self.to_param
"discord"
end
def help
"This service sends notifications about projects events to Discord channels.<br />
To set up this service:
<ol>
2018-11-05 00:24:26 +00:00
<li><a href='https://support.discordapp.com/hc/en-us/articles/228383668-Intro-to-Webhooks'>Setup a custom Incoming Webhook</a>.</li>
2018-10-30 11:23:20 +00:00
<li>Paste the <strong>Webhook URL</strong> into the field below.</li>
<li>Select events below to enable notifications.</li>
</ol>"
end
def webhook_placeholder
"https://discordapp.com/api/webhooks/..."
end
def event_field(event)
2018-11-05 00:24:26 +00:00
# No-op.
2018-10-30 11:23:20 +00:00
end
def default_channel_placeholder
2018-11-05 00:24:26 +00:00
# No-op.
2018-10-30 11:23:20 +00:00
end
def default_fields
[
{ type: "text", name: "webhook", placeholder: "e.g. #{webhook_placeholder}" },
{ type: "checkbox", name: "notify_only_broken_pipelines" },
{ type: "checkbox", name: "notify_only_default_branch" }
]
end
private
def notify(message, opts)
client = Discordrb::Webhooks::Client.new(url: webhook)
2018-11-05 00:24:26 +00:00
2018-10-30 11:23:20 +00:00
client.execute do |builder|
builder.content = message.pretext
end
end
def custom_data(data)
super(data).merge(markdown: true)
end
end