Skip to content

Commit 302fe1a

Browse files
committed
feat: add function to sync GitHub data in bulk
1 parent a0e2bf7 commit 302fe1a

File tree

2 files changed

+66
-7
lines changed

2 files changed

+66
-7
lines changed

lib/algora/admin/admin.ex

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,31 @@ defmodule Algora.Admin do
2222
alias Algora.Workspace.Installation
2323
alias Algora.Workspace.Jobs.FetchTopContributions
2424
alias Algora.Workspace.Jobs.ImportStargazer
25+
alias Algora.Workspace.Jobs.SyncUser
2526
alias Algora.Workspace.Repository
2627
alias Algora.Workspace.Ticket
2728

2829
require Logger
2930

30-
def sync_user(provider_login) do
31-
with {:ok, data} <- Github.get_user_by_username(token(), provider_login),
32-
{:ok, user} <- Workspace.ensure_user(token(), data["login"]) do
33-
user
34-
|> change(%{display_name: data["name"], location: data["location"]})
35-
|> Repo.update()
36-
end
31+
def sync_users do
32+
Repo.transact(
33+
fn ->
34+
User
35+
# |> where([u], is_nil(u.display_name))
36+
|> where([u], not is_nil(u.provider_login))
37+
|> where([u], not is_nil(u.display_name))
38+
|> where([u], u.display_name == u.provider_login)
39+
|> where([u], not is_nil(u.provider_id))
40+
|> where([u], u.type == :individual)
41+
|> Repo.stream()
42+
|> Enum.each(fn user -> sync_user(provider_id: user.provider_id) end)
43+
end,
44+
timeout: :infinity
45+
)
46+
end
47+
48+
def sync_user(opts) do
49+
opts |> Map.new() |> SyncUser.new() |> Oban.insert()
3750
end
3851

3952
def backfill_charge(id) do
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
defmodule Algora.Workspace.Jobs.SyncUser do
2+
@moduledoc false
3+
use Oban.Worker,
4+
queue: :sync_contribution,
5+
max_attempts: 3
6+
7+
import Ecto.Changeset
8+
9+
alias Algora.Github
10+
alias Algora.Repo
11+
alias Algora.Workspace
12+
13+
@impl Oban.Worker
14+
def perform(%Oban.Job{args: %{"provider_id" => provider_id}}) do
15+
token = Github.TokenPool.get_token()
16+
17+
with {:ok, data} <- Github.get_user(token, provider_id),
18+
{:ok, user} <- Workspace.ensure_user_by_provider_id(token, data["id"]) do
19+
user
20+
|> change(%{
21+
display_name: data["name"],
22+
location: data["location"],
23+
provider_meta: data,
24+
provider_login: data["login"]
25+
})
26+
|> Repo.update()
27+
end
28+
end
29+
30+
@impl Oban.Worker
31+
def perform(%Oban.Job{args: %{"provider_login" => provider_login}}) do
32+
token = Github.TokenPool.get_token()
33+
34+
with {:ok, data} <- Github.get_user_by_username(token, provider_login),
35+
{:ok, user} <- Workspace.ensure_user_by_provider_id(token, data["id"]) do
36+
user
37+
|> change(%{
38+
display_name: data["name"],
39+
location: data["location"],
40+
provider_meta: data,
41+
provider_login: data["login"]
42+
})
43+
|> Repo.update()
44+
end
45+
end
46+
end

0 commit comments

Comments
 (0)