Skip to content

Commit

Permalink
Add status fixes and correct error flashes
Browse files Browse the repository at this point in the history
  • Loading branch information
zacck committed Aug 23, 2023
1 parent 9be39cc commit b501546
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 43 deletions.
45 changes: 28 additions & 17 deletions lib/lightning/version_control/github_client.ex
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,6 @@ defmodule Lightning.VersionControl.GithubClient do
end
end

defp installation_id_error do
{:error,
%{
message:
"Invalid installation ID, ensure to use the ID provided by Github"
}}
end

defp invalid_pem_error do
{:error,
%{
message:
"Invalid Github PEM KEY, ensure to use the KEY provided by Github"
}}
end

def get_repo_branches(installation_id, repo_name) do
with {:ok, installation_client} <- build_client(installation_id),
{:ok, %{status: 200} = branches} <-
Expand Down Expand Up @@ -83,6 +67,33 @@ defmodule Lightning.VersionControl.GithubClient do
end
end

def send_sentry_error(msg) do
Sentry.capture_message("Github configuration error",
message: msg,
tags: %{type: "github"}
)
end

defp installation_id_error do
send_sentry_error("Github Installation APP ID is misconfigured")

{:error,
%{
message:
"Sorry, it seems that the GitHub App ID has not been properly configured for this instance of Lightning. Please contact the instance administrator"
}}
end

defp invalid_pem_error do
send_sentry_error("Github Cert is misconfigured")

{:error,
%{
message:
"Sorry, it seems that the GitHub cert has not been properly configured for this instance of Lightning. Please contact the instance administrator"
}}
end

defp build_client(installation_id) do
%{cert: cert, app_id: app_id} =
Application.get_env(:lightning, :github_app)
Expand All @@ -99,7 +110,7 @@ defmodule Lightning.VersionControl.GithubClient do
{:ok, installation_token_resp} <-
client
|> post("/app/installations/#{installation_id}/access_tokens", ""),
200 <-
201 <-
installation_token_resp.status do
installation_token = installation_token_resp.body["token"]

Expand Down
57 changes: 44 additions & 13 deletions lib/lightning_web/live/project_live/settings.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ defmodule LightningWeb.ProjectLive.Settings do
@moduledoc """
Index Liveview for Runs
"""
alias Lightning.VersionControl.GithubClient
use LightningWeb, :live_view

alias Lightning.VersionControl
Expand Down Expand Up @@ -246,28 +247,58 @@ defmodule LightningWeb.ProjectLive.Settings do
user_id = socket.assigns.current_user.id
project_id = socket.assigns.project.id

{:ok, _connection} =
VersionControl.create_github_connection(%{
user_id: user_id,
project_id: project_id
})
case Application.get_env(:lightning, :github_app) |> Map.new() do
%{app_name: nil} ->
# Send to sentry and show cozy error

GithubClient.send_sentry_error("Github App Name Misconfigured")

{:noreply,
socket
|> put_flash(
:error,
"Sorry, it seems that the GitHub App Name has not been properly configured for this instance of Lighting. Please contact the instance administrator"
)}

%{app_name: app_name} ->
{:ok, _connection} =
VersionControl.create_github_connection(%{
user_id: user_id,
project_id: project_id
})

{:noreply, redirect(socket, external: "https://github.com/apps/openfn")}
{:noreply,
redirect(socket, external: "https://github.com/apps/#{app_name}")}
end
end

def handle_event("reinstall_app", _, socket) do
user_id = socket.assigns.current_user.id
project_id = socket.assigns.project.id

{:ok, _} = VersionControl.remove_github_connection(project_id)
case Application.get_env(:lightning, :github_app) |> Map.new() do
%{app_name: nil} ->
GithubClient.send_sentry_error("Github App Name Misconfigured")

{:ok, _connection} =
VersionControl.create_github_connection(%{
user_id: user_id,
project_id: project_id
})
{:noreply,
socket
|> put_flash(
:error,
"Sorry, it seems that the GitHub App Name has not been properly configured for this instance of Lighting. Please contact the instance administrator"
)}

%{app_name: app_name} ->
{:ok, _} = VersionControl.remove_github_connection(project_id)

{:noreply, redirect(socket, external: "https://github.com/apps/openfn")}
{:ok, _connection} =
VersionControl.create_github_connection(%{
user_id: user_id,
project_id: project_id
})

{:noreply,
redirect(socket, external: "https://github.com/apps/#{app_name}")}
end
end

def handle_event("delete_repo_connection", _, socket) do
Expand Down
12 changes: 10 additions & 2 deletions test/lightning/version_control/github_client_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ defmodule Lightning.VersionControl.GithubClientTest do

describe "Non success Github Client" do
setup do
put_temporary_env(:lightning, :github_app, cert: @cert, app_id: "111111", app_name: "test-github")
put_temporary_env(:lightning, :github_app,
cert: @cert,
app_id: "111111",
app_name: "test-github"
)

