New repository: changing the owner no longer crashes the form

7784d39 · Gabriel Morell · 2026-09-29 18:55

10 files +28 -2
Message
{commit_body(@commit)}

Files changed

modified lib/git_gud_web/live/repo_live/new.ex
+7 −2
@@ -34,8 +34,11 @@ defmodule GitGudWeb.RepoLive.New do
34 34 do: Enum.filter(orgs, &Organizations.org_admin?(&1, user))
35 35
36 36 @impl true
37 − def handle_event("validate", %{"repository" => attrs} = params, socket) do
37 + def handle_event("validate", params, socket) do
38 38 namespace = params["namespace"] || socket.assigns.namespace_choice
39 + # Anything changing only the owner arrives without the repository
40 + # fields; validate what the form last held rather than crashing.
41 + attrs = params["repository"] || socket.assigns.form.params
39 42
40 43 cs =
41 44 %Repository{}
@@ -100,7 +103,9 @@ defmodule GitGudWeb.RepoLive.New do
100 103 >
101 104 <label class="flex flex-col gap-1">
102 105 <span class="text-sm opacity-70">Owner</span>
103 − <select name="namespace" class="select select-bordered" phx-change="validate">
106 + <%!-- No phx-change of its own: the form's sends every field,
107 + where one here sent only `namespace` and crashed validate. --%>
108 + <select name="namespace" class="select select-bordered">
104 109 <option value="user" selected={@namespace_choice == "user"}>
105 110 {Storage.owner_handle(@user)} (you)
106 111 </option>
modified test/git_gud_web/live/repo_live/new_owner_default_test.exs
+21 −0
@@ -43,4 +43,25 @@ defmodule GitGudWeb.RepoLive.NewOwnerDefaultTest do
43 43 {:ok, view, _html} = live(conn, ~p"/repositories/new")
44 44 assert has_element?(view, ~s{select[name="namespace"] option[value="user"][selected]})
45 45 end
46 +
47 + test "changing the owner doesn't crash the form", %{conn: conn, org: org} do
48 + {:ok, view, _html} = live(conn, ~p"/repositories/new")
49 +
50 + view
51 + |> form("#new-repo-form", %{
52 + "namespace" => "org:#{org.handle}",
53 + "repository" => %{"name" => "x"}
54 + })
55 + |> render_change()
56 +
57 + assert has_element?(
58 + view,
59 + ~s{select[name="namespace"] option[value="org:#{org.handle}"][selected]}
60 + )
61 +
62 + # The shape the old select-level phx-change sent: the owner alone.
63 + # It used to be a FunctionClauseError and a reconnect.
64 + render_hook(view, "validate", %{"namespace" => "user"})
65 + assert has_element?(view, ~s{select[name="namespace"] option[value="user"][selected]})
66 + end
46 67 end

Parents: 59086ac