Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure GitHubRepo is always find or create #1081

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions lib/code_corps/github/event/installation/repos.ex
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,16 @@ defmodule CodeCorps.GitHub.Event.Installation.Repos do
%{github_id: github_id} = repo_attributes) do

case github_repos |> Enum.find(fn %GithubRepo{} = gr -> gr.github_id == github_id end) do
nil -> create_or_update_repo(installation, repo_attributes)
%GithubRepo{} = github_repo -> update(github_repo, installation, repo_attributes)
end
end

@spec create_or_update_repo(GithubAppInstallation.t, map) :: {:ok, GithubRepo.t}
defp create_or_update_repo(%GithubAppInstallation{} = installation, %{github_id: github_id} = repo_attributes) do
case Repo.get_by(GithubRepo, github_id: github_id) do
nil -> create(installation, repo_attributes)
%GithubRepo{} = github_repo -> github_repo |> update(repo_attributes)
%GithubRepo{} = github_repo -> update(github_repo, installation, repo_attributes)
end
end

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

@spec update(GithubRepo.t, map) :: {:ok, GithubRepo.t}
defp update(%GithubRepo{} = github_repo, %{} = repo_attributes) do
@spec update(GithubRepo.t, GithubAppInstallation.t, map) :: {:ok, GithubRepo.t}
defp update(%GithubRepo{} = github_repo, %GithubAppInstallation{} = installation, %{} = repo_attributes) do
github_repo
|> Repo.preload([:github_app_installation])
|> changeset(repo_attributes)
|> Changeset.put_assoc(:github_app_installation, installation)
|> Repo.update()
end

Expand Down