Skip to content

Commit

Permalink
Store failure reason for github event when marking it as errored
Browse files Browse the repository at this point in the history
  • Loading branch information
begedin authored and joshsmith committed Sep 26, 2017
1 parent 87e65f7 commit 602307e
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 38 deletions.
24 changes: 14 additions & 10 deletions lib/code_corps/github/event.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ defmodule CodeCorps.GitHub.Event do
are already being processed.
"""
@spec start_processing(GithubEvent.t) :: {:ok, GithubEvent.t}
def start_processing(%GithubEvent{} = event), do: event |> set_status("processing")
def start_processing(%GithubEvent{} = event) do
event
|> Changeset.change(%{status: "processing"})
|> Repo.update()
end

@doc ~S"""
Sets record status to "processed" or "errored" based on the first element of
Expand All @@ -24,16 +28,16 @@ defmodule CodeCorps.GitHub.Event do
the tuple is suported.
"""
@spec stop_processing(tuple, GithubEvent.t) :: {:ok, GithubEvent.t}
def stop_processing(result, %GithubEvent{} = event) when is_tuple(result) do
result |> Tuple.to_list |> do_stop_processing(event)
def stop_processing({:ok, _data}, %GithubEvent{} = event) do
event
|> Changeset.change(%{status: "processed"})
|> Repo.update()
end
def stop_processing({:error, reason}, %GithubEvent{} = event) do
changes = %{status: "errored", failure_reason: reason |> Atom.to_string}

@spec do_stop_processing(list, GithubEvent.t) :: {:ok, GithubEvent.t}
defp do_stop_processing([:ok | _data], %GithubEvent{} = event), do: event |> set_status("processed")
defp do_stop_processing([:error | _reason], %GithubEvent{} = event), do: event |> set_status("errored")

@spec set_status(GithubEvent.t, String.t) :: {:ok, GithubEvent.t}
defp set_status(%GithubEvent{} = event, status) when status in ~w(processing processed errored) do
event |> Changeset.change(%{status: status}) |> Repo.update()
event
|> Changeset.change(changes)
|> Repo.update()
end
end
12 changes: 8 additions & 4 deletions lib/code_corps/github/event/installation/repos.ex
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@ defmodule CodeCorps.GitHub.Event.Installation.Repos do
"""
@spec process_async(GithubAppInstallation.t) :: {:ok, GithubAppInstallation.t, Task.t}
def process_async(%GithubAppInstallation{} = installation) do
{:ok, %GithubAppInstallation{} = processing_installation} = installation |> set_state("processing")
{:ok, %GithubAppInstallation{} = updated_installation} =
installation |> set_state("processing")

task = Task.Supervisor.async(:background_processor, fn -> processing_installation |> process() end)
repo_processing_task = Task.Supervisor.async(
:background_processor, fn -> updated_installation |> process() end)

{:ok, processing_installation, task}
{:ok, {updated_installation, repo_processing_task}}
end

