-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6231 from ministryofjustice/lnd/slack-alerts
Chore: Implement Slack alerts
Showing
7 changed files
with
81 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |