11defmodule CodeCorps.GitHub.Event.IssueComment.CommentLinker do
22 @ moduledoc ~S"""
3- In charge of finding a issue to link with a Task when processing an Issues
4- webhook.
3+ 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.
56
6- The only entry point is `create_or_update_issue /1`.
7+ The only entry point is `create_or_update_comment /1`.
78 """
89
910 alias CodeCorps . {
@@ -29,24 +30,29 @@ defmodule CodeCorps.GitHub.Event.IssueComment.CommentLinker do
2930 payload data.
3031 """
3132 @ spec create_or_update_comment ( GithubIssue . t , map ) :: linking_result
32- def create_or_update_comment ( % GithubIssue { } = github_issue , % { "id" => github_comment_id } = attrs ) do
33+ def create_or_update_comment ( % GithubIssue { } = github_issue , % { } = attrs ) do
3334 params = Adapters.Comment . to_github_comment ( attrs )
3435
35- case Repo . get_by ( GithubComment , github_id: github_comment_id ) do
36+ case attrs |> find_comment ( ) do
3637 nil -> create_comment ( github_issue , params )
37- % GithubComment { } = github_comment -> github_comment |> update_issue ( params )
38+ % GithubComment { } = github_comment -> github_comment |> update_comment ( params )
3839 end
3940 end
4041
41- defp create_comment ( % GithubIssue { id: github_issue_id } , params ) do
42+ @ spec find_comment ( map ) :: GithubComment . t | nil
43+ defp find_comment ( % { "id" => github_id } ) , do: GithubComment |> Repo . get_by ( github_id: github_id )
44+
45+ @ spec create_comment ( GithubIssue . t , map ) :: linking_result
46+ defp create_comment ( % GithubIssue { id: github_issue_id } , % { } = params ) do
4247 params = Map . put ( params , :github_issue_id , github_issue_id )
4348
4449 % GithubComment { }
4550 |> GithubComment . create_changeset ( params )
4651 |> Repo . insert
4752 end
4853
49- defp update_issue ( % GithubComment { } = github_comment , params ) do
54+ @ spec update_comment ( GithubComment . t , map ) :: linking_result
55+ defp update_comment ( % GithubComment { } = github_comment , % { } = params ) do
5056 github_comment
5157 |> GithubComment . update_changeset ( params )
5258 |> Repo . update
0 commit comments