|
1 | 1 | defmodule CodeCorps.GitHub.Sync.Comment do |
2 | | - alias CodeCorps.{ |
3 | | - GitHub, |
4 | | - GithubComment, |
5 | | - GithubIssue |
6 | | - } |
7 | | - alias GitHub.Sync.Comment.Comment, as: CommentCommentSyncer |
8 | | - alias GitHub.Sync.Comment.GithubComment, as: CommentGithubCommentSyncer |
9 | | - alias GitHub.Sync.User.RecordLinker, as: UserRecordLinker |
| 2 | + alias CodeCorps.GitHub.Sync |
10 | 3 | alias Ecto.Multi |
11 | 4 |
|
12 | 5 | @doc ~S""" |
13 | | - Syncs a GitHub comment API payload with our data. |
| 6 | + Creates an `Ecto.Multi` intended to process a GitHub issue comment related API |
| 7 | + payload. |
14 | 8 |
|
15 | | - Expects a `CodeCorps.GithubIssue` record and a list of `CodeCorps.Task` |
16 | | - records passed in with the changes. |
| 9 | + Expects a partial transaction outcome with `:github_issue` and :task keys. |
17 | 10 |
|
18 | | - The process is as follows: |
| 11 | + Returns an `Ecto.Multi` with the follwing steps |
19 | 12 |
|
20 | | - - create a `CodeCorps.GithubComment` related to the `CodeCorps.GithubIssue` |
21 | | - - match the comment payload with a `CodeCorps.User` using |
22 | | - `CodeCorps.GitHub.Sync.User.RecordLinker` |
23 | | - - for each `CodeCorps.Task`: |
24 | | - - create or update `CodeCorps.Comment` for the `CodeCorps.Task` |
| 13 | + - create or update a `CodeCorps.GithubComment` from the |
| 14 | + provided `CodeCorps.GithubIssue` and API payload |
| 15 | + - match the `CodeCorps.GithubComment` with a new or existing `CodeCorps.User` |
| 16 | + - create or update a `CodeCorps.Comment` using the created |
| 17 | + `CodeCorps.GithubComment`, related to the matched `CodeCorps.User` and the |
| 18 | + provided `CodeCorps.Task` |
25 | 19 | """ |
26 | 20 | @spec sync(map, map) :: Multi.t |
27 | | - def sync(%{github_issue: github_issue, tasks: tasks}, payload) do |
| 21 | + def sync(%{github_issue: github_issue, task: task}, %{} = payload) do |
28 | 22 | Multi.new |
29 | | - |> Multi.run(:github_comment, fn _ -> sync_github_comment(github_issue, payload) end) |
30 | | - |> Multi.run(:comment_user, fn %{github_comment: github_comment} -> UserRecordLinker.link_to(github_comment, payload) end) |
31 | | - |> Multi.run(:comments, fn %{github_comment: github_comment, comment_user: user} -> CommentCommentSyncer.sync_all(tasks, github_comment, user, payload) end) |
| 23 | + |> Multi.run(:github_comment, fn _ -> Sync.Comment.GithubComment.create_or_update_comment(github_issue, payload) end) |
| 24 | + |> Multi.run(:comment_user, fn %{github_comment: github_comment} -> Sync.User.RecordLinker.link_to(github_comment, payload) end) |
| 25 | + |> Multi.run(:comment, fn %{github_comment: github_comment, comment_user: user} -> Sync.Comment.Comment.sync(task, github_comment, user) end) |
32 | 26 | end |
33 | 27 |
|
34 | | - @doc """ |
35 | | - When provided a GitHub API payload, it deletes each `Comment` associated to |
36 | | - the specified `IssueComment` and then deletes the `GithubComment`. |
| 28 | + @doc ~S""" |
| 29 | + Creates an `Ecto.Multi` intended to delete a `CodeCorps.GithubComment` |
| 30 | + specified by `github_id`, as well as 0 to 1 `CodeCorps.Comment` records |
| 31 | + associated to `CodeCorps.GithubComment` |
37 | 32 | """ |
38 | | - @spec delete(map, map) :: Multi.t |
39 | | - def delete(_, %{"id" => github_id}) do |
| 33 | + @spec delete(map) :: Multi.t |
| 34 | + def delete(%{"id" => github_id}) do |
40 | 35 | Multi.new |
41 | | - |> Multi.run(:deleted_comments, fn _ -> CommentCommentSyncer.delete_all(github_id) end) |
42 | | - |> Multi.run(:deleted_github_comment, fn _ -> CommentGithubCommentSyncer.delete(github_id) end) |
43 | | - end |
44 | | - |
45 | | - @spec sync_github_comment(GithubIssue.t, map) :: {:ok, GithubComment.t} | {:error, Ecto.Changeset.t} |
46 | | - defp sync_github_comment(github_issue, attrs) do |
47 | | - CommentGithubCommentSyncer.create_or_update_comment(github_issue, attrs) |
| 36 | + |> Multi.run(:deleted_comments, fn _ -> Sync.Comment.Comment.delete(github_id) end) |
| 37 | + |> Multi.run(:deleted_github_comment, fn _ -> Sync.Comment.GithubComment.delete(github_id) end) |
48 | 38 | end |
49 | 39 | end |
0 commit comments