mirror of
https://github.com/discourse/discourse-chat-integration.git
synced 2025-07-05 21:42:31 +00:00
Add notifications support for Gitter
This commit is contained in:
parent
5ea6553236
commit
e90d65f5d5
@ -178,3 +178,16 @@ en:
|
|||||||
help: "e.g. #channel, @username."
|
help: "e.g. #channel, @username."
|
||||||
errors:
|
errors:
|
||||||
invalid_channel: "That channel does not exist on Rocket Chat"
|
invalid_channel: "That channel does not exist on Rocket Chat"
|
||||||
|
|
||||||
|
#######################################
|
||||||
|
############ GITTER STRINGS ###########
|
||||||
|
#######################################
|
||||||
|
gitter:
|
||||||
|
title: "Gitter"
|
||||||
|
param:
|
||||||
|
name:
|
||||||
|
title: "Name"
|
||||||
|
help: "A Gitter room's name e.g. gitterHQ/services."
|
||||||
|
webhook_url:
|
||||||
|
title: "Webhook URL"
|
||||||
|
help: "The URL provided when you create a new integration in a Gitter room."
|
||||||
|
@ -72,6 +72,11 @@ en:
|
|||||||
chat_integration_rocketchat_webhook_url: "The URL for the Rocket Chat integration webhook"
|
chat_integration_rocketchat_webhook_url: "The URL for the Rocket Chat integration webhook"
|
||||||
chat_integration_rocketchat_excerpt_length: "Rocket Chat post excerpt length"
|
chat_integration_rocketchat_excerpt_length: "Rocket Chat post excerpt length"
|
||||||
|
|
||||||
|
#######################################
|
||||||
|
############ GITTER SETTINGS ##########
|
||||||
|
#######################################
|
||||||
|
chat_integration_gitter_enabled: "Enable the Gitter chat integration provider"
|
||||||
|
|
||||||
chat_integration:
|
chat_integration:
|
||||||
|
|
||||||
all_categories: "(all categories)"
|
all_categories: "(all categories)"
|
||||||
|
@ -108,3 +108,9 @@ plugins:
|
|||||||
default: ''
|
default: ''
|
||||||
chat_integration_rocketchat_excerpt_length:
|
chat_integration_rocketchat_excerpt_length:
|
||||||
default: 400
|
default: 400
|
||||||
|
|
||||||
|
#######################################
|
||||||
|
########## GITTER SETTINGS ############
|
||||||
|
#######################################
|
||||||
|
chat_integration_gitter_enabled:
|
||||||
|
default: false
|
||||||
|
@ -97,3 +97,4 @@ require_relative "provider/mattermost/mattermost_provider.rb"
|
|||||||
require_relative "provider/matrix/matrix_provider.rb"
|
require_relative "provider/matrix/matrix_provider.rb"
|
||||||
require_relative "provider/zulip/zulip_provider.rb"
|
require_relative "provider/zulip/zulip_provider.rb"
|
||||||
require_relative "provider/rocketchat/rocketchat_provider.rb"
|
require_relative "provider/rocketchat/rocketchat_provider.rb"
|
||||||
|
require_relative "provider/gitter/gitter_provider.rb"
|
||||||
|
30
lib/discourse_chat/provider/gitter/gitter_provider.rb
Normal file
30
lib/discourse_chat/provider/gitter/gitter_provider.rb
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
module DiscourseChat
|
||||||
|
module Provider
|
||||||
|
module GitterProvider
|
||||||
|
PROVIDER_NAME = 'gitter'.freeze
|
||||||
|
PROVIDER_ENABLED_SETTING = :chat_integration_gitter_enabled
|
||||||
|
CHANNEL_PARAMETERS = [
|
||||||
|
{ key: "name", regex: '^\S+$', unique: true },
|
||||||
|
{ key: "webhook_url", regex: '^https://webhooks.gitter.im/e/\S+$', unique: true, hidden: true }
|
||||||
|
]
|
||||||
|
|
||||||
|
def self.trigger_notification(post, channel)
|
||||||
|
message = gitter_message(post)
|
||||||
|
response = Net::HTTP.post_form(URI(channel.data['webhook_url']), message: message)
|
||||||
|
unless response.kind_of? Net::HTTPSuccess
|
||||||
|
error_key = nil
|
||||||
|
raise ::DiscourseChat::ProviderError.new info: { error_key: error_key, message: message, response_body: response.body }
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.gitter_message(post)
|
||||||
|
display_name = post.user.username
|
||||||
|
topic = post.topic
|
||||||
|
parent_category = topic.category.try :parent_category
|
||||||
|
category_name = parent_category ? "[#{parent_category.name}/#{topic.category.name}]" : "[#{topic.category.name}]"
|
||||||
|
|
||||||
|
"[__#{display_name}__ - #{topic.title} - #{category_name}](#{post.full_url})"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
@ -0,0 +1,26 @@
|
|||||||
|
require 'rails_helper'
|
||||||
|
|
||||||
|
RSpec.describe DiscourseChat::Provider::GitterProvider do
|
||||||
|
let(:post) { Fabricate(:post) }
|
||||||
|
|
||||||
|
describe '.trigger_notifications' do
|
||||||
|
before do
|
||||||
|
SiteSetting.chat_integration_gitter_enabled = true
|
||||||
|
end
|
||||||
|
|
||||||
|
let(:chan1) { DiscourseChat::Channel.create!(provider: 'gitter', data: { name: 'gitterHQ/services', webhook_url: 'https://webhooks.gitter.im/e/a1e2i3o4u5' }) }
|
||||||
|
|
||||||
|
it 'sends a webhook request' do
|
||||||
|
stub1 = stub_request(:post, chan1.data['webhook_url']).to_return(body: "OK")
|
||||||
|
described_class.trigger_notification(post, chan1)
|
||||||
|
expect(stub1).to have_been_requested.once
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'handles errors correctly' do
|
||||||
|
stub1 = stub_request(:post, chan1.data['webhook_url']).to_return(status: 404, body: "{ \"error\": \"Not Found\"}")
|
||||||
|
expect(stub1).to have_been_requested.times(0)
|
||||||
|
expect { described_class.trigger_notification(post, chan1) }.to raise_exception(::DiscourseChat::ProviderError)
|
||||||
|
expect(stub1).to have_been_requested.once
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
Loading…
x
Reference in New Issue
Block a user