Skip to content

Commit cc9c36c

Browse files
authored
Merge pull request #1093 from code-corps/reorganize-github-syncing
Move syncers and changesets out of event
2 parents 52539d0 + ec57cc7 commit cc9c36c

File tree

36 files changed

+115
-127
lines changed

36 files changed

+115
-127
lines changed

lib/code_corps/comment/service.ex

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ defmodule CodeCorps.Comment.Service do
77
alias CodeCorps.{
88
Comment,
99
GitHub,
10-
GitHub.Event.IssueComment.CommentLinker,
1110
GithubComment,
1211
GithubIssue,
1312
Task,
1413
Repo
1514
}
15+
alias CodeCorps.GitHub.Sync.Comment.GithubComment, as: GithubCommentSyncer
1616
alias Ecto.{Changeset, Multi}
1717

1818
require Logger
@@ -75,7 +75,7 @@ defmodule CodeCorps.Comment.Service do
7575
defp create_on_github(%Comment{task: %Task{github_issue_id: nil}} = comment), do: {:ok, comment}
7676
defp create_on_github(%Comment{task: %Task{github_issue: github_issue}} = comment) do
7777
with {:ok, payload} <- comment |> GitHub.API.Comment.create,
78-
{:ok, %GithubComment{} = github_comment} <- CommentLinker.create_or_update_comment(github_issue, payload)do
78+
{:ok, %GithubComment{} = github_comment} <- GithubCommentSyncer.create_or_update_comment(github_issue, payload)do
7979
comment |> link_with_github_changeset(github_comment) |> Repo.update
8080
else
8181
{:error, github_error} -> {:error, github_error}
@@ -92,7 +92,7 @@ defmodule CodeCorps.Comment.Service do
9292
defp update_on_github(%Comment{} = comment) do
9393
with %Comment{task: %Task{github_issue: %GithubIssue{} = github_issue}} = comment <- comment |> Repo.preload(@preloads),
9494
{:ok, payload} <- comment |> GitHub.API.Comment.update,
95-
{:ok, %GithubComment{}} <- CommentLinker.create_or_update_comment(github_issue, payload) do
95+
{:ok, %GithubComment{}} <- GithubCommentSyncer.create_or_update_comment(github_issue, payload) do
9696

9797
{:ok, comment}
9898
else
File renamed without changes.

lib/code_corps/github/event/installation/repos.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ defmodule CodeCorps.GitHub.Event.Installation.Repos do
88
GithubRepo,
99
GitHub.API.Installation,
1010
GitHub,
11-
GitHub.Event.Common.ResultAggregator,
11+
GitHub.Utils.ResultAggregator,
1212
Repo
1313
}
1414

lib/code_corps/github/event/installation_repositories.ex renamed to lib/code_corps/github/event/installation_repositories/installation_repositories.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ defmodule CodeCorps.GitHub.Event.InstallationRepositories do
1111
alias CodeCorps.{
1212
GithubAppInstallation,
1313
GithubRepo,
14-
GitHub.Event.Common.ResultAggregator,
14+
GitHub.Utils.ResultAggregator,
1515
GitHub.Event.InstallationRepositories,
1616
Repo
1717
}

lib/code_corps/github/event/issue_comment.ex renamed to lib/code_corps/github/event/issue_comment/issue_comment.ex

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,19 @@ defmodule CodeCorps.GitHub.Event.IssueComment do
88

99
alias CodeCorps.{
1010
Comment,
11+
GitHub,
1112
GithubComment,
1213
GithubRepo,
1314
GitHub.Event.Common.RepoFinder,
14-
GitHub.Event.Issues,
15-
GitHub.Event.Issues.IssueLinker,
16-
GitHub.Event.Issues.TaskSyncer,
17-
GitHub.Event.IssueComment,
18-
GitHub.Event.IssueComment.CommentLinker,
19-
GitHub.Event.IssueComment.CommentSyncer,
2015
GitHub.Event.IssueComment.CommentDeleter,
16+
GitHub.Event.IssueComment.Validator,
2117
Repo
2218
}
23-
alias CodeCorps.GitHub.Syncers.User.RecordLinker, as: UserRecordLinker
19+
alias GitHub.Sync.Comment.Comment, as: CommentCommentSyncer
20+
alias GitHub.Sync.Comment.GithubComment, as: CommentGithubCommentSyncer
21+
alias GitHub.Sync.Issue.GithubIssue, as: IssueGithubIssueSyncer
22+
alias GitHub.Sync.Issue.Task, as: IssueTaskSyncer
23+
alias GitHub.Sync.User.RecordLinker, as: UserRecordLinker
2424
alias Ecto.Multi
2525

