Skip to content

Commit

Permalink
Cover more cases to avoid event stuck in "processing"
Browse files Browse the repository at this point in the history
  • Loading branch information
begedin committed Jul 3, 2017
1 parent 8b78a8c commit 32d768a
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 3 deletions.
20 changes: 17 additions & 3 deletions lib/code_corps/github/event/installation_repositories.ex
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ defmodule CodeCorps.GitHub.Event.InstallationRepositories do
@typep outcome :: {:ok, ProjectGithubRepo.t} |
{:error, :no_installation} |
{:error, :no_repo} |
{:error, :no_project_github_repo}
{:error, :no_project_github_repo} |
{:error, :unexpected_action_or_payload} |
{:error, :unexpected_installation_payload} |
{:error, :unexpected_repo_payload}

@spec do_handle({:ok, GithubEvent.t}, String.t, map) :: outcome
defp do_handle({:ok, %GithubEvent{}}, "added", %{"installation" => installation_attrs, "repositories_added" => [repo_attrs]}) do
Expand All @@ -40,6 +43,10 @@ defmodule CodeCorps.GitHub.Event.InstallationRepositories do
# both are created when handling the "installation created" event, which
# triggers and handles before this one.
{nil, _} -> {:error, :no_installation}
# One of the nested payloads has an incorrect structure.
# Probably GitHub updated it's API
{:unexpected_installation_payload, _} -> {:error, :unexpected_installation_payload}
{_, :unexpected_repo_payload} -> {:error, :unexpected_repo_payload}
# We got a repo added event, the installation exists, but the repo does
# not. This should not be happening, since both are created when
# handling the "installation created" event, which triggers and handles
Expand Down Expand Up @@ -67,6 +74,10 @@ defmodule CodeCorps.GitHub.Event.InstallationRepositories do
# both are created when handling the "installation created" event, which
# triggers and handles before this one.
{nil, _} -> {:error, :no_installation}
# One of the nested payloads has an incorrect structure.
# Probably GitHub updated it's API
{:unexpected_installation_payload, _} -> {:error, :unexpected_installation_payload}
{_, :unexpected_repo_payload} -> {:error, :unexpected_repo_payload}
# We got a removed added event, the installation exists, but the repo does
# not. This should not be happening, since both are created when
# handling the "installation created" event, which triggers and handles
Expand All @@ -85,16 +96,19 @@ defmodule CodeCorps.GitHub.Event.InstallationRepositories do
end
end
end
defp do_handle({:ok, %GithubEvent{}}, _action, _payload), do: {:error, :unexpected_action_or_payload}

@spec find_installation(map) :: GithubAppInstallation.t | nil
@spec find_installation(any) :: GithubAppInstallation.t | nil | :unexpected_installation_payload
defp find_installation(%{"id" => github_id}) do
GithubAppInstallation
|> Repo.get_by(github_id: github_id)
|> Repo.preload(:project)
end
defp find_installation(_payload), do: :unexpected_installation_payload

@spec find_repo(map) :: GithubRepo.t | nil
@spec find_repo(any) :: GithubRepo.t | nil | :unexpected_repo_payload
defp find_repo(%{"id" => github_id}), do: GithubRepo |> Repo.get_by(github_id: github_id)
defp find_repo(_payload), do: :unexpected_repo_payload

@spec find_project_github_repo(Project.t, GithubRepo.t) :: ProjectGithubRepo.t | nil
defp find_project_github_repo(%Project{id: project_id}, %GithubRepo{id: github_repo_id}) do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,5 +89,59 @@ defmodule CodeCorps.GitHub.Event.InstallationRepositoriesTest do
updated_event = Repo.one(GithubEvent)
assert updated_event.status == "processed"
end

test "marks event as errored if invalid action" do
payload = %{}
event = insert(:github_event, action: "foo", type: "installation_repositories")
assert InstallationRepositories.handle(event, payload)

updated_event = Repo.one(GithubEvent)
assert updated_event.status == "errored"
end

test "marks event as errored if invalid payload" do
payload = %{}
event = insert(:github_event, action: "added", type: "installation_repositories")
assert InstallationRepositories.handle(event, payload)

updated_event = Repo.one(GithubEvent)
assert updated_event.status == "errored"
end

test "marks event as errored if invalid instalation payload for added" do
payload = load_event_fixture("installation_repositories_added")
event = insert(:github_event, action: "added", type: "installation_repositories")
assert InstallationRepositories.handle(event, payload |> Map.put("installation", "foo"))

updated_event = Repo.one(GithubEvent)
assert updated_event.status == "errored"
end

test "marks event as errored if invalid instalation payload for removed" do
payload = load_event_fixture("installation_repositories_removed")
event = insert(:github_event, action: "removed", type: "installation_repositories")
assert InstallationRepositories.handle(event, payload |> Map.put("installation", "foo"))

updated_event = Repo.one(GithubEvent)
assert updated_event.status == "errored"
end

test "marks event as errored if invalid repo payload for added" do
payload = load_event_fixture("installation_repositories_added")
event = insert(:github_event, action: "added", type: "installation_repositories")
assert InstallationRepositories.handle(event, payload |> Map.put("repositories_removed", ["foo"]))

updated_event = Repo.one(GithubEvent)
assert updated_event.status == "errored"
end

test "marks event as errored if invalid repo payload for removed" do
payload = load_event_fixture("installation_repositories_removed")
event = insert(:github_event, action: "removed", type: "installation_repositories")
assert InstallationRepositories.handle(event, payload |> Map.put("repositories_removed", ["foo"]))

updated_event = Repo.one(GithubEvent)
assert updated_event.status == "errored"
end
end
end

0 comments on commit 32d768a

Please sign in to comment.