Skip to content

Commit 95746bf

Browse files
committed
Refactor comment deletion and ensure all comment records are deleted
1 parent fdcae0b commit 95746bf

File tree

16 files changed

+118
-73
lines changed

16 files changed

+118
-73
lines changed

lib/code_corps/github/event/issue_comment/comment_deleter.ex

Lines changed: 0 additions & 24 deletions
This file was deleted.

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
defmodule CodeCorps.GitHub.Sync.Comment do
22
alias CodeCorps.{
33
GitHub,
4-
GithubComment,
5-
GitHub.Event.IssueComment.CommentDeleter
4+
GithubComment
65
}
76
alias GitHub.Sync.Comment.Comment, as: CommentCommentSyncer
87
alias GitHub.Sync.Comment.GithubComment, as: CommentGithubCommentSyncer
@@ -33,12 +32,13 @@ defmodule CodeCorps.GitHub.Sync.Comment do
3332

3433
@doc """
3534
When provided a GitHub API payload, it deletes each `Comment` associated to
36-
the specified `IssueComment`.
35+
the specified `IssueComment` and then deletes the `GithubComment`.
3736
"""
3837
@spec delete(map, map) :: Multi.t
39-
def delete(_changes, payload) do
38+
def delete(_, %{"id" => github_id}) do
4039
Multi.new
41-
|> Multi.run(:comments, fn _ -> CommentDeleter.delete_all(payload) end)
40+
|> Multi.run(:deleted_comments, fn _ -> CommentCommentSyncer.delete_all(github_id) end)
41+
|> Multi.run(:deleted_github_comment, fn _ -> CommentGithubCommentSyncer.delete(github_id) end)
4242
end
4343

4444
@spec sync_github_comment(GithubIssue.t, map) :: {:ok, GithubComment.t} | {:error, Ecto.Changeset.t}

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,23 @@ defmodule CodeCorps.GitHub.Sync.Comment.Comment do
4141
|> ResultAggregator.aggregate
4242
end
4343

44+
@doc ~S"""
45+
Deletes all `Comment` records related to `GithubComment` using the GitHub ID
46+
from a GitHub API comment payload.
47+
48+
Returns a list of the deleted `Comment` records.
49+
"""
50+
@spec delete_all(String.t) :: {:ok, list(Comment.t)}
51+
def delete_all(github_id) do
52+
query =
53+
from c in Comment,
54+
join: gc in GithubComment, on: gc.id == c.github_comment_id, where: gc.github_id == ^github_id
55+
56+
query
57+
|> Repo.delete_all(returning: true)
58+
|> (fn {_count, comments} -> {:ok, comments} end).()
59+
end
60+
4461
@spec sync(Task.t, GithubComment.t, User.t, map) :: {:ok, Comment.t} | {:error, Changeset.t}
4562
defp sync(%Task{} = task, %GithubComment{} = github_comment, %User{} = user, %{} = payload) do
4663
task

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,22 @@ defmodule CodeCorps.GitHub.Sync.Comment.GithubComment do
3737
end
3838
end
3939

40+
@doc ~S"""
41+
Deletes the `GithubComment` record using the GitHub ID from a GitHub API
42+
comment payload.
43+
44+
Returns the deleted `Comment` record or an empty `Comment` record if no such
45+
record existed.
46+
"""
47+
@spec delete(String.t) :: {:ok, GithubComment.t}
48+
def delete(github_id) do
49+
comment = Repo.get_by(GithubComment, github_id: github_id)
50+
case comment do
51+
nil -> {:ok, %GithubComment{}}
52+
_ -> Repo.delete(comment, returning: true)
53+
end
54+
end
55+
4056
@spec create_comment(map) :: linking_result
4157
defp create_comment(params) do
4258
%GithubComment{}

lib/code_corps/github/sync/sync.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ defmodule CodeCorps.GitHub.Sync do
127127

