Skip to content

Commit 9142b04

Browse files
toil(guard): Error handling in update emails script (#423)
## 📝 Description While I was working on [this task](renderedtext/tasks#8114 (comment)), I encountered an error with the update script, when users GH API token is no longer valid. This fixes it. Also, the script is duplicated over 2 services, which is not needed ## ✅ Checklist - [x] I have tested this change - [ ] This change requires documentation update
1 parent 7cacfe3 commit 9142b04

File tree

2 files changed

+26
-134
lines changed

2 files changed

+26
-134
lines changed

ee/rbac/lib/rbac/user/update_mails.ex

Lines changed: 0 additions & 107 deletions
This file was deleted.

guard/lib/guard/user/update_mails.ex

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,12 @@ defmodule Guard.User.UpdateMails do
88
the only use-case for this script was when a organization wants to use SCIM/SAML for SSO, and emails
99
in their SAML provider need to match emails on Semaphoere.
1010
"""
11+
1112
import Ecto.Query
1213
require Logger
1314

15+
@github_api_domain "https://api.github.com/user/emails"
16+
1417
@doc """
1518
If an organization has corporate email address that ends with important-org.org, that would be given
1619
as a parameter togeather with the org's semaphore id.
@@ -20,33 +23,21 @@ defmodule Guard.User.UpdateMails do
2023
token is not valid, or more likely they did not connect ther GitHub account with their corporate mail.
2124
"""
2225
def migrate(org_id, corporate_email_domain) do
23-
user_ids = get_wrong_email_users(org_id, corporate_email_domain)
24-
25-
all_emails =
26-
user_ids
27-
|> Enum.each(fn id ->
28-
{:ok, token} = get_api_token(id)
29-
30-
{:ok, resp} =
31-
HTTPoison.get("https://api.github.com/user/emails", [
32-
{"Authorization", "Token #{token}"}
33-
])
34-
35-
{:ok, body} = resp |> Map.get(:body) |> Jason.decode()
36-
37-
if is_list(body) do
38-
body
39-
|> Enum.map(fn email ->
40-
email["email"]
41-
end)
42-
|> update_email(id, corporate_email_domain)
43-
else
44-
Logger.error("Bad request for user #{id}: #{inspect(resp)}")
45-
nil
46-
end
47-
end)
48-
49-
all_emails
26+
get_wrong_email_users(org_id, corporate_email_domain)
27+
|> Enum.map(fn id -> {id, get_api_token(id)} end)
28+
|> Enum.filter(fn {_, token} -> token != nil end)
29+
|> Enum.each(fn {id, token} ->
30+
{:ok, resp} = HTTPoison.get(@github_api_domain, [{"Authorization", "Token #{token}"}])
31+
{:ok, body} = resp |> Map.get(:body) |> Jason.decode()
32+
33+
if is_list(body) do
34+
body
35+
|> Enum.map(fn email -> email["email"] end)
36+
|> update_email(id, corporate_email_domain)
37+
else
38+
Logger.error("Bad request for user #{id}: #{inspect(resp)}")
39+
end
40+
end)
5041
end
5142

5243
def update_email(emails, user_id, corporate_email_domain) do
@@ -103,5 +94,13 @@ defmodule Guard.User.UpdateMails do
10394

10495
defp get_api_token(user_id) do
10596
Guard.FrontRepo.RepoHostAccount.get_github_token(user_id)
97+
|> case do
98+
{:error, reason} ->
99+
Logger.info("Failed to get GitHub token for user #{user_id}: #{inspect(reason)}")
100+
nil
101+
102+
{:ok, token} ->
103+
token
104+
end
106105
end
107106
end

0 commit comments

Comments
 (0)