Hash :
00ca3d6f
Author :
Thomas de Grivel
Date :
2021-12-07T09:39:05
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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
defmodule KmxgitWeb.Admin.RepositoryController do
use KmxgitWeb, :controller
alias Kmxgit.GitManager
alias Kmxgit.RepositoryManager
alias Kmxgit.RepositoryManager.Repository
alias Kmxgit.Repo
alias Kmxgit.SlugManager
alias KmxgitWeb.ErrorView
def index(conn, _params) do
repos = RepositoryManager.list_repositories
conn
|> assign(:repos, repos)
|> render("index.html")
end
def new(conn, _params) do
changeset = RepositoryManager.change_repository
conn
|> assign(:action, Routes.admin_repository_path(conn, :create))
|> assign(:changeset, changeset)
|> render("new.html")
end
def create(conn, params) do
current_user = conn.assigns.current_user
slug = SlugManager.get_slug(params["owner"])
if slug do
user = slug.user
if user do
if user == current_user do
create_repo(conn, user, params["repository"])
else
not_found(conn)
end
else
org = slug.organisation
if org do
create_repo(conn, org, params["repository"])
else
not_found(conn)
end
end
else
not_found(conn)
end
end
defp create_repo(conn, owner, params) do
case Repo.transaction(fn ->
case RepositoryManager.create_repository(owner, params) do
{:ok, repo} -> repo
{:error, changeset} -> Repo.rollback changeset
end
end) do
{:ok, repo} ->
case GitManager.update_auth() do
:ok -> nil
error -> IO.inspect(error)
end
conn
|> redirect(to: Routes.admin_repository_path(conn, :show, repo))
{:error, changeset} ->
conn
|> assign(:action, Routes.admin_repository_path(conn, :create))
|> assign(:changeset, changeset)
|> assign(:owner, owner)
|> render("new.html")
end
end
def show(conn, params) do
repo = RepositoryManager.get_repository!(params["id"])
conn
|> assign(:members, Repository.members(repo))
|> assign(:repo, repo)
|> render("show.html")
end
def edit(conn, params) do
repo = RepositoryManager.get_repository!(params["id"])
changeset = RepositoryManager.change_repository(repo)
conn
|> assign(:action, Routes.admin_repository_path(conn, :update, repo))
|> assign(:changeset, changeset)
|> assign(:repo, repo)
|> render("edit.html")
end
def update(conn, params) do
repo = RepositoryManager.get_repository!(params["id"])
case Repo.transaction(fn ->
case RepositoryManager.update_repository(repo, params["repository"]) do
{:ok, repo1} ->
s = Repository.full_slug(repo)
s1 = Repository.full_slug(repo1)
if s != s1 do
GitManager.rename(s, s1)
end
repo1
{:error, changeset} -> Repo.rollback changeset
end
end) do
{:ok, repo1} ->
case GitManager.update_auth() do
:ok -> nil
error -> IO.inspect(error)
end
conn
|> redirect(to: Routes.admin_repository_path(conn, :show, repo1))
{:error, changeset} ->
conn
|> assign(:action, Routes.admin_repository_path(conn, :update, repo))
|> assign(:changeset, changeset)
|> assign(:repo, repo)
|> render("edit.html")
end
end
def add_user(conn, params) do
repo = RepositoryManager.get_repository!(params["repository_id"])
conn
|> assign(:action, Routes.admin_repository__path(conn, :add_user_post, repo))
|> assign(:repo, repo)
|> render("add_user.html")
end
def add_user_post(conn, params) do
login = params["repository"]["login"]
repo = RepositoryManager.get_repository!(params["repository_id"])
case RepositoryManager.add_member(repo, login) do
{:ok, repo} ->
case GitManager.update_auth() do
:ok -> nil
error -> IO.inspect(error)
end
conn
|> redirect(to: Routes.admin_repository_path(conn, :show, repo))
{:error, _} ->
conn
|> assign(:action, Routes.admin_repository__path(conn, :add_user_post, repo))
|> assign(:repo, repo)
|> render("add_user.html")
end
end
def remove_user(conn, params) do
repo = RepositoryManager.get_repository!(params["repository_id"])
conn
|> assign(:action, Routes.admin_repository__path(conn, :remove_user_post, repo))
|> assign(:repo, repo)
|> render("remove_user.html")
end
def remove_user_post(conn, params) do
login = params["repository"]["login"]
repo = RepositoryManager.get_repository!(params["repository_id"])
case RepositoryManager.remove_member(repo, login) do
{:ok, repo} ->
case GitManager.update_auth() do
:ok -> nil
error -> IO.inspect(error)
end
conn
|> redirect(to: Routes.admin_repository_path(conn, :show, repo))
{:error, _} ->
conn
|> assign(:action, Routes.admin_repository__path(conn, :remove_user_post, repo))
|> assign(:repo, repo)
|> render("remove_user.html")
end
end
def delete(conn, params) do
repo = RepositoryManager.get_repository(params["id"])
if repo do
case Repo.transaction(fn ->
case RepositoryManager.delete_repository(repo) do
{:ok, _} -> GitManager.delete(Repository.full_slug(repo))
{:error, changeset} -> Repo.rollback(changeset)
end
end) do
{:ok, _} ->
case GitManager.update_auth() do
:ok -> nil
error -> IO.inspect(error)
end
conn
|> redirect(to: Routes.admin_repository_path(conn, :index))
{:error, changeset} ->
conn
|> assign(:action, Routes.admin_repository_path(conn, :update, repo))
|> assign(:changeset, changeset)
|> assign(:repo, repo)
|> render("edit.html")
end
else
not_found(conn)
end
end
end