Skip to content

Commit

Permalink
Merge pull request #1051 from OpenFn/github-sentry-message
Browse files Browse the repository at this point in the history
Coerce correct message to sentry and attempt to fix flaky test
  • Loading branch information
taylordowns2000 authored Aug 25, 2023
2 parents 20f9422 + 8480f2d commit d5d26f0
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 21 deletions.
29 changes: 15 additions & 14 deletions lib/lightning/version_control/github_client.ex
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ defmodule Lightning.VersionControl.GithubClient do
repos_resp.body["repositories"]
|> Enum.map(fn g_repo -> g_repo["full_name"] end)}
else
{:error, :installation_not_found} ->
installation_id_error()
{:error, :installation_not_found, meta} ->
installation_id_error(meta)

{:error, :invalid_pem} ->
invalid_pem_error()
Expand All @@ -38,8 +38,8 @@ defmodule Lightning.VersionControl.GithubClient do

{:ok, branch_names}
else
{:error, :installation_not_found} ->
installation_id_error()
{:error, :installation_not_found, meta} ->
installation_id_error(meta)

{:error, :invalid_pem} ->
invalid_pem_error()
Expand All @@ -51,15 +51,15 @@ defmodule Lightning.VersionControl.GithubClient do
{:ok, %{status: 204}} <-
installation_client
|> post("/repos/#{repo_name}/dispatches", %{
event_type: "Sync by: #{user_email}",
event_type: "sync_project",
client_payload: %{
message: "#{user_email} initiated a sync from Lightning"
}
}) do
{:ok, :fired}
else
{:error, :installation_not_found} ->
installation_id_error()
{:error, :installation_not_found, meta} ->
installation_id_error(meta)

{:error, :invalid_pem} ->
invalid_pem_error()
Expand All @@ -70,15 +70,17 @@ defmodule Lightning.VersionControl.GithubClient do
end
end

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

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

{:error,
%{
Expand Down Expand Up @@ -113,8 +115,7 @@ defmodule Lightning.VersionControl.GithubClient do
{:ok, installation_token_resp} <-
client
|> post("/app/installations/#{installation_id}/access_tokens", ""),
201 <-
installation_token_resp.status do
%{status: 201} <- installation_token_resp do
installation_token = installation_token_resp.body["token"]

{:ok,
Expand All @@ -125,8 +126,8 @@ defmodule Lightning.VersionControl.GithubClient do
]}
])}
else
404 ->
{:error, :installation_not_found}
%{status: 404} = err ->
{:error, :installation_not_found, err}

_unused_status ->
{:error, :invalid_pem}
Expand Down
14 changes: 7 additions & 7 deletions test/lightning/version_control/github_client_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ defmodule Lightning.VersionControl.GithubClientTest do
Tesla.Mock.mock(fn env ->
case env.url do
"https://api.github.com/app/installations/some-id/access_tokens" ->
%Tesla.Env{status: 404}
%Tesla.Env{status: 400}

"https://api.github.com/app/installations/fail-id/access_tokens" ->
%Tesla.Env{status: 400}
%Tesla.Env{status: 404}

"https://api.github.com/installation/repositories" ->
%Tesla.Env{status: 404}
Expand All @@ -35,13 +35,13 @@ defmodule Lightning.VersionControl.GithubClientTest do
end)
end

test "client can handle invalid application message from github" do
test "client can handle invalid application message from github" do
p_repo = insert(:project_repo)

assert {: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"
"Sorry, it seems that the GitHub cert has not been properly configured for this instance of Lightning. Please contact the instance administrator"
}} =
VersionControl.fetch_installation_repos(p_repo.project_id)
end
Expand All @@ -52,7 +52,7 @@ defmodule Lightning.VersionControl.GithubClientTest do
assert {:error,
%{
message:
"Sorry, it seems that the GitHub cert has not been properly configured for this instance of Lightning. Please contact the instance administrator"
"Sorry, it seems that the GitHub App ID has not been properly configured for this instance of Lightning. Please contact the instance administrator"
}} =
VersionControl.run_sync(p_repo.project_id, "some-user-name")
end
Expand All @@ -63,7 +63,7 @@ defmodule Lightning.VersionControl.GithubClientTest do
assert {: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"
"Sorry, it seems that the GitHub cert has not been properly configured for this instance of Lightning. Please contact the instance administrator"
}} =
VersionControl.fetch_repo_branches(p_repo.project_id, p_repo.repo)
end
Expand All @@ -74,7 +74,7 @@ defmodule Lightning.VersionControl.GithubClientTest do
assert {: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"
"Sorry, it seems that the GitHub cert has not been properly configured for this instance of Lightning. Please contact the instance administrator"
}} =
VersionControl.fetch_installation_repos(p_repo.project_id)
end
Expand Down

0 comments on commit d5d26f0

Please sign in to comment.