@@ -3,19 +3,37 @@ defmodule CodeCorps.Task.Service do
33 Handles special CRUD operations for `CodeCorps.Task`.
44 """
55
6- alias CodeCorps . { GitHub , GithubIssue , Repo , Task }
6+ alias CodeCorps . { GitHub , GitHub.Sync , GithubIssue , Repo , Task }
77 alias Ecto . { Changeset , Multi }
88
99 require Logger
1010
11+ # :user, :github_issue and :github_repo are required for connecting to github
12+ # :project and :organization are required in order to add a header to the
13+ # github issue body when the user themselves are not connected to github, but
14+ # the task is
15+ #
16+ # Right now, all of these preloads are loaded at once. If there are
17+ # performance issues, we can split them up according the the information
18+ # provided here.
19+ @ preloads [
20+ :github_issue ,
21+ [ github_repo: :github_app_installation ] ,
22+ [ project: :organization ] ,
23+ :user
24+ ]
25+
1126 @ doc ~S"""
1227 Performs all actions involved in creating a task on a project
1328 """
1429 @ spec create ( map ) :: { :ok , Task . t } | { :error , Changeset . t } | { :error , :github }
1530 def create ( % { } = attributes ) do
1631 Multi . new
1732 |> Multi . insert ( :task , % Task { } |> Task . create_changeset ( attributes ) )
18- |> Multi . run ( :github , ( fn % { task: % Task { } = task } -> task |> create_on_github ( ) end ) )
33+ |> Multi . run ( :preload , fn % { task: % Task { } = task } ->
34+ { :ok , task |> Repo . preload ( @ preloads ) }
35+ end )
36+ |> Multi . run ( :github , ( fn % { preload: % Task { } = task } -> task |> create_on_github ( ) end ) )
1937 |> Repo . transaction
2038 |> marshall_result ( )
2139 end
@@ -24,7 +42,10 @@ defmodule CodeCorps.Task.Service do
2442 def update ( % Task { } = task , % { } = attributes ) do
2543 Multi . new
2644 |> Multi . update ( :task , task |> Task . update_changeset ( attributes ) )
27- |> Multi . run ( :github , ( fn % { task: % Task { } = task } -> task |> update_on_github ( ) end ) )
45+ |> Multi . run ( :preload , fn % { task: % Task { } = task } ->
46+ { :ok , task |> Repo . preload ( @ preloads ) }
47+ end )
48+ |> Multi . run ( :github , ( fn % { preload: % Task { } = task } -> task |> update_on_github ( ) end ) )
2849 |> Repo . transaction
2950 |> marshall_result ( )
3051 end
@@ -39,25 +60,16 @@ defmodule CodeCorps.Task.Service do
3960 { :error , :github }
4061 end
4162
42- # :user, :github_issue and :github_repo are required for connecting to github
43- # :project and :organization are required in order to add a header to the
44- # github issue body when the user themselves are not connected to github, but
45- # the task is
46- #
47- # Right now, all of these preloads are loaded at once. If there are
48- # performance issues, we can split them up according the the information
49- # provided here.
50- @ preloads [ :github_issue , [ github_repo: :github_app_installation ] , :user , [ project: :organization ] ]
51-
52- @ spec create_on_github ( Task . t ) :: { :ok , Task . t } :: { :error , GitHub . api_error_struct }
63+ @ spec create_on_github ( Task . t ) :: { :ok , Task . t } | { :error , Changeset . t } | { :error , GitHub . api_error_struct }
5364 defp create_on_github ( % Task { github_repo_id: nil } = task ) do
5465 # Don't create: no GitHub repo was selected
5566 { :ok , task }
5667 end
57- defp create_on_github ( % Task { github_repo: _ } = task ) do
58- with % Task { github_repo: github_repo } = task <- task |> Repo . preload ( @ preloads ) ,
59- { :ok , payload } <- GitHub.API.Issue . create ( task ) ,
60- { :ok , % GithubIssue { } = github_issue } <- payload |> GitHub.Sync.Issue.GithubIssue . create_or_update_issue ( github_repo ) do
68+ defp create_on_github ( % Task { github_repo: github_repo } = task ) do
69+ with { :ok , payload } <- GitHub.API.Issue . create ( task ) ,
70+ { :ok , % GithubIssue { } = github_issue } <-
71+ payload
72+ |> Sync.Issue.GithubIssue . create_or_update_issue ( github_repo ) do
6173 task |> link_with_github_changeset ( github_issue ) |> Repo . update
6274 else
6375 { :error , error } -> { :error , error }
@@ -69,16 +81,17 @@ defmodule CodeCorps.Task.Service do
6981 task |> Changeset . change ( % { github_issue: github_issue } )
7082 end
7183
72- @ spec update_on_github ( Task . t ) :: { :ok , Task . t } :: { :error , GitHub . api_error_struct }
84+ @ spec update_on_github ( Task . t ) :: { :ok , Task . t } | { :error , Changeset . t } | { :error , GitHub . api_error_struct }
7385 defp update_on_github ( % Task { github_repo_id: nil , github_issue_id: nil } = task ) , do: { :ok , task }
7486 defp update_on_github ( % Task { github_repo_id: _ , github_issue_id: nil } = task ) , do: task |> create_on_github ( )
75- defp update_on_github ( % Task { github_repo_id: _ } = task ) do
76- with % Task { github_repo: github_repo } = task <- task |> Repo . preload ( @ preloads ) ,
77- { :ok , payload } <- GitHub.API.Issue . update ( task ) ,
78- { :ok , % GithubIssue { } } <- payload |> GitHub.Sync.Issue.GithubIssue . create_or_update_issue ( github_repo ) do
87+ defp update_on_github ( % Task { github_repo: github_repo } = task ) do
88+ with { :ok , payload } <- GitHub.API.Issue . update ( task ) ,
89+ { :ok , % GithubIssue { } } <-
90+ payload
91+ |> Sync.Issue.GithubIssue . create_or_update_issue ( github_repo ) do
7992 { :ok , Task |> Repo . get ( task . id ) }
8093 else
81- { :error , github_error } -> { :error , github_error }
94+ { :error , error } -> { :error , error }
8295 end
8396 end
8497end
0 commit comments