2626
@type outcome :: {:ok, list(Comment.t)} |
@@ -71,11 +71,11 @@ defmodule CodeCorps.GitHub.Event.IssueComment do
7171
Multi.new
7272
|> Multi.run(:repo, fn _ -> RepoFinder.find_repo(payload) end)
7373
|> Multi.run(:github_issue, fn %{repo: github_repo} -> github_repo |> link_issue(payload) end)
74-
|> Multi.run(:github_comment, fn %{github_issue: github_issue} -> github_issue |> link_comment(payload) end)
74+
|> Multi.run(:github_comment, fn %{github_issue: github_issue} -> github_issue |> sync_comment(payload) end)
7575
|> Multi.run(:issue_user, fn %{github_issue: github_issue} -> UserRecordLinker.link_to(github_issue, payload) end)
7676
|> Multi.run(:comment_user, fn %{github_comment: github_comment} -> UserRecordLinker.link_to(github_comment, payload) end)
77-
|> Multi.run(:tasks, fn %{github_issue: github_issue, issue_user: user} -> github_issue |> TaskSyncer.sync_all(user, payload) end)
78-
|> Multi.run(:comments, fn %{github_comment: github_comment, tasks: tasks, comment_user: user} -> CommentSyncer.sync_all(tasks, github_comment, user, payload) end)
77+
|> Multi.run(:tasks, fn %{github_issue: github_issue, issue_user: user} -> github_issue |> IssueTaskSyncer.sync_all(user, payload) end)
78+
|> Multi.run(:comments, fn %{github_comment: github_comment, tasks: tasks, comment_user: user} -> CommentCommentSyncer.sync_all(tasks, github_comment, user, payload) end)
7979
end
8080
defp operational_multi(%{"action" => "deleted"} = payload) do
8181
Multi.new
@@ -85,12 +85,12 @@ defmodule CodeCorps.GitHub.Event.IssueComment do
8585

8686
@spec link_issue(GithubRepo.t, map) :: {:ok, GithubIssue.t} | {:error, Ecto.Changeset.t}
8787
defp link_issue(github_repo, %{"issue" => attrs}) do
88-
IssueLinker.create_or_update_issue(github_repo, attrs)
88+
IssueGithubIssueSyncer.create_or_update_issue(github_repo, attrs)
8989
end
9090

91-
@spec link_comment(GithubIssue.t, map) :: {:ok, GithubComment.t} | {:error, Ecto.Changeset.t}
92-
defp link_comment(github_issue, %{"comment" => attrs}) do
93-
CommentLinker.create_or_update_comment(github_issue, attrs)
91+
@spec sync_comment(GithubIssue.t, map) :: {:ok, GithubComment.t} | {:error, Ecto.Changeset.t}
92+
defp sync_comment(github_issue, %{"comment" => attrs}) do
93+
CommentGithubCommentSyncer.create_or_update_comment(github_issue, attrs)
9494
end
9595

9696
@spec marshall_result(tuple) :: tuple
@@ -111,7 +111,7 @@ defmodule CodeCorps.GitHub.Event.IssueComment do
111111

112112
@spec validate_payload(map) :: {:ok, :valid} | {:error, :invalid}
113113
defp validate_payload(%{} = payload) do
114-
case payload |> IssueComment.Validator.valid? do
114+
case payload |> Validator.valid? do
115115
true -> {:ok, :valid}
116116
false -> {:error, :invalid}
117117
end

lib/code_corps/github/event/issues.ex renamed to lib/code_corps/github/event/issues/issues.ex

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,16 @@ defmodule CodeCorps.GitHub.Event.Issues do
88
@behaviour CodeCorps.GitHub.Event.Handler
99

