Skip to content

Commit

Permalink
Merge pull request #6231 from ministryofjustice/lnd/slack-alerts
Browse files Browse the repository at this point in the history
Chore: Implement Slack alerts
colinbruce authored Jan 26, 2024
2 parents 4d410aa + e20b840 commit 5f05a02
Showing 7 changed files with 81 additions and 0 deletions.
37 changes: 37 additions & 0 deletions app/services/slack/send_message.rb
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions helm_deploy/apply-for-legal-aid/templates/_envs.tpl
Original file line number Diff line number Diff line change
@@ -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 }}
1 change: 1 addition & 0 deletions helm_deploy/apply-for-legal-aid/templates/secret.yaml
Original file line number Diff line number Diff line change
@@ -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 }}
Binary file modified helm_deploy/apply-for-legal-aid/values-production.yaml
Binary file not shown.
Binary file modified helm_deploy/apply-for-legal-aid/values-staging.yaml
Binary file not shown.
Binary file modified helm_deploy/apply-for-legal-aid/values-uat.yaml
Binary file not shown.
38 changes: 38 additions & 0 deletions spec/services/slack/send_message_spec.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 5f05a02

Please sign in to comment.