Skip to content

Commit 020888d

Browse files
committed
Refactor webhook handler to take only the payload
1 parent b6a6b60 commit 020888d

File tree

10 files changed

+104
-144
lines changed

10 files changed

+104
-144
lines changed

lib/code_corps/github/event/handler.ex

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,12 @@ defmodule CodeCorps.GitHub.Event.Handler do
33
Default behavior for all GitHub webhook event handlers.
44
"""
55

6-
alias CodeCorps.GithubEvent
7-
86
@doc ~S"""
97
The only entry point a GitHub webhook event handler function should contain.
108
11-
Receives a `CodeCorps.GithubEvent` record and a payload, returns an `:ok`
12-
tuple if the process was successful, or an `:error` tuple, where the second
13-
element is an atom, if it failed.
9+
Receives the GitHub payload, returns an `:ok` tuple if the process was
10+
successful, or an `:error` tuple, where the second element is an atom, if it
11+
failed.
1412
"""
15-
@callback handle(GithubEvent.t, map) :: {:ok, any} | {:error, atom}
13+
@callback handle(map) :: {:ok, any} | {:error, atom}
1614
end

lib/code_corps/github/event/installation.ex

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ defmodule CodeCorps.GitHub.Event.Installation do
88

99
alias CodeCorps.{
1010
GithubAppInstallation,
11-
GithubEvent,
1211
GitHub.Event.Installation,
1312
Repo,
1413
User
@@ -48,8 +47,8 @@ defmodule CodeCorps.GitHub.Event.Installation do
4847
If a step in the process failes, an `:error` tuple will be returned, where the
4948
second element is an atom indicating which step of the process failed.
5049
"""
51-
@spec handle(GithubEvent.t, map) :: outcome
52-
def handle(%GithubEvent{}, payload) do
50+
@spec handle(map) :: outcome
51+
def handle(payload) do
5352
Multi.new
5453
|> Multi.run(:payload, fn _ -> payload |> validate_payload() end)
5554
|> Multi.run(:action, fn _ -> payload |> validate_action() end)

lib/code_corps/github/event/installation_repositories.ex

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ defmodule CodeCorps.GitHub.Event.InstallationRepositories do
1010

