-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathsync.ex
More file actions
164 lines (145 loc) · 6.75 KB
/
sync.ex
File metadata and controls
164 lines (145 loc) · 6.75 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
defmodule CodeCorps.GitHub.Sync do
alias CodeCorps.{
Comment,
GitHub,
GithubPullRequest,
GithubRepo,
GitHub.Sync.Utils.RepoFinder,
Repo
}
alias Ecto.Multi
@type outcome :: {:ok, list(Comment.t)}
| {:ok, GithubPullRequest.t}
| {:ok, list(CodeCorps.Task.t)}
| {:error, :repo_not_found}
| {:error, :fetching_issue}
| {:error, :fetching_pull_request}
| {:error, :multiple_issue_users_match}
| {:error, :multiple_comment_users_match}
| {:error, :validating_github_pull_request}
| {:error, :validating_github_issue}
| {:error, :validating_github_comment}
| {:error, :validating_user}
| {:error, :validating_tasks}
| {:error, :validating_comments}
| {:error, :unexpected_transaction_outcome}
@doc ~S"""
Syncs a GitHub Issues event.
- Finds the `CodeCorps.GithubRepo`
- Syncs the issue portion of the event with `GitHub.Sync.Issue`
[https://developer.github.com/v3/activity/events/types/#issuesevent](https://developer.github.com/v3/activity/events/types/#issuesevent)
"""
def issue_event(%{"issue" => issue} = payload) do
Multi.new
|> Multi.merge(__MODULE__, :find_repo, [payload])
|> Multi.merge(GitHub.Sync.Issue, :sync, [issue])
|> transact()
end
@doc ~S"""
Syncs a GitHub IssueComment event.
- For the deleted action
- Deletes the related comment records with `GitHub.Sync.Comment`
- For any other action
- Finds the `CodeCorps.GithubRepo`
- If it's a pull request, it fetches the pull request from the GitHub API
and syncs it with `GitHub.Sync.PullRequest`
- Syncs the issue portion of the event with `GitHub.Sync.Issue`
- Syncs the comment portion of the event with `GitHub.Sync.Comment`
[https://developer.github.com/v3/activity/events/types/#issuecommentevent](https://developer.github.com/v3/activity/events/types/#issuecommentevent)
"""
def issue_comment_event(%{"action" => "deleted", "comment" => comment}) do
Multi.new
|> Multi.merge(GitHub.Sync.Comment, :delete, [comment])
|> transact()
end
def issue_comment_event(%{"issue" => %{"pull_request" => %{"url" => pull_request_url}} = issue, "comment" => comment} = payload) do
# Pull Request
Multi.new
|> Multi.merge(__MODULE__, :find_repo, [payload])
|> Multi.merge(__MODULE__, :fetch_pull_request, [pull_request_url])
|> Multi.merge(GitHub.Sync.PullRequest, :sync, [payload])
|> Multi.merge(GitHub.Sync.Issue, :sync, [issue])
|> Multi.merge(GitHub.Sync.Comment, :sync, [comment])
|> transact()
end
def issue_comment_event(%{"issue" => issue, "comment" => comment} = payload) do
# Issue
Multi.new
|> Multi.merge(__MODULE__, :find_repo, [payload])
|> Multi.merge(GitHub.Sync.Issue, :sync, [issue])
|> Multi.merge(GitHub.Sync.Comment, :sync, [comment])
|> transact()
end
@doc ~S"""
Syncs a GitHub PullRequest event.
- Finds the `CodeCorps.GithubRepo`
- Fetches the issue from the GitHub API
- Syncs the pull request portion of the event with `GitHub.Sync.PullRequest`
- Syncs the issue portion of the event with `GitHub.Sync.Issue`, using the
changes passed in from the issue fetching and the pull request syncing
[https://developer.github.com/v3/activity/events/types/#pullrequestevent](https://developer.github.com/v3/activity/events/types/#pullrequestevent)
"""
def pull_request_event(%{"pull_request" => %{"issue_url" => issue_url} = pull_request} = payload) do
Multi.new
|> Multi.merge(__MODULE__, :find_repo, [payload])
|> Multi.merge(__MODULE__, :fetch_issue, [issue_url])
|> Multi.merge(GitHub.Sync.PullRequest, :sync, [pull_request])
|> Multi.merge(GitHub.Sync.Issue, :sync, [payload])
|> transact()
end
def sync_issues(repo) do
{:ok, issues} = GitHub.API.Repository.issues(repo)
Enum.map(issues, &sync_issue(&1, repo))
end
def sync_issue(issue, repo) do
Multi.new
|> Multi.merge(__MODULE__, :return_repo, [repo])
|> Multi.merge(GitHub.Sync.Issue, :sync, [issue])
|> transact()
end
@doc false
def return_repo(_, repo) do
Multi.new
|> Multi.run(:repo, fn _ -> {:ok, repo} end)
end
@doc false
def find_repo(_, payload) do
Multi.new
|> Multi.run(:repo, fn _ -> RepoFinder.find_repo(payload) end)
end
@doc false
def fetch_issue(%{repo: %GithubRepo{} = github_repo}, url) do
Multi.new
|> Multi.run(:fetch_issue, fn _ -> GitHub.API.Issue.from_url(url, github_repo) end)
end
@doc false
def fetch_pull_request(%{repo: %GithubRepo{} = github_repo}, url) do
Multi.new
|> Multi.run(:fetch_pull_request, fn _ -> GitHub.API.PullRequest.from_url(url, github_repo) end)
end
@spec transact(Multi.t) :: any
defp transact(multi) do
multi
|> Repo.transaction
|> marshall_result()
end
@spec marshall_result(tuple) :: tuple
defp marshall_result({:ok, %{comments: comments}}), do: {:ok, comments}
defp marshall_result({:ok, %{deleted_comments: _, deleted_github_comment: _}}), do: {:ok, nil}
defp marshall_result({:ok, %{github_pull_request: pull_request}}), do: {:ok, pull_request}
defp marshall_result({:ok, %{tasks: tasks}}), do: {:ok, tasks}
defp marshall_result({:error, :repo, :unmatched_project, _steps}), do: {:ok, []}
defp marshall_result({:error, :repo, :unmatched_repository, _steps}), do: {:error, :repo_not_found}
defp marshall_result({:error, :fetch_issue, _, _steps}), do: {:error, :fetching_issue}
defp marshall_result({:error, :fetch_pull_request, _, _steps}), do: {:error, :fetching_pull_request}
defp marshall_result({:error, :github_pull_request, %Ecto.Changeset{}, _steps}), do: {:error, :validating_github_pull_request}
defp marshall_result({:error, :github_issue, %Ecto.Changeset{}, _steps}), do: {:error, :validating_github_issue}
defp marshall_result({:error, :github_comment, %Ecto.Changeset{}, _steps}), do: {:error, :validating_github_comment}
defp marshall_result({:error, :comment_user, %Ecto.Changeset{}, _steps}), do: {:error, :validating_user}
defp marshall_result({:error, :comment_user, :multiple_users, _steps}), do: {:error, :multiple_comment_users_match}
defp marshall_result({:error, :issue_user, %Ecto.Changeset{}, _steps}), do: {:error, :validating_user}
defp marshall_result({:error, :issue_user, :multiple_users, _steps}), do: {:error, :multiple_issue_users_match}
defp marshall_result({:error, :comments, {_comments, _errors}, _steps}), do: {:error, :validating_comments}
defp marshall_result({:error, :tasks, {_tasks, _errors}, _steps}), do: {:error, :validating_tasks}
defp marshall_result({:error, _errored_step, _error_response, _steps}), do: {:error, :unexpected_transaction_outcome}
end