@doc ~S"""
Expand Down Expand Up @@ -58,7 +60,9 @@ defmodule CodeCorps.GitHub.Event.Installation.Repos do
end

@spec set_state(GithubAppInstallation.t, String.t) :: {:ok, GithubAppInstallation.t}
defp set_state(%GithubAppInstallation{} = installation, state) when state in ~w(processing processed errored) do
defp set_state(%GithubAppInstallation{} = installation, state)
when state in ~w(processing processed errored) do

installation
|> Changeset.change(%{state: state})
|> Repo.update
Expand Down
1 change: 1 addition & 0 deletions lib/code_corps/model/github_event.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ defmodule CodeCorps.GithubEvent do
field :payload, :map
field :status, :string
field :type, :string
field :failure_reason, :string

timestamps()
end
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
defmodule CodeCorps.Repo.Migrations.AddFailureReasonToGithubEvents do
use Ecto.Migration

def change do
alter table(:github_events) do
add :failure_reason, :string
end
end
end
9 changes: 5 additions & 4 deletions priv/repo/structure.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
-- PostgreSQL database dump
--

-- Dumped from database version 9.5.1
-- Dumped by pg_dump version 9.5.1
-- Dumped from database version 9.5.4
-- Dumped by pg_dump version 9.5.4

SET statement_timeout = 0;
SET lock_timeout = 0;
Expand Down Expand Up @@ -239,7 +239,8 @@ CREATE TABLE github_events (
type character varying(255),
inserted_at timestamp without time zone NOT NULL,
updated_at timestamp without time zone NOT NULL,
payload jsonb
payload jsonb,
failure_reason character varying(255)
);


Expand Down Expand Up @@ -3034,5 +3035,5 @@ ALTER TABLE ONLY user_tasks
-- PostgreSQL database dump complete
--

INSERT INTO "schema_migrations" (version) VALUES (20160723215749), (20160804000000), (20160804001111), (20160805132301), (20160805203929), (20160808143454), (20160809214736), (20160810124357), (20160815125009), (20160815143002), (20160816020347), (20160816034021), (20160817220118), (20160818000944), (20160818132546), (20160820113856), (20160820164905), (20160822002438), (20160822004056), (20160822011624), (20160822020401), (20160822044612), (20160830081224), (20160830224802), (20160911233738), (20160912002705), (20160912145957), (20160918003206), (20160928232404), (20161003185918), (20161019090945), (20161019110737), (20161020144622), (20161021131026), (20161031001615), (20161121005339), (20161121014050), (20161121043941), (20161121045709), (20161122015942), (20161123081114), (20161123150943), (20161124085742), (20161125200620), (20161126045705), (20161127054559), (20161205024856), (20161207112519), (20161209192504), (20161212005641), (20161214005935), (20161215052051), (20161216051447), (20161218005913), (20161219160401), (20161219163909), (20161220141753), (20161221085759), (20161226213600), (20161231063614), (20170102130055), (20170102181053), (20170104113708), (20170104212623), (20170104235423), (20170106013143), (20170115035159), (20170115230549), (20170121014100), (20170131234029), (20170201014901), (20170201025454), (20170201035458), (20170201183258), (20170220032224), (20170224233516), (20170226050552), (20170228085250), (20170308214128), (20170308220713), (20170308222552), (20170313130611), (20170318032449), (20170318082740), (20170324194827), (20170424215355), (20170501225441), (20170505224222), (20170526095401), (20170602000208), (20170622205732), (20170626231059), (20170628092119), (20170628213609), (20170629183404), (20170630140136), (20170706132431), (20170707213648), (20170711122252), (20170717092127), (20170725060612), (20170727052644), (20170731130121), (20170814131722), (20170913114958), (20170921014405), (20170925214512), (20170925230419);
INSERT INTO "schema_migrations" (version) VALUES (20160723215749), (20160804000000), (20160804001111), (20160805132301), (20160805203929), (20160808143454), (20160809214736), (20160810124357), (20160815125009), (20160815143002), (20160816020347), (20160816034021), (20160817220118), (20160818000944), (20160818132546), (20160820113856), (20160820164905), (20160822002438), (20160822004056), (20160822011624), (20160822020401), (20160822044612), (20160830081224), (20160830224802), (20160911233738), (20160912002705), (20160912145957), (20160918003206), (20160928232404), (20161003185918), (20161019090945), (20161019110737), (20161020144622), (20161021131026), (20161031001615), (20161121005339), (20161121014050), (20161121043941), (20161121045709), (20161122015942), (20161123081114), (20161123150943), (20161124085742), (20161125200620), (20161126045705), (20161127054559), (20161205024856), (20161207112519), (20161209192504), (20161212005641), (20161214005935), (20161215052051), (20161216051447), (20161218005913), (20161219160401), (20161219163909), (20161220141753), (20161221085759), (20161226213600), (20161231063614), (20170102130055), (20170102181053), (20170104113708), (20170104212623), (20170104235423), (20170106013143), (20170115035159), (20170115230549), (20170121014100), (20170131234029), (20170201014901), (20170201025454), (20170201035458), (20170201183258), (20170220032224), (20170224233516), (20170226050552), (20170228085250), (20170308214128), (20170308220713), (20170308222552), (20170313130611), (20170318032449), (20170318082740), (20170324194827), (20170424215355), (20170501225441), (20170505224222), (20170526095401), (20170602000208), (20170622205732), (20170626231059), (20170628092119), (20170628213609), (20170629183404), (20170630140136), (20170706132431), (20170707213648), (20170711122252), (20170717092127), (20170725060612), (20170727052644), (20170731130121), (20170814131722), (20170913114958), (20170921014405), (20170925214512), (20170925230419), (20170926134646);

Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ defmodule CodeCorps.GitHub.Event.Installation.ReposTest do
unmatched_repo = insert(:github_repo, github_app_installation: installation)
_matched_repo = insert(:github_repo, matched_repo_attrs |> Map.put(:github_app_installation, installation))

{:ok, %GithubAppInstallation{state: intermediate_state}, task} =
{:ok, {%GithubAppInstallation{state: intermediate_state}, task}} =
installation
|> Repo.preload(:github_repos)
|> Repos.process_async()
Expand Down
8 changes: 4 additions & 4 deletions test/lib/code_corps/github/event/installation_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ defmodule CodeCorps.GitHub.Event.InstallationTest do
payload = %{"installation" => %{"id" => installation_github_id}} = @installation_created
event = build(:github_event, action: "created", type: "installation")

{:ok, %GithubAppInstallation{} = installation, %Task{} = task}
{:ok, {%GithubAppInstallation{} = installation, %Task{} = task}}
= Installation.handle(event, payload)

assert installation.github_id == installation_github_id
Expand All @@ -62,7 +62,7 @@ defmodule CodeCorps.GitHub.Event.InstallationTest do

user = insert(:user, github_id: user_github_id)

{:ok, %GithubAppInstallation{} = installation, %Task{} = task}
{:ok, {%GithubAppInstallation{} = installation, %Task{} = task}}
= Installation.handle(event, payload)

assert installation.github_id == (payload |> get_in(["installation", "id"]))
Expand All @@ -89,7 +89,7 @@ defmodule CodeCorps.GitHub.Event.InstallationTest do
github_id: nil
)

{:ok, %GithubAppInstallation{} = installation, %Task{} = task}
{:ok, {%GithubAppInstallation{} = installation, %Task{} = task}}
= Installation.handle(event, payload)

assert installation.origin == "codecorps"
Expand All @@ -109,7 +109,7 @@ defmodule CodeCorps.GitHub.Event.InstallationTest do
insert(:github_app_installation, github_id: installation_github_id)
event = build(:github_event, action: "created", type: "installation")

{:ok, %GithubAppInstallation{} = installation, %Task{} = task}
{:ok, {%GithubAppInstallation{} = installation, %Task{} = task}}
= Installation.handle(event, payload)

assert installation.origin == "codecorps"
Expand Down
26 changes: 11 additions & 15 deletions test/lib/code_corps/github/event_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,18 @@ defmodule CodeCorps.GitHub.EventTest do
end
end

defp get_resulting_status(tuple) do
event = insert(:github_event, status: "processing")
{:ok, %GithubEvent{} = updated_event} = Event.stop_processing(tuple, event)
updated_event.status
end

describe "stop_processing/2" do
test "sets proper status for event, based on first argument" do
assert {:ok} |> get_resulting_status() == "processed"
assert {:ok, "foo"} |> get_resulting_status() == "processed"
assert {:ok, "foo", "bar"} |> get_resulting_status() == "processed"
assert {:ok, "foo", "bar", "baz"} |> get_resulting_status() == "processed"
assert {:error} |> get_resulting_status() == "errored"
assert {:error, "foo"} |> get_resulting_status() == "errored"
assert {:error, "foo", "bar"} |> get_resulting_status() == "errored"
assert {:error, "foo", "bar", "baz"} |> get_resulting_status() == "errored"
test "sets event as processed if resulting tuple starts with :ok" do
event = insert(:github_event, status: "processing")
{:ok, %GithubEvent{} = updated_event} = Event.stop_processing({:ok, "foo"}, event)
assert updated_event.status == "processed"
end

test "marks event errored, with failure_reason, if resulting tuple starts with :error" do
event = insert(:github_event, status: "processing")
{:ok, %GithubEvent{} = updated_event} = Event.stop_processing({:error, :bar}, event)
assert updated_event.status == "errored"
assert updated_event.failure_reason == "bar"
end
end
end

0 comments on commit 602307e

Please sign in to comment.