2019-07-20 23:08:56 +00:00
|
|
|
defmodule FrenzyWeb.PipelineController do
|
|
|
|
use FrenzyWeb, :controller
|
2019-11-09 03:27:46 +00:00
|
|
|
alias Frenzy.{Repo, Pipeline}
|
2019-07-20 23:08:56 +00:00
|
|
|
alias FrenzyWeb.Endpoint
|
|
|
|
import Ecto.Query
|
|
|
|
|
2019-11-09 03:27:46 +00:00
|
|
|
plug :user_owns_pipeline
|
2019-07-20 23:08:56 +00:00
|
|
|
|
2019-11-09 03:27:46 +00:00
|
|
|
defp user_owns_pipeline(%Plug.Conn{path_params: %{"id" => pipeline_id}} = conn, _opts) do
|
2019-07-20 23:08:56 +00:00
|
|
|
user = conn.assigns[:user]
|
|
|
|
|
2019-11-09 03:27:46 +00:00
|
|
|
pipeline = Repo.get(Pipeline, pipeline_id)
|
2019-07-20 23:08:56 +00:00
|
|
|
|
2019-11-09 03:27:46 +00:00
|
|
|
if pipeline.user_id == user.id do
|
2019-07-20 23:08:56 +00:00
|
|
|
conn
|
2019-11-09 03:27:46 +00:00
|
|
|
|> assign(:pipeline, pipeline)
|
2019-07-20 23:08:56 +00:00
|
|
|
else
|
|
|
|
conn
|
|
|
|
|> put_flash(:error, "You do not have permission to access that resource.")
|
|
|
|
|> redirect(to: Routes.group_path(Endpoint, :index))
|
|
|
|
|> halt()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-11-09 03:27:46 +00:00
|
|
|
defp user_owns_pipeline(conn, _opts), do: conn
|
2019-07-20 23:08:56 +00:00
|
|
|
|
2019-11-09 03:27:46 +00:00
|
|
|
def index(conn, _params) do
|
|
|
|
user = conn.assigns[:user]
|
2019-07-20 23:08:56 +00:00
|
|
|
|
2019-11-09 03:27:46 +00:00
|
|
|
pipelines = Repo.all(from p in Pipeline, where: p.user_id == ^user.id, preload: [:feeds])
|
2019-07-20 23:08:56 +00:00
|
|
|
|
2019-11-09 03:27:46 +00:00
|
|
|
render(conn, "index.html", %{pipelines: pipelines})
|
2019-07-20 23:08:56 +00:00
|
|
|
end
|
|
|
|
|
2019-11-09 03:27:46 +00:00
|
|
|
def new(conn, _params) do
|
2019-07-21 00:06:23 +00:00
|
|
|
changeset =
|
2019-11-09 03:27:46 +00:00
|
|
|
Pipeline.changeset(%Pipeline{}, %{
|
|
|
|
name: "",
|
|
|
|
stages: "[\n]"
|
2019-07-21 00:06:23 +00:00
|
|
|
})
|
|
|
|
|
2019-11-09 03:27:46 +00:00
|
|
|
render(conn, "new.html", %{changeset: changeset})
|
2019-07-20 23:08:56 +00:00
|
|
|
end
|
|
|
|
|
2019-11-09 03:27:46 +00:00
|
|
|
def create(conn, %{"pipeline" => %{"name" => name, "stages" => stages_json}}) do
|
|
|
|
user = conn.assigns[:user]
|
2019-07-20 23:08:56 +00:00
|
|
|
|
2019-11-09 03:27:46 +00:00
|
|
|
with {:ok, stages} <- Jason.decode(stages_json),
|
|
|
|
{:ok, stages} <- validate_pipeline(stages) do
|
|
|
|
changeset = Pipeline.changeset(%Pipeline{}, %{user_id: user.id, name: name, stages: stages})
|
|
|
|
|
|
|
|
{:ok, _pipeline} = Repo.insert(changeset)
|
2019-07-21 00:06:23 +00:00
|
|
|
|
|
|
|
conn
|
2019-11-09 03:27:46 +00:00
|
|
|
|> put_flash(:info, "Pipeline created")
|
|
|
|
|> redirect(to: Routes.pipeline_path(Endpoint, :index))
|
2019-07-21 00:06:23 +00:00
|
|
|
else
|
2019-11-09 03:27:46 +00:00
|
|
|
{:error, reason} ->
|
|
|
|
error_changeset = Pipeline.changeset(%Pipeline{}, %{name: name, stages: stages_json})
|
2019-07-21 00:06:23 +00:00
|
|
|
|
|
|
|
conn
|
2019-11-09 03:27:46 +00:00
|
|
|
|> put_flash(:error, "Unable to create pipeline: #{reason}")
|
|
|
|
|> render("new.html", %{changeset: error_changeset})
|
2019-07-21 00:06:23 +00:00
|
|
|
end
|
2019-07-20 23:08:56 +00:00
|
|
|
end
|
|
|
|
|
2019-11-09 03:27:46 +00:00
|
|
|
def show(conn, _params) do
|
|
|
|
pipeline = conn.assigns[:pipeline]
|
2019-07-21 00:06:23 +00:00
|
|
|
|
2019-11-09 03:27:46 +00:00
|
|
|
render(conn, "show.html", %{pipeline: pipeline})
|
|
|
|
end
|
2019-07-20 23:08:56 +00:00
|
|
|
|
2019-11-09 03:27:46 +00:00
|
|
|
def delete(conn, _params) do
|
|
|
|
conn.assigns[:pipeline]
|
|
|
|
|> Repo.delete()
|
|
|
|
|
|
|
|
redirect(conn, to: Routes.pipeline_path(Endpoint, :index))
|
|
|
|
end
|
|
|
|
|
|
|
|
def edit(conn, _params) do
|
|
|
|
pipeline = conn.assigns[:pipeline]
|
|
|
|
|
|
|
|
{:ok, stages_json} = Jason.encode(pipeline.stages, pretty: true)
|
|
|
|
|
|
|
|
render(conn, "edit.html", %{
|
|
|
|
pipeline: pipeline,
|
|
|
|
name: pipeline.name,
|
|
|
|
stages_json: stages_json
|
2019-07-20 23:08:56 +00:00
|
|
|
})
|
|
|
|
end
|
|
|
|
|
2019-11-09 03:27:46 +00:00
|
|
|
def update(conn, %{"pipeline" => %{"name" => name, "stages" => stages_json}}) do
|
|
|
|
pipeline = conn.assigns[:pipeline]
|
|
|
|
|
|
|
|
with {:ok, stages} <- Jason.decode(stages_json),
|
|
|
|
{:ok, stages} <- validate_pipeline(stages) do
|
2020-01-30 03:20:49 +00:00
|
|
|
changeset = Pipeline.changeset(pipeline, %{name: name, stages: stages})
|
2019-07-20 23:08:56 +00:00
|
|
|
|
2019-11-09 03:27:46 +00:00
|
|
|
{:ok, _pipeline} = Repo.update(changeset)
|
2019-07-20 23:08:56 +00:00
|
|
|
|
2019-07-21 00:06:23 +00:00
|
|
|
conn
|
2019-11-09 03:27:46 +00:00
|
|
|
|> put_flash(:info, "Pipeline edited")
|
|
|
|
|> redirect(to: Routes.pipeline_path(Endpoint, :show, pipeline.id))
|
2019-07-21 00:06:23 +00:00
|
|
|
else
|
2019-11-09 03:27:46 +00:00
|
|
|
{:error, reason} ->
|
2019-07-21 00:06:23 +00:00
|
|
|
conn
|
2020-05-31 19:56:27 +00:00
|
|
|
|> put_flash(:error, "Unable to edit pipeline: #{reason}")
|
2019-11-09 03:27:46 +00:00
|
|
|
|> render("edit.html", %{
|
|
|
|
pipeline: pipeline,
|
|
|
|
name: name,
|
|
|
|
stages_json: stages_json
|
2019-07-21 00:06:23 +00:00
|
|
|
})
|
|
|
|
end
|
2019-07-20 23:08:56 +00:00
|
|
|
end
|
|
|
|
|
2019-11-09 03:27:46 +00:00
|
|
|
defp validate_pipeline(stages) do
|
|
|
|
stages
|
|
|
|
|> Enum.with_index()
|
|
|
|
|> Enum.reduce_while({:ok, []}, fn {stage, index}, {:ok, new_stages} ->
|
|
|
|
case validate_stage(stage) do
|
|
|
|
{:ok, stage} ->
|
|
|
|
{:cont, {:ok, new_stages ++ [stage]}}
|
|
|
|
|
|
|
|
{:error, reason} ->
|
2020-05-31 19:56:27 +00:00
|
|
|
{:halt, {:error, "invalid stage at index #{index}: #{reason}"}}
|
2019-11-09 03:27:46 +00:00
|
|
|
end
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
|
|
|
defp validate_stage(%{"module_name" => module_name, "options" => options}) do
|
|
|
|
with true <- module_exists(module_name),
|
|
|
|
{:ok, options} <-
|
|
|
|
apply(String.to_existing_atom("Elixir." <> module_name), :validate_opts, [
|
|
|
|
options
|
|
|
|
]) do
|
|
|
|
{:ok, %{"module_name" => module_name, "options" => options}}
|
|
|
|
else
|
|
|
|
false ->
|
|
|
|
{:error, "module #{module_name} does not exist"}
|
2019-07-20 23:08:56 +00:00
|
|
|
|
2019-11-09 03:27:46 +00:00
|
|
|
{:error, reason} ->
|
|
|
|
{:error, "invalid options for #{module_name}: #{reason}"}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defp validate_stage(_),
|
|
|
|
do: {:error, "pipeline stage must be a map with module_name and options"}
|
|
|
|
|
|
|
|
defp module_exists(module_name) do
|
|
|
|
try do
|
|
|
|
String.to_existing_atom("Elixir." <> module_name)
|
|
|
|
true
|
|
|
|
rescue
|
|
|
|
ArgumentError ->
|
|
|
|
false
|
|
|
|
end
|
2019-07-20 23:08:56 +00:00
|
|
|
end
|
|
|
|
end
|