From b50154648b21d61705b5d0b1aeeb81b81244a817 Mon Sep 17 00:00:00 2001 From: Zacck Date: Wed, 23 Aug 2023 09:58:03 +0200 Subject: [PATCH] Add status fixes and correct error flashes --- .../version_control/github_client.ex | 45 +++++++++----- .../live/project_live/settings.ex | 57 +++++++++++++---- .../version_control/github_client_test.exs | 12 +++- test/lightning_web/live/project_live_test.exs | 62 +++++++++++++++---- 4 files changed, 133 insertions(+), 43 deletions(-) diff --git a/lib/lightning/version_control/github_client.ex b/lib/lightning/version_control/github_client.ex index e052fc8bef..a86d3b05e1 100644 --- a/lib/lightning/version_control/github_client.ex +++ b/lib/lightning/version_control/github_client.ex @@ -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} <- @@ -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) @@ -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"] diff --git a/lib/lightning_web/live/project_live/settings.ex b/lib/lightning_web/live/project_live/settings.ex index 0dabe80fc7..2574000d99 100644 --- a/lib/lightning_web/live/project_live/settings.ex +++ b/lib/lightning_web/live/project_live/settings.ex @@ -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 @@ -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 diff --git a/test/lightning/version_control/github_client_test.exs b/test/lightning/version_control/github_client_test.exs index 427af428a5..790d0da00d 100644 --- a/test/lightning/version_control/github_client_test.exs +++ b/test/lightning/version_control/github_client_test.exs @@ -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 @@ -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 diff --git a/test/lightning_web/live/project_live_test.exs b/test/lightning_web/live/project_live_test.exs index 293a3bea6b..4ac483d439 100644 --- a/test/lightning_web/live/project_live_test.exs +++ b/test/lightning_web/live/project_live_test.exs @@ -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( @@ -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, @@ -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, @@ -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 @@ -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, @@ -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 @@ -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" @@ -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( @@ -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} = @@ -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} = @@ -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,