11defmodule CodeCorpsWeb.GithubEventController do
22 @ moduledoc false
33 use CodeCorpsWeb , :controller
4+ import Ecto.Query , only: [ from: 2 ]
45
56 alias CodeCorps . {
67 GithubEvent ,
8+ GithubRepo ,
79 GitHub.Webhook.Handler ,
810 GitHub.Webhook.EventSupport ,
911 Helpers.Query ,
@@ -43,14 +45,29 @@ defmodule CodeCorpsWeb.GithubEventController do
4345
4446 @ spec create ( Conn . t , map ) :: Conn . t
4547 def create ( % Conn { } = conn , % { } = payload ) do
46- type = conn |> get_event_type
47- delivery_id = conn |> get_delivery_id ( )
48+ type = conn |> event_type ( )
49+ delivery_id = conn |> delivery_id ( )
4850 action = payload |> Map . get ( "action" , "" )
49- event_support = type |> EventSupport . status ( action )
50- event_support |> process_event ( type , delivery_id , payload )
51+ event_support =
52+ if should_process? ( payload ) do
53+ process_status = type |> EventSupport . status ( action )
54+ process_status |> process_event ( type , delivery_id , payload )
55+ process_status
56+ else
57+ :ignored
58+ end
5159 conn |> respond_to_webhook ( event_support )
5260 end
5361
62+ @ spec should_process? ( map ) :: boolean
63+ defp should_process? ( % { "repository" => % { "id" => repository_id } } ) do
64+ query = from repo in GithubRepo ,
65+ where: repo . github_id == ^ repository_id ,
66+ where: not ( is_nil ( repo . project_id ) )
67+ Repo . one ( query ) != nil
68+ end
69+ defp should_process? ( _ ) , do: true
70+
5471 @ spec update ( Conn . t , map ) :: Conn . t
5572 def update ( % Conn { } = conn , % { "id" => id } = params ) do
5673 with % GithubEvent { } = github_event <- GithubEvent |> Repo . get ( id ) ,
@@ -63,13 +80,13 @@ defmodule CodeCorpsWeb.GithubEventController do
6380 end
6481 end
6582
66- @ spec get_event_type ( Conn . t ) :: String . t
67- defp get_event_type ( % Conn { } = conn ) do
83+ @ spec event_type ( Conn . t ) :: String . t
84+ defp event_type ( % Conn { } = conn ) do
6885 conn |> get_req_header ( "x-github-event" ) |> List . first
6986 end
7087
71- @ spec get_delivery_id ( Conn . t ) :: String . t
72- defp get_delivery_id ( % Conn { } = conn ) do
88+ @ spec delivery_id ( Conn . t ) :: String . t
89+ defp delivery_id ( % Conn { } = conn ) do
7390 conn |> get_req_header ( "x-github-delivery" ) |> List . first
7491 end
7592
0 commit comments