Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 0 additions & 24 deletions lib/code_corps/github/event/issue_comment/comment_deleter.ex

This file was deleted.

10 changes: 5 additions & 5 deletions lib/code_corps/github/sync/comment/comment.ex
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
defmodule CodeCorps.GitHub.Sync.Comment do
alias CodeCorps.{
GitHub,
GithubComment,
GitHub.Event.IssueComment.CommentDeleter
GithubComment
}
alias GitHub.Sync.Comment.Comment, as: CommentCommentSyncer
alias GitHub.Sync.Comment.GithubComment, as: CommentGithubCommentSyncer
Expand Down Expand Up @@ -33,12 +32,13 @@ defmodule CodeCorps.GitHub.Sync.Comment do

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

@spec sync_github_comment(GithubIssue.t, map) :: {:ok, GithubComment.t} | {:error, Ecto.Changeset.t}
Expand Down
17 changes: 17 additions & 0 deletions lib/code_corps/github/sync/comment/comment/comment.ex
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,23 @@ defmodule CodeCorps.GitHub.Sync.Comment.Comment do
|> ResultAggregator.aggregate
end

@doc ~S"""
Deletes all `Comment` records related to `GithubComment` using the GitHub ID
from a GitHub API comment payload.

Returns a list of the deleted `Comment` records.
"""
@spec delete_all(String.t) :: {:ok, list(Comment.t)}
def delete_all(github_id) do
query =
from c in Comment,
join: gc in GithubComment, on: gc.id == c.github_comment_id, where: gc.github_id == ^github_id

query
|> Repo.delete_all(returning: true)
|> (fn {_count, comments} -> {:ok, comments} end).()
end

@spec sync(Task.t, GithubComment.t, User.t, map) :: {:ok, Comment.t} | {:error, Changeset.t}
defp sync(%Task{} = task, %GithubComment{} = github_comment, %User{} = user, %{} = payload) do
task
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,22 @@ defmodule CodeCorps.GitHub.Sync.Comment.GithubComment do
end
end

@doc ~S"""
Deletes the `GithubComment` record using the GitHub ID from a GitHub API
comment payload.

Returns the deleted `Comment` record or an empty `Comment` record if no such
record existed.
"""
@spec delete(String.t) :: {:ok, GithubComment.t}
def delete(github_id) do
comment = Repo.get_by(GithubComment, github_id: github_id)
case comment do
nil -> {:ok, %GithubComment{}}
_ -> Repo.delete(comment, returning: true)
end
end

@spec create_comment(map) :: linking_result
defp create_comment(params) do
%GithubComment{}
Expand Down
1 change: 1 addition & 0 deletions lib/code_corps/github/sync/sync.ex
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ defmodule CodeCorps.GitHub.Sync do

@spec marshall_result(tuple) :: tuple
defp marshall_result({:ok, %{comments: comments}}), do: {:ok, comments}
defp marshall_result({:ok, %{deleted_comments: _, deleted_github_comment: _}}), do: {:ok, nil}
defp marshall_result({:ok, %{github_pull_request: pull_request}}), do: {:ok, pull_request}
defp marshall_result({:ok, %{tasks: tasks}}), do: {:ok, tasks}
defp marshall_result({:error, :repo, :unmatched_project, _steps}), do: {:ok, []}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
defmodule CodeCorps.GitHub.Event.IssueCommentTest do
@moduledoc false

use CodeCorps.BackgroundProcessingCase
use CodeCorps.DbAccessCase

import CodeCorps.GitHub.TestHelpers
Expand Down Expand Up @@ -76,10 +77,9 @@ defmodule CodeCorps.GitHub.Event.IssueCommentTest do
github_comment = insert(:github_comment, github_id: github_id, github_issue: github_issue)
insert(:comment, github_comment: github_comment)

{:ok, comments} = IssueComment.handle(@payload)
assert Enum.count(comments) == 1
{:ok, nil} = IssueComment.handle(@payload)
assert Repo.aggregate(Comment, :count, :id) == 0
assert Repo.aggregate(GithubComment, :count, :id) == 1 # FIXME
assert Repo.aggregate(GithubComment, :count, :id) == 0
end

test "returns error if payload is wrong" do
Expand Down
1 change: 1 addition & 0 deletions test/lib/code_corps/github/event/issues/issues_test.exs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
defmodule CodeCorps.GitHub.Event.IssuesTest do
@moduledoc false

use CodeCorps.BackgroundProcessingCase
use CodeCorps.DbAccessCase

import CodeCorps.GitHub.TestHelpers
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
defmodule CodeCorps.GitHub.Event.PullRequestTest do
@moduledoc false

use CodeCorps.BackgroundProcessingCase
use CodeCorps.DbAccessCase

import CodeCorps.GitHub.TestHelpers
Expand Down
39 changes: 36 additions & 3 deletions test/lib/code_corps/github/sync/comment/comment/comment_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ defmodule CodeCorps.GitHub.Sync.Comment.CommentTest do
import CodeCorps.GitHub.TestHelpers

