@@ -11,46 +11,59 @@ defmodule CodeCorps.GitHub.Webhook.Handler do
1111 GitHub.Event.IssueComment ,
1212 GitHub.Event.Issues ,
1313 GitHub.Event.PullRequest ,
14- GitHub.Webhook.EventSupport ,
1514 Repo
1615 }
1716
1817 @ doc """
19- Handles a GitHub event based on its type.
18+ Handles a fully supported GitHub event based on its type and action.
19+
20+ The handling process consistes of 3 steps
21+
22+ - create event record marked as "unprocessed"
23+ - mark event record as processing and handle it
24+ - mark event record as processed or errored depending on handling outcome
2025 """
21- def handle ( type , id , payload ) do
22- with % { } = params <- build_params ( type , id , payload ) ,
23- { :ok , % GithubEvent { } = event } <- params |> create_event ( ) ,
24- { :ok , % GithubEvent { status: "processing" } = event } <- event |> Event . start_processing
25- do
26- payload |> do_handle ( type ) |> Event . stop_processing ( event )
26+ @ spec handle_supported ( String . t , String . t , map ) :: { :ok , GithubEvent . t }
27+ def handle_supported ( type , id , % { } = payload ) do
28+ with { :ok , % GithubEvent { } = event } <- type |> build_params ( id , "unprocessed" , payload ) |> create_event ( ) do
29+ payload |> apply_handler ( type ) |> Event . stop_processing ( event )
2730 end
2831 end
2932
30- defp build_params ( type , id , % { "action" => action , "sender" => _ } = payload ) do
33+ @ doc ~S"""
34+ Handles an unsupported supported GitHub event.
35+
36+ "unsupported" means that, while we generally support this event type,
37+ we do not yet support this specific event action.
38+
39+ The process consistes of simply storing the event and marking it as
40+ "unsupported".
41+ """
42+ @ spec handle_unsupported ( String . t , String . t , map ) :: { :ok , GithubEvent . t }
43+ def handle_unsupported ( type , id , % { } = payload ) do
44+ type |> build_params ( id , "unsupported" , payload ) |> create_event ( )
45+ end
46+
47+ @ spec build_params ( String . t , String . t , String . t , map ) :: map
48+ defp build_params ( type , id , status , % { "action" => action } = payload ) do
3149 % {
3250 action: action ,
3351 github_delivery_id: id ,
3452 payload: payload ,
35- status: type |> get_status ( ) ,
53+ status: status ,
3654 type: type
3755 }
3856 end
3957
40- defp create_event ( params ) do
41- % GithubEvent { } |> GithubEvent . changeset ( params ) |> Repo . insert
42- end
43-
44- defp get_status ( type ) do
45- case EventSupport . status ( type ) do
46- :unsupported -> "unhandled"
47- :supported -> "unprocessed"
48- end
58+ @ spec create_event ( map ) :: { :ok , GithubEvent . t }
59+ defp create_event ( % { } = params ) do
60+ % GithubEvent { } |> GithubEvent . changeset ( params ) |> Repo . insert ( )
4961 end
5062
51- defp do_handle ( payload , "installation" ) , do: Installation . handle ( payload )
52- defp do_handle ( payload , "installation_repositories" ) , do: InstallationRepositories . handle ( payload )
53- defp do_handle ( payload , "issue_comment" ) , do: IssueComment . handle ( payload )
54- defp do_handle ( payload , "issues" ) , do: Issues . handle ( payload )
55- defp do_handle ( payload , "pull_request" ) , do: PullRequest . handle ( payload )
63+ @ spec apply_handler ( map , String . t ) :: tuple
64+ defp apply_handler ( payload , "installation" ) , do: Installation . handle ( payload )
65+ defp apply_handler ( payload , "installation_repositories" ) , do: InstallationRepositories . handle ( payload )
66+ defp apply_handler ( payload , "issue_comment" ) , do: IssueComment . handle ( payload )
67+ defp apply_handler ( payload , "issues" ) , do: Issues . handle ( payload )
68+ defp apply_handler ( payload , "pull_request" ) , do: PullRequest . handle ( payload )
5669end
0 commit comments