Skip to content

Commit 5292ee3

Browse files
committed
Experiment of syncing all issues
1 parent 5f92720 commit 5292ee3

File tree

3 files changed

+30
-12
lines changed

3 files changed

+30
-12
lines changed

lib/code_corps/github/api/eager_api.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
defmodule CodeCorps.GitHub.EagerAPI do
22
@moduledoc """
3-
This module attempts to implement eager loading of a resource, by trying to fetch
4-
all of it's pages in paralel.
3+
This module attempts to implement eager loading of a resource, by trying to
4+
fetch all of its pages in parallel.
55
66
This should technically be faster than lazy loading. However, it fails due to
77
timeout errors, even when loading just two pages.

lib/code_corps/github/api/repository.ex

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@ defmodule CodeCorps.GitHub.API.Repository do
1515
@spec issues(GithubRepo.t) :: {:ok, list(map)} | {:error, GitHub.api_error_struct}
1616
def issues(%GithubRepo{github_app_installation: %GithubAppInstallation{} = installation} = github_repo) do
1717
with {:ok, access_token} <- installation |> API.Installation.get_access_token(),
18-
issues <-github_repo |> fetch_issues(access_token) do
18+
issues <- github_repo |> fetch_issues(access_token) do
1919
{:ok, issues}
2020
else
2121
{:error, error} -> {:error, error}
2222
end
2323
end
2424

25-
defp fetch_issues(%GithubRepo{github_account_login: owner, name: repo}, access_token) do
26-
path = "repos/#{owner}/#{repo}/issues"
25+
defp fetch_issues(%GithubRepo{github_app_installation: %GithubAppInstallation{github_account_login: owner}, name: repo}, access_token) do
26+
path = "repos/#{owner}/#{repo}/issues?state=all&per_page=100"
2727
opts = [access_token: access_token]
2828

2929
# stream/lazy
@@ -33,10 +33,10 @@ defmodule CodeCorps.GitHub.API.Repository do
3333
IO.puts("Retrieving #{results |> Enum.count} took #{Timex.diff(after_operation, before_operation)} microseconds")
3434

3535
# eager
36-
before_operation = Timex.now
37-
results = path |> fetch_eager(opts)
38-
after_operation = Timex.now
39-
IO.puts("Retrieving #{results |> Enum.count} took #{Timex.diff(after_operation, before_operation)} microseconds")
36+
# before_operation = Timex.now
37+
# results = path |> fetch_eager(opts)
38+
# after_operation = Timex.now
39+
# IO.puts("Retrieving #{results |> Enum.count} took #{Timex.diff(after_operation, before_operation)} microseconds")
4040

4141
results
4242
end

lib/code_corps/github/sync/sync.ex

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,13 @@ defmodule CodeCorps.GitHub.Sync do
66
GithubPullRequest,
77
GithubRepo,
88
GitHub.Sync.Utils.RepoFinder,
9-
Repo,
10-
Task
9+
Repo
1110
}
1211
alias Ecto.Multi
1312

1413
@type outcome :: {:ok, list(Comment.t)}
1514
| {:ok, GithubPullRequest.t}
16-
| {:ok, list(Task.t)}
15+
| {:ok, list(CodeCorps.Task.t)}
1716
| {:error, :repo_not_found}
1817
| {:error, :fetching_issue}
1918
| {:error, :fetching_pull_request}
@@ -100,6 +99,25 @@ defmodule CodeCorps.GitHub.Sync do
10099
|> transact()
101100
end
102101

102+
def sync_issues(repo) do
103+
{:ok, issues} = GitHub.API.Repository.issues(repo)
104+
issues
105+
|> Enum.map(&sync_issue(&1, repo))
106+
end
107+
108+
def sync_issue(issue, repo) do
109+
Multi.new
110+
|> Multi.merge(__MODULE__, :return_repo, [repo])
111+
|> Multi.merge(GitHub.Sync.Issue, :sync, [issue])
112+
|> transact()
113+
end
114+
115+
@doc false
116+
def return_repo(_, repo) do
117+
Multi.new
118+
|> Multi.run(:repo, fn _ -> {:ok, repo} end)
119+
end
120+
103121
@doc false
104122
def find_repo(_, payload) do
105123
Multi.new

0 commit comments

Comments
 (0)