diff --git a/app/controllers/webhook_controller.rb b/app/controllers/webhook_controller.rb index 1a2a25385..f26d92f30 100644 --- a/app/controllers/webhook_controller.rb +++ b/app/controllers/webhook_controller.rb @@ -1,10 +1,22 @@ class WebhookController < ActionController::Base - before_action :verify_signature! + before_action :verify_helpscout_signature!, only: [:helpscout, :helpscout_support_dev] skip_before_action :verify_authenticity_token + def sendinblue + if Rails.application.secrets.dig(:mattermost, :send_in_blue_outage_webhook_url).present? + send_mattermost_notification( + Rails.application.secrets.dig(:mattermost, :send_in_blue_outage_webhook_url), + message_to_mattermost_send_in_blue_channel + ) + end + end + def helpscout_support_dev - if tagged_dev? && status_active? - send_mattermost_notification(message_to_mattermost_channel) + if tagged_dev? && status_active? && Rails.application.secrets.dig(:mattermost, :support_webhook_url).present? + send_mattermost_notification( + Rails.application.secrets.dig(:mattermost, :support_webhook_url), + message_to_mattermost_support_channel + ) end head :no_content @@ -42,17 +54,15 @@ class WebhookController < ActionController::Base private - def send_mattermost_notification(text) - return if Rails.application.secrets.dig(:mattermost, :support_webhook_url).blank? - + def send_mattermost_notification(url, text) Net::HTTP.post( - URI.parse(Rails.application.secrets.mattermost[:support_webhook_url]), + URI.parse(url), { "text": text }.to_json, "Content-Type" => "application/json" ) end - def message_to_mattermost_channel + def message_to_mattermost_support_channel %Q( Nouveau bug taggué #dev : https://secure.helpscout.net/conversation/#{params["id"]}/#{params["number"]}?folderId=#{params["folderId"]} @@ -62,6 +72,13 @@ Nouveau bug taggué #dev : https://secure.helpscout.net/conversation/#{params["i **utilisateur en attente depuis** : #{params['customerWaitingSince']['friendly']}) end + def message_to_mattermost_send_in_blue_channel + %Q{Incident sur SIB : #{params['title']}. +Etat de SIB: #{params['current_status']} +L'Incident a commencé à #{params['datetime_start']} et est p-e terminé a #{params['datetime_resolve']} +les composant suivants sont affectés : #{params["components"].map { _1['name'] }.join(", ")}} + end + def threads params['_embedded']['threads'] end @@ -83,7 +100,7 @@ Nouveau bug taggué #dev : https://secure.helpscout.net/conversation/#{params["i "Emails##{user.id}" end - def verify_signature! + def verify_helpscout_signature! if generate_body_signature(request.body.read) != request.headers['X-Helpscout-Signature'] request_http_token_authentication end diff --git a/config/env.example.optional b/config/env.example.optional index 8a1d83d1a..71eb6ad3c 100644 --- a/config/env.example.optional +++ b/config/env.example.optional @@ -159,5 +159,7 @@ SAML_DOLIST_CERTIFICATE="spcertificate" SAML_DOLIST_HOST="dolisthoname" DOLIST_LOGIN_URL="https://clientpreprod.dolist.net" -# pour des rappel web +# rappel web du support SUPPORT_WEBHOOK_URL="" +# rappel web de sendinblue +SIB_WEBHOOK_URL="" diff --git a/config/routes.rb b/config/routes.rb index 87b74e2b1..542a6b02c 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -192,6 +192,7 @@ Rails.application.routes.draw do get "contact-admin", to: "support#admin" + post "webhooks/sendinblue", to: "webhook#sendinblue" post "webhooks/helpscout", to: "webhook#helpscout" post "webhooks/helpscout_support_dev", to: "webhook#helpscout_support_dev" match "webhooks/helpscout", to: lambda { |_| [204, {}, nil] }, via: :head diff --git a/config/secrets.yml b/config/secrets.yml index 1e69adc34..286874072 100644 --- a/config/secrets.yml +++ b/config/secrets.yml @@ -59,6 +59,7 @@ defaults: &defaults smtp_key: <%= ENV['SENDINBLUE_SMTP_KEY'] %> api_v3_key: <%= ENV['SENDINBLUE_API_V3_KEY'] %> mattermost: + send_in_blue_outage_webhook_url: <%= ENV['SEND_IN_BLUE_OUTAGE_WEBHOOK_URL'] %> support_webhook_url: <%= ENV['SUPPORT_WEBHOOK_URL'] %> matomo: cookie_domain: "<%= ENV['MATOMO_COOKIE_DOMAIN'] %>" diff --git a/spec/controllers/webhook_controller_spec.rb b/spec/controllers/webhook_controller_spec.rb index 60ff87c1a..e95a00cfb 100644 --- a/spec/controllers/webhook_controller_spec.rb +++ b/spec/controllers/webhook_controller_spec.rb @@ -1,16 +1,16 @@ describe WebhookController, type: :controller do before do - allow(controller).to receive(:verify_signature!).and_return(true) + allow(controller).to receive(:verify_helpscout_signature!).and_return(true) allow(controller).to receive(:verify_authenticity_token) end describe '#helpscout_support_dev' do subject(:response) { post :helpscout_support_dev, params: payload } let(:payload) { JSON.parse(File.read(Rails.root.join('spec', 'fixtures', 'files', 'helpscout', 'tagged-dev.json'))) } - + let(:webhook_url) { "https://notification_url" } it 'works' do - allow(Rails.application.secrets).to receive(:dig).with(:mattermost, :support_webhook_url).and_return("https://notification_url") - expect(controller).to receive(:send_mattermost_notification).with("\nNouveau bug taggué #dev : https://secure.helpscout.net/conversation/123456789/123456789?folderId=123456789\n\n> Bonjour, Je voudrais faire une demande de changement d'adresse et la plateforme m'indique que j'ai plusieurs comptes et que je dois d'abord les fusionner. Cela fait 3 jours que j'essaie de fusio\n\n**personnes impliquées** : anonymous@anon.fr\n**utilisateur en attente depuis** : 11 min ago") + allow(Rails.application.secrets).to receive(:dig).with(:mattermost, :support_webhook_url).and_return(webhook_url) + expect(controller).to receive(:send_mattermost_notification).with(webhook_url, "\nNouveau bug taggué #dev : https://secure.helpscout.net/conversation/123456789/123456789?folderId=123456789\n\n> Bonjour, Je voudrais faire une demande de changement d'adresse et la plateforme m'indique que j'ai plusieurs comptes et que je dois d'abord les fusionner. Cela fait 3 jours que j'essaie de fusio\n\n**personnes impliquées** : anonymous@anon.fr\n**utilisateur en attente depuis** : 11 min ago") subject end end @@ -65,4 +65,16 @@ describe WebhookController, type: :controller do end end end + + describe '#sendinblue' do + subject(:response) { post :sendinblue, params: payload } + let(:payload) { JSON.parse(File.read(Rails.root.join('spec', 'fixtures', 'files', 'sendinblue', 'incident.json'))) } + + it 'sends notification to mattermost' do + notification_url = "https://notification_url" + allow(Rails.application.secrets).to receive(:dig).with(:mattermost, :send_in_blue_outage_webhook_url).and_return(notification_url) + expect(controller).to receive(:send_mattermost_notification).with(notification_url, "Incident sur SIB : Database Issues.\nEtat de SIB: Degraded Performance\nL'Incident a commencé à 2015-04-03T18:27:15+00:00 et est p-e terminé a \nles composant suivants sont affectés : Chat Service, Voice Services, Admin Dashboard") + subject + end + end end diff --git a/spec/fixtures/files/sendinblue/incident.json b/spec/fixtures/files/sendinblue/incident.json new file mode 100644 index 000000000..dd65cc1d5 --- /dev/null +++ b/spec/fixtures/files/sendinblue/incident.json @@ -0,0 +1,36 @@ +{ + "id":"551edb8331a9664b11000005", + "message_id":"531adb8331a9553b11000008", + "title":"Database Issues", + "datetime":"2015-04-03T18:27:15.344Z", + "datetime_start":"2015-04-03T18:27:15+00:00", + "datetime_resolve":"", + "current_status":"Degraded Performance", + "current_state":"Identified", + "previous_status":"", + "previous_state":"", + "infrastructure_affected":[ + {"component":"551ed627b556f14210000005", "container":"5516e01e2e55e4e917000014"}, + {"component":"551ed627b556f14210000005", "container":"551ed5d3590f5a3b10000008"}, + {"component":"551ed5f5590f5a3b10000009", "container":"551ed5d3590f5a3b10000008"}, + {"component":"5516e01e2e55e4e917000015", "container":"5516e01e2e55e4e917000014"}, + {"component":"5516e01e2e55e4e917000015", "container":"551ed5d3590f5a3b10000008"} + ], + "components":[ + {"name":"Chat Service", + "_id":"551ed627b556f14210000005"}, + {"name":"Voice Services", + "_id":"551ed5f5590f5a3b10000009"}, + {"name":"Admin Dashboard", + "_id":"5516e01e2e55e4e917000015"} + ], + "containers":[ + {"name":"Ireland", + "_id":"5516e01e2e55e4e917000014"}, + {"name":"London", + "_id":"551ed5d3590f5a3b10000008"} + ], + "details":"A database instance has become unhealthy and removed from the cluster. Europe based endpoints will continue experiencing higher than normal latency until a new instance is fired up. Updates to follow.", + "incident_url":"https://status.io/pages/incident/5516e01e2e55e4e917000005/5116e01e2e33e4e413000001", + "status_page_url":"https://status.io/pages/5516e01e2e55e4e917000005" +}