Skip to content

Commit 0e0edd0

Browse files
committed
in new credential dialog, add credentials to root project too
1 parent 1f3151f commit 0e0edd0

6 files changed

Lines changed: 103 additions & 19 deletions

File tree

lib/lightning_web/live/credential_live/credential_index_component.ex

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -91,15 +91,9 @@ defmodule LightningWeb.CredentialLive.CredentialIndexComponent do
9191
def handle_event("show_modal", %{"target" => "new_credential"}, socket) do
9292
if socket.assigns.can_create_project_credential do
9393
project_credentials =
94-
if socket.assigns.project do
95-
[
96-
%Lightning.Projects.ProjectCredential{
97-
project_id: socket.assigns.project.id
98-
}
99-
]
100-
else
101-
[]
102-
end
94+
LightningWeb.CredentialLive.Helpers.default_project_credentials(
95+
socket.assigns.project
96+
)
10397

10498
{:noreply,
10599
assign(socket,

lib/lightning_web/live/credential_live/helpers.ex

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,24 @@ defmodule LightningWeb.CredentialLive.Helpers do
145145
}
146146
end
147147

148+
@doc """
149+
Builds the `project_credentials` to pre-select when creating a credential in
150+
a project context: the active project plus its root parent project (when the
151+
active project is a sandbox), deduplicated and order-preserving.
152+
153+
Returns an empty list when there is no project context.
154+
"""
155+
@spec default_project_credentials(Lightning.Projects.Project.t() | nil) ::
156+
[Lightning.Projects.ProjectCredential.t()]
157+
def default_project_credentials(nil), do: []
158+
159+
def default_project_credentials(%Lightning.Projects.Project{} = project) do
160+
[project, Lightning.Projects.root_of(project)]
161+
|> Enum.map(& &1.id)
162+
|> Enum.uniq()
163+
|> Enum.map(&%Lightning.Projects.ProjectCredential{project_id: &1})
164+
end
165+
148166
def handle_save_response(socket, credential) do
149167
if socket.assigns[:on_save] do
150168
socket.assigns[:on_save].(credential)

lib/lightning_web/live/workflow_live/collaborate.ex

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ defmodule LightningWeb.WorkflowLive.Collaborate do
1818
alias Lightning.Workflows.WebhookAuthMethod
1919
alias Lightning.Workflows.Workflow
2020
alias LightningWeb.Channels.WorkflowJSON
21+
alias LightningWeb.CredentialLive
2122

2223
on_mount({LightningWeb.Hooks, :project_scope})
2324
on_mount({LightningWeb.Hooks, :check_limits})
@@ -45,6 +46,8 @@ defmodule LightningWeb.WorkflowLive.Collaborate do
4546
show_credential_modal: false,
4647
credential_schema: nil,
4748
credential_to_edit: nil,
49+
new_credential_project_credentials:
50+
CredentialLive.Helpers.default_project_credentials(project),
4851
show_webhook_auth_modal: false,
4952
webhook_auth_method: nil,
5053
ai_assistant_enabled: AiAssistant.enabled?()
@@ -223,11 +226,7 @@ defmodule LightningWeb.WorkflowLive.Collaborate do
223226
%Lightning.Credentials.Credential{
224227
schema: @credential_schema,
225228
user_id: @current_user.id,
226-
project_credentials: [
227-
%Lightning.Projects.ProjectCredential{
228-
project_id: @project.id
229-
}
230-
]
229+
project_credentials: @new_credential_project_credentials
231230
}
232231
}
233232
on_save={

lib/lightning_web/live/workflow_live/edit.ex

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -454,11 +454,7 @@ defmodule LightningWeb.WorkflowLive.Edit do
454454
credential={
455455
%Lightning.Credentials.Credential{
456456
user_id: @current_user.id,
457-
project_credentials: [
458-
%Lightning.Projects.ProjectCredential{
459-
project_id: @project.id
460-
}
461-
]
457+
project_credentials: @new_credential_project_credentials
462458
}
463459
}
464460
current_user={@current_user}
@@ -1462,6 +1458,10 @@ defmodule LightningWeb.WorkflowLive.Edit do
14621458
show_canvas_placeholder: assigns.live_action == :new,
14631459
show_workflow_ai_chat: false,
14641460
show_job_credential_modal: false,
1461+
new_credential_project_credentials:
1462+
LightningWeb.CredentialLive.Helpers.default_project_credentials(
1463+
assigns.project
1464+
),
14651465
active_modal: nil,
14661466
active_modal_assigns: nil,
14671467
admin_contacts: Projects.list_project_admin_emails(assigns.project.id),

test/lightning_web/live/credential_live_helpers_test.exs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,4 +247,33 @@ defmodule LightningWeb.CredentialLive.HelpersTest do
247247
assert updated.available_projects == projects
248248
end
249249
end
250+
251+
describe "default_project_credentials/1" do
252+
test "returns an empty list without a project context" do
253+
assert Helpers.default_project_credentials(nil) == []
254+
end
255+
256+
test "returns only the project itself for a root project" do
257+
project = insert(:project)
258+
259+
assert [%{project_id: project_id}] =
260+
Helpers.default_project_credentials(project)
261+
262+
assert project_id == project.id
263+
end
264+
265+
test "returns the sandbox and its root parent for a sandbox" do
266+
root = insert(:project)
267+
child = insert(:project, parent_id: root.id)
268+
grandchild = insert(:project, parent_id: child.id)
269+
270+
project_ids =
271+
grandchild
272+
|> Helpers.default_project_credentials()
273+
|> Enum.map(& &1.project_id)
274+
275+
# The active (sandbox) project and the root parent, not the intermediate.
276+
assert project_ids == [grandchild.id, root.id]
277+
end
278+
end
250279
end

test/lightning_web/live/project_live_test.exs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1021,6 +1021,50 @@ defmodule LightningWeb.ProjectLiveTest do
10211021
end)
10221022
end
10231023

1024+
test "new credential in a sandbox pre-selects the sandbox and its root parent",
1025+
%{conn: conn, user: user} do
1026+
root =
1027+
insert(:project,
1028+
name: "root-project",
1029+
project_users: [%{user_id: user.id, role: :admin}]
1030+
)
1031+
1032+
intermediate = insert(:project, name: "intermediate", parent_id: root.id)
1033+
1034+
sandbox =
1035+
insert(:project,
1036+
name: "sandbox-project",
1037+
parent_id: intermediate.id,
1038+
project_users: [%{user_id: user.id, role: :admin}]
1039+
)
1040+
1041+
{:ok, view, _html} =
1042+
live(conn, ~p"/projects/#{sandbox}/settings#credentials",
1043+
on_error: :raise
1044+
)
1045+
1046+
view |> element("#new-credential-option-menu-item") |> render_click()
1047+
view |> select_credential_type("http")
1048+
view |> click_continue()
1049+
1050+
# Both the active sandbox and the root parent are pre-selected; the
1051+
# intermediate project in the chain is not.
1052+
assert has_element?(
1053+
view,
1054+
"#remove-project-credential-button-new-#{sandbox.id}"
1055+
)
1056+
1057+
assert has_element?(
1058+
view,
1059+
"#remove-project-credential-button-new-#{root.id}"
1060+
)
1061+
1062+
refute has_element?(
1063+
view,
1064+
"#remove-project-credential-button-new-#{intermediate.id}"
1065+
)
1066+
end
1067+
10241068
test "support users can create new credentials in the project credentials page",
10251069
%{
10261070
conn: conn,

0 commit comments

Comments
 (0)