Skip to content

Commit 162e2bd

Browse files
authored
Merge pull request #1086 from code-corps/code-and-comment-cleanups-for-issue-comment
Code and comment cleanups for IssueComment
2 parents f9f151f + 3ba5e6a commit 162e2bd

File tree

3 files changed

+32
-13
lines changed

3 files changed

+32
-13
lines changed

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

+14-8
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
defmodule 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

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

+17-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
11
defmodule CodeCorps.GitHub.Event.IssueComment.CommentSyncer do
2+
@moduledoc ~S"""
3+
In charge of syncing `CodeCorps.Comment` records with a GitHub comment
4+
payload.
5+
6+
A single GitHub comment always matches a single `CodeCorps.GithubComment`, but
7+
it can match multiple `CodeCorps.Comment` records. This module handles
8+
creating or updating all those records.
9+
"""
210

311
import Ecto.Query
412

@@ -17,10 +25,15 @@ defmodule CodeCorps.GitHub.Event.IssueComment.CommentSyncer do
1725
@type outcome :: {:ok, list(Comment.t)} |
1826
{:error, {list(Comment.t), list(Changeset.t)}}
1927

20-
@doc """
21-
When provided a list of `Task`s, a `User` and a GitHub API payload, for each
22-
`Comment` associated to those `Task`s it creates or updates a `Comment`
23-
associated to the specified `User`.
28+
@doc ~S"""
29+
Creates or updates `CodeCorps.Comment` records for the speciifed list of
30+
`CodeCorps.Task` records.
31+
32+
When provided a list of `CodeCorps.Task` records, a `CodeCorps.GithubComment`,
33+
a `CodeCorps.User`, and a GitHub API payload , for each `CodeCorps.Task`
34+
record, it creates or updates a `CodeCorps.Comment` record, using the provided
35+
GitHub API payload, associated to the specified `CodeCorps.GithubComment` and
36+
`CodeCorps.User`
2437
"""
2538
@spec sync_all(list(Task.t), GithubComment.t, User.t, map) :: outcome
2639
def sync_all(tasks, %GithubComment{} = github_comment, %User{} = user, %{} = payload) do

priv/repo/structure.sql

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
-- PostgreSQL database dump
33
--
44

5-
-- Dumped from database version 9.5.9
5+
-- Dumped from database version 10.0
66
-- Dumped by pg_dump version 10.0
77

88
SET statement_timeout = 0;

0 commit comments

Comments
 (0)