1
1
defmodule CodeCorps.GitHub.Event.Issues.IssueLinker 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.GithubIssue` to link with a
4
+ `CodeCorps.Issue` when processing an Issues webhook, or handling a
5
+ `CodeCorpsWeb.TaskController` request.
5
6
6
7
The only entry point is `create_or_update_issue/1`.
7
8
"""
8
9
9
10
alias CodeCorps . {
11
+ GitHub.Adapters ,
10
12
GithubIssue ,
11
13
GithubRepo ,
12
14
Repo
13
15
}
14
16
15
- alias CodeCorps.GitHub.Adapters.Issue , as: IssueAdapter
16
-
17
- @ typep linking_result :: { :ok , GithubIssue . t } |
18
- { :error , Ecto.Changeset . t }
17
+ @ typep linking_result :: { :ok , GithubIssue . t } | { :error , Ecto.Changeset . t }
19
18
20
19
@ doc ~S"""
21
- Finds or creates a `GithubIssue` using the data in a GitHub Issue payload.
20
+ Finds or creates a `CodeCorps.GithubIssue` using the data in a GitHub Issue
21
+ payload.
22
22
23
23
The process is as follows:
24
-
25
24
- Search for the issue in our database with the payload data.
26
- - If we return a single `GithubIssue`, then the `GithubIssue` should be
27
- updated.
28
- - If there are no matching `GithubIssue` records, then a `GithubIssue`
29
- should be created .
25
+ - If found, update it with payload data
26
+ - If not found, create it from payload data
27
+
28
+ `CodeCorps.GitHub.AdaptersIssue.to_issue/1` is used to adapt the payload data .
30
29
"""
31
30
@ spec create_or_update_issue ( GithubRepo . t , map ) :: linking_result
32
31
def create_or_update_issue ( % GithubRepo { } = github_repo , % { "id" => github_issue_id } = attrs ) do
33
- params = IssueAdapter . to_issue ( attrs )
32
+ params = Adapters.Issue . to_issue ( attrs )
34
33
35
34
case Repo . get_by ( GithubIssue , github_id: github_issue_id ) do
36
35
nil -> create_issue ( github_repo , params )
37
36
% GithubIssue { } = issue -> update_issue ( issue , params )
38
37
end
39
38
end
40
39
40
+ @ spec create_issue ( GithubRepo . t , map ) :: linking_result
41
41
defp create_issue ( % GithubRepo { id: github_repo_id } , params ) do
42
42
params = Map . put ( params , :github_repo_id , github_repo_id )
43
43
@@ -46,6 +46,7 @@ defmodule CodeCorps.GitHub.Event.Issues.IssueLinker do
46
46
|> Repo . insert
47
47
end
48
48
49
+ @ spec update_issue ( GithubIssue . t , map ) :: linking_result
49
50
defp update_issue ( % GithubIssue { } = github_issue , params ) do
50
51
github_issue
51
52
|> GithubIssue . update_changeset ( params )
0 commit comments