128128
@spec marshall_result(tuple) :: tuple
129129
defp marshall_result({:ok, %{comments: comments}}), do: {:ok, comments}
130+
defp marshall_result({:ok, %{deleted_comments: _, deleted_github_comment: _}}), do: {:ok, nil}
130131
defp marshall_result({:ok, %{github_pull_request: pull_request}}), do: {:ok, pull_request}
131132
defp marshall_result({:ok, %{tasks: tasks}}), do: {:ok, tasks}
132133
defp marshall_result({:error, :repo, :unmatched_project, _steps}), do: {:ok, []}

test/lib/code_corps/github/event/issue_comment/comment_deleter_test.exs

Lines changed: 0 additions & 33 deletions
This file was deleted.

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
defmodule CodeCorps.GitHub.Event.IssueCommentTest do
22
@moduledoc false
33

4+
use CodeCorps.BackgroundProcessingCase
45
use CodeCorps.DbAccessCase
56

67
import CodeCorps.GitHub.TestHelpers
@@ -76,10 +77,9 @@ defmodule CodeCorps.GitHub.Event.IssueCommentTest do
7677
github_comment = insert(:github_comment, github_id: github_id, github_issue: github_issue)
7778
insert(:comment, github_comment: github_comment)
7879

79-
{:ok, comments} = IssueComment.handle(@payload)
80-
assert Enum.count(comments) == 1
80+
{:ok, nil} = IssueComment.handle(@payload)
8181
assert Repo.aggregate(Comment, :count, :id) == 0
82-
assert Repo.aggregate(GithubComment, :count, :id) == 1 # FIXME
82+
assert Repo.aggregate(GithubComment, :count, :id) == 0
8383
end
8484

8585
test "returns error if payload is wrong" do

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
defmodule CodeCorps.GitHub.Event.IssuesTest do
22
@moduledoc false
33

4+
use CodeCorps.BackgroundProcessingCase
45
use CodeCorps.DbAccessCase
56

67
import CodeCorps.GitHub.TestHelpers

test/lib/code_corps/github/event/pull_request/pull_request_test.exs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
defmodule CodeCorps.GitHub.Event.PullRequestTest do
22
@moduledoc false
33

4+
use CodeCorps.BackgroundProcessingCase
45
use CodeCorps.DbAccessCase
56

67
import CodeCorps.GitHub.TestHelpers

test/lib/code_corps/github/sync/comment/comment/comment_test.exs

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@ defmodule CodeCorps.GitHub.Sync.Comment.CommentTest do
66
import CodeCorps.GitHub.TestHelpers
77

88
alias CodeCorps.{
9-
Repo,
10-
Comment
9+
Comment,
10+
GithubComment,
11+
Repo
1112
}
1213
alias CodeCorps.GitHub.Sync.Comment.Comment, as: CommentCommentSyncer
1314

14-
describe "sync all/3" do
15+
describe "sync_all/3" do
1516
@payload load_event_fixture("issue_comment_created")
1617

1718
test "creates missing, updates existing comments for each project associated with the github repo" do
@@ -77,4 +78,36 @@ defmodule CodeCorps.GitHub.Sync.Comment.CommentTest do
7778
assert Enum.count(errors) == 1
7879
end
7980
end
81+
82+
describe "delete_all/1" do
83+
test "deletes all the Comment records for a GithubComment" do
84+
github_comment = insert(:github_comment)
85+
comments = insert_list(2, :comment, github_comment: github_comment)
86+
insert(:comment)
87+
88+
comment_ids = Enum.map(comments, &Map.get(&1, :id))
89+
90+
{:ok, deleted_comments} =
91+
github_comment.github_id
92+
|> CommentCommentSyncer.delete_all()
93+
94+
assert Enum.count(deleted_comments) == 2
95+
assert Repo.aggregate(Comment, :count, :id) == 1
96+
assert Repo.aggregate(GithubComment, :count, :id) == 1
97+
98+
for deleted_comment <- deleted_comments do
99+
assert deleted_comment.id in comment_ids
100+
end
101+
end
102+
103+
test "works when there are no Comment records for a GithubComment" do
104+
github_comment = insert(:github_comment)
105+
106+
{:ok, deleted_comments} =
107+
github_comment.github_id
108+
|> CommentCommentSyncer.delete_all()
109+
110+
assert Enum.count(deleted_comments) == 0
111+
end
112+
end
80113
end

0 commit comments

Comments
 (0)