forked from code-corps/code-corps-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomment.ex
More file actions
92 lines (83 loc) · 2.24 KB
/
comment.ex
File metadata and controls
92 lines (83 loc) · 2.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
defmodule CodeCorps.GitHub.API.Comment do
@moduledoc ~S"""
Functions for working with comments on GitHub.
"""
alias CodeCorps.{
Comment,
GitHub,
GithubAppInstallation,
GithubComment,
GithubIssue,
GithubRepo,
Task,
User
}
@doc """
Create a comment on GitHub's API for a `CodeCorps.Comment`.
"""
@spec create(Comment.t) :: GitHub.response
def create(
%Comment{
task: %Task{
github_repo: %GithubRepo{
github_app_installation: %GithubAppInstallation{} = installation
}
},
user: %User{} = user
} = comment) do
endpoint = comment |> create_endpoint_for()
attrs = comment |> GitHub.Adapters.Comment.to_api
with opts when is_list(opts) <- GitHub.API.opts_for(user, installation) do
GitHub.request(:post, endpoint, attrs, %{}, opts)
else
{:error, github_error} -> {:error, github_error}
end
end
@doc """
Update a comment on GitHub's API for a `CodeCorps.Comment`.
"""
@spec update(Comment.t) :: GitHub.response
def update(
%Comment{
task: %Task{
github_repo: %GithubRepo{
github_app_installation: %GithubAppInstallation{} = installation
}
},
user: %User{} = user
} = comment) do
endpoint = comment |> update_endpoint_for()
attrs = comment |> GitHub.Adapters.Comment.to_api
with opts when is_list(opts) <- GitHub.API.opts_for(user, installation) do
GitHub.request(:patch, endpoint, attrs, %{}, opts)
else
{:error, github_error} -> {:error, github_error}
end
end
@spec update_endpoint_for(Comment.t) :: String.t
defp update_endpoint_for(
%Comment{
github_comment: %GithubComment{github_id: id},
task: %Task{
github_repo: %GithubRepo{
github_account_login: owner, name: repo
}
}
}) do
"/repos/#{owner}/#{repo}/issues/comments/#{id}"
end
@spec create_endpoint_for(Comment.t) :: String.t
defp create_endpoint_for(
%Comment{
task: %Task{
github_issue: %GithubIssue{
number: number
},
github_repo: %GithubRepo{
github_account_login: owner, name: repo
},
}
}) do
"/repos/#{owner}/#{repo}/issues/#{number}/comments"
end
end