Skip to content

Commit

Permalink
Merge pull request #89 from dwyl/unknow-event
Browse files Browse the repository at this point in the history
catch unknown event
  • Loading branch information
nelsonic authored Mar 14, 2018
2 parents 7c7c2de + 0bc8469 commit 92e11a5
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 9 deletions.
7 changes: 6 additions & 1 deletion lib/app_web/controllers/event_type.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,15 @@ defmodule AppWeb.EventType do
# type("installation_repositories", action, conn, payload)
"issues" -> type("issues", action, conn, payload)
"issue_comment" -> type("issue_comment", action, conn, payload)
_ -> nil
_ -> type("unknow", conn)

end
end

defp type("unknow", conn) do
EventTypeHandlers.unknow_event(conn)
end

defp type("installation", action, conn, payload) do
case action do
"created" -> EventTypeHandlers.new_installation(conn, payload)
Expand Down
6 changes: 6 additions & 0 deletions lib/app_web/controllers/event_type_handlers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ defmodule AppWeb.EventTypeHandlers do
Determines the type of event received by the Github Webhooks requests
"""

def unknow_event(conn) do
conn
|> put_status(404)
|> json(%{ok: "event unkown"})
end

def new_installation(conn, payload) do
token = @github_api.get_installation_token(payload["installation"]["id"])
repositories = payload["repositories"]
Expand Down
17 changes: 9 additions & 8 deletions test/app_web/controllers/event_controller_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ defmodule AppWeb.EventTestController do
alias Plug.Conn

@fixtures [
%{payload: "installation", event: "installation", json_reply: "new installation"},
%{payload: "issue_opened", event: "issues", json_reply: "issue created"},
%{payload: "issue_title_edited", event: "issues", json_reply: "issue edited"},
%{payload: "issue_edited", event: "issues", json_reply: "issue edited"},
%{payload: "comment_created", event: "issue_comment", json_reply: "comment created"},
%{payload: "comment_edited", event: "issue_comment", json_reply: "comment edited"},
%{payload: "comment_deleted", event: "issue_comment", json_reply: "comment deleted"}
%{payload: "installation", event: "installation", json_reply: "new installation", status: 200},
%{payload: "issue_opened", event: "issues", json_reply: "issue created", status: 200},
%{payload: "issue_title_edited", event: "issues", json_reply: "issue edited", status: 200},
%{payload: "issue_edited", event: "issues", json_reply: "issue edited", status: 200},
%{payload: "comment_created", event: "issue_comment", json_reply: "comment created", status: 200},
%{payload: "comment_edited", event: "issue_comment", json_reply: "comment edited", status: 200},
%{payload: "comment_deleted", event: "issue_comment", json_reply: "comment deleted", status: 200},
%{payload: "integration_installation", event: "integration_installation", json_reply: "event unknow", status: 404}
]
|> Enum.map(&(%{&1 | payload: "./test/fixtures/#{&1.payload}.json"}))

Expand All @@ -20,7 +21,7 @@ defmodule AppWeb.EventTestController do
conn = conn
|> Conn.put_req_header("x-github-event", "#{fixture.event}")
|> post("/event/new", payload)
assert json_response(conn, 200)
assert json_response(conn, fixture.status)
end
end
end
74 changes: 74 additions & 0 deletions test/fixtures/integration_installation.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
{
"action": "created",
"installation": {
"id": 108622,
"account": {
"login": "SimonLab",
"id": 6057298,
"avatar_url": "https://avatars2.githubusercontent.com/u/6057298?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/SimonLab",
"html_url": "https://github.com/SimonLab",
"followers_url": "https://api.github.com/users/SimonLab/followers",
"following_url": "https://api.github.com/users/SimonLab/following{/other_user}",
"gists_url": "https://api.github.com/users/SimonLab/gists{/gist_id}",
"starred_url": "https://api.github.com/users/SimonLab/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/SimonLab/subscriptions",
"organizations_url": "https://api.github.com/users/SimonLab/orgs",
"repos_url": "https://api.github.com/users/SimonLab/repos",
"events_url": "https://api.github.com/users/SimonLab/events{/privacy}",
"received_events_url": "https://api.github.com/users/SimonLab/received_events",
"type": "User",
"site_admin": false
},
"repository_selection": "selected",
"access_tokens_url": "https://api.github.com/installations/108622/access_tokens",
"repositories_url": "https://api.github.com/installation/repositories",
"html_url": "https://github.com/settings/installations/108622",
"app_id": 9496,
"target_id": 6057298,
"target_type": "User",
"permissions": {
"issues": "write",
"metadata": "read"
},
"events": [
"issues",
"issue_comment"
],
"created_at": 1520927918,
"updated_at": 1520927918,
"single_file_name": null
},
"repositories": [
{
"id": 40962142,
"name": "cv-2",
"full_name": "SimonLab/cv-2"
},
{
"id": 92847353,
"name": "github_app",
"full_name": "SimonLab/github_app"
}
],
"sender": {
"login": "SimonLab",
"id": 6057298,
"avatar_url": "https://avatars2.githubusercontent.com/u/6057298?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/SimonLab",
"html_url": "https://github.com/SimonLab",
"followers_url": "https://api.github.com/users/SimonLab/followers",
"following_url": "https://api.github.com/users/SimonLab/following{/other_user}",
"gists_url": "https://api.github.com/users/SimonLab/gists{/gist_id}",
"starred_url": "https://api.github.com/users/SimonLab/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/SimonLab/subscriptions",
"organizations_url": "https://api.github.com/users/SimonLab/orgs",
"repos_url": "https://api.github.com/users/SimonLab/repos",
"events_url": "https://api.github.com/users/SimonLab/events{/privacy}",
"received_events_url": "https://api.github.com/users/SimonLab/received_events",
"type": "User",
"site_admin": false
}
}

0 comments on commit 92e11a5

Please sign in to comment.