1
1
defmodule CodeCorps.GitHub.Event.IssueComment.CommentLinker do
2
2
@ 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.
5
6
6
- The only entry point is `create_or_update_issue /1`.
7
+ The only entry point is `create_or_update_comment /1`.
7
8
"""
8
9
9
10
alias CodeCorps . {
@@ -29,24 +30,29 @@ defmodule CodeCorps.GitHub.Event.IssueComment.CommentLinker do
29
30
payload data.
30
31
"""
31
32
@ 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
33
34
params = Adapters.Comment . to_github_comment ( attrs )
34
35
35
- case Repo . get_by ( GithubComment , github_id: github_comment_id ) do
36
+ case attrs |> find_comment ( ) do
36
37
nil -> create_comment ( github_issue , params )
37
- % GithubComment { } = github_comment -> github_comment |> update_issue ( params )
38
+ % GithubComment { } = github_comment -> github_comment |> update_comment ( params )
38
39
end
39
40
end
40
41
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
42
47
params = Map . put ( params , :github_issue_id , github_issue_id )
43
48
44
49
% GithubComment { }
45
50
|> GithubComment . create_changeset ( params )
46
51
|> Repo . insert
47
52
end
48
53
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
50
56
github_comment
51
57
|> GithubComment . update_changeset ( params )
52
58
|> Repo . update
0 commit comments