Skip to content

Commit 49ef5a9

Browse files
committed
refactor: replace notify_event with alert in GitHub and Stripe controllers
- Removed the notify_event function and replaced its usage with the alert function for better error handling and logging. - Updated the alert function to format messages for both successful and error events in the GitHub and Stripe webhooks.
1 parent dda1c48 commit 49ef5a9

File tree

2 files changed

+20
-176
lines changed

2 files changed

+20
-176
lines changed

lib/algora_web/controllers/webhooks/github_controller.ex

Lines changed: 10 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ defmodule AlgoraWeb.Webhooks.GithubController do
55
import Ecto.Query
66

77
alias Algora.Accounts
8-
alias Algora.Activities.SendDiscord
98
alias Algora.Bounties
109
alias Algora.Bounties.Bounty
1110
alias Algora.Bounties.Claim
@@ -30,21 +29,20 @@ defmodule AlgoraWeb.Webhooks.GithubController do
3029
{:ok, commands} <- process_commands(webhook),
3130
:ok <- process_event(webhook, commands) do
3231
Logger.debug("✅ #{inspect(webhook.event_action)}")
33-
notify_event(webhook, :ok)
3432
:ok
3533
else
3634
{:error, :bot_event} ->
3735
:ok
3836

3937
{:error, reason} ->
4038
Logger.error("❌ #{inspect(webhook.event_action)}: #{inspect(reason)}")
41-
notify_event(webhook, {:error, reason})
39+
alert(webhook, {:error, reason})
4240
{:error, reason}
4341
end
4442
rescue
4543
error ->
4644
Logger.error("❌ #{inspect(webhook.event_action)}: #{inspect(error)}")
47-
notify_event(webhook, {:error, error})
45+
alert(webhook, {:error, error})
4846
{:error, error}
4947
end
5048

@@ -700,101 +698,17 @@ defmodule AlgoraWeb.Webhooks.GithubController do
700698
end
701699
end
702700

703-
defp notify_event(%Webhook{event_action: event_action, author: author, payload: payload} = webhook, :ok) do
704-
with github_ticket when not is_nil(github_ticket) <- get_github_ticket(webhook),
705-
ticket when not is_nil(ticket) <-
706-
Workspace.get_ticket(
707-
payload["repository"]["owner"]["login"],
708-
payload["repository"]["name"],
709-
github_ticket["number"]
710-
) do
711-
discord_payload = %{
712-
payload: %{
713-
embeds: [
714-
%{
715-
color: 0x64748B,
716-
title: event_action,
717-
author: %{
718-
name: payload["repository"]["owner"]["login"],
719-
icon_url: payload["repository"]["owner"]["avatar_url"],
720-
url: github_ticket["html_url"]
721-
},
722-
footer: %{
723-
text: author["login"],
724-
icon_url: author["avatar_url"]
725-
},
726-
fields: [
727-
%{
728-
name: "Ticket",
729-
value: "#{payload["repository"]["name"]}##{github_ticket["number"]}: #{github_ticket["title"]}",
730-
inline: false
731-
}
732-
],
733-
url: github_ticket["html_url"],
734-
timestamp: DateTime.utc_now()
735-
}
736-
]
737-
}
738-
}
739-
740-
case discord_payload |> SendDiscord.changeset() |> Oban.insert() do
741-
{:ok, _} ->
742-
:ok
701+
defp alert(%Webhook{event_action: event_action} = webhook, {:error, error}) do
702+
message =
703+
case get_github_ticket(webhook) do
704+
github_ticket when not is_nil(github_ticket) ->
705+
"Error processing event: #{event_action}. Ticket: #{github_ticket["html_url"]}. Hook ID: #{webhook.hook_id}. Error: #{inspect(error)}"
743706

744-
{:error, reason} ->
745-
Logger.error("Error sending discord notification: #{inspect(reason)}")
746-
{:error, reason}
707+
_ ->
708+
"Error processing event: #{event_action}. Hook ID: #{webhook.hook_id}. Error: #{inspect(error)}"
747709
end
748-
else
749-
_ -> :ok
750-
end
751-
end
752-
753-
defp notify_event(%Webhook{event_action: event_action, author: author, payload: payload} = webhook, {:error, error}) do
754-
case get_github_ticket(webhook) do
755-
github_ticket when not is_nil(github_ticket) ->
756-
discord_payload = %{
757-
payload: %{
758-
embeds: [
759-
%{
760-
color: 0xEF4444,
761-
title: event_action,
762-
description: inspect(error),
763-
author: %{
764-
name: payload["repository"]["owner"]["login"],
765-
icon_url: payload["repository"]["owner"]["avatar_url"],
766-
url: github_ticket["html_url"]
767-
},
768-
footer: %{
769-
text: author["login"],
770-
icon_url: author["avatar_url"]
771-
},
772-
fields: [
773-
%{
774-
name: "Ticket",
775-
value: "#{payload["repository"]["name"]}##{github_ticket["number"]}: #{github_ticket["title"]}",
776-
inline: false
777-
}
778-
],
779-
url: github_ticket["html_url"],
780-
timestamp: DateTime.utc_now()
781-
}
782-
]
783-
}
784-
}
785710

