diff --git a/app/services/slack/send_message.rb b/app/services/slack/send_message.rb new file mode 100644 index 0000000000..2424ee51ae --- /dev/null +++ b/app/services/slack/send_message.rb @@ -0,0 +1,37 @@ +module Slack + class SendMessage + def self.call(message) + new(message).call + end + + def initialize(message) + raise "No slack webhook found" if webhook.nil? + + @message = message + end + + def call + request.body + end + + private + + def webhook + @webhook ||= ENV.fetch("SLACK_ALERT_WEBHOOK", nil) + end + + def request + conn.post do |request| + request.body = @message.to_json + end + end + + def conn + @conn ||= Faraday.new(url: webhook, headers:) + end + + def headers + { "Content-Type" => "application/json" } + end + end +end diff --git a/helm_deploy/apply-for-legal-aid/templates/_envs.tpl b/helm_deploy/apply-for-legal-aid/templates/_envs.tpl index 028d1a18b7..d26b89e985 100644 --- a/helm_deploy/apply-for-legal-aid/templates/_envs.tpl +++ b/helm_deploy/apply-for-legal-aid/templates/_envs.tpl @@ -386,4 +386,9 @@ env: secretKeyRef: name: {{ template "apply-for-legal-aid.fullname" . }} key: maintenanceMode + - name: SLACK_ALERT_WEBHOOK + valueFrom: + secretKeyRef: + name: {{ template "apply-for-legal-aid.fullname" . }} + key: slackAlertWebhook {{- end }} diff --git a/helm_deploy/apply-for-legal-aid/templates/secret.yaml b/helm_deploy/apply-for-legal-aid/templates/secret.yaml index 4a4a419616..88e1b5d16b 100644 --- a/helm_deploy/apply-for-legal-aid/templates/secret.yaml +++ b/helm_deploy/apply-for-legal-aid/templates/secret.yaml @@ -75,3 +75,4 @@ data: encryptionDeterministicKey: {{ .Values.active_record_encryption.deterministic_key | b64enc | quote }} encryptionKeyDerivationSalt: {{ .Values.active_record_encryption.key_derivation_salt | b64enc | quote }} maintenanceMode: {{ .Values.maintenance_mode.enabled | b64enc | quote }} + slackAlertWebhook: {{ .Values.slack_webhooks.alert | b64enc | quote }} diff --git a/helm_deploy/apply-for-legal-aid/values-production.yaml b/helm_deploy/apply-for-legal-aid/values-production.yaml index cfdac0e3c9..10e4b6e1b5 100644 Binary files a/helm_deploy/apply-for-legal-aid/values-production.yaml and b/helm_deploy/apply-for-legal-aid/values-production.yaml differ diff --git a/helm_deploy/apply-for-legal-aid/values-staging.yaml b/helm_deploy/apply-for-legal-aid/values-staging.yaml index be610080f6..2aea2884cd 100644 Binary files a/helm_deploy/apply-for-legal-aid/values-staging.yaml and b/helm_deploy/apply-for-legal-aid/values-staging.yaml differ diff --git a/helm_deploy/apply-for-legal-aid/values-uat.yaml b/helm_deploy/apply-for-legal-aid/values-uat.yaml index a483b23997..4310cb3bc2 100644 Binary files a/helm_deploy/apply-for-legal-aid/values-uat.yaml and b/helm_deploy/apply-for-legal-aid/values-uat.yaml differ diff --git a/spec/services/slack/send_message_spec.rb b/spec/services/slack/send_message_spec.rb new file mode 100644 index 0000000000..3e990aed98 --- /dev/null +++ b/spec/services/slack/send_message_spec.rb @@ -0,0 +1,38 @@ +require "rails_helper" + +RSpec.describe Slack::SendMessage do + subject(:slack_send_message) { described_class } + + before do + allow(ENV).to receive(:fetch) + allow(ENV).to receive(:fetch).with("SLACK_ALERT_WEBHOOK", nil).and_return(webhook_url) + stub_request(:post, webhook_url).to_return(body: "OK", status: 200) + end + + let(:message) { { text: "Hello, again, World!" } } + let(:webhook_url) { "https://hooks.slack.com/services/F4KE/T0K3N/12345678" } + + describe ".call" do + subject(:call) { slack_send_message.new(message).call } + + it "expect call to send a message" do + call + expect(WebMock).to have_requested(:post, webhook_url).once + end + end + + describe "#call" do + subject(:call) { slack_send_message.call(message) } + + it "expect call to send a message" do + call + expect(WebMock).to have_requested(:post, webhook_url).once + end + + describe "when no environment variable is set" do + before { allow(ENV).to receive(:fetch).with("SLACK_ALERT_WEBHOOK", nil).and_return(nil) } + + it { expect { call }.to raise_error("No slack webhook found") } + end + end +end