Skip to content

Commit 190f295

Browse files
authored
Merge pull request #1254 from code-corps/issue-1221
Ignore repo syncs for projects without project id.
2 parents 937e1b3 + 1b11b03 commit 190f295

File tree

2 files changed

+36
-8
lines changed

2 files changed

+36
-8
lines changed

lib/code_corps_web/controllers/github_event_controller.ex

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
defmodule 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

test/lib/code_corps_web/controllers/github_event_controller_test.exs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,22 @@ defmodule CodeCorpsWeb.GithubEventControllerTest do
101101
test "responds with 200 for an unsupported event", %{conn: conn} do
102102
path = conn |> github_events_path(:create)
103103
payload = load_event_fixture("pull_request_synchronize")
104+
insert(:github_repo, github_id: payload["repository"]["id"])
104105
assert conn |> for_event("pull_request", "foo") |> post(path, payload) |> response(200)
105106

106107
assert Repo.get_by(GithubEvent, github_delivery_id: "foo")
107108
end
108109

110+
@tag :github_webhook
111+
test "responds with 202 for a supported event but no project_id", %{conn: conn} do
112+
path = conn |> github_events_path(:create)
113+
payload = load_event_fixture("pull_request_synchronize")
114+
insert(:github_repo, github_id: payload["repository"]["id"], project: nil)
115+
assert conn |> for_event("pull_request", "foo") |> post(path, payload) |> response(202)
116+
117+
refute Repo.get_by(GithubEvent, github_delivery_id: "foo")
118+
end
119+
109120
@tag :github_webhook
110121
test "responds with 202 for an unknown event", %{conn: conn} do
111122
path = conn |> github_events_path(:create)

0 commit comments

Comments
 (0)