Skip to content

Commit

Permalink
Changes secrets wording form label to name (#1419)
Browse files Browse the repository at this point in the history
  • Loading branch information
cristineguadelupe committed Sep 18, 2022
1 parent 2766cf3 commit 52f7d74
Show file tree
Hide file tree
Showing 11 changed files with 39 additions and 39 deletions.
2 changes: 1 addition & 1 deletion assets/js/hooks/js_view.js
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ const JSView = {
} else if (event.type == "secretSelected") {
this.postMessage({
type: "secretSelected",
secretLabel: event.secretLabel,
secretName: event.secretName,
});
}
},
Expand Down
2 changes: 1 addition & 1 deletion assets/js/hooks/js_view/iframe.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import { sha256Base64 } from "../../lib/utils";
// (2): https://developer.mozilla.org/en-US/docs/Web/HTML/Element/iframe#attr-sandbox
// (3): https://developer.mozilla.org/en-US/docs/Web/Security/Secure_Contexts

const IFRAME_SHA256 = "6aUFRKEwZbbohB7OpFdKqnJJVRhccJxHGV+tArREQ+I=";
const IFRAME_SHA256 = "Ogec/87xIEbz3xVctg9QCMqCPCTCf6vyL88bl41PR3I=";

export function initializeIframeSource(iframe, iframePort, iframeUrl) {
const url = getIframeUrl(iframePort, iframeUrl);
Expand Down
8 changes: 4 additions & 4 deletions assets/js/hooks/session.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,8 @@ const Session = {

this.handleEvent(
"secret_selected",
({ select_secret_ref, secret_label }) => {
this.handleSecretSelected(select_secret_ref, secret_label);
({ select_secret_ref, secret_name }) => {
this.handleSecretSelected(select_secret_ref, secret_name);
}
);

Expand Down Expand Up @@ -1041,10 +1041,10 @@ const Session = {
});
},

handleSecretSelected(select_secret_ref, secretLabel) {
handleSecretSelected(select_secret_ref, secretName) {
globalPubSub.broadcast(`js_views:${select_secret_ref}`, {
type: "secretSelected",
secretLabel,
secretName,
});
},

Expand Down
2 changes: 1 addition & 1 deletion iframe/priv/static/iframe/v4.html
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@
}
);
} else if (message.type === "secretSelected") {
state.secretHandler && state.secretHandler(message.secretLabel);
state.secretHandler && state.secretHandler(message.secretName);
}
}

Expand Down
4 changes: 2 additions & 2 deletions lib/livebook/runtime/evaluator/default_formatter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,8 @@ defmodule Livebook.Runtime.Evaluator.DefaultFormatter do
IO.ANSI.format([:red, string], true)
end

defp error_type(%System.EnvError{env: "LB_" <> secret_label}),
do: {:missing_secret, secret_label}
defp error_type(%System.EnvError{env: "LB_" <> secret_name}),
do: {:missing_secret, secret_name}

defp error_type(_), do: :other
end
2 changes: 1 addition & 1 deletion lib/livebook/session.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1571,7 +1571,7 @@ defmodule Livebook.Session do
end

defp set_runtime_secrets(state, secrets) do
secrets = Enum.map(secrets, &{"LB_#{&1.label}", &1.value})
secrets = Enum.map(secrets, &{"LB_#{&1.name}", &1.value})
Runtime.put_system_envs(state.data.runtime, secrets)
end

Expand Down
2 changes: 1 addition & 1 deletion lib/livebook/session/data.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1508,7 +1508,7 @@ defmodule Livebook.Session.Data do
end

defp put_secret({data, _} = data_actions, secret) do
idx = Enum.find_index(data.secrets, &(&1.label == secret.label))
idx = Enum.find_index(data.secrets, &(&1.name == secret.name))

secrets =
if idx do
Expand Down
8 changes: 4 additions & 4 deletions lib/livebook_web/live/output.ex
Original file line number Diff line number Diff line change
Expand Up @@ -235,11 +235,11 @@ defmodule LivebookWeb.Output do
)
end