Tesla.Mock.mock(fn env ->
case env.url do
Expand Down Expand Up @@ -78,7 +82,11 @@ defmodule Lightning.VersionControl.GithubClientTest do

describe "Github Client" do
setup do
put_temporary_env(:lightning, :github_app, cert: @cert, app_id: "111111", app_name: "test-github")
put_temporary_env(:lightning, :github_app,
cert: @cert,
app_id: "111111",
app_name: "test-github"
)

Tesla.Mock.mock(fn env ->
case env.url do
Expand Down
62 changes: 51 additions & 11 deletions test/lightning_web/live/project_live_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,11 @@ defmodule LightningWeb.ProjectLiveTest do
conn: conn,
project: project
} do
put_temporary_env(:lightning, :github_app, cert: @cert, app_id: "111111", app_name: "test-github")
put_temporary_env(:lightning, :github_app,
cert: @cert,
app_id: "111111",
app_name: "test-github"
)

{:ok, _view, html} =
live(
Expand All @@ -456,7 +460,11 @@ defmodule LightningWeb.ProjectLiveTest do
project: project,
user: user
} do
put_temporary_env(:lightning, :github_app, cert: @cert, app_id: "111111", app_name: "test-github")
put_temporary_env(:lightning, :github_app,
cert: @cert,
app_id: "111111",
app_name: "test-github"
)

insert(:project_repo, %{
project: project,
Expand All @@ -480,7 +488,11 @@ defmodule LightningWeb.ProjectLiveTest do
project: project,
user: user
} do
put_temporary_env(:lightning, :github_app, cert: @cert, app_id: "111111", app_name: "test-github")
put_temporary_env(:lightning, :github_app,
cert: @cert,
app_id: "111111",
app_name: "test-github"
)

insert(:project_repo, %{
project: project,
Expand All @@ -496,7 +508,8 @@ defmodule LightningWeb.ProjectLiveTest do
~p"/projects/#{project.id}/settings#vcs"
)

assert render(view) =~ "Sorry, it seems that the GitHub App ID has not been properly configured for this instance of Lightning. Please contact the instance administrator"
assert render(view) =~
"Sorry, it seems that the GitHub App ID has not been properly configured for this instance of Lightning. Please contact the instance administrator"
end

@tag role: :admin
Expand All @@ -505,7 +518,11 @@ defmodule LightningWeb.ProjectLiveTest do
project: project,
user: user
} do
put_temporary_env(:lightning, :github_app, cert: @cert, app_id: "111111", app_name: "test-github")
put_temporary_env(:lightning, :github_app,
cert: @cert,
app_id: "111111",
app_name: "test-github"
)

insert(:project_repo, %{
project: project,
Expand All @@ -521,7 +538,8 @@ defmodule LightningWeb.ProjectLiveTest do
~p"/projects/#{project.id}/settings#vcs"
)

assert render(view) =~ "Sorry, it seems that the GitHub cert has not been properly configured for this instance of Lightning. Please contact the instance administrator"
assert render(view) =~
"Sorry, it seems that the GitHub cert has not been properly configured for this instance of Lightning. Please contact the instance administrator"
end

@tag role: :admin
Expand All @@ -530,7 +548,11 @@ defmodule LightningWeb.ProjectLiveTest do
project: project,
user: user
} do
put_temporary_env(:lightning, :github_app, cert: @cert, app_id: "111111", app_name: "test-github")
put_temporary_env(:lightning, :github_app,
cert: @cert,
app_id: "111111",
app_name: "test-github"
)

repository = "some-repo"

Expand All @@ -555,7 +577,11 @@ defmodule LightningWeb.ProjectLiveTest do
conn: conn,
project: project
} do
put_temporary_env(:lightning, :github_app, cert: @cert, app_id: "111111", app_name: "test-github")
put_temporary_env(:lightning, :github_app,
cert: @cert,
app_id: "111111",
app_name: "test-github"
)

{:ok, view, _html} =
live(
Expand All @@ -571,7 +597,12 @@ defmodule LightningWeb.ProjectLiveTest do
conn: conn,
project: project
} do
put_temporary_env(:lightning, :github_app, cert: @cert, app_id: "111111", app_name: "test-github")
put_temporary_env(:lightning, :github_app,
cert: @cert,
app_id: "111111",
app_name: "test-github"
)

insert(:project_repo, %{project_id: project.id, project: nil})

{:ok, view, _html} =
Expand All @@ -588,7 +619,12 @@ defmodule LightningWeb.ProjectLiveTest do
conn: conn,
project: project
} do
put_temporary_env(:lightning, :github_app, cert: @cert, app_id: "111111", app_name: "test-github")
put_temporary_env(:lightning, :github_app,
cert: @cert,
app_id: "111111",
app_name: "test-github"
)

insert(:project_repo, %{project_id: project.id, project: nil})

{:ok, view, _html} =
Expand All @@ -606,7 +642,11 @@ defmodule LightningWeb.ProjectLiveTest do
conn: conn,
project: project
} do
put_temporary_env(:lightning, :github_app, cert: @cert, app_id: "111111", app_name: "test-github")
put_temporary_env(:lightning, :github_app,
cert: @cert,
app_id: "111111",
app_name: "test-github"
)

insert(:project_repo, %{
project_id: project.id,
Expand Down

0 comments on commit b501546

Please sign in to comment.