1010
alias CodeCorps.{
11+
GitHub,
1112
GitHub.Event.Common.RepoFinder,
12-
GitHub.Event.Issues.IssueLinker,
13-
GitHub.Event.Issues.TaskSyncer,
1413
GitHub.Event.Issues.Validator,
14+
GitHub.Sync.Issue.Task,
1515
Repo,
1616
Task
1717
}
18-
alias CodeCorps.GitHub.Syncers.User.RecordLinker, as: UserRecordLinker
18+
alias GitHub.Sync.Issue.GithubIssue, as: IssueGithubIssueSyncer
19+
alias GitHub.Sync.Issue.Task, as: IssueTaskSyncer
20+
alias GitHub.Sync.User.RecordLinker, as: UserRecordLinker
1921
alias Ecto.Multi
2022

2123
@type outcome :: {:ok, list(Task.t)} |
@@ -53,14 +55,14 @@ defmodule CodeCorps.GitHub.Event.Issues do
5355
|> Multi.run(:repo, fn _ -> RepoFinder.find_repo(payload) end)
5456
|> Multi.run(:issue, fn %{repo: github_repo} -> link_issue(github_repo, payload) end)
5557
|> Multi.run(:user, fn %{issue: github_issue} -> UserRecordLinker.link_to(github_issue, payload) end)
56-
|> Multi.run(:tasks, fn %{issue: github_issue, user: user} -> github_issue |> TaskSyncer.sync_all(user, payload) end)
58+
|> Multi.run(:tasks, fn %{issue: github_issue, user: user} -> github_issue |> IssueTaskSyncer.sync_all(user, payload) end)
5759
|> Repo.transaction
5860
|> marshall_result()
5961
end
6062

6163
@spec link_issue(GithubRepo.t, map) :: {:ok, GithubIssue.t} | {:error, Ecto.Changeset.t}
6264
defp link_issue(github_repo, %{"issue" => attrs}) do
63-
IssueLinker.create_or_update_issue(github_repo, attrs)
65+
IssueGithubIssueSyncer.create_or_update_issue(github_repo, attrs)
6466
end
6567

6668
@spec marshall_result(tuple) :: tuple

lib/code_corps/github/event/pull_request.ex renamed to lib/code_corps/github/event/pull_request/pull_request.ex

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ defmodule CodeCorps.GitHub.Event.PullRequest do
99

1010
alias CodeCorps.{
1111
GitHub.Event.Common.RepoFinder,
12-
GitHub.Event.PullRequest.PullRequestLinker,
13-
GitHub.Event.PullRequest.TaskSyncer,
1412
GitHub.Event.PullRequest.Validator,
1513
Repo,
1614
Task
1715
}
18-
alias CodeCorps.GitHub.Syncers.User.RecordLinker, as: UserRecordLinker
16+
alias CodeCorps.GitHub.Sync.PullRequest.GithubPullRequest, as: GithubPullRequestSyncer
17+
alias CodeCorps.GitHub.Sync.PullRequest.Task, as: PullRequestTaskSyncer
18+
alias CodeCorps.GitHub.Sync.User.RecordLinker, as: UserRecordLinker
1919
alias Ecto.Multi
2020

2121
@type outcome :: {:ok, list(Task.t)} |
@@ -56,14 +56,14 @@ defmodule CodeCorps.GitHub.Event.PullRequest do
5656
|> Multi.run(:repo, fn _ -> RepoFinder.find_repo(payload) end)
5757
|> Multi.run(:pull_request, fn %{repo: github_repo} -> link_pull_request(github_repo, payload) end)
5858
|> Multi.run(:user, fn %{pull_request: github_pull_request} -> UserRecordLinker.link_to(github_pull_request, payload) end)
59-
|> Multi.run(:tasks, fn %{pull_request: github_pull_request, user: user} -> github_pull_request |> TaskSyncer.sync_all(user, payload) end)
59+
|> Multi.run(:tasks, fn %{pull_request: github_pull_request, user: user} -> github_pull_request |> PullRequestTaskSyncer.sync_all(user, payload) end)
6060
|> Repo.transaction
6161
|> marshall_result()
6262
end
6363

6464
@spec link_pull_request(GithubRepo.t, map) :: {:ok, GithubIssue.t} | {:error, Ecto.Changeset.t}
6565
defp link_pull_request(github_repo, %{"pull_request" => attrs}) do
66-
PullRequestLinker.create_or_update_pull_request(github_repo, attrs)
66+
GithubPullRequestSyncer.create_or_update_pull_request(github_repo, attrs)
6767
end
6868

