api: return ssh_url on repo create (turnkey push-local loop)
aa56cab · gmorell · 2026-06-25 22:34
Files changed
modified
lib/git_gud_web/controllers/api/v1/org_controller.ex
+13
−3
@@ -32,7 +32,7 @@ defmodule GitGudWeb.Api.V1.OrgController do
| 32 | 32 | |
| 33 | 33 | case Repositories.create_repository_for_org(org, user, attrs) do |
| 34 | 34 | {:ok, repo} -> |
| 35 | − conn |> put_status(201) |> json(repo_json(handle, repo)) | |
| 35 | + conn |> put_status(201) |> json(repo_json(user, handle, repo)) | |
| 36 | 36 | |
| 37 | 37 | {:error, :forbidden} -> |
| 38 | 38 | send_error(conn, 403, "must be an admin of #{handle}") |
@@ -70,7 +70,7 @@ defmodule GitGudWeb.Api.V1.OrgController do
| 70 | 70 | end |
| 71 | 71 | end |
| 72 | 72 | |
| 73 | − defp repo_json(handle, repo) do | |
| 73 | + defp repo_json(user, handle, repo) do | |
| 74 | 74 | full = handle <> "/" <> repo.name |
| 75 | 75 | base = GitGudWeb.Endpoint.url() |
| 76 | 76 |
@@ -81,10 +81,20 @@ defmodule GitGudWeb.Api.V1.OrgController do
| 81 | 81 | "private" => repo.visibility == "private", |
| 82 | 82 | "default_branch" => repo.default_branch, |
| 83 | 83 | "html_url" => base <> "/r/" <> full, |
| 84 | − "clone_url" => base <> "/" <> full <> ".git" | |
| 84 | + "clone_url" => base <> "/" <> full <> ".git", | |
| 85 | + "ssh_url" => ssh_url(user, full) | |
| 85 | 86 | } |
| 86 | 87 | end |
| 87 | 88 | |
| 89 | + # Mirrors the repo page's SSH clone URL. `<user>` is the authenticated | |
| 90 | + # token owner's handle (whoever pushes, validated by their SSH key). | |
| 91 | + defp ssh_url(user, full) do | |
| 92 | + cfg = Application.get_env(:git_gud, GitGud.Ssh.Server, []) | |
| 93 | + host = cfg[:external_host] || URI.parse(GitGudWeb.Endpoint.url()).host || "localhost" | |
| 94 | + port = cfg[:external_port] || cfg[:port] || 2222 | |
| 95 | + "ssh://#{user.handle}@#{host}:#{port}/#{full}.git" | |
| 96 | + end | |
| 97 | + | |
| 88 | 98 | defp changeset_message(cs) do |
| 89 | 99 | cs |
| 90 | 100 | |> Ecto.Changeset.traverse_errors(fn {m, _} -> m end) |
Parents: bc58dcd