11defmodule CodeCorps.GitHub.Sync.Issue.GithubIssue do
22 @ moduledoc ~S"""
3- In charge of finding a `CodeCorps.GithubIssue` to link with a
4- `CodeCorps.Issue ` when processing a GitHub Issue payload.
3+ In charge of finding or creating a `CodeCorps.GithubIssue` to link with a
4+ `CodeCorps.Task ` when processing a GitHub Issue payload.
55
66 The only entry point is `create_or_update_issue/2`.
77 """
@@ -17,93 +17,38 @@ defmodule CodeCorps.GitHub.Sync.Issue.GithubIssue do
1717 }
1818
1919 alias Ecto.Changeset
20- alias Sync.User.GithubUser , as: GithubUserSyncer
2120
2221 @ typep linking_result :: { :ok , GithubIssue . t } | { :error , Changeset . t }
2322
2423 @ doc ~S"""
25- Finds or creates a `CodeCorps.GithubIssue` using the data in a GitHub Issue
26- payload.
24+ Creates or updates a `CodeCorps.GithubIssue` from a github issue API payload.
2725
28- The process is as follows:
29- - Search for the issue in our database with the payload data.
30- - If found, update it with payload data
31- - If not found, create it from payload data
26+ The created record is associated to the provided `CodeCorps.GithubRepo` and, optionaly,
27+ to a provided `CodeCorps.GithubPullRequest`.
3228
33- `CodeCorps.GitHub.AdaptersIssue.to_issue/1` is used to adapt the payload data.
29+ The created record is also associated with a matched `CodeCorps.GithubUser`, which is
30+ created if necessary.
3431 """
35- @ spec create_or_update_issue ( { GithubRepo . t , GithubPullRequest . t | nil } , map ) :: linking_result
36- def create_or_update_issue ( { github_repo , github_pull_request } , % { "id" => github_issue_id } = attrs ) do
37- params = to_params ( attrs , github_repo , github_pull_request )
38- case Repo . get_by ( GithubIssue , github_id: github_issue_id ) do
39- nil -> create_issue ( params )
40- % GithubIssue { } = issue -> update_issue ( issue , params )
41- end
42- end
43-
44- @ doc ~S"""
45- Creates a `CodeCorps.GithubIssue` for the provided `CodeCorps.GithubRepo`
46- using specified attributes.
47-
48- Links to existing `CodeCorps.GithubPullRequest` if matched by
49- `github_repo_id` and `number`.
50- """
51- @ spec create_or_update_issue ( GithubRepo . t , map ) :: linking_result
52- def create_or_update_issue ( % GithubRepo { } = repo , attrs ) do
53- with { :ok , % GithubUser { } = github_user } <- GithubUserSyncer . create_or_update_github_user ( attrs ) ,
54- { :ok , % GithubIssue { } = github_issue } <- do_create_or_update_issue ( repo , attrs , github_user )
55- do
56- { :ok , github_issue }
32+ @ spec create_or_update_issue ( map , GithubRepo . t , GithubPullRequest . t | nil ) :: linking_result
33+ def create_or_update_issue ( % { } = payload , % GithubRepo { } = github_repo , github_pull_request \\ nil ) do
34+ with { :ok , % GithubUser { } = github_user } <- Sync.User.GithubUser . create_or_update_github_user ( payload ) do
35+ payload
36+ |> find_or_init ( )
37+ |> GithubIssue . changeset ( payload |> Adapters.Issue . to_issue )
38+ |> Changeset . put_assoc ( :github_user , github_user )
39+ |> Changeset . put_assoc ( :github_repo , github_repo )
40+ |> Changeset . put_assoc ( :github_pull_request , github_pull_request )
41+ |> Repo . insert_or_update
5742 else
5843 { :error , error } -> { :error , error }
5944 end
6045 end
6146
62- defp do_create_or_update_issue (
63- % GithubRepo { id: repo_id } = repo ,
64- % { "id" => github_id , "number" => number } = attrs ,
65- % GithubUser { } = github_user ) do
66-
67- case Repo . get_by ( GithubIssue , github_id: github_id ) |> Repo . preload ( [ :github_pull_request , :github_user ] ) do
68- nil ->
69- % GithubIssue { }
70- |> GithubIssue . create_changeset ( attrs |> Adapters.Issue . to_issue )
71- |> Changeset . put_assoc ( :github_pull_request , GithubPullRequest |> Repo . get_by ( github_repo_id: repo_id , number: number ) )
72- |> Changeset . put_assoc ( :github_repo , repo )
73- |> Changeset . put_assoc ( :github_user , github_user )
74- |> Repo . insert
75- % GithubIssue { } = issue ->
76- issue
77- |> GithubIssue . update_changeset ( attrs |> Adapters.Issue . to_issue )
78- |> Changeset . put_assoc ( :github_pull_request , GithubPullRequest |> Repo . get_by ( github_repo_id: repo_id , number: number ) )
79- |> Changeset . put_assoc ( :github_user , github_user )
80- |> Repo . update
47+ @ spec find_or_init ( map ) :: GithubIssue . t
48+ defp find_or_init ( % { "id" => github_id } ) do
49+ case GithubIssue |> Repo . get_by ( github_id: github_id ) |> Repo . preload ( [ :github_user , :github_repo , :github_pull_request ] ) do
50+ nil -> % GithubIssue { }
51+ % GithubIssue { } = github_issue -> github_issue
8152 end
8253 end
83-
84- @ spec create_issue ( map ) :: linking_result
85- defp create_issue ( params ) do
86- % GithubIssue { }
87- |> GithubIssue . create_changeset ( params )
88- |> Repo . insert
89- end
90-
91- @ spec update_issue ( GithubIssue . t , map ) :: linking_result
92- defp update_issue ( % GithubIssue { } = github_issue , params ) do
93- github_issue
94- |> GithubIssue . update_changeset ( params )
95- |> Repo . update
96- end
97-
98- defp to_params ( attrs , % GithubRepo { id: github_repo_id } , % GithubPullRequest { id: github_pull_request_id } ) do
99- attrs
100- |> Adapters.Issue . to_issue ( )
101- |> Map . put ( :github_repo_id , github_repo_id )
102- |> Map . put ( :github_pull_request_id , github_pull_request_id )
103- end
104- defp to_params ( attrs , % GithubRepo { id: github_repo_id } , _ ) do
105- attrs
106- |> Adapters.Issue . to_issue ( )
107- |> Map . put ( :github_repo_id , github_repo_id )
108- end
10954end
0 commit comments