6969
@spec marshall_result(tuple) :: tuple

lib/code_corps/github/event/issue_comment/changeset_builder.ex renamed to lib/code_corps/github/sync/comment/comment/changeset.ex

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
defmodule CodeCorps.GitHub.Event.IssueComment.ChangesetBuilder do
1+
defmodule CodeCorps.GitHub.Sync.Comment.Comment.Changeset do
22
@moduledoc ~S"""
33
In charge of building a `Changeset` to update a `Comment` with, when handling
4-
an IssueComment webhook.
4+
a GitHub Comment payload.
55
"""
66

77
alias CodeCorps.{
@@ -16,8 +16,8 @@ defmodule CodeCorps.GitHub.Event.IssueComment.ChangesetBuilder do
1616
alias Ecto.Changeset
1717

1818
@doc ~S"""
19-
Constructs a changeset for syncing a task when processing an IssueComment
20-
webhook
19+
Constructs a changeset for syncing a task when processing a GitHub Comment
20+
payload
2121
"""
2222
@spec build_changeset(Comment.t, map, GithubComment.t, Task.t, User.t) :: Changeset.t
2323
def build_changeset(

lib/code_corps/github/event/issue_comment/comment_syncer.ex renamed to lib/code_corps/github/sync/comment/comment/comment.ex

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
defmodule CodeCorps.GitHub.Event.IssueComment.CommentSyncer do
1+
defmodule CodeCorps.GitHub.Sync.Comment.Comment do
22
@moduledoc ~S"""
33
In charge of syncing `CodeCorps.Comment` records with a GitHub comment
44
payload.
@@ -12,21 +12,20 @@ defmodule CodeCorps.GitHub.Event.IssueComment.CommentSyncer do
1212

1313
alias CodeCorps.{
1414
Comment,
15-
GitHub.Event.Common.ResultAggregator,
16-
GitHub.Event.IssueComment.ChangesetBuilder,
15+
GitHub.Utils.ResultAggregator,
1716
GithubComment,
1817
Task,
1918
User,
2019
Repo
2120
}
22-
21+
alias CodeCorps.GitHub.Sync.Comment.Comment.Changeset, as: CommentChangeset
2322
alias Ecto.Changeset
2423

2524
@type outcome :: {:ok, list(Comment.t)} |
2625
{:error, {list(Comment.t), list(Changeset.t)}}
2726

2827
@doc ~S"""
29-
Creates or updates `CodeCorps.Comment` records for the speciifed list of
28+
Creates or updates `CodeCorps.Comment` records for the specified list of
3029
`CodeCorps.Task` records.
3130
3231
When provided a list of `CodeCorps.Task` records, a `CodeCorps.GithubComment`,
@@ -46,7 +45,7 @@ defmodule CodeCorps.GitHub.Event.IssueComment.CommentSyncer do
4645
defp sync(%Task{} = task, %GithubComment{} = github_comment, %User{} = user, %{} = payload) do
4746
task
4847
|> find_or_init_comment(payload)
49-
|> ChangesetBuilder.build_changeset(payload, github_comment, task, user)
48+
|> CommentChangeset.build_changeset(payload, github_comment, task, user)
5049
|> commit()
5150
end
5251

lib/code_corps/github/event/issue_comment/comment_linker.ex renamed to lib/code_corps/github/sync/comment/github_comment/github_comment.ex

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
defmodule CodeCorps.GitHub.Event.IssueComment.CommentLinker do
1+
defmodule CodeCorps.GitHub.Sync.Comment.GithubComment do
22
@moduledoc ~S"""
33
In charge of finding a `CodeCorps.GithubComment` to link with a
4-
`CodeCorps.Comment` when processing an Issue Comment webhook, or handling a
5-
`CodeCorpsWeb.CommentController` request.
4+
`CodeCorps.Comment` when processing a GitHub Comment payload.
65
7-
The only entry point is `create_or_update_comment/1`.
6+
The only entry point is `create_or_update_comment/2`.
87
"""
98

109
alias CodeCorps.{

0 commit comments

Comments
 (0)