786-
case discord_payload |> SendDiscord.changeset() |> Oban.insert() do
787-
{:ok, _} ->
788-
:ok
789-
790-
{:error, reason} ->
791-
Logger.error("Error sending discord notification: #{inspect(reason)}")
792-
{:error, reason}
793-
end
794-
795-
_ ->
796-
:ok
797-
end
711+
Algora.Admin.alert(message, :error)
798712
end
799713

800714
defp get_github_ticket(%Webhook{event: event, payload: payload}) do

lib/algora_web/controllers/webhooks/stripe_controller.ex

Lines changed: 10 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,18 @@ defmodule AlgoraWeb.Webhooks.StripeController do
3232
case result do
3333
:ok ->
3434
Logger.debug("✅ #{inspect(event.type)}")
35-
notify_event(event, :ok)
35+
alert(event, :ok)
3636
:ok
3737

3838
{:error, reason} ->
3939
Logger.error("❌ #{inspect(event.type)}: #{inspect(reason)}")
40-
notify_event(event, {:error, reason})
40+
alert(event, {:error, reason})
4141
{:error, reason}
4242
end
4343
rescue
4444
error ->
4545
Logger.error("❌ #{inspect(event.type)}: #{inspect(error)}")
46-
notify_event(event, {:error, error})
46+
alert(event, {:error, error})
4747
{:error, error}
4848
end
4949

@@ -193,84 +193,14 @@ defmodule AlgoraWeb.Webhooks.StripeController do
193193
end)
194194
end
195195

196-
defp notify_event(%Stripe.Event{} = event, :ok) do
197-
discord_payload = %{
198-
payload: %{
199-
embeds: [
200-
%{
201-
color: 0x64748B,
202-
title: event.type,
203-
footer: %{
204-
text: "Stripe",
205-
icon_url: "https://github.com/stripe.png"
206-
},
207-
fields: [
208-
%{
209-
name: "Event",
210-
value: event.id,
211-
inline: false
212-
},
213-
%{
214-
name: event.data.object.object,
215-
value: event.data.object.id,
216-
inline: false
217-
}
218-
],
219-
url: "https://dashboard.stripe.com/payments?status[0]=successful",
220-
timestamp: DateTime.utc_now()
221-
}
222-
]
223-
}
224-
}
225-
226-
case discord_payload |> SendDiscord.changeset() |> Oban.insert() do
227-
{:ok, _} ->
228-
:ok
229-
230-
{:error, reason} ->
231-
Logger.error("Error sending discord notification: #{inspect(reason)}")
232-
{:error, reason}
233-
end
196+
defp alert(%Stripe.Event{} = event, :ok) do
197+
Algora.Admin.alert("Stripe event: #{event.type} #{event.id} https://dashboard.stripe.com/logs?success=true", :info)
234198
end
235199

236-
defp notify_event(%Stripe.Event{} = event, {:error, error}) do
237-
discord_payload = %{
238-
payload: %{
239-
embeds: [
240-
%{
241-
color: 0xEF4444,
242-
title: event.type,
243-
description: inspect(error),
244-
footer: %{
245-
text: "Stripe",
246-
icon_url: "https://github.com/stripe.png"
247-
},
248-
fields: [
249-
%{
250-
name: "Event",
251-
value: event.id,
252-
inline: false
253-
},
254-
%{
255-
name: event.data.object.object,
256-
value: event.data.object.id,
257-
inline: false
258-
}
259-
],
260-
url: "https://dashboard.stripe.com/payments?status[0]=failed",
261-
timestamp: DateTime.utc_now()
262-
}
263-
]
264-
}
265-
}
266-
267-
case discord_payload |> SendDiscord.changeset() |> Oban.insert() do
268-
{:ok, _} ->
269-
:ok
270-
271-
{:error, reason} ->
272-
Logger.error("Error sending discord notification: #{inspect(reason)}")
273-
{:error, reason}
274-
end
200+
defp alert(%Stripe.Event{} = event, {:error, error}) do
201+
Algora.Admin.alert(
202+
"Stripe event: #{event.type} #{event.id} https://dashboard.stripe.com/logs?success=false Error: #{inspect(error)}",
203+
:error
204+
)
275205
end
276206
end

0 commit comments

Comments
 (0)