From d2fcc4aaddb130ee02e9ebeba031a1b4a1e641c3 Mon Sep 17 00:00:00 2001 From: Josh Smith Date: Mon, 6 Nov 2017 14:18:37 -0800 Subject: [PATCH] Fix installation_repositories event to pull in account data --- .../github/adapters/app_installation.ex | 23 ++++++++++++ .../event/installation/changeset_builder.ex | 8 ++--- .../installation_repositories.ex | 13 +++++-- .../installation_repositories_test.exs | 36 ++++++++++++++++--- 4 files changed, 70 insertions(+), 10 deletions(-) diff --git a/lib/code_corps/github/adapters/app_installation.ex b/lib/code_corps/github/adapters/app_installation.ex index 571055f93..5c0d19a7f 100644 --- a/lib/code_corps/github/adapters/app_installation.ex +++ b/lib/code_corps/github/adapters/app_installation.ex @@ -4,6 +4,11 @@ defmodule CodeCorps.GitHub.Adapters.AppInstallation do `GithubAppInstallation`. """ + alias CodeCorps.{ + Adapter.MapTransformer, + GithubAppInstallation + } + @installation_event_mapping [ {:github_account_avatar_url, ["installation", "account", "avatar_url"]}, {:github_account_id, ["installation", "account", "id"]}, @@ -22,4 +27,22 @@ defmodule CodeCorps.GitHub.Adapters.AppInstallation do payload |> CodeCorps.Adapter.MapTransformer.transform(@installation_event_mapping) end + + @github_app_installation_to_repo_mapping [ + {:github_account_avatar_url, [:github_account_avatar_url]}, + {:github_account_id, [:github_account_id]}, + {:github_account_login, [:github_account_login]}, + {:github_account_type, [:github_account_type]} + ] + + @doc ~S""" + Converts a `GithubAppInstallation` record attributes into a map of attributes + that can be used for a `GithubRepo` record. + """ + @spec to_github_repo_attrs(GithubAppInstallation.t) :: map + def to_github_repo_attrs(%GithubAppInstallation{} = installation) do + installation + |> Map.from_struct + |> MapTransformer.transform(@github_app_installation_to_repo_mapping) + end end diff --git a/lib/code_corps/github/event/installation/changeset_builder.ex b/lib/code_corps/github/event/installation/changeset_builder.ex index 2f1b4a4be..d152deb78 100644 --- a/lib/code_corps/github/event/installation/changeset_builder.ex +++ b/lib/code_corps/github/event/installation/changeset_builder.ex @@ -25,7 +25,7 @@ defmodule CodeCorps.GitHub.Event.Installation.ChangesetBuilder do github_app_installation |> Changeset.change(attrs) |> Changeset.put_change(:installed, true) - |> inferr_origin() + |> infer_origin() |> Changeset.unique_constraint(:github_id, name: :github_app_installations_github_id_index) end @@ -47,12 +47,12 @@ defmodule CodeCorps.GitHub.Event.Installation.ChangesetBuilder do |> Changeset.assoc_constraint(:user) end - @spec inferr_origin(Changeset.t) :: Changeset.t - defp inferr_origin(%Changeset{ + @spec infer_origin(Changeset.t) :: Changeset.t + defp infer_origin(%Changeset{ data: %GithubAppInstallation{id: nil}} = changeset) do changeset |> Changeset.put_change(:origin, "github") end - defp inferr_origin(%Changeset{} = changeset), do: changeset + defp infer_origin(%Changeset{} = changeset), do: changeset end diff --git a/lib/code_corps/github/event/installation_repositories/installation_repositories.ex b/lib/code_corps/github/event/installation_repositories/installation_repositories.ex index 9e4fcd62f..ff191c2f1 100644 --- a/lib/code_corps/github/event/installation_repositories/installation_repositories.ex +++ b/lib/code_corps/github/event/installation_repositories/installation_repositories.ex @@ -16,6 +16,7 @@ defmodule CodeCorps.GitHub.Event.InstallationRepositories do Repo } + alias CodeCorps.GitHub.Adapters.AppInstallation, as: AppInstallationAdapter alias Ecto.{Changeset, Multi} @type outcome :: {:ok, list(GithubRepo.t)} | @@ -91,11 +92,19 @@ defmodule CodeCorps.GitHub.Event.InstallationRepositories do end @spec find_or_create(GithubAppInstallation.t, map) :: {:ok, GithubRepo.t} | {:error, Changeset.t} - defp find_or_create(%GithubAppInstallation{} = installation, %{"id" => github_id, "name" => name} = attrs) do + defp find_or_create(%GithubAppInstallation{} = installation, %{"id" => id, "name" => name} = attrs) do case find_repo(installation, attrs) do nil -> + installation_repo_attributes = + installation + |> AppInstallationAdapter.to_github_repo_attrs() + + params = + %{github_id: id, name: name} + |> Map.merge(installation_repo_attributes) + %GithubRepo{} - |> Changeset.change(%{github_id: github_id, name: name}) + |> Changeset.change(params) |> Changeset.put_assoc(:github_app_installation, installation) |> Repo.insert() %GithubRepo{} = github_repo -> diff --git a/test/lib/code_corps/github/event/installation_repositories/installation_repositories_test.exs b/test/lib/code_corps/github/event/installation_repositories/installation_repositories_test.exs index 172526933..cd21e12b1 100644 --- a/test/lib/code_corps/github/event/installation_repositories/installation_repositories_test.exs +++ b/test/lib/code_corps/github/event/installation_repositories/installation_repositories_test.exs @@ -32,32 +32,56 @@ defmodule CodeCorps.GitHub.Event.InstallationRepositoriesTest do test "creates repos" do %{ - "installation" => %{"id" => installation_github_id}, + "installation" => %{ + "account" => %{ + "avatar_url" => installation_account_avatar_url, + "id" => installation_account_id, + "login" => installation_account_login, + "type" => installation_account_type + }, + "id" => installation_github_id + }, "repositories_added" => [repo_1_payload, repo_2_payload] } = @payload - %{id: installation_id} = insert(:github_app_installation, github_id: installation_github_id) + %{id: installation_id} = insert(:github_app_installation, github_account_avatar_url: installation_account_avatar_url, github_account_id: installation_account_id, github_account_login: installation_account_login, github_account_type: installation_account_type, github_id: installation_github_id) {:ok, [%GithubRepo{}, %GithubRepo{}]} = InstallationRepositories.handle(@payload) github_repo_1 = Repo.get_by(GithubRepo, github_id: repo_1_payload["id"]) assert github_repo_1 assert github_repo_1.name == repo_1_payload["name"] + assert github_repo_1.github_account_avatar_url == installation_account_avatar_url + assert github_repo_1.github_account_id == installation_account_id + assert github_repo_1.github_account_login == installation_account_login + assert github_repo_1.github_account_type == installation_account_type assert github_repo_1.github_app_installation_id == installation_id github_repo_2 = Repo.get_by(GithubRepo, github_id: repo_2_payload["id"]) assert github_repo_2 assert github_repo_2.name == repo_2_payload["name"] + assert github_repo_2.github_account_avatar_url == installation_account_avatar_url + assert github_repo_2.github_account_id == installation_account_id + assert github_repo_2.github_account_login == installation_account_login + assert github_repo_2.github_account_type == installation_account_type assert github_repo_2.github_app_installation_id == installation_id end test "skips creating existing repos" do %{ - "installation" => %{"id" => installation_github_id}, + "installation" => %{ + "account" => %{ + "avatar_url" => installation_account_avatar_url, + "id" => installation_account_id, + "login" => installation_account_login, + "type" => installation_account_type + }, + "id" => installation_github_id + }, "repositories_added" => [repo_1_payload, repo_2_payload] } = @payload - installation = insert(:github_app_installation, github_id: installation_github_id) + installation = insert(:github_app_installation, github_account_avatar_url: installation_account_avatar_url, github_account_id: installation_account_id, github_account_login: installation_account_login, github_account_type: installation_account_type, github_id: installation_github_id) preinserted_repo = insert(:github_repo, github_app_installation: installation, github_id: repo_1_payload["id"]) {:ok, [%GithubRepo{}, %GithubRepo{}]} = InstallationRepositories.handle(@payload) @@ -68,6 +92,10 @@ defmodule CodeCorps.GitHub.Event.InstallationRepositoriesTest do github_repo_2 = Repo.get_by(GithubRepo, github_id: repo_2_payload["id"]) assert github_repo_2 assert github_repo_2.name == repo_2_payload["name"] + assert github_repo_2.github_account_avatar_url == installation_account_avatar_url + assert github_repo_2.github_account_id == installation_account_id + assert github_repo_2.github_account_login == installation_account_login + assert github_repo_2.github_account_type == installation_account_type assert github_repo_2.github_app_installation_id == installation.id assert Repo.aggregate(GithubRepo, :count, :id) == 2