1.8 KiB · text 5af55d9
defmodule GitGud.Workflows.WorkflowRun do
use Ecto.Schema
import Ecto.Changeset
alias GitGud.Accounts.User
alias GitGud.Repositories.Repository
alias GitGud.Workflows.WorkflowJob
@valid_statuses ~w(queued running success failure cancelled)
schema "workflow_runs" do
field :number, :integer
field :workflow_path, :string
field :workflow_name, :string
field :trigger, :string
field :head_sha, :binary
field :head_ref, :string
field :status, :string, default: "queued"
field :conclusion, :string
field :started_at, :utc_datetime
field :completed_at, :utc_datetime
field :workflow_doc, :map
belongs_to :repository, Repository
# The user who triggered the run (the pusher), when attributable.
# nil for runs we can't attribute (e.g. anonymous HTTP, re-runs).
belongs_to :triggered_by, User
has_many :jobs, WorkflowJob
timestamps(type: :utc_datetime)
end
def create_changeset(run, attrs) do
run
|> cast(attrs, [
:number,
:workflow_path,
:workflow_name,
:trigger,
:head_sha,
:head_ref,
:workflow_doc,
:triggered_by_id
])
|> validate_required([:number, :workflow_path, :trigger, :head_sha])
end
def status_changeset(run, status, opts \\ []) when status in @valid_statuses do
now = DateTime.utc_now(:second)
fields = %{status: status}
fields =
cond do
status == "running" ->
Map.put(fields, :started_at, now)
status in ~w(success failure cancelled) ->
fields
|> Map.put(:completed_at, now)
|> Map.put(:conclusion, Keyword.get(opts, :conclusion, status))
true ->
fields
end
change(run, fields)
end
def valid_statuses, do: @valid_statuses
end