Hash :
fe8eaa2e
Author :
Thomas de Grivel
Date :
2022-02-01T01:39:38
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 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765
defmodule KmxgitWeb.RepositoryController do
use KmxgitWeb, :controller
alias Kmxgit.GitManager
alias Kmxgit.OrganisationManager.Organisation
alias Kmxgit.RepositoryManager
alias Kmxgit.RepositoryManager.Repository
alias Kmxgit.SlugManager
alias Kmxgit.UserManager.User
alias Kmxgit.Repo
def new(conn, params) do
action = Routes.repository_path(conn, :create, params["owner"])
current_user = conn.assigns.current_user
slug = SlugManager.get_slug(params["owner"])
if !slug do
not_found(conn)
else
if slug.user && slug.user.id == current_user.id do
changeset = RepositoryManager.change_repository(slug.user)
conn
|> assign(:action, action)
|> assign(:changeset, changeset)
|> assign(:owner, slug.user)
|> render("new.html")
else
org = slug.organisation
if org && Organisation.owner?(org, current_user) do
changeset = RepositoryManager.change_repository(org)
conn
|> assign(:action, action)
|> assign(:changeset, changeset)
|> assign(:current_organisation, org)
|> assign(:owner, org)
|> render("new.html")
else
not_found(conn)
end
end
end
end
def create(conn, params) do
current_user = conn.assigns.current_user
slug = SlugManager.get_slug(params["owner"])
if !slug do
not_found(conn)
else
user = slug.user
if user && user.id == current_user.id do
create_repo(conn, params["repository"], user)
else
org = slug.organisation
if org && Organisation.owner?(org, current_user) do
create_repo(conn, params["repository"], org)
else
not_found(conn)
end
end
end
end
defp create_repo(conn, params, owner) do
case Repo.transaction(fn ->
case RepositoryManager.create_repository(params, owner) do
{:ok, repo} ->
case GitManager.create(Repository.full_slug(repo)) do
{:ok, _} -> repo
{:error, e} ->
repo
|> Repository.changeset(params)
|> Ecto.Changeset.add_error(:git, e)
|> Repo.rollback
end
{:error, changeset} ->
Repo.rollback(changeset)
end
end) do
{:ok, repo} ->
case GitManager.update_auth() do
:ok -> :ok = GitManager.public_access(Repository.full_slug(repo), repo.public_access)
error -> IO.inspect(error)
end
conn
|> redirect(to: Routes.repository_path(conn, :show, owner.slug.slug, Repository.splat(repo)))
{:error, changeset} ->
IO.inspect changeset
conn
|> assign(:action, Routes.repository_path(conn, :create, owner.slug.slug))
|> assign(:changeset, changeset)
|> assign_current_organisation(owner)
|> assign(:owner, owner)
|> render("new.html")
end
end
def show(conn, params) do
current_user = conn.assigns[:current_user]
chunks = params["slug"] |> chunk_path()
slug = chunks |> Enum.at(0) |> Enum.join("/")
op = get_op(chunks)
op_params = get_op_params(op, chunks)
repo = RepositoryManager.get_repository_by_owner_and_slug(params["owner"], slug)
if repo && repo.public_access || Repository.member?(repo, current_user) do
org = repo.organisation
user = repo.user
git = setup_git(repo, conn, op, op_params)
first_tree = case Enum.at(git.trees, 0) do
{_, first_tree, _} -> first_tree
nil -> nil
end
tree1 = op_params.tree || first_tree
op_params = %{op_params | tree: tree1, git: git, org: org, repo: repo, user: user}
if git.valid do
show_op(conn, op || :tree, op_params)
else
IO.inspect(:invalid_git)
not_found(conn)
end
else
IO.inspect(:no_repo)
not_found(conn)
end
end
def chunk_path(path) do
chunk_path(path, [[]])
end
def chunk_path([], acc) do
acc
|> Enum.reverse()
|> Enum.map(&Enum.reverse/1)
end
def chunk_path([first | rest], acc = [acc_first | acc_rest]) do
if Regex.match?(~r/^_/, first) do
chunk_path(rest, [[first] | acc])
else
chunk_path(rest, [[first | acc_first] | acc_rest])
end
end
defp get_op(chunks) do
if path = chunks |> Enum.at(1) do
case path |> Enum.at(0) do
"_blob" -> :blob
"_commit" -> :commit
"_diff" -> :diff
"_log" -> :log
"_tag" -> :tag
"_tree" -> :tree
x ->
IO.puts "Unknown operation #{x}"
:unknown
end
end
end
defp get_op_params(nil, _) do
%{tree: nil, from: nil, git: nil, org: nil, path: nil, repo: nil, to: nil, user: nil}
end
defp get_op_params(:diff, chunks) do
path = chunks |> Enum.at(1)
{[_, from, to], _} = path |> Enum.split(3)
%{tree: nil, from: from, git: nil, org: nil, path: nil, repo: nil, to: to, user: nil}
end
defp get_op_params(_, chunks) do
path = chunks |> Enum.at(1)
{_, rest} = chunks |> Enum.split(2)
rest1 = rest |> Enum.map(fn x ->
Enum.join(x, "/")
end)
{[_, tree], path1} = path |> Enum.split(2)
path2 = (path1 ++ rest1)
|> Enum.reject(&(!&1 || &1 == ""))
|> Enum.join("/")
path3 = if path2 != "", do: path2
%{tree: tree, from: nil, git: nil, org: nil, path: path3, repo: nil, to: nil, user: nil}
end
defp setup_git(repo, conn, op, %{path: path}) do
%{trees: [],
content: nil,
content_html: nil,
content_type: nil,
filename: nil,
files: [],
line_numbers: nil,
log1: nil,
markdown_html: nil,
readme: [],
status: "",
tags: [],
valid: true}
|> git_put_branches(repo, conn, op, path)
end
defp git_put_branches(git = %{valid: true}, repo, conn, op, path) do
case GitManager.branches(Repository.full_slug(repo)) do
{:ok, branches} ->
branch_trees = branches
|> Enum.map(fn branch ->
url = Routes.repository_path(conn, :show, Repository.owner_slug(repo), Repository.splat(repo) ++ ["_#{op || :tree}", branch] ++ (if path, do: String.split(path, "/"), else: []))
{:branch, branch, url}
end)
%{git | trees: git.trees ++ branch_trees}
{:error, status} -> %{git | status: status, valid: false}
end
end
defp git_put_branches(git, _, _, _, _) do
git
end
defp git_put_files(git, _, nil, _, _) do
git
end
defp git_put_files(git = %{valid: true}, repo, tree, subdir, conn) do
case GitManager.files(Repository.full_slug(repo), tree, subdir || "") do
{:ok, []} -> git
{:ok, files} ->
files = files
|> Enum.map(fn f = %{url: url} ->
%{f | url: Routes.repository_path(conn, :show, Repository.owner_slug(repo), Repository.splat(repo) ++ ["_tree", tree | String.split(url, "/")])}
end)
%{git | files: files}
{:error, status} -> %{git | status: "#{git.status}\n#{status}", valid: false}
end
end
defp git_put_files(git, _, _, _, _) do
git
end
defp mime_type(content, ext \\ nil) do
if String.valid?(content) do
"text/plain"
else
MIME.type(ext)
end
end
defp filename(path) do
path
|> String.split("/")
|> List.last()
end
defp git_put_content(git = %{files: [%{name: name, sha1: sha1, type: "blob"}], valid: true}, repo, path) do
if (path == name) do
case GitManager.content(Repository.full_slug(repo), sha1) do
{:ok, content} ->
{type, ext} = case Regex.run(~r/[.]([^.]+)$/, path) do
[_, ext] -> {mime_type(content, ext), ext}
_ -> {mime_type(content), nil}
end
content_html = Pygmentize.html(content, filename(name))
line_numbers = line_numbers(content)
markdown_html = if ext && String.match?(ext, ~r/md/i) do
Earmark.as_html!(content)
end
IO.inspect(path: path, name: name, type: type)
%{git | content: content, content_html: content_html, content_type: type, filename: name, line_numbers: line_numbers, markdown_html: markdown_html}
{:error, error} -> %{git | status: error}
end
else
git
end
end
defp git_put_content(git, _, _) do
git
end
defp git_put_readme(git = %{files: files, valid: true}, repo) do
readme = Enum.map(files, fn f ->
if String.match?(f.name, ~r/^readme\.md$/i) do
{:ok, content} = GitManager.content(Repository.full_slug(repo), f.sha1)
%{html: Earmark.as_html!(content),
name: f.name,
txt: content}
else
if String.match?(f.name, ~r/^readme(.txt)?$/i) do
{:ok, content} = GitManager.content(Repository.full_slug(repo), f.sha1)
%{html: nil,
name: f.name,
txt: content}
end
end
end)
|> Enum.filter(& &1)
%{git | readme: readme}
end
defp git_put_readme(git, _) do
git
end
defp git_put_log1(git = %{valid: true}, repo, tree, path) do
slug = Repository.full_slug(repo)
log1 = case if path, do: GitManager.log1_file(slug, path, tree), else: GitManager.log1(slug, tree) do
{:ok, log1} -> log1
{:error, err} ->
IO.inspect(err)
nil
end
%{git | log1: log1}
end
defp git_put_log1(git, _, _, _) do
git
end
defp git_put_tags(git = %{valid: true}, repo, conn, op, path) do
case GitManager.tags(Repository.full_slug(repo)) do
{:ok, tags} ->
tag_trees = tags
|> Enum.map(fn %{tag: tag} ->
url = Routes.repository_path(conn, :show, Repository.owner_slug(repo), Repository.splat(repo) ++ ["_#{op || :tree}", tag] ++ (if path, do: String.split(path, "/"), else: []))
{:tag, tag, url}
end)
|> Enum.reverse()
%{git | tags: tags, trees: git.trees ++ tag_trees}
{:error, status} -> %{git | status: status, valid: false}
end
end
defp git_put_tags(git, _, _, _, _) do
git
end
defp git_put_commit(git = %{valid: true}, repo, conn, op, tree, path) do
if Enum.find(git.trees, fn {_, id, _} -> id == tree end) do
git
else
url = Routes.repository_path(conn, :show, Repository.owner_slug(repo), Repository.splat(repo) ++ ["_#{op || :tree}", tree] ++ (if path, do: String.split(path, "/"), else: []))
%{git | trees: [{:commit, tree, url} | git.trees]}
end
end
defp git_put_commit(git, _, _, _, _, _) do
git
end
defp git_log(repo, tree, path) do
slug = Repository.full_slug(repo)
{:ok, log} = if path do
GitManager.log_file(slug, path, tree)
else
GitManager.log(slug, tree)
end
log
end
defp show_op(conn, :blob, %{git: git, path: path, repo: repo}) do
git = git
|> git_put_content(repo, path)
if (git.content) do
conn
|> put_resp_content_type("application/octet-stream")
|> put_resp_header("Content-Disposition", "attachment; filename=#{git.filename |> URI.encode()}")
|> resp(200, git.content)
else
not_found(conn)
end
end
defp show_op(conn, :commit = op, %{git: git, org: org, path: path, repo: repo, tree: tree}) do
git = git
|> git_put_log1(repo, tree, path)
|> git_put_commit(repo, conn, op, tree, path)
IO.inspect(git)
diff = case GitManager.diff(Repository.full_slug(repo), "#{git.log1.hash}~1", git.log1.hash) do
{:ok, diff} -> diff
_ -> nil
end
diff_html = diff && Pygmentize.html(diff, "diff.patch")
diff_line_numbers = diff && line_numbers(diff)
conn
|> assign(:commit, git.log1)
|> assign_current_organisation(org)
|> assign(:current_repository, repo)
|> assign(:diff_html, diff_html)
|> assign(:diff_line_numbers, diff_line_numbers)
|> assign(:path, path)
|> assign(:repo, repo)
|> render("commit.html")
end
defp show_op(conn, :diff, %{from: from, org: org, path: path, repo: repo, to: to}) do
case GitManager.diff(Repository.full_slug(repo), from, to) do
{:ok, diff} ->
diff_html = Pygmentize.html(diff, "diff.patch")
diff_line_numbers = line_numbers(diff)
IO.inspect(from: from, to: to, diff: diff, html: diff_html, line_numbers: diff_line_numbers)
conn
|> assign_current_organisation(org)
|> assign(:current_repository, repo)
|> assign(:diff_from, from)
|> assign(:diff_html, diff_html)
|> assign(:diff_line_numbers, diff_line_numbers)
|> assign(:diff_to, to)
|> assign(:path, path)
|> assign(:repo, repo)
|> render("diff.html")
{:error, e} ->
IO.inspect(e)
not_found(conn)
end
end
defp show_op(conn, :log, %{tree: tree, git: git, org: org, path: path, repo: repo}) do
log = git_log(repo, tree, path)
IO.inspect([:log, tree: tree, git: git, path: path, log: log])
conn
|> assign(:tree, tree)
|> assign(:tree_url, Routes.repository_path(conn, :show, Repository.owner_slug(repo), Repository.splat(repo, ["_log", tree] ++ (if path, do: String.split(path, "/"), else: []))))
|> assign_current_organisation(org)
|> assign(:current_repository, repo)
|> assign(:git, git)
|> assign(:log, log)
|> assign(:path, path)
|> assign(:repo, repo)
|> render("log.html")
end
defp show_op(conn, :tag = op, %{tree: tree, git: git, org: org, repo: repo}) do
git = git
|> git_put_tags(repo, conn, op, nil)
tag = Enum.find(git.tags, fn tag -> tag.tag == tree end)
conn
|> assign_current_organisation(org)
|> assign(:current_repository, repo)
|> assign(:path, nil)
|> assign(:repo, repo)
|> assign(:tag, tag)
|> render("tag.html")
end
defp show_op(conn, :tree = op, %{tree: tree, git: git, org: org, path: path, repo: repo, user: user}) do
git = git
|> git_put_files(repo, tree, path, conn)
|> git_put_content(repo, path)
|> git_put_readme(repo)
|> git_put_log1(repo, tree, path)
|> git_put_tags(repo, conn, op, path)
conn
|> assign_current_organisation(org)
|> assign(:current_repository, repo)
|> assign(:disk_usage, Repository.disk_usage(repo))
|> assign(:git, git)
|> assign(:repo, repo)
|> assign(:members, Repository.members(repo))
|> assign(:owner, org || user)
|> assign(:path, path)
|> assign(:tree, tree)
|> assign(:tree_url, tree && Routes.repository_path(conn, :show, Repository.owner_slug(repo), Repository.splat(repo, ["_tree", tree] ++ (if path, do: String.split(path, "/"), else: []))))
|> render("show.html")
end
defp show_op(conn, _, _) do
not_found(conn)
end
def edit(conn, params) do
current_user = conn.assigns.current_user
slug = Enum.join(params["slug"], "/")
repo = RepositoryManager.get_repository_by_owner_and_slug(params["owner"], slug)
if repo && Repository.owner?(repo, current_user) do
org = repo.organisation
changeset = RepositoryManager.change_repository(repo)
conn
|> assign(:action, Routes.repository_path(conn, :update, params["owner"], Repository.splat(repo)))
|> assign(:changeset, changeset)
|> assign_current_organisation(org)
|> assign(:current_repository, repo)
|> assign(:repo, repo)
|> render("edit.html")
else
not_found(conn)
end
end
def line_numbers(string) do
string
|> String.split("\n")
|> tl()
|> Enum.with_index()
|> Enum.map(fn {_, i} -> "#{i + 1}" end)
end
def update(conn, params) do
current_user = conn.assigns.current_user
slug = Enum.join(params["slug"], "/")
repo = RepositoryManager.get_repository_by_owner_and_slug(params["owner"], slug)
if repo && Repository.owner?(repo, current_user) do
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
case GitManager.rename(s, s1) do
:ok -> repo1
{:error, err} -> Repo.rollback(err)
end
else
repo1
end
{:error, changeset} -> Repo.rollback(changeset)
end
end) do
{:ok, repo1} ->
case GitManager.update_auth() do
:ok -> :ok = GitManager.public_access(Repository.full_slug(repo1), repo1.public_access)
error -> IO.inspect(error)
end
conn
|> redirect(to: Routes.repository_path(conn, :show, Repository.owner_slug(repo1), Repository.splat(repo1)))
{:error, changeset} ->
conn
|> assign(:action, Routes.repository_path(conn, :update, params["owner"], Repository.splat(repo)))
|> assign(:changeset, changeset)
|> assign_current_organisation(repo.organisation)
|> assign(:current_repository, repo)
|> assign(:repo, repo)
|> render("edit.html")
end
else
not_found(conn)
end
end
def add_user(conn, params) do
current_user = conn.assigns.current_user
slug = Enum.join(params["slug"], "/")
repo = RepositoryManager.get_repository_by_owner_and_slug(params["owner"], slug)
if repo && Repository.owner?(repo, current_user) do
org = repo.organisation
conn
|> assign(:action, Routes.repository_path(conn, :add_user_post, params["owner"], Repository.splat(repo)))
|> assign_current_organisation(org)
|> assign(:current_repository, repo)
|> assign(:repo, repo)
|> render("add_user.html")
else
not_found(conn)
end
end
def add_user_post(conn, params) do
current_user = conn.assigns.current_user
login = params["repository"]["login"]
slug = Enum.join(params["slug"], "/")
repo = RepositoryManager.get_repository_by_owner_and_slug(params["owner"], slug)
if repo && Repository.owner?(repo, current_user) do
org = repo.organisation
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.repository_path(conn, :show, params["owner"], Repository.splat(repo)))
{:error, _} ->
conn
|> assign(:action, Routes.repository_path(conn, :add_user_post, params["owner"], Repository.splat(repo)))
|> assign_current_organisation(org)
|> assign(:current_repository, repo)
|> assign(:repo, repo)
|> render("add_user.html")
end
else
not_found(conn)
end
end
def remove_user(conn, params) do
current_user = conn.assigns.current_user
slug = Enum.join(params["slug"], "/")
repo = RepositoryManager.get_repository_by_owner_and_slug(params["owner"], slug)
if repo && Repository.owner?(repo, current_user) do
org = repo.organisation
conn
|> assign(:action, Routes.repository_path(conn, :remove_user_post, params["owner"], Repository.splat(repo)))
|> assign_current_organisation(org)
|> assign(:current_repository, repo)
|> assign(:repo, repo)
|> render("remove_user.html")
else
not_found(conn)
end
end
def remove_user_post(conn, params) do
current_user = conn.assigns.current_user
login = params["repository"]["login"]
slug = Enum.join(params["slug"], "/")
repo = RepositoryManager.get_repository_by_owner_and_slug(params["owner"], slug)
if repo && Repository.owner?(repo, current_user) do
org = repo.organisation
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.repository_path(conn, :show, params["owner"], Repository.splat(repo)))
{:error, _} ->
conn
|> assign(:action, Routes.repository_path(conn, :remove_user_post, params["owner"], Repository.splat(repo)))
|> assign_current_organisation(org)
|> assign(:current_repository, repo)
|> assign(:repo, repo)
|> render("remove_user.html")
end
else
not_found(conn)
end
end
def delete(conn, params) do
current_user = conn.assigns.current_user
slug = Enum.join(params["slug"], "/")
repo = RepositoryManager.get_repository_by_owner_and_slug(params["owner"], slug)
if repo && Repository.owner?(repo, current_user) do
case Repo.transaction(fn ->
case RepositoryManager.delete_repository(repo) do
{:ok, _} -> :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.slug_path(conn, :show, params["owner"]))
{:error, _changeset} ->
conn
|> redirect(to: Routes.slug_path(conn, :edit, params["owner"]))
end
else
not_found(conn)
end
end
defp assign_current_organisation(conn, nil), do: conn
defp assign_current_organisation(conn, %User{}), do: conn
defp assign_current_organisation(conn, org = %Organisation{}) do
assign(conn, :current_organisation, org)
end
def fork(conn, params) do
current_user = conn.assigns.current_user
slug = Enum.join(params["slug"], "/")
repo = RepositoryManager.get_repository_by_owner_and_slug(params["owner"], slug)
if repo && Repository.member?(repo, current_user) do
org = repo.organisation
changeset = RepositoryManager.change_repository(repo)
conn
|> assign(:action, Routes.repository_path(conn, :fork_post, params["owner"], Repository.splat(repo)))
|> assign(:changeset, changeset)
|> assign_current_organisation(org)
|> assign(:current_repository, repo)
|> assign(:repo, repo)
|> render("fork.html")
else
not_found(conn)
end
end
def fork_post(conn, params) do
current_user = conn.assigns.current_user
slug = Enum.join(params["slug"], "/")
repo = RepositoryManager.get_repository_by_owner_and_slug(params["owner"], slug)
if repo && Repository.member?(repo, current_user) do
fork_to = params["repository"]["fork_to"]
slug = String.split(fork_to, "/") |> Enum.at(0) |> SlugManager.get_slug()
if slug do
user = slug.user
if user do
if user.id == current_user.id do
fork_repo(conn, params["repository"], user, repo)
else
changeset = repo
|> Repository.changeset(params["repository"])
|> Ecto.Changeset.add_error(:fork_to, "you cannot fork to another user")
|> changeset_put_action(:fork)
conn
|> assign(:action, Routes.repository_path(conn, :fork_post, Repository.owner_slug(repo), Repository.splat(repo)))
|> assign(:changeset, changeset)
|> assign_current_organisation(repo.organisation)
|> assign(:current_repository, repo)
|> assign(:repo, repo)
|> render("fork.html")
end
else
%Organisation{} = org = slug.organisation
if Organisation.owner?(org, current_user) do
fork_repo(conn, params["repository"], org, repo)
else
changeset = repo
|> Repository.changeset(params["repository"])
|> Ecto.Changeset.add_error(:fork_to, "you don't have the permission to fork to this organisation")
|> changeset_put_action(:fork)
conn
|> assign(:action, Routes.repository_path(conn, :fork_post, Repository.owner_slug(repo), Repository.splat(repo)))
|> assign(:changeset, changeset)
|> assign_current_organisation(repo.organisation)
|> assign(:current_repository, repo)
|> assign(:repo, repo)
|> render("fork.html")
end
end
else
changeset = repo
|> Repository.changeset(params["repository"])
|> Ecto.Changeset.add_error(:fork_to, "owner was not found")
|> changeset_put_action(:fork)
conn
|> assign(:action, Routes.repository_path(conn, :fork_post, Repository.owner_slug(repo), Repository.splat(repo)))
|> assign(:changeset, changeset)
|> assign_current_organisation(repo.organisation)
|> assign(:current_repository, repo)
|> assign(:repo, repo)
|> render("fork.html")
end
else
not_found(conn)
end
end
defp fork_repo(conn, params, owner, origin) do
[_ | slug] = String.split(params["fork_to"], "/")
slug = Enum.join(slug, "/")
case Repo.transaction(fn ->
case RepositoryManager.fork_repository(origin, owner, slug) do
{:ok, repo} ->
case GitManager.fork(Repository.full_slug(origin), Repository.full_slug(repo)) do
:ok -> repo
{:error, e} ->
repo
|> Repository.changeset(params)
|> Ecto.Changeset.add_error(:fork_to, e)
|> changeset_put_action(:fork)
|> Repo.rollback
end
{: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.repository_path(conn, :show, owner.slug.slug, Repository.splat(repo)))
{:error, changeset} ->
IO.inspect changeset
conn
|> assign(:action, Routes.repository_path(conn, :fork_post, Repository.owner_slug(origin), Repository.splat(origin)))
|> assign(:changeset, changeset)
|> assign_current_organisation(origin.organisation)
|> assign(:current_repository, origin)
|> assign(:repo, origin)
|> render("fork.html")
end
end
defp changeset_put_action(changeset, action) do
%Ecto.Changeset{changeset | action: action}
end
end