diff --git a/Gemfile b/Gemfile index 6674edc1d0c..1182fb7eded 100644 --- a/Gemfile +++ b/Gemfile @@ -204,6 +204,9 @@ gem 'redis-rails', '~> 5.0.2' gem 'redis', '~> 3.2' gem 'connection_pool', '~> 2.0' +# Discord integration +gem 'discordrb-webhooks', '~> 3.3', github: 'blackst0ne/discordrb', require: false + # HipChat integration gem 'hipchat', '~> 1.5.0' diff --git a/Gemfile.lock b/Gemfile.lock index e755b0e0a8d..0cc549b0b30 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,3 +1,10 @@ +GIT + remote: git://github.com/blackst0ne/discordrb.git + revision: 2de6ec4dda1e2ea2966f1ced063698d7c5fc5225 + specs: + discordrb-webhooks (3.3.0) + rest-client (~> 2.0) + GEM remote: https://rubygems.org/ specs: @@ -972,6 +979,7 @@ DEPENDENCIES devise (~> 4.4) devise-two-factor (~> 3.0.0) diffy (~> 3.1.0) + discordrb-webhooks (~> 3.3)! doorkeeper (~> 4.3) doorkeeper-openid_connect (~> 1.5) ed25519 (~> 1.2) diff --git a/Gemfile.rails5.lock b/Gemfile.rails5.lock index 6ae7444f18f..22b46333aec 100644 --- a/Gemfile.rails5.lock +++ b/Gemfile.rails5.lock @@ -1,3 +1,10 @@ +GIT + remote: git://github.com/blackst0ne/discordrb.git + revision: 2de6ec4dda1e2ea2966f1ced063698d7c5fc5225 + specs: + discordrb-webhooks (3.3.0) + rest-client (~> 2.0) + GEM remote: https://rubygems.org/ specs: @@ -981,6 +988,7 @@ DEPENDENCIES devise (~> 4.4) devise-two-factor (~> 3.0.0) diffy (~> 3.1.0) + discordrb-webhooks (~> 3.3)! doorkeeper (~> 4.3) doorkeeper-openid_connect (~> 1.5) ed25519 (~> 1.2) diff --git a/app/models/project.rb b/app/models/project.rb index 382fb4f463a..8a6b7abfa6e 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -135,6 +135,7 @@ class Project < ActiveRecord::Base # Project services has_one :campfire_service + has_one :discord_service has_one :drone_ci_service has_one :emails_on_push_service has_one :pipelines_email_service diff --git a/app/models/project_services/discord_service.rb b/app/models/project_services/discord_service.rb new file mode 100644 index 00000000000..ffd82afc131 --- /dev/null +++ b/app/models/project_services/discord_service.rb @@ -0,0 +1,63 @@ +# 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.
+ To set up this service: +
    +
  1. Setup a custom Incoming Webhook.
  2. +
  3. Paste the Webhook URL into the field below.
  4. +
  5. Select events below to enable notifications.
  6. +
" + end + + def webhook_placeholder + "https://discordapp.com/api/webhooks/..." + end + + def event_field(event) + end + + def default_channel_placeholder + 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) + client.execute do |builder| + builder.content = message.pretext + # builder.add_embed do |embed| + # embed.title = 'Embed title' + # embed.description = 'Embed description' + # embed.timestamp = Time.now + # end + end + end + + def custom_data(data) + super(data).merge(markdown: true) + end +end diff --git a/app/models/service.rb b/app/models/service.rb index 4dbda7acab6..5b8bf6e7cf0 100644 --- a/app/models/service.rb +++ b/app/models/service.rb @@ -253,6 +253,7 @@ class Service < ActiveRecord::Base bugzilla campfire custom_issue_tracker + discord drone_ci emails_on_push external_wiki diff --git a/lib/api/services.rb b/lib/api/services.rb index 0ae05ce08f1..1cb3b8a7277 100644 --- a/lib/api/services.rb +++ b/lib/api/services.rb @@ -298,6 +298,14 @@ module API desc: 'Title' } ], + 'discord' => [ + { + required: true, + name: :webhook, + type: String, + desc: 'Discord webhook. e.g. https://discordapp.com/api/webhooks/…' + } + ], 'drone-ci' => [ { required: true, @@ -677,6 +685,7 @@ module API BuildkiteService, CampfireService, CustomIssueTrackerService, + DiscordService, DroneCiService, EmailsOnPushService, ExternalWikiService, diff --git a/spec/lib/gitlab/import_export/all_models.yml b/spec/lib/gitlab/import_export/all_models.yml index a63f34b5536..1f1d23e39eb 100644 --- a/spec/lib/gitlab/import_export/all_models.yml +++ b/spec/lib/gitlab/import_export/all_models.yml @@ -198,6 +198,7 @@ project: - last_event - services - campfire_service +- discord_service - drone_ci_service - emails_on_push_service - pipelines_email_service diff --git a/spec/models/project_spec.rb b/spec/models/project_spec.rb index 62a38c66d99..77e747fcdc9 100644 --- a/spec/models/project_spec.rb +++ b/spec/models/project_spec.rb @@ -32,6 +32,7 @@ describe Project do it { is_expected.to have_one(:asana_service) } it { is_expected.to have_many(:boards) } it { is_expected.to have_one(:campfire_service) } + it { is_expected.to have_one(:discord_service) } it { is_expected.to have_one(:drone_ci_service) } it { is_expected.to have_one(:emails_on_push_service) } it { is_expected.to have_one(:pipelines_email_service) }