From 0f85abfda2f317fc2c78a4fad8a9347e910b4e9f Mon Sep 17 00:00:00 2001 From: Connor Turland Date: Sat, 5 Nov 2016 09:34:50 -0700 Subject: [PATCH] add channel to webhooks (#915) --- app/models/webhooks/slack/base.rb | 6 +++++- app/serializers/webhook_serializer.rb | 5 +++++ app/services/webhook_service.rb | 2 +- db/migrate/20161105160340_add_channel_to_webhook.rb | 5 +++++ db/schema.rb | 3 ++- 5 files changed, 18 insertions(+), 3 deletions(-) create mode 100644 db/migrate/20161105160340_add_channel_to_webhook.rb diff --git a/app/models/webhooks/slack/base.rb b/app/models/webhooks/slack/base.rb index 960775dd..2274e32c 100644 --- a/app/models/webhooks/slack/base.rb +++ b/app/models/webhooks/slack/base.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true -Webhooks::Slack::Base = Struct.new(:event) do +Webhooks::Slack::Base = Struct.new(:webhook, :event) do include Routing def username @@ -14,6 +14,10 @@ Webhooks::Slack::Base = Struct.new(:event) do 'something' end + def channel + webhook.channel + end + def attachments [{ title: attachment_title, diff --git a/app/serializers/webhook_serializer.rb b/app/serializers/webhook_serializer.rb index e27ad0b9..a2acf869 100644 --- a/app/serializers/webhook_serializer.rb +++ b/app/serializers/webhook_serializer.rb @@ -1,4 +1,9 @@ # frozen_string_literal: true class WebhookSerializer < ActiveModel::Serializer attributes :text, :username, :icon_url # , :attachments + attribute :channel, if: :has_channel? + + def has_channel? + true if object.channel + end end diff --git a/app/services/webhook_service.rb b/app/services/webhook_service.rb index 0efe9392..c2a91a80 100644 --- a/app/services/webhook_service.rb +++ b/app/services/webhook_service.rb @@ -11,7 +11,7 @@ class WebhookService end def webhook_object_for(webhook, event) - "Webhooks::#{webhook.kind.classify}::#{event.kind.classify}".constantize.new(event) + "Webhooks::#{webhook.kind.classify}::#{event.kind.classify}".constantize.new(webhook, event) end end end diff --git a/db/migrate/20161105160340_add_channel_to_webhook.rb b/db/migrate/20161105160340_add_channel_to_webhook.rb new file mode 100644 index 00000000..9faae036 --- /dev/null +++ b/db/migrate/20161105160340_add_channel_to_webhook.rb @@ -0,0 +1,5 @@ +class AddChannelToWebhook < ActiveRecord::Migration[5.0] + def change + add_column :webhooks, :channel, :string + end +end diff --git a/db/schema.rb b/db/schema.rb index 527e8335..b9445c1b 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20161013162214) do +ActiveRecord::Schema.define(version: 20161105160340) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -276,6 +276,7 @@ ActiveRecord::Schema.define(version: 20161013162214) do t.string "kind", null: false t.string "uri", null: false t.text "event_types", default: [], array: true + t.string "channel" t.index ["hookable_type", "hookable_id"], name: "index_webhooks_on_hookable_type_and_hookable_id", using: :btree end