@@ -91,8 +91,16 @@ defmodule CodeCorps.GitHub.Event.Installation.Repos do
91
91
% { github_id: github_id } = repo_attributes ) do
92
92
93
93
case github_repos |> Enum . find ( fn % GithubRepo { } = gr -> gr . github_id == github_id end ) do
94
+ nil -> create_or_update_repo ( installation , repo_attributes )
95
+ % GithubRepo { } = github_repo -> update ( github_repo , installation , repo_attributes )
96
+ end
97
+ end
98
+
99
+ @ spec create_or_update_repo ( GithubAppInstallation . t , map ) :: { :ok , GithubRepo . t }
100
+ defp create_or_update_repo ( % GithubAppInstallation { } = installation , % { github_id: github_id } = repo_attributes ) do
101
+ case Repo . get_by ( GithubRepo , github_id: github_id ) do
94
102
nil -> create ( installation , repo_attributes )
95
- % GithubRepo { } = github_repo -> github_repo |> update ( repo_attributes )
103
+ % GithubRepo { } = github_repo -> update ( github_repo , installation , repo_attributes )
96
104
end
97
105
end
98
106
@@ -104,10 +112,12 @@ defmodule CodeCorps.GitHub.Event.Installation.Repos do
104
112
|> Repo . insert ( )
105
113
end
106
114
107
- @ spec update ( GithubRepo . t , map ) :: { :ok , GithubRepo . t }
108
- defp update ( % GithubRepo { } = github_repo , % { } = repo_attributes ) do
115
+ @ spec update ( GithubRepo . t , GithubAppInstallation . t , map ) :: { :ok , GithubRepo . t }
116
+ defp update ( % GithubRepo { } = github_repo , % GithubAppInstallation { } = installation , % { } = repo_attributes ) do
109
117
github_repo
118
+ |> Repo . preload ( [ :github_app_installation ] )
110
119
|> changeset ( repo_attributes )
120
+ |> Changeset . put_assoc ( :github_app_installation , installation )
111
121
|> Repo . update ( )
112
122
end
113
123
0 commit comments