-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathgithub_event.ex
More file actions
52 lines (44 loc) · 1.46 KB
/
github_event.ex
File metadata and controls
52 lines (44 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
defmodule CodeCorps.GithubEvent do
use CodeCorps.Model
use Scrivener, page_size: 20
alias Ecto.Changeset
@type t :: %__MODULE__{}
schema "github_events" do
field :action, :string
field :data, :string
field :error, :string
field :failure_reason, :string
field :github_delivery_id, :string
field :payload, :map
field :retry, :boolean, virtual: true
field :status, :string
field :type, :string
timestamps()
end
@doc """
Builds a changeset based on the `struct` and `params`.
"""
def changeset(struct, params \\ %{}) do
struct
|> cast(params, [:action, :data, :github_delivery_id, :payload, :error, :status, :type])
|> validate_required([:action, :github_delivery_id, :payload, :status, :type])
|> validate_inclusion(:status, statuses())
end
def update_changeset(struct, params \\ %{}) do
struct
|> cast(params, [:retry, :status])
|> validate_acceptance(:retry)
|> validate_retry()
|> validate_inclusion(:status, statuses())
end
def statuses do
~w{unprocessed processing processed errored unsupported reprocessing}
end
defp validate_retry(%Changeset{changes: %{retry: true}} = changeset) do
case changeset |> Changeset.get_field(:status) do
"errored" -> Changeset.put_change(changeset, :status, "reprocessing")
_ -> Changeset.add_error(changeset, :retry, "only possible when status is errored")
end
end
defp validate_retry(changeset), do: changeset
end