defp render_output({:error, formatted, {:missing_secret, secret_label}}, %{
defp render_output({:error, formatted, {:missing_secret, secret_name}}, %{
socket: socket,
session_id: session_id
}) do
assigns = %{message: formatted, secret_label: secret_label}
assigns = %{message: formatted, secret_name: secret_name}

~H"""
<div class="-m-4 space-x-4 py-4">
Expand All @@ -249,9 +249,9 @@ defmodule LivebookWeb.Output do
>
<div class="flex space-x-2 font-editor">
<.remix_icon icon="close-circle-line" />
<span>Missing secret <%= inspect(@secret_label) %></span>
<span>Missing secret <%= inspect(@secret_name) %></span>
</div>
<%= live_patch to: Routes.session_path(socket, :secrets, session_id, secret_label: secret_label),
<%= live_patch to: Routes.session_path(socket, :secrets, session_id, secret_name: secret_name),
class: "button-base button-gray",
aria_label: "add secret",
role: "button" do %>
Expand Down
6 changes: 3 additions & 3 deletions lib/livebook_web/live/session_live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ defmodule LivebookWeb.SessionLive do
id="secrets"
session={@session}
secrets={@data_view.secrets}
prefill_secret_label={@prefill_secret_label}
prefill_secret_name={@prefill_secret_name}
select_secret_ref={@select_secret_ref}
preselect_name={@preselect_name}
return_to={@self_path}
Expand Down Expand Up @@ -556,7 +556,7 @@ defmodule LivebookWeb.SessionLive do
<%= for secret <- @data_view.secrets do %>
<div class="flex justify-between items-center text-gray-500">
<span class="break-all">
<%= secret.label %>
<%= secret.name %>
</span>
<span class="rounded-full bg-gray-200 px-2 text-xs text-gray-600">
Session
Expand Down Expand Up @@ -757,7 +757,7 @@ defmodule LivebookWeb.SessionLive do
when socket.assigns.live_action == :secrets do
{:noreply,
assign(socket,
prefill_secret_label: params["secret_label"],
prefill_secret_name: params["secret_name"],
preselect_name: params["preselect_name"],
select_secret_ref: if(params["preselect_name"], do: socket.assigns.select_secret_ref)
)}
Expand Down
38 changes: 19 additions & 19 deletions lib/livebook_web/live/session_live/secrets_component.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ defmodule LivebookWeb.SessionLive.SecretsComponent do
if socket.assigns[:data] do
socket
else
assign(socket, data: %{"label" => prefill_secret_label(socket), "value" => ""})
assign(socket, data: %{"name" => prefill_secret_name(socket), "value" => ""})
end

{:ok, socket}
Expand All @@ -35,14 +35,14 @@ defmodule LivebookWeb.SessionLive.SecretsComponent do
errors={data_errors(@data)}
>
<div class="flex flex-col space-y-4">
<.input_wrapper form={f} field={:label}>
<.input_wrapper form={f} field={:name}>
<div class="input-label">
Label <span class="text-xs text-gray-500">(alphanumeric and underscore)</span>
Name <span class="text-xs text-gray-500">(alphanumeric and underscore)</span>
</div>
<%= text_input(f, :label,
value: @data["label"],
<%= text_input(f, :name,
value: @data["name"],
class: "input",
autofocus: !@prefill_secret_label,
autofocus: !@prefill_secret_name,
spellcheck: "false"
) %>
</.input_wrapper>
Expand All @@ -51,7 +51,7 @@ defmodule LivebookWeb.SessionLive.SecretsComponent do
<%= text_input(f, :value,
value: @data["value"],
class: "input",
autofocus: !!@prefill_secret_label || unavailable_secret?(@preselect_name, @secrets),
autofocus: !!@prefill_secret_name || unavailable_secret?(@preselect_name, @secrets),
spellcheck: "false"
) %>
</.input_wrapper>
Expand Down Expand Up @@ -83,22 +83,22 @@ defmodule LivebookWeb.SessionLive.SecretsComponent do

@impl true
def handle_event("save", %{"data" => data}, socket) do
secret_label = String.upcase(data["label"])
secret_name = String.upcase(data["name"])

if data_errors(data) == [] do
secret = %{label: secret_label, value: data["value"]}
secret = %{name: secret_name, value: data["value"]}
Livebook.Session.put_secret(socket.assigns.session.pid, secret)

{:noreply,
socket |> push_patch(to: socket.assigns.return_to) |> push_secret_selected(secret_label)}
socket |> push_patch(to: socket.assigns.return_to) |> push_secret_selected(secret_name)}
else
{:noreply, assign(socket, data: data)}
end
end

def handle_event("select_secret", %{"secret" => secret_label}, socket) do
def handle_event("select_secret", %{"secret" => secret_name}, socket) do
{:noreply,
socket |> push_patch(to: socket.assigns.return_to) |> push_secret_selected(secret_label)}
socket |> push_patch(to: socket.assigns.return_to) |> push_secret_selected(secret_name)}
end

def handle_event("validate", %{"data" => data}, socket) do
Expand All @@ -115,7 +115,7 @@ defmodule LivebookWeb.SessionLive.SecretsComponent do
end)
end

defp data_error("label", value) do
defp data_error("name", value) do
cond do
String.match?(value, ~r/^\w+$/) -> nil
value == "" -> "can't be blank"
Expand All @@ -126,16 +126,16 @@ defmodule LivebookWeb.SessionLive.SecretsComponent do
defp data_error("value", ""), do: "can't be blank"
defp data_error(_key, _value), do: nil

defp secret_options(secrets), do: [{"", ""} | Enum.map(secrets, &{&1.label, &1.label})]
defp secret_options(secrets), do: [{"", ""} | Enum.map(secrets, &{&1.name, &1.name})]

defp push_secret_selected(%{assigns: %{select_secret_ref: nil}} = socket, _), do: socket

defp push_secret_selected(%{assigns: %{select_secret_ref: ref}} = socket, secret_label) do
push_event(socket, "secret_selected", %{select_secret_ref: ref, secret_label: secret_label})
defp push_secret_selected(%{assigns: %{select_secret_ref: ref}} = socket, secret_name) do
push_event(socket, "secret_selected", %{select_secret_ref: ref, secret_name: secret_name})
end

defp prefill_secret_label(socket) do
case socket.assigns.prefill_secret_label do
defp prefill_secret_name(socket) do
case socket.assigns.prefill_secret_name do
nil ->
if unavailable_secret?(socket.assigns.preselect_name, socket.assigns.secrets),
do: socket.assigns.preselect_name,
Expand All @@ -150,6 +150,6 @@ defmodule LivebookWeb.SessionLive.SecretsComponent do
defp unavailable_secret?("", _), do: false

defp unavailable_secret?(preselect_name, secrets) do
preselect_name not in Enum.map(secrets, & &1.label)
preselect_name not in Enum.map(secrets, & &1.name)
end
end
4 changes: 2 additions & 2 deletions test/livebook_web/live/session_live_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -921,9 +921,9 @@ defmodule LivebookWeb.SessionLiveTest do

view
|> element(~s{form[phx-submit="save"]})
|> render_submit(%{data: %{label: "foo", value: "123"}})
|> render_submit(%{data: %{name: "foo", value: "123"}})

assert %{secrets: [%{label: "FOO", value: "123"}]} = Session.get_data(session.pid)
assert %{secrets: [%{name: "FOO", value: "123"}]} = Session.get_data(session.pid)
end

test "shows the 'Add secret' button for unavailable secrets", %{conn: conn, session: session} do
Expand Down

0 comments on commit 52f7d74

Please sign in to comment.