Pass device code to teams server

This commit is contained in:
José Valim 2023-05-25 17:17:57 +02:00
parent 30a87e8803
commit 705f122b75
5 changed files with 26 additions and 18 deletions

View file

@ -69,12 +69,12 @@ defmodule Livebook.Teams do
@doc """
Send a request to Livebook Teams API to get an org request.
"""
@spec get_org_request_completion_data(Org.t()) ::
@spec get_org_request_completion_data(Org.t(), binary()) ::
{:ok, map() | :awaiting_confirmation}
| {:error, :expired}
| {:transport_error, String.t()}
def get_org_request_completion_data(%Org{id: id}) do
case Client.get_org_request_completion_data(id) do
def get_org_request_completion_data(%Org{id: id}, device_code) do
case Client.get_org_request_completion_data(id, device_code) do
{:ok, %{"status" => "awaiting_confirmation"}} -> {:ok, :awaiting_confirmation}
{:ok, completion_data} -> {:ok, completion_data}
{:error, %{"status" => "expired"}} -> {:error, :expired}

View file

@ -25,10 +25,10 @@ defmodule Livebook.Teams.Client do
@doc """
Send a request to Livebook Team API to get an org request.
"""
@spec get_org_request_completion_data(pos_integer()) ::
@spec get_org_request_completion_data(pos_integer(), binary) ::
{:ok, map()} | {:error, map() | String.t()} | {:transport_error, String.t()}
def get_org_request_completion_data(id) do
get("/api/org-request/#{id}")
def get_org_request_completion_data(id, device_code) do
get("/api/org-request/#{id}?device_code=#{device_code}")
end
defp post(path, json, header \\ []) do

View file

@ -228,12 +228,16 @@ defmodule LivebookWeb.Hub.NewLive do
end
case result do
{:ok, response} ->
{:ok, %{"device_code" => device_code} = response} ->
attrs = Map.merge(attrs, response)
changeset = Teams.change_org(socket.assigns.org, attrs)
org = Ecto.Changeset.apply_action!(changeset, :insert)
Process.send_after(self(), :check_completion_data, @check_completion_data_interval)
Process.send_after(
self(),
{:check_completion_data, device_code},
@check_completion_data_interval
)
{:noreply,
socket
@ -249,10 +253,14 @@ defmodule LivebookWeb.Hub.NewLive do
end
@impl true
def handle_info(:check_completion_data, %{assigns: %{org: org}} = socket) do
case Teams.get_org_request_completion_data(org) do
def handle_info({:check_completion_data, device_code}, %{assigns: %{org: org}} = socket) do
case Teams.get_org_request_completion_data(org, device_code) do
{:ok, :awaiting_confirmation} ->
Process.send_after(self(), :check_completion_data, @check_completion_data_interval)
Process.send_after(
self(),
{:check_completion_data, device_code},
@check_completion_data_interval
)
{:noreply, socket}

View file

@ -46,7 +46,10 @@ defmodule LivebookWeb.SessionLive.SecretsListComponent do
id={"session-secret-#{secret.name}-delete"}
type="button"
phx-click={
JS.push("delete_session_secret", value: %{secret_name: secret.name}, target: @myself)
JS.push("delete_session_secret",
value: %{secret_name: secret.name},
target: @myself
)
}
class="hover:text-gray-900"
>

View file

@ -4,6 +4,7 @@ defmodule LivebookWeb.Hub.NewLiveTest do
alias Livebook.Teams.Org
import Phoenix.LiveViewTest
@check_completion_data_interval 5000
test "render hub selection cards", %{conn: conn} do
{:ok, view, _html} = live(conn, ~p"/hub")
@ -54,7 +55,7 @@ defmodule LivebookWeb.Hub.NewLiveTest do
# check if the page redirected to edit hub page
# and check the flash message
%{"success" => "Hub added successfully"} =
assert_redirect(view, path, check_completion_data_interval())
assert_redirect(view, path, @check_completion_data_interval)
# checks if the hub is in the sidebar
{:ok, view, _html} = live(conn, path)
@ -111,7 +112,7 @@ defmodule LivebookWeb.Hub.NewLiveTest do
# check if the page redirected to edit hub page
# and check the flash message
%{"success" => "Hub added successfully"} =
assert_redirect(view, path, check_completion_data_interval())
assert_redirect(view, path, @check_completion_data_interval)
# checks if the hub is in the sidebar
{:ok, view, _html} = live(conn, path)
@ -121,8 +122,4 @@ defmodule LivebookWeb.Hub.NewLiveTest do
assert hubs_html =~ name
end
end
defp check_completion_data_interval do
Application.fetch_env!(:livebook, :check_completion_data_interval) + 100
end
end