Skip to content

Commit 96594fc

Browse files
joshsmithbegedin
authored andcommitted
Find or create GithubRepo records
1 parent bf84d4e commit 96594fc

File tree

1 file changed

+13
-3
lines changed
  • lib/code_corps/github/event/installation

1 file changed

+13
-3
lines changed

lib/code_corps/github/event/installation/repos.ex

+13-3
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,16 @@ defmodule CodeCorps.GitHub.Event.Installation.Repos do
9191
%{github_id: github_id} = repo_attributes) do
9292

9393
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
94102
nil -> create(installation, repo_attributes)
95-
%GithubRepo{} = github_repo -> github_repo |> update(repo_attributes)
103+
%GithubRepo{} = github_repo -> update(github_repo, installation, repo_attributes)
96104
end
97105
end
98106

@@ -104,10 +112,12 @@ defmodule CodeCorps.GitHub.Event.Installation.Repos do
104112
|> Repo.insert()
105113
end
106114

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
109117
github_repo
118+
|> Repo.preload([:github_app_installation])
110119
|> changeset(repo_attributes)
120+
|> Changeset.put_assoc(:github_app_installation, installation)
111121
|> Repo.update()
112122
end
113123

0 commit comments

Comments
 (0)