Skip to content

Commit 1a005ea

Browse files
begedinjoshsmith
authored andcommitted
Eliminated need for github repo as argument in task syncer
1 parent 59e5644 commit 1a005ea

File tree

10 files changed

+51
-206
lines changed

10 files changed

+51
-206
lines changed

lib/code_corps/github/event/common/repo_finder.ex

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,10 @@ defmodule CodeCorps.GitHub.Event.Common.RepoFinder do
1414
- `{:error, :unmatched_project}` if record was found, but has no associated
1515
`ProjectGithubRepo` children
1616
"""
17-
@spec find_repo(map) :: {:ok, GithubRepo.t} | {:error, :unmatched_repository} | {:error, :unmatched_project}
17+
@spec find_repo(map) :: {:ok, GithubRepo.t} | {:error, :unmatched_repository}
1818
def find_repo(%{"repository" => %{"id" => github_id}}) do
19-
case GithubRepo |> Repo.get_by(github_id: github_id) |> Repo.preload(:project_github_repos) do
20-
# a GithubRepo with at least some ProjectGithubRepo children
21-
%GithubRepo{project_github_repos: [_ | _]} = github_repo -> {:ok, github_repo}
22-
# a GithubRepo with no ProjectGithubRepo children
23-
%GithubRepo{project_github_repos: []} -> {:error, :unmatched_project}
19+
case GithubRepo |> Repo.get_by(github_id: github_id) do
20+
%GithubRepo{} = github_repo -> {:ok, github_repo}
2421
nil -> {:error, :unmatched_repository}
2522
end
2623
end

lib/code_corps/github/event/issue_comment.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ defmodule CodeCorps.GitHub.Event.IssueComment do
6868
|> Multi.run(:issue, fn %{repo: %GithubRepo{} = github_repo} -> github_repo |> Issues.IssueLinker.create_or_update_issue(issue_payload) end)
6969
|> Multi.run(:issue_user, fn %{issue: github_issue} -> github_issue |> Issues.UserLinker.find_or_create_user(payload) end)
7070
|> Multi.run(:comment_user, fn _ -> IssueComment.UserLinker.find_or_create_user(payload) end)
71-
|> Multi.run(:tasks, fn %{issue: github_issue, repo: github_repo, issue_user: user} -> github_issue |> TaskSyncer.sync_all(github_repo, user, payload) end)
71+
|> Multi.run(:tasks, fn %{issue: github_issue, issue_user: user} -> github_issue |> TaskSyncer.sync_all(user, payload) end)
7272
|> Multi.run(:comments, fn %{tasks: tasks, comment_user: user} -> CommentSyncer.sync_all(tasks, user, payload) end)
7373
end
7474
defp operational_multi(%{"action" => "deleted"} = payload) do

lib/code_corps/github/event/issues.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ defmodule CodeCorps.GitHub.Event.Issues do
5353
|> Multi.run(:repo, fn _ -> RepoFinder.find_repo(payload) end)
5454
|> Multi.run(:issue, fn %{repo: github_repo} -> link_issue(github_repo, payload) end)
5555
|> Multi.run(:user, fn %{issue: github_issue} -> UserLinker.find_or_create_user(github_issue, payload) end)
56-
|> Multi.run(:tasks, fn %{issue: github_issue, repo: github_repo, user: user} -> TaskSyncer.sync_all(github_issue, github_repo, user, payload) end)
56+
|> Multi.run(:tasks, fn %{issue: github_issue, user: user} -> github_issue |> TaskSyncer.sync_all(user, payload) end)
5757
|> Repo.transaction
5858
|> marshall_result()
5959
end

lib/code_corps/github/event/issues/task_syncer.ex

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,18 @@ defmodule CodeCorps.GitHub.Event.Issues.TaskSyncer do
1616
{:error, {list(Task.t), list(Changeset.t)}}
1717

1818
@doc """
19-
When provided a `GithubRepo`, a `User` and a GitHub API payload, for each
20-
`Project` associated to that `GithubRepo` via a `ProjectGithubRepo`, it
21-
creates or updates a `Task` associated to the specified `User`.
19+
When provided a `CodeCorps.GithubIssue`, a `CodeCorps.User` and a GitHub API
20+
payload, for each `CodeCorps.Project` associated to that
21+
`CodeCorps.GithubRepo` via a `CodeCorps.ProjectGithubRepo`, it
22+
creates or updates a `CodeCorps.Task`.
2223
"""
23-
@spec sync_all(GithubIssue.t, GithubRepo.t, User.t, map) :: {:ok, list(Task.t)}
24-
def sync_all(%GithubIssue{} = github_issue, %GithubRepo{project_github_repos: project_github_repos}, %User{} = user, %{} = payload) do
24+
@spec sync_all(GithubIssue.t, User.t, map) :: {:ok, list(Task.t)}
25+
def sync_all(%GithubIssue{} = github_issue, %User{} = user, %{} = payload) do
26+
27+
%GithubIssue{
28+
github_repo: %GithubRepo{project_github_repos: project_github_repos}
29+
} = github_issue |> Repo.preload(github_repo: :project_github_repos)
30+
2531
project_github_repos
2632
|> Enum.map(&sync(github_issue, &1, user, payload))
2733
|> ResultAggregator.aggregate

test/lib/code_corps/github/event/common/repo_finder_test.exs

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,23 @@ defmodule CodeCorps.GitHub.Event.Common.RepoFinderTest do
77

88
@loadable_event_fixtures ~w(issue_comment_created issue_comment_edited issue_comment_deleted issues_closed issues_opened issues_edited issues_reopened)
99

10-
describe "find_repo/1" do
11-
@loadable_event_fixtures |> Enum.each(fn fixture ->
12-
@fixture fixture
10+
@loadable_event_fixtures |> Enum.each(fn fixture ->
11+
@fixture fixture
1312

14-
setup do
15-
{:ok, %{payload: load_event_fixture(@fixture)}}
16-
end
13+
setup do
14+
{:ok, %{payload: load_event_fixture(@fixture)}}
15+
end
1716

18-
test "loads properly for event fixture #{@fixture}", %{payload: payload} do
19-
# if no repo locally, returns error
17+
describe "finder_repo for #{@fixture}" do
18+
test "returns error if no matched repository", %{payload: payload} do
2019
assert RepoFinder.find_repo(payload) == {:error, :unmatched_repository}
20+
end
2121

22-
# if repo is found but is not connected to any projects, returns error
22+
test "returns repository if matched, preloads project github repos", %{payload: payload} do
2323
github_repo = insert(:github_repo, github_id: payload["repository"]["id"])
24-
assert RepoFinder.find_repo(payload) == {:error, :unmatched_project}
25-
26-
# returns repo if all is in order
27-
%{id: project_github_repo_id} = insert(:project_github_repo, github_repo: github_repo)
28-
{:ok, %{id: found_repo_id, project_github_repos: project_github_repos}} = RepoFinder.find_repo(payload)
24+
{:ok, %{id: found_repo_id}} = RepoFinder.find_repo(payload)
2925
assert found_repo_id == github_repo.id
30-
assert Enum.map(project_github_repos, &Map.get(&1, :id)) == [project_github_repo_id]
3126
end
32-
end)
33-
end
27+
end
28+
end)
3429
end

test/lib/code_corps/github/event/issue_comment_test.exs

Lines changed: 0 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -27,21 +27,6 @@ defmodule CodeCorps.GitHub.Event.IssueCommentTest do
2727
describe "handle/1 for IssueComment::#{action}" do
2828
@payload load_event_fixture("issue_comment_#{action}")
2929

30-
test "with unmatched both users, passes with no changes made if no matching projects" do
31-
%{
32-
"issue" => %{"user" => %{"id" => issue_user_github_id}},
33-
"comment" => %{"user" => %{"id" => comment_user_github_id}},
34-
"repository" => %{"id" => repo_github_id}
35-
} = @payload
36-
37-
insert(:github_repo, github_id: repo_github_id)
38-
assert IssueComment.handle(@payload) == {:ok, []}
39-
assert Repo.aggregate(Task, :count, :id) == 0
40-
assert Repo.aggregate(Comment, :count, :id) == 0
41-
refute Repo.get_by(User, github_id: issue_user_github_id)
42-
refute Repo.get_by(User, github_id: comment_user_github_id)
43-
end
44-
4530
test "with unmatched both users, creates users, creates missing tasks, missing comments, for all projects connected with the github repo" do
4631
%{
4732
"issue" => %{
@@ -102,21 +87,6 @@ defmodule CodeCorps.GitHub.Event.IssueCommentTest do
10287
refute Repo.one(User)
10388
end
10489

105-
test "with matched issue user, unmatched comment_user, passes with no changes made if no matching projects" do
106-
%{
107-
"issue" => %{"user" => %{"id" => issue_user_github_id}},
108-
"comment" => %{"user" => %{"id" => comment_user_github_id}},
109-
"repository" => %{"id" => repo_github_id}
110-
} = @payload
111-
112-
insert(:user, github_id: issue_user_github_id)
113-
insert(:github_repo, github_id: repo_github_id)
114-
assert IssueComment.handle(@payload) == {:ok, []}
115-
assert Repo.aggregate(Task, :count, :id) == 0
116-
assert Repo.aggregate(Comment, :count, :id) == 0
117-
refute Repo.get_by(User, github_id: comment_user_github_id)
118-
end
119-
12090
test "with matched issue user, unmatched comment user, creates and updates tasks, comments and comment user, for each related project" do
12191
%{
12292
"issue" => %{
@@ -191,21 +161,6 @@ defmodule CodeCorps.GitHub.Event.IssueCommentTest do
191161
assert IssueComment.handle(@payload) == {:error, :repository_not_found}
192162
end
193163

194-
test "with unmatched issue user, matched comment_user, passes with no changes made if no matching projects" do
195-
%{
196-
"issue" => %{"user" => %{"id" => issue_user_github_id}},
197-
"comment" => %{"user" => %{"id" => comment_user_github_id}},
198-
"repository" => %{"id" => repo_github_id}
199-
} = @payload
200-
201-
insert(:user, github_id: comment_user_github_id)
202-
insert(:github_repo, github_id: repo_github_id)
203-
assert IssueComment.handle(@payload) == {:ok, []}
204-
assert Repo.aggregate(Task, :count, :id) == 0
205-
assert Repo.aggregate(Comment, :count, :id) == 0
206-
refute Repo.get_by(User, github_id: issue_user_github_id)
207-
end
208-
209164
test "with unmatched issue user, matched comment user, creates and updates tasks, comments and issue user, for each related project" do
210165
%{
211166
"issue" => %{
@@ -269,21 +224,6 @@ defmodule CodeCorps.GitHub.Event.IssueCommentTest do
269224
assert IssueComment.handle(@payload) == {:error, :repository_not_found}
270225
end
271226

272-
test "with matched issue and comment_user, passes with no changes made if no matching projects" do
273-
%{
274-
"issue" => %{"user" => %{"id" => issue_user_github_id}},
275-
"comment" => %{"user" => %{"id" => comment_user_github_id}},
276-
"repository" => %{"id" => repo_github_id}
277-
} = @payload
278-
279-
insert(:user, github_id: comment_user_github_id)
280-
insert(:user, github_id: issue_user_github_id)
281-
insert(:github_repo, github_id: repo_github_id)
282-
assert IssueComment.handle(@payload) == {:ok, []}
283-
assert Repo.aggregate(Task, :count, :id) == 0
284-
assert Repo.aggregate(Comment, :count, :id) == 0
285-
end
286-
287227
test "with matched issue and comment user, creates and updates tasks, comments, for each related project" do
288228
%{
289229
"issue" => %{

test/lib/code_corps/github/event/issues/task_syncer_test.exs

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ defmodule CodeCorps.GitHub.Event.Issues.TaskSyncerTest do
1717

1818
test "creates missing, updates existing tasks for each project associated with the github repo" do
1919
user = insert(:user)
20-
github_issue = insert(:github_issue)
21-
github_repo = insert(:github_repo)
20+
%{github_repo: github_repo} = github_issue = insert(:github_issue)
2221

2322
%{"issue" => %{"body" => issue_body}} = @payload
2423

25-
[%{project: project_1}, _, _] = project_github_repos = insert_list(3, :project_github_repo, github_repo: github_repo)
24+
[%{project: project_1}, _, _] = project_github_repos =
25+
insert_list(3, :project_github_repo, github_repo: github_repo)
2626

2727
task_1 = insert(:task, project: project_1, github_issue: github_issue, github_repo: github_repo, user: user)
2828

@@ -33,11 +33,7 @@ defmodule CodeCorps.GitHub.Event.Issues.TaskSyncerTest do
3333
insert(:task_list, project: project, inbox: true)
3434
end)
3535

36-
github_repo = Repo.preload(github_repo, :project_github_repos)
37-
38-
{:ok, tasks} =
39-
github_issue
40-
|> TaskSyncer.sync_all(github_repo, user, @payload)
36+
{:ok, tasks} = github_issue |> TaskSyncer.sync_all(user, @payload)
4137

4238
assert Repo.aggregate(Task, :count, :id) == 3
4339

@@ -52,20 +48,19 @@ defmodule CodeCorps.GitHub.Event.Issues.TaskSyncerTest do
5248
end
5349

5450
test "fails on validation errors" do
55-
github_issue = insert(:github_issue)
51+
%{github_repo: github_repo} = github_issue = insert(:github_issue)
5652

5753
bad_payload = @payload |> put_in(~w(issue title), nil)
5854

59-
%{project: project, github_repo: github_repo} = insert(:project_github_repo)
55+
%{project: project} =
56+
insert(:project_github_repo, github_repo: github_repo)
57+
6058
%{user: user} = insert(:task, project: project, github_issue: github_issue, github_repo: github_repo)
6159

6260
insert(:task_list, project: project, inbox: true)
6361

64-
github_repo = Repo.preload(github_repo, :project_github_repos)
65-
6662
{:error, {tasks, errors}} =
67-
github_issue
68-
|> TaskSyncer.sync_all(github_repo, user, bad_payload)
63+
github_issue |> TaskSyncer.sync_all(user, bad_payload)
6964

7065
assert tasks |> Enum.count == 0
7166
assert errors |> Enum.count == 1

test/lib/code_corps/github/event/issues/user_linker_test.exs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@ defmodule CodeCorps.GitHub.Event.Issues.UserLinkerTest do
2626
} = @payload
2727

2828
user = insert(:user)
29-
github_issue = insert(:github_issue, number: number)
30-
repo = insert(:github_repo, github_id: github_repo_id)
29+
github_repo = insert(:github_repo, github_id: github_repo_id)
30+
github_issue = insert(:github_issue, number: number, github_repo: github_repo)
3131
# multiple tasks, all with same user is ok
3232
insert_pair(
33-
:task, user: user, github_repo: repo, github_issue: github_issue)
33+
:task, user: user, github_repo: github_repo, github_issue: github_issue)
3434

3535
{:ok, %User{} = returned_user} = UserLinker.find_or_create_user(github_issue, @payload)
3636

@@ -43,10 +43,10 @@ defmodule CodeCorps.GitHub.Event.Issues.UserLinkerTest do
4343
"repository" => %{"id" => github_repo_id}
4444
} = @payload
4545

46-
github_issue = insert(:github_issue, number: number)
47-
repo = insert(:github_repo, github_id: github_repo_id)
46+
github_repo = insert(:github_repo, github_id: github_repo_id)
47+
github_issue = insert(:github_issue, number: number, github_repo: github_repo)
4848
# multiple tasks, each with different user is not ok
49-
insert_pair(:task, github_repo: repo, github_issue: github_issue)
49+
insert_pair(:task, github_repo: github_repo, github_issue: github_issue)
5050

5151
assert {:error, :multiple_users} ==
5252
UserLinker.find_or_create_user(github_issue, @payload)
@@ -61,17 +61,16 @@ defmodule CodeCorps.GitHub.Event.Issues.UserLinkerTest do
6161
{:ok, %User{} = returned_user} = UserLinker.find_or_create_user(github_issue, @payload)
6262

6363
assert preinserted_user.id == returned_user.id
64+
assert Repo.get_by(User, attributes)
65+
6466

65-
assert Repo.one(User)
6667
end
6768

6869
test "creates user if none is found by any other method" do
6970
%{"issue" => %{"number" => number}} = @payload
7071
github_issue = insert(:github_issue, number: number)
7172
{:ok, %User{} = returned_user} = UserLinker.find_or_create_user(github_issue, @payload)
7273

73-
assert Repo.one(User)
74-
7574
created_attributes = UserAdapter.from_github_user(@user_payload)
7675
created_user = Repo.get_by(User, created_attributes)
7776
assert created_user.id == returned_user.id
@@ -105,8 +104,6 @@ defmodule CodeCorps.GitHub.Event.Issues.UserLinkerTest do
105104
github_issue = insert(:github_issue, number: number)
106105
{:ok, %User{} = returned_user} = UserLinker.find_or_create_user(github_issue, @bot_payload)
107106

108-
assert Repo.one(User)
109-
110107
created_attributes = UserAdapter.from_github_user(@bot_user_payload)
111108
created_user = Repo.get_by(User, created_attributes)
112109
assert created_user.id == returned_user.id

0 commit comments

Comments
 (0)