alias CodeCorps.{
Repo,
Comment
Comment,
GithubComment,
Repo
}
alias CodeCorps.GitHub.Sync.Comment.Comment, as: CommentCommentSyncer

describe "sync all/3" do
describe "sync_all/3" do
@payload load_event_fixture("issue_comment_created")

test "creates missing, updates existing comments for each project associated with the github repo" do
Expand Down Expand Up @@ -77,4 +78,36 @@ defmodule CodeCorps.GitHub.Sync.Comment.CommentTest do
assert Enum.count(errors) == 1
end
end

describe "delete_all/1" do
test "deletes all the Comment records for a GithubComment" do
github_comment = insert(:github_comment)
comments = insert_list(2, :comment, github_comment: github_comment)
insert(:comment)

comment_ids = Enum.map(comments, &Map.get(&1, :id))

{:ok, deleted_comments} =
github_comment.github_id
|> CommentCommentSyncer.delete_all()

assert Enum.count(deleted_comments) == 2
assert Repo.aggregate(Comment, :count, :id) == 1
assert Repo.aggregate(GithubComment, :count, :id) == 1

for deleted_comment <- deleted_comments do
assert deleted_comment.id in comment_ids
end
end

test "works when there are no Comment records for a GithubComment" do
github_comment = insert(:github_comment)

{:ok, deleted_comments} =
github_comment.github_id
|> CommentCommentSyncer.delete_all()

assert Enum.count(deleted_comments) == 0
end
end
end
16 changes: 12 additions & 4 deletions test/lib/code_corps/github/sync/comment/comment_test.exs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
defmodule CodeCorps.GitHub.Sync.CommentTest do
@moduledoc false

use CodeCorps.BackgroundProcessingCase
use CodeCorps.DbAccessCase

import CodeCorps.GitHub.TestHelpers
Expand Down Expand Up @@ -67,7 +68,7 @@ defmodule CodeCorps.GitHub.Sync.CommentTest do
end
end

describe "delete/2 for IssueComment::deleted" do
describe "delete/2" do
@payload load_event_fixture("issue_comment_deleted")

test "deletes all comments with github_id specified in the payload" do
Expand All @@ -79,10 +80,17 @@ defmodule CodeCorps.GitHub.Sync.CommentTest do
insert_list(2, :comment)
insert_list(4, :comment, github_comment: github_comment_2)

{:ok, %{comments: comments}} = CommentSyncer.delete(%{}, comment) |> Repo.transaction
assert Enum.count(comments) == 3
changes = %{}

{:ok, %{deleted_comments: deleted_comments, deleted_github_comment: deleted_github_comment}} =
changes
|> CommentSyncer.delete(comment)
|> Repo.transaction

assert Enum.count(deleted_comments) == 3
assert deleted_github_comment.id == github_comment_1.id
assert Repo.aggregate(Comment, :count, :id) == 6
assert Repo.aggregate(GithubComment, :count, :id) == 2 # FIXME to 1
assert Repo.aggregate(GithubComment, :count, :id) == 1
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,27 @@ defmodule CodeCorps.GitHub.Sync.Comment.GithubCommentTest do
refute changeset.valid?
end
end

describe "delete/1" do
test "deletes the GithubComment" do
github_comment = insert(:github_comment)

{:ok, deleted_github_comment} =
github_comment.github_id
|> GithubCommentSyncer.delete()

assert Repo.aggregate(GithubComment, :count, :id) == 0
assert deleted_github_comment.id == github_comment.id
end

test "works when there are no GithubComment reocrds" do
assert Repo.aggregate(GithubComment, :count, :id) == 0

{:ok, %GithubComment{} = empty_github_comment} =
"123"
|> GithubCommentSyncer.delete()

refute empty_github_comment.id
end
end
end
1 change: 1 addition & 0 deletions test/lib/code_corps/github/sync/issue/issue_test.exs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
defmodule CodeCorps.GitHub.Sync.IssueTest do
@moduledoc false

use CodeCorps.BackgroundProcessingCase
use CodeCorps.DbAccessCase

import CodeCorps.GitHub.TestHelpers
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ defmodule CodeCorps.GitHub.Sync.PullRequest.BodyParserTest do

use ExUnit.Case, async: true


alias CodeCorps.{
GitHub.Sync.PullRequest.BodyParser
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
defmodule CodeCorps.GitHub.Sync.PullRequestTest do
@moduledoc false

use CodeCorps.BackgroundProcessingCase
use CodeCorps.DbAccessCase

import CodeCorps.GitHub.TestHelpers
Expand Down
1 change: 1 addition & 0 deletions test/lib/code_corps/github/sync/sync_test.exs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
defmodule CodeCorps.GitHub.SyncTest do
@moduledoc false

use CodeCorps.BackgroundProcessingCase
use CodeCorps.DbAccessCase

import CodeCorps.GitHub.TestHelpers
Expand Down