-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathissue.ex
More file actions
76 lines (66 loc) · 2.16 KB
/
issue.ex
File metadata and controls
76 lines (66 loc) · 2.16 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
defmodule CodeCorps.GitHub.API.Issue do
@moduledoc ~S"""
Functions for working with issues on GitHub.
"""
alias CodeCorps.{
GitHub,
GithubAppInstallation,
GithubIssue,
GithubRepo,
Task,
User
}
@doc """
Fetches an issue from the GitHub API, given the API URL for the issue and the
`CodeCorps.GithubRepo` record that points to its GitHub repository.
"""
def from_url(url, %GithubRepo{github_app_installation: %GithubAppInstallation{} = installation}) do
"https://api.github.com/" <> endpoint = url
with opts when is_list(opts) <- GitHub.API.opts_for(installation) do
GitHub.request(:get, endpoint, %{}, %{}, opts)
else
{:error, github_error} -> {:error, github_error}
end
end
@doc """
Create an issue on GitHub's API for a `CodeCorps.Task`.
"""
@spec create(Task.t) :: GitHub.response
def create(%Task{
github_repo: %GithubRepo{
github_app_installation: %GithubAppInstallation{} = installation
} = github_repo,
user: %User{} = user
} = task) do
endpoint = github_repo |> get_endpoint()
attrs = task |> GitHub.Adapters.Issue.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 an issue on GitHub's API for a `CodeCorps.Task`.
"""
@spec update(Task.t) :: GitHub.response
def update(%Task{
github_issue: %GithubIssue{number: number},
github_repo: %GithubRepo{
github_app_installation: %GithubAppInstallation{} = installation
} = github_repo,
user: %User{} = user,
} = task) do
endpoint = "#{github_repo |> get_endpoint()}/#{number}"
attrs = task |> GitHub.Adapters.Issue.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 get_endpoint(GithubRepo.t) :: String.t
defp get_endpoint(%GithubRepo{github_account_login: owner, name: repo}) do
"/repos/#{owner}/#{repo}/issues"
end
end