-
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 #46 from pluralsh/slack-notifs
slack notif sync
- Loading branch information
Showing
5 changed files
with
113 additions
and
5 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,58 @@ | ||
defmodule Console.Webhooks.Formatter.Slack do | ||
use Console.Webhooks.Formatter | ||
alias Console.Schema.Build | ||
alias Console.Schema.{Build, Notification} | ||
alias Console.Services.Plural | ||
|
||
@impl Formatter | ||
def format(%Build{status: status, repository: repo, id: id} = build) do | ||
cluster_name = Console.Services.Plural.cluster_name() | ||
cluster_name = Plural.cluster_name() | ||
{:ok, %{attachments: [ | ||
%{ | ||
color: color(status), | ||
blocks: [ | ||
%{type: :section, text: %{ | ||
type: "mrkdwn", | ||
section(text: mrkdwn( | ||
text: "[cluster=#{cluster_name}] #{emoji(status)}#{status_modifier(status)} #{repo} using console: <#{build_url(id)}|build logs>\n\n#{build.message}" | ||
}} | ||
)) | ||
] | ||
} | ||
]}} | ||
end | ||
|
||
def format(%Notification{title: title, description: desc, repository: repo, severity: sev, annotations: anns, labels: labels}) do | ||
app = Plural.application(repo) | ||
cluster_name = Plural.cluster_name() | ||
{:ok, %{ | ||
blocks: [ | ||
section( | ||
text: mrkdwn(text: "New Notification in cluster: #{cluster_name} for #{repo}\n*#{title}*"), | ||
accessory: image(image_url: Plural.app_icon(app), alt_text: repo) | ||
), | ||
section(text: mrkdwn(text: desc)), | ||
section(fields: [ | ||
mrkdwn(text: "*Severity*\n#{sev}"), | ||
mrkdwn(text: "*Annotations*\n#{format_pairs(anns)}"), | ||
mrkdwn(text: "*Labels*\n#{format_pairs(labels)}") | ||
]) | ||
] | ||
}} | ||
end | ||
|
||
defp section(attrs), do: block(:section, attrs) | ||
|
||
defp mrkdwn(attrs), do: block(:mrkdwn, attrs) | ||
|
||
defp image(attrs), do: block(:image, attrs) | ||
|
||
defp format_pairs(pairs) do | ||
Enum.map(pairs, fn {k, v} -> "*#{k}*: #{v}" end) | ||
|> Enum.join(" ") | ||
end | ||
|
||
defp block(type, attrs) do | ||
Map.new(attrs) | ||
|> Map.put(:type, type) | ||
end | ||
|
||
defp emoji(:successful), do: ":rocket: " | ||
defp emoji(_), do: "" | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
defmodule Console.Webhook.NotificationTest do | ||
use Console.DataCase, async: false | ||
use Mimic | ||
alias Kube.Application | ||
alias Console.PubSub.Consumers.Webhook | ||
alias Console.PubSub | ||
|
||
setup :set_mimic_global | ||
|
||
describe "NotificationCreated" do | ||
test "it will send a failed webhook" do | ||
notif = insert(:notification) | ||
%{url: url} = wh = insert(:webhook, type: :slack) | ||
|
||
myself = self() | ||
expect(HTTPoison, :post, fn ^url, payload, _ -> | ||
decoded = Jason.decode!(payload) | ||
send myself, {:payload, decoded} | ||
{:ok, decoded} | ||
end) | ||
|
||
expect(Kazan, :run, fn _ -> | ||
{:ok, %Application{spec: %Application.Spec{descriptor: %{links: [%{url: "img"}]}}}} | ||
end) | ||
|
||
event = %PubSub.NotificationCreated{item: notif} | ||
Webhook.handle_event(event) | ||
|
||
assert_receive {:payload, %{"blocks" => [first, second, third]}} | ||
|
||
assert refetch(wh).health == :healthy | ||
|
||
assert first["text"] | ||
assert first["accessory"] | ||
|
||
assert second["text"] | ||
|
||
assert third["fields"] | ||
end | ||
end | ||
end |