1111
alias CodeCorps.{
1212
GithubAppInstallation,
13-
GithubEvent,
1413
GithubRepo,
1514
GitHub.Event.Common.ResultAggregator,
1615
GitHub.Event.InstallationRepositories,
@@ -43,8 +42,8 @@ defmodule CodeCorps.GitHub.Event.InstallationRepositories do
4342
`CodeCorps.ProjectGithubRepo` records, they are deleted automatically,
4443
due to `on_delete: :delete_all` set at the database level.
4544
"""
46-
@spec handle(GithubEvent.t, map) :: outcome
47-
def handle(%GithubEvent{}, payload) do
45+
@spec handle(map) :: outcome
46+
def handle(payload) do
4847
Multi.new
4948
|> Multi.run(:payload, fn _ -> payload |> validate_payload() end)
5049
|> Multi.run(:action, fn _ -> payload |> validate_action() end)

lib/code_corps/github/event/issue_comment.ex

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ defmodule CodeCorps.GitHub.Event.IssueComment do
88

99
alias CodeCorps.{
1010
Comment,
11-
GithubEvent,
1211
GitHub.Event.Common.RepoFinder,
1312
GitHub.Event.Issues,
1413
GitHub.Event.Issues.TaskSyncer,
@@ -50,8 +49,8 @@ defmodule CodeCorps.GitHub.Event.IssueComment do
5049
If it fails, it will instead return an `:error` tuple, where the second
5150
element is the atom indicating a reason.
5251
"""
53-
@spec handle(GithubEvent.t, map) :: outcome
54-
def handle(%GithubEvent{}, payload) do
52+
@spec handle(map) :: outcome
53+
def handle(payload) do
5554
Multi.new
5655
|> Multi.run(:payload, fn _ -> payload |> validate_payload() end)
5756
|> Multi.run(:action, fn _ -> payload |> validate_action() end)

lib/code_corps/github/event/issues.ex

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ defmodule CodeCorps.GitHub.Event.Issues do
88
@behaviour CodeCorps.GitHub.Event.Handler
99

1010
alias CodeCorps.{
11-
GithubEvent,
1211
GitHub.Event.Common.RepoFinder,
1312
GitHub.Event.Issues.TaskSyncer,
1413
GitHub.Event.Issues.UserLinker,
@@ -45,8 +44,8 @@ defmodule CodeCorps.GitHub.Event.Issues do
4544
If it fails, it will instead return an `:error` tuple, where the second
4645
element is the atom indicating a reason.
4746
"""
48-
@spec handle(GithubEvent.t, map) :: outcome
49-
def handle(%GithubEvent{}, %{} = payload) do
47+
@spec handle(map) :: outcome
48+
def handle(payload) do
5049
Multi.new
5150
|> Multi.run(:payload, fn _ -> payload |> validate_payload() end)
5251
|> Multi.run(:action, fn _ -> payload |> validate_action() end)

lib/code_corps/github/webhook/handler.ex

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ defmodule CodeCorps.GitHub.Webhook.Handler do
2020
def handle(type, id, payload) do
2121
with %{} = params <- build_params(type, id, payload),
2222
{:ok, %GithubEvent{} = event} <- params |> create_event(),
23-
{:ok, %GithubEvent{status: "processing"} = processing_event} <- event |> Event.start_processing
23+
{:ok, %GithubEvent{status: "processing"} = event} <- event |> Event.start_processing
2424
do
25-
processing_event |> do_handle(payload) |> Event.stop_processing(event)
25+
payload |> do_handle(type) |> Event.stop_processing(event)
2626
end
2727
end
2828

@@ -47,8 +47,8 @@ defmodule CodeCorps.GitHub.Webhook.Handler do
4747
end
4848
end
4949

50-
defp do_handle(%GithubEvent{type: "installation"} = event, payload), do: Installation.handle(event, payload)
51-
defp do_handle(%GithubEvent{type: "installation_repositories"} = event, payload), do: InstallationRepositories.handle(event, payload)
52-
defp do_handle(%GithubEvent{type: "issue_comment"} = event, payload), do: IssueComment.handle(event, payload)
53-
defp do_handle(%GithubEvent{type: "issues"} = event, payload), do: Issues.handle(event, payload)
50+
defp do_handle(payload, "installation"), do: Installation.handle(payload)
51+
defp do_handle(payload, "installation_repositories"), do: InstallationRepositories.handle(payload)
52+
defp do_handle(payload, "issue_comment"), do: IssueComment.handle(payload)
53+
defp do_handle(payload, "issues"), do: Issues.handle(payload)
5454
end

test/lib/code_corps/github/event/installation_repositories_test.exs

Lines changed: 15 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,22 @@ defmodule CodeCorps.GitHub.Event.InstallationRepositoriesTest do
1212
Repo
1313
}
1414

15-
describe "handle/2" do
15+
describe "handle/1" do
1616
@payload load_event_fixture("installation_repositories_added")
1717

1818
test "marks event as errored if invalid action" do
1919
payload = @payload |> Map.put("action", "foo")
20-
event = build(:github_event, action: "foo", type: "installation_repositories")
21-
assert {:error, :unexpected_action} == InstallationRepositories.handle(event, payload)
20+
assert {:error, :unexpected_action} == InstallationRepositories.handle(payload)
2221
end
2322

2423
test "marks event as errored if invalid payload" do
25-
event = build(:github_event, action: "added", type: "installation_repositories")
2624
payload = @payload |> Map.delete("action")
2725
assert {:error, :unexpected_payload} ==
28-
InstallationRepositories.handle(event, payload)
26+
InstallationRepositories.handle(payload)
2927
end
3028
end
3129

32-
describe "handle/2 for InstallationRepositories::added" do
30+
describe "handle/1 for InstallationRepositories::added" do
3331
@payload load_event_fixture("installation_repositories_added")
3432

3533
test "creates repos" do
@@ -40,8 +38,7 @@ defmodule CodeCorps.GitHub.Event.InstallationRepositoriesTest do
4038

4139
%{id: installation_id} = insert(:github_app_installation, github_id: installation_github_id)
4240

43-
event = build(:github_event, action: "added", type: "installation_repositories")
44-
{:ok, [%GithubRepo{}, %GithubRepo{}]} = InstallationRepositories.handle(event, @payload)
41+
{:ok, [%GithubRepo{}, %GithubRepo{}]} = InstallationRepositories.handle(@payload)
4542

4643
github_repo_1 = Repo.get_by(GithubRepo, github_id: repo_1_payload["id"])
4744
assert github_repo_1
@@ -63,8 +60,7 @@ defmodule CodeCorps.GitHub.Event.InstallationRepositoriesTest do
6360
installation = insert(:github_app_installation, github_id: installation_github_id)
6461
preinserted_repo = insert(:github_repo, github_app_installation: installation, github_id: repo_1_payload["id"])
6562

66-
event = build(:github_event, action: "added", type: "installation_repositories")
67-
{:ok, [%GithubRepo{}, %GithubRepo{}]} = InstallationRepositories.handle(event, @payload)
63+
{:ok, [%GithubRepo{}, %GithubRepo{}]} = InstallationRepositories.handle(@payload)
6864

6965
github_repo_1 = Repo.get_by(GithubRepo, github_id: repo_1_payload["id"])
7066
assert github_repo_1.id == preinserted_repo.id
@@ -78,23 +74,20 @@ defmodule CodeCorps.GitHub.Event.InstallationRepositoriesTest do
7874
end
7975

8076
test "marks event as errored if invalid instalation payload" do
81-
event = insert(:github_event, action: "added", type: "installation_repositories")
82-
assert {:error, :unexpected_payload} == InstallationRepositories.handle(event, @payload |> Map.put("installation", "foo"))
77+
assert {:error, :unexpected_payload} == InstallationRepositories.handle(@payload |> Map.put("installation", "foo"))
8378
end
8479

8580
test "marks event as errored if invalid repo payload" do
86-
event = insert(:github_event, action: "added", type: "installation_repositories")
8781
insert(:github_app_installation, github_id: @payload["installation"]["id"])
88-
assert {:error, :unexpected_payload} == InstallationRepositories.handle(event, @payload |> Map.put("repositories_added", ["foo"]))
82+
assert {:error, :unexpected_payload} == InstallationRepositories.handle(@payload |> Map.put("repositories_added", ["foo"]))
8983
end
9084

9185
test "marks event as errored if no installation" do
92-
event = insert(:github_event, action: "added", type: "installation_repositories")
93-
assert {:error, :unmatched_installation} == InstallationRepositories.handle(event, @payload)
86+
assert {:error, :unmatched_installation} == InstallationRepositories.handle(@payload)
9487
end
9588
end
9689

97-
describe "handle/2 for InstallationRepositories::removed" do
90+
describe "handle/1 for InstallationRepositories::removed" do
9891
@payload load_event_fixture("installation_repositories_removed")
9992

10093
test "deletes github repos and associated project github repos" do
@@ -108,8 +101,7 @@ defmodule CodeCorps.GitHub.Event.InstallationRepositoriesTest do
108101
insert(:project_github_repo, project: project, github_repo: github_repo_1)
109102
insert(:github_repo, github_app_installation: installation, github_id: repo_2_payload["id"])
110103

111-
event = build(:github_event, action: "removed", type: "installation_repositories")
112-
{:ok, [%GithubRepo{}, %GithubRepo{}]} = InstallationRepositories.handle(event, @payload)
104+
{:ok, [%GithubRepo{}, %GithubRepo{}]} = InstallationRepositories.handle(@payload)
113105

114106
assert Repo.aggregate(GithubRepo, :count, :id) == 0
115107
assert Repo.aggregate(ProjectGithubRepo, :count, :id) == 0
@@ -125,31 +117,27 @@ defmodule CodeCorps.GitHub.Event.InstallationRepositoriesTest do
125117
github_repo_1 = insert(:github_repo, github_app_installation: installation, github_id: repo_1_payload["id"])
126118
insert(:project_github_repo, project: project, github_repo: github_repo_1)
127119

128-
event = build(:github_event, action: "removed", type: "installation_repositories")
129-
{:ok, [%GithubRepo{}]} = InstallationRepositories.handle(event, @payload)
120+
{:ok, [%GithubRepo{}]} = InstallationRepositories.handle(@payload)
130121

131122
assert Repo.aggregate(GithubRepo, :count, :id) == 0
132123
assert Repo.aggregate(ProjectGithubRepo, :count, :id) == 0
133124
end
134125

135126
test "marks event as errored if invalid instalation payload" do
136-
event = build(:github_event, action: "removed", type: "installation_repositories")
137127
payload = @payload |> Map.put("installation", "foo")
138128
assert {:error, :unexpected_payload} ==
139-
InstallationRepositories.handle(event, payload)
129+
InstallationRepositories.handle(payload)
140130
end
141131

142132
test "marks event as errored if invalid repo payload" do
143-
event = build(:github_event, action: "removed", type: "installation_repositories")
144133
insert(:github_app_installation, github_id: @payload["installation"]["id"])
145134
payload = @payload |> Map.put("repositories_removed", ["foo"])
146135
assert {:error, :unexpected_payload} ==
147-
InstallationRepositories.handle(event, payload)
136+
InstallationRepositories.handle(payload)
148137
end
149138

150139
test "marks event as errored if no installation" do
151-
event = build(:github_event, action: "added", type: "installation_repositories")
152-
assert {:error, :unmatched_installation} == InstallationRepositories.handle(event, @payload)
140+
assert {:error, :unmatched_installation} == InstallationRepositories.handle(@payload)
153141
end
154142
end
155143
end

test/lib/code_corps/github/event/installation_test.exs

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -44,56 +44,47 @@ defmodule CodeCorps.GitHub.Event.InstallationTest do
4444
@bad_sender_payload @installation_created |> Map.put("sender", "foo")
4545
@bad_installation_payload @installation_created |> Map.put("installation", "foo")
4646

47-
describe "handle/2" do
47+
describe "handle/1" do
4848
test "returns error if action of the event is wrong" do
49-
event = build(:github_event, action: "foo", type: "installation")
5049
assert {:error, :unexpected_action} ==
51-
Installation.handle(event, @bad_action_payload)
50+
Installation.handle(@bad_action_payload)
5251
end
5352

5453
test "returns error if payload is wrong" do
55-
event = build(:github_event, action: "created", type: "installation")
56-
assert {:error, :unexpected_payload} == Installation.handle(event, %{})
54+
assert {:error, :unexpected_payload} == Installation.handle(%{})
5755
end
5856

5957
test "returns error if user payload is wrong" do
60-
event = build(:github_event, action: "created", type: "installation")
6158
assert {:error, :unexpected_payload} ==
62-
Installation.handle(event, @bad_sender_payload)
59+
Installation.handle(@bad_sender_payload)
6360
end
6461

6562
test "returns error if installation payload is wrong" do
66-
event = build(:github_event, action: "created", type: "installation")
6763
assert {:error, :unexpected_payload} ==
68-
Installation.handle(event, @bad_installation_payload)
64+
Installation.handle(@bad_installation_payload)
6965
end
7066

7167
test "returns installation as errored if api error" do
72-
event = build(:github_event, action: "created", type: "installation")
73-
7468
with_mock_api(BadRepoRequest) do
7569
assert {:error, :github_api_error_on_syncing_repos}
76-
= Installation.handle(event, @installation_created)
70+
= Installation.handle(@installation_created)
7771
end
7872
end
7973

8074
test "returns installation as errored if error creating repos" do
81-
event = build(:github_event, action: "created", type: "installation")
82-
8375
with_mock_api(InvalidRepoRequest) do
8476
assert {:error, :validation_error_on_syncing_existing_repos} ==
85-
Installation.handle(event, @installation_created)
77+
Installation.handle(@installation_created)
8678
end
8779
end
8880
end
8981

90-
describe "handle/2 for Installation::created" do
82+
describe "handle/1 for Installation::created" do
9183
test "creates installation for unmatched user if no user, syncs repos" do
9284
payload = %{"installation" => %{"id" => installation_github_id}} = @installation_created
93-
event = build(:github_event, action: "created", type: "installation")
9485

9586
{:ok, %GithubAppInstallation{} = installation}
96-
= Installation.handle(event, payload)
87+
= Installation.handle(payload)
9788

9889
assert installation.github_id == installation_github_id
9990
assert installation.origin == "github"
@@ -105,12 +96,11 @@ defmodule CodeCorps.GitHub.Event.InstallationTest do
10596

10697
test "creates installation if user matched but installation unmatched, syncs repos" do
10798
%{"sender" => %{"id" => user_github_id}} = payload = @installation_created
108-
event = build(:github_event, action: "created", type: "installation")
10999

110100
user = insert(:user, github_id: user_github_id)
111101

112102
{:ok, %GithubAppInstallation{} = installation}
113-
= Installation.handle(event, payload)
103+
= Installation.handle(payload)
114104

115105
assert installation.github_id == (payload |> get_in(["installation", "id"]))
116106
assert installation.origin == "github"
@@ -123,7 +113,6 @@ defmodule CodeCorps.GitHub.Event.InstallationTest do
123113

124114
test "updates installation, if both user and installation matched, syncs repos" do
125115
%{"sender" => %{"id" => user_github_id}, "installation" => %{"id" => installation_github_id}} = payload = @installation_created
126-
event = build(:github_event, action: "created", type: "installation")
127116

128117
user = insert(:user, github_id: user_github_id)
129118
insert(
@@ -134,7 +123,7 @@ defmodule CodeCorps.GitHub.Event.InstallationTest do
134123
)
135124

136125
{:ok, %GithubAppInstallation{} = installation}
137-
= Installation.handle(event, payload)
126+
= Installation.handle(payload)
138127

139128
assert installation.origin == "codecorps"
140129
assert installation.state == "processed"
@@ -148,10 +137,9 @@ defmodule CodeCorps.GitHub.Event.InstallationTest do
148137
test "updates installation if there is an installation, but no user, syncs repos" do
149138
%{"installation" => %{"id" => installation_github_id}, "sender" => %{"id" => sender_github_id}} = payload = @installation_created
150139
insert(:github_app_installation, github_id: installation_github_id)
151-
event = build(:github_event, action: "created", type: "installation")
152140

153141
{:ok, %GithubAppInstallation{} = installation}
154-
= Installation.handle(event, payload)
142+
= Installation.handle(payload)
155143

156144
assert installation.origin == "codecorps"
157145
assert installation.state == "processed"

0 commit comments

Comments
 (0)