Skip to content

Commit 76acde5

Browse files
committed
Testing and refactoring
Get acceptance tests for github repo sync working Get all GitHub acceptance tests passing Add syncing count to repo sync Update sync states, add to views, update syncing to start when creating project repo Add tests for add_merged in GithubPullRequest Add more sync tests and some docs
1 parent 1c2e5ae commit 76acde5

File tree

19 files changed

+1264
-111
lines changed

19 files changed

+1264
-111
lines changed

lib/code_corps/github/sync/comment/comment/changeset.ex

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ defmodule CodeCorps.GitHub.Sync.Comment.Comment.Changeset do
77
alias CodeCorps.{
88
Comment,
99
GithubComment,
10-
ProjectGithubRepo,
1110
Services.MarkdownRendererService,
1211
Task,
1312
User,
@@ -17,8 +16,7 @@ defmodule CodeCorps.GitHub.Sync.Comment.Comment.Changeset do
1716
alias Ecto.Changeset
1817

1918
@doc ~S"""
20-
Constructs a changeset for syncing a task when processing a GitHub Comment
21-
payload
19+
Constructs a changeset for syncing a task from a GitHub API Comment payload.
2220
"""
2321
@spec build_changeset(Comment.t, map, GithubComment.t, Task.t, User.t) :: Changeset.t
2422
def build_changeset(
@@ -34,6 +32,9 @@ defmodule CodeCorps.GitHub.Sync.Comment.Comment.Changeset do
3432
comment |> update_changeset(attrs)
3533
end
3634

35+
@doc ~S"""
36+
Constructs a changeset for syncing a task from a `GithubComment` record.
37+
"""
3738
@spec build_changeset(Comment.t, GithubComment.t, Task.t, User.t) :: Changeset.t
3839
def build_changeset(
3940
%Comment{id: comment_id} = comment,

lib/code_corps/github/sync/sync.ex

Lines changed: 46 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ defmodule CodeCorps.GitHub.Sync do
22

33
alias CodeCorps.{
44
Comment,
5-
GithubIssue,
65
GitHub,
76
GitHub.Utils.ResultAggregator,
87
GithubPullRequest,
@@ -108,44 +107,62 @@ defmodule CodeCorps.GitHub.Sync do
108107
defp sync_step({:ok, _} = result, _step), do: result
109108
defp sync_step({:error, _ = error}, _step), do: {:error, error}
110109

111-
@spec mark(GithubRepo.t, String.t) :: {:ok, GithubRepo.t} | {:error, Changeset.t}
112-
defp mark(%GithubRepo{} = repo, sync_state) when is_binary(sync_state) do
113-
IO.puts("------------#{sync_state}-----------------")
110+
@spec mark_repo(GithubRepo.t, String.t, Keyword.t) :: {:ok, GithubRepo.t} | {:error, Changeset.t}
111+
defp mark_repo(%GithubRepo{} = repo, sync_state, opts \\ []) do
112+
params = build_sync_params(sync_state, opts)
114113
repo
115-
|> GithubRepo.update_sync_changeset(%{sync_state: sync_state})
114+
|> GithubRepo.update_sync_changeset(params)
116115
|> Repo.update
117116
end
118-
@spec mark(ProjectGithubRepo.t, String.t) :: {:ok, GithubRepo.t} | {:error, Changeset.t}
119-
defp mark(%ProjectGithubRepo{} = project_github_repo, sync_state) when is_binary(sync_state) do
120-
IO.puts("------------#{sync_state}-----------------")
117+
118+
@spec mark_project_repo(ProjectGithubRepo.t, String.t, Keyword.t) :: {:ok, GithubRepo.t} | {:error, Changeset.t}
119+
defp mark_project_repo(%ProjectGithubRepo{} = project_github_repo, sync_state, opts \\ []) do
120+
params = build_sync_params(sync_state, opts)
121121
project_github_repo
122-
|> ProjectGithubRepo.update_sync_changeset(%{sync_state: sync_state})
122+
|> ProjectGithubRepo.update_sync_changeset(params)
123123
|> Repo.update
124124
end
125125

126+
127+
@count_fields [:syncing_comments_count, :syncing_issues_count, :syncing_pull_requests_count]
128+
129+
defp build_sync_params(sync_state, opts) do
130+
Enum.reduce @count_fields, %{sync_state: sync_state}, fn field, acc ->
131+
put_sync_opt(opts, field, acc)
132+
end
133+
end
134+
135+
defp put_sync_opt(opts, key, map) do
136+
case Keyword.get(opts, key) do
137+
nil -> map
138+
count -> map |> Map.put(key, count)
139+
end
140+
end
141+
126142
@spec sync_repo(GithubRepo.t) :: {:ok, GithubRepo.t}
127143
def sync_repo(%GithubRepo{} = repo) do
128-
with {:ok, pr_payloads} <- repo |> GitHub.API.Repository.pulls |> sync_step(:fetch_pull_requests),
129-
{:ok, repo} <- repo |> mark("syncing_pull_requests"),
144+
with {:ok, repo} <- repo |> mark_repo("fetching_pull_requests"),
145+
{:ok, pr_payloads} <- repo |> GitHub.API.Repository.pulls |> sync_step(:fetch_pull_requests),
146+
{:ok, repo} <- repo |> mark_repo("syncing_github_pull_requests", [syncing_pull_requests_count: pr_payloads |> Enum.count]),
130147
{:ok, _pull_requests} <- pr_payloads |> Enum.map(&Sync.PullRequest.GithubPullRequest.create_or_update_pull_request(repo, &1)) |> ResultAggregator.aggregate |> sync_step(:sync_pull_requests),
131-
{:ok, repo} <- repo |> mark("fetching_issues"),
148+
{:ok, repo} <- repo |> mark_repo("fetching_issues"),
132149
{:ok, issue_payloads} <- repo |> GitHub.API.Repository.issues |> sync_step(:fetch_issues),
133-
{:ok, repo} <- repo |> mark("syncing_issues"),
150+
{:ok, repo} <- repo |> mark_repo("syncing_github_issues", [syncing_issues_count: issue_payloads |> Enum.count]),
134151
{:ok, _issues} <- issue_payloads |> Enum.map(&Sync.Issue.GithubIssue.create_or_update_issue(repo, &1)) |> ResultAggregator.aggregate |> sync_step(:sync_issues),
135-
{:ok, repo} <- repo |> mark("fetching_comments"),
152+
{:ok, repo} <- repo |> mark_repo("fetching_comments"),
136153
{:ok, comment_payloads} <- repo |> GitHub.API.Repository.issue_comments |> sync_step(:fetch_comments),
137-
{:ok, repo} <- repo |> mark("syncing_comments"),
154+
{:ok, repo} <- repo |> mark_repo("syncing_github_comments", [syncing_comments_count: comment_payloads |> Enum.count]),
138155
{:ok, _comments} <- comment_payloads |> Enum.map(&Sync.Comment.GithubComment.create_or_update_comment(repo, &1)) |> ResultAggregator.aggregate |> sync_step(:sync_comments),
139-
{:ok, repo} <- repo |> mark("receiving_webhooks")
156+
{:ok, repo} <- repo |> mark_repo("receiving_webhooks")
140157
do
141158
{:ok, repo}
142159
else
143-
{:error, :fetch_pull_requests} -> repo |> mark("errored_fetching_pull_requests")
144-
{:error, :sync_pull_requests} -> repo |> mark("errored_syncing_pull_requests")
145-
{:error, :fetch_issues} -> repo |> mark("errored_fetching_issues")
146-
{:error, :sync_issues} -> repo |> mark("errored_syncing_issues")
147-
{:error, :fetch_comments} -> repo |> mark("errored_fetching_comments")
148-
{:error, :sync_comments} -> repo |> mark("errored_syncing_comments")
160+
{:error, :fetch_pull_requests} -> repo |> mark_repo("errored_fetching_pull_requests")
161+
{:error, :sync_pull_requests} -> repo |> mark_repo("errored_syncing_pull_requests")
162+
{:error, :fetch_issues} -> repo |> mark_repo("errored_fetching_issues")
163+
{:error, :sync_issues} -> repo |> mark_repo("errored_syncing_issues")
164+
{:error, :fetch_comments} -> repo |> mark_repo("errored_fetching_comments")
165+
{:error, :sync_comments} -> repo |> mark_repo("errored_syncing_comments")
149166
end
150167
end
151168

@@ -154,20 +171,20 @@ defmodule CodeCorps.GitHub.Sync do
154171
project_github_repo
155172
|> preload_project_github_repo
156173

157-
with {:ok, project_github_repo} <- project_github_repo |> mark("syncing_repo"),
174+
with {:ok, project_github_repo} <- project_github_repo |> mark_project_repo("syncing_github_repo"),
158175
{:ok, %GithubRepo{sync_state: "receiving_webhooks"}} <- repo |> sync_repo(),
159176
project_github_repo <- Repo.get(ProjectGithubRepo, project_github_repo.id) |> preload_project_github_repo(),
160-
{:ok, project_github_repo} <- project_github_repo |> mark("syncing_tasks"),
177+
{:ok, project_github_repo} <- project_github_repo |> mark_project_repo("syncing_tasks"),
161178
{:ok, _tasks} <- project_github_repo |> Sync.Issue.Task.sync_project_github_repo() |> sync_step(:sync_tasks),
162-
{:ok, project_github_repo} <- project_github_repo |> mark("syncing_comments"),
179+
{:ok, project_github_repo} <- project_github_repo |> mark_project_repo("syncing_comments"),
163180
{:ok, _comments} <- project_github_repo |> Sync.Comment.Comment.sync_project_github_repo() |> sync_step(:sync_comments),
164-
{:ok, project_github_repo} <- project_github_repo |> mark("synced")
181+
{:ok, project_github_repo} <- project_github_repo |> mark_project_repo("synced")
165182
do
166183
{:ok, project_github_repo}
167184
else
168-
{:ok, %GithubRepo{}} -> project_github_repo |> mark("errored_syncing_repo")
169-
{:error, :sync_tasks} -> repo |> mark("errored_syncing_tasks")
170-
{:error, :sync_comments} -> repo |> mark("errored_syncing_comments")
185+
{:ok, %GithubRepo{}} -> project_github_repo |> mark_project_repo("errored_syncing_github_repo")
186+
{:error, :sync_tasks} -> repo |> mark_project_repo("errored_syncing_tasks")
187+
{:error, :sync_comments} -> repo |> mark_project_repo("errored_syncing_comments")
171188
end
172189
end
173190

lib/code_corps/model/github_repo.ex

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,11 @@ defmodule CodeCorps.GithubRepo do
5151
~w{
5252
unsynced
5353
fetching_pull_requests errored_fetching_pull_requests
54-
syncing_pull_requests errored_syncing_pull_requests
54+
syncing_github_pull_requests errored_syncing_github_pull_requests
5555
fetching_issues errored_fetching_issues
56-
syncing_issues errored_syncing_issues
56+
syncing_github_issues errored_syncing_github_issues
5757
fetching_comments errored_fetching_comments
58-
syncing_comments errored_syncing_comments
58+
syncing_github_comments errored_syncing_github_comments
5959
receiving_webhooks
6060
}
6161
end

lib/code_corps/model/project_github_repo.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ defmodule CodeCorps.ProjectGithubRepo do
4141
def sync_states do
4242
~w{
4343
unsynced
44-
syncing_repo errored_syncing_repo
44+
syncing_github_repo errored_syncing_github_repo
4545
syncing_tasks errored_syncing_tasks
4646
syncing_comments errored_syncing_comments
4747
synced

lib/code_corps_web/controllers/project_github_repo_controller.ex

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ defmodule CodeCorpsWeb.ProjectGithubRepoController do
22
@moduledoc false
33
use CodeCorpsWeb, :controller
44

5+
alias Task.Supervisor, as: TaskSupervisor
56
alias CodeCorps.{Analytics.SegmentTracker, ProjectGithubRepo, User, Helpers.Query}
67

78
action_fallback CodeCorpsWeb.FallbackController
@@ -28,6 +29,8 @@ defmodule CodeCorpsWeb.ProjectGithubRepoController do
2829
{:ok, :authorized} <- current_user |> Policy.authorize(:create, %ProjectGithubRepo{}, params),
2930
{:ok, %ProjectGithubRepo{} = project_github_repo} <- create_project_repo_changeset(params) |> Repo.insert do
3031

32+
TaskSupervisor.start_child(:background_processor, fn -> CodeCorps.GitHub.Sync.sync_project_github_repo(project_github_repo) end)
33+
3134
current_user |> track_created(project_github_repo)
3235

3336
conn |> put_status(:created) |> render("show.json-api", data: project_github_repo)

lib/code_corps_web/views/github_repo_view.ex

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ defmodule CodeCorpsWeb.GithubRepoView do
55

66
attributes [:github_account_avatar_url, :github_account_id,
77
:github_account_login, :github_account_type, :github_id, :inserted_at,
8-
:name, :updated_at]
8+
:name, :syncing_comments_count, :syncing_issues_count,
9+
:syncing_pull_requests_count, :sync_state, :updated_at]
910

1011
has_one :github_app_installation, type: "github-app-installation", field: :github_app_installation_id
1112
end

lib/code_corps_web/views/project_github_repo_view.ex

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ defmodule CodeCorpsWeb.ProjectGithubRepoView do
33
use CodeCorpsWeb, :view
44
use JaSerializer.PhoenixView
55

6+
attributes [:sync_state]
7+
68
has_one :github_repo, type: "github-repo", field: :github_repo_id
79
has_one :project, type: "project", field: :project_id
810
end

0 commit comments

Comments
 (0)