@@ -25,7 +25,7 @@ defmodule CodeCorps.GitHub.Webhook.Handler do
2525 """
2626 @ spec handle_supported ( String . t , String . t , map ) :: { :ok , GithubEvent . t }
2727 def handle_supported ( type , id , % { } = payload ) do
28- with { :ok , % GithubEvent { } = event } <- type |> build_params ( id , "unprocessed" , payload ) |> create_event ( ) do
28+ with { :ok , % GithubEvent { } = event } <- find_or_create_event ( type , id , payload , "unprocessed" ) do
2929 payload |> apply_handler ( type ) |> Event . stop_processing ( event )
3030 end
3131 end
@@ -41,7 +41,7 @@ defmodule CodeCorps.GitHub.Webhook.Handler do
4141 """
4242 @ spec handle_unsupported ( String . t , String . t , map ) :: { :ok , GithubEvent . t }
4343 def handle_unsupported ( type , id , % { } = payload ) do
44- type |> build_params ( id , "unsupported" , payload ) |> create_event ( )
44+ find_or_create_event ( type , id , payload , "unsupported" )
4545 end
4646
4747 @ spec build_params ( String . t , String . t , String . t , map ) :: map
@@ -55,6 +55,14 @@ defmodule CodeCorps.GitHub.Webhook.Handler do
5555 }
5656 end
5757
58+ @ spec find_or_create_event ( String . t , String . t , map , String . t ) :: { :ok , GithubEvent . t }
59+ defp find_or_create_event ( type , id , payload , status ) do
60+ case GithubEvent |> Repo . get_by ( github_delivery_id: id ) do
61+ nil -> type |> build_params ( id , status , payload ) |> create_event ( )
62+ % GithubEvent { } = github_event -> { :ok , github_event }
63+ end
64+ end
65+
5866 @ spec create_event ( map ) :: { :ok , GithubEvent . t }
5967 defp create_event ( % { } = params ) do
6068 % GithubEvent { } |> GithubEvent . changeset ( params ) |> Repo . insert ( )
0 commit comments