Skip to content

Commit d2fcc4a

Browse files
committed
Fix installation_repositories event to pull in account data
1 parent f869409 commit d2fcc4a

File tree

4 files changed

+70
-10
lines changed

4 files changed

+70
-10
lines changed

lib/code_corps/github/adapters/app_installation.ex

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ defmodule CodeCorps.GitHub.Adapters.AppInstallation do
44
`GithubAppInstallation`.
55
"""
66

7+
alias CodeCorps.{
8+
Adapter.MapTransformer,
9+
GithubAppInstallation
10+
}
11+
712
@installation_event_mapping [
813
{:github_account_avatar_url, ["installation", "account", "avatar_url"]},
914
{:github_account_id, ["installation", "account", "id"]},
@@ -22,4 +27,22 @@ defmodule CodeCorps.GitHub.Adapters.AppInstallation do
2227
payload
2328
|> CodeCorps.Adapter.MapTransformer.transform(@installation_event_mapping)
2429
end
30+
31+
@github_app_installation_to_repo_mapping [
32+
{:github_account_avatar_url, [:github_account_avatar_url]},
33+
{:github_account_id, [:github_account_id]},
34+
{:github_account_login, [:github_account_login]},
35+
{:github_account_type, [:github_account_type]}
36+
]
37+
38+
@doc ~S"""
39+
Converts a `GithubAppInstallation` record attributes into a map of attributes
40+
that can be used for a `GithubRepo` record.
41+
"""
42+
@spec to_github_repo_attrs(GithubAppInstallation.t) :: map
43+
def to_github_repo_attrs(%GithubAppInstallation{} = installation) do
44+
installation
45+
|> Map.from_struct
46+
|> MapTransformer.transform(@github_app_installation_to_repo_mapping)
47+
end
2548
end

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ defmodule CodeCorps.GitHub.Event.Installation.ChangesetBuilder do
2525
github_app_installation
2626
|> Changeset.change(attrs)
2727
|> Changeset.put_change(:installed, true)
28-
|> inferr_origin()
28+
|> infer_origin()
2929
|> Changeset.unique_constraint(:github_id, name: :github_app_installations_github_id_index)
3030
end
3131

@@ -47,12 +47,12 @@ defmodule CodeCorps.GitHub.Event.Installation.ChangesetBuilder do
4747
|> Changeset.assoc_constraint(:user)
4848
end
4949

50-
@spec inferr_origin(Changeset.t) :: Changeset.t
51-
defp inferr_origin(%Changeset{
50+
@spec infer_origin(Changeset.t) :: Changeset.t
51+
defp infer_origin(%Changeset{
5252
data: %GithubAppInstallation{id: nil}} = changeset) do
5353

5454
changeset
5555
|> Changeset.put_change(:origin, "github")
5656
end
57-
defp inferr_origin(%Changeset{} = changeset), do: changeset
57+
defp infer_origin(%Changeset{} = changeset), do: changeset
5858
end

lib/code_corps/github/event/installation_repositories/installation_repositories.ex

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ defmodule CodeCorps.GitHub.Event.InstallationRepositories do
1616
Repo
1717
}
1818

19+
alias CodeCorps.GitHub.Adapters.AppInstallation, as: AppInstallationAdapter
1920
alias Ecto.{Changeset, Multi}
2021

2122
@type outcome :: {:ok, list(GithubRepo.t)} |
@@ -91,11 +92,19 @@ defmodule CodeCorps.GitHub.Event.InstallationRepositories do
9192
end
9293

9394
@spec find_or_create(GithubAppInstallation.t, map) :: {:ok, GithubRepo.t} | {:error, Changeset.t}
94-
defp find_or_create(%GithubAppInstallation{} = installation, %{"id" => github_id, "name" => name} = attrs) do
95+
defp find_or_create(%GithubAppInstallation{} = installation, %{"id" => id, "name" => name} = attrs) do
9596
case find_repo(installation, attrs) do
9697
nil ->
98+
installation_repo_attributes =
99+
installation
100+
|> AppInstallationAdapter.to_github_repo_attrs()
101+
102+
params =
103+
%{github_id: id, name: name}
104+
|> Map.merge(installation_repo_attributes)
105+
97106
%GithubRepo{}
98-
|> Changeset.change(%{github_id: github_id, name: name})
107+
|> Changeset.change(params)
99108
|> Changeset.put_assoc(:github_app_installation, installation)
100109
|> Repo.insert()
101110
%GithubRepo{} = github_repo ->

test/lib/code_corps/github/event/installation_repositories/installation_repositories_test.exs

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,32 +32,56 @@ defmodule CodeCorps.GitHub.Event.InstallationRepositoriesTest do
3232

3333
test "creates repos" do
3434
%{
35-
"installation" => %{"id" => installation_github_id},
35+
"installation" => %{
36+
"account" => %{
37+
"avatar_url" => installation_account_avatar_url,
38+
"id" => installation_account_id,
39+
"login" => installation_account_login,
40+
"type" => installation_account_type
41+
},
42+
"id" => installation_github_id
43+
},
3644
"repositories_added" => [repo_1_payload, repo_2_payload]
3745
} = @payload
3846

39-
%{id: installation_id} = insert(:github_app_installation, github_id: installation_github_id)
47+
%{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)
4048

4149
{:ok, [%GithubRepo{}, %GithubRepo{}]} = InstallationRepositories.handle(@payload)
4250

4351
github_repo_1 = Repo.get_by(GithubRepo, github_id: repo_1_payload["id"])
4452
assert github_repo_1
4553
assert github_repo_1.name == repo_1_payload["name"]
54+
assert github_repo_1.github_account_avatar_url == installation_account_avatar_url
55+
assert github_repo_1.github_account_id == installation_account_id
56+
assert github_repo_1.github_account_login == installation_account_login
57+
assert github_repo_1.github_account_type == installation_account_type
4658
assert github_repo_1.github_app_installation_id == installation_id
4759

4860
github_repo_2 = Repo.get_by(GithubRepo, github_id: repo_2_payload["id"])
4961
assert github_repo_2
5062
assert github_repo_2.name == repo_2_payload["name"]
63+
assert github_repo_2.github_account_avatar_url == installation_account_avatar_url
64+
assert github_repo_2.github_account_id == installation_account_id
65+
assert github_repo_2.github_account_login == installation_account_login
66+
assert github_repo_2.github_account_type == installation_account_type
5167
assert github_repo_2.github_app_installation_id == installation_id
5268
end
5369

5470
test "skips creating existing repos" do
5571
%{
56-
"installation" => %{"id" => installation_github_id},
72+
"installation" => %{
73+
"account" => %{
74+
"avatar_url" => installation_account_avatar_url,
75+
"id" => installation_account_id,
76+
"login" => installation_account_login,
77+
"type" => installation_account_type
78+
},
79+
"id" => installation_github_id
80+
},
5781
"repositories_added" => [repo_1_payload, repo_2_payload]
5882
} = @payload
5983

60-
installation = insert(:github_app_installation, github_id: installation_github_id)
84+
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)
6185
preinserted_repo = insert(:github_repo, github_app_installation: installation, github_id: repo_1_payload["id"])
6286

6387
{:ok, [%GithubRepo{}, %GithubRepo{}]} = InstallationRepositories.handle(@payload)
@@ -68,6 +92,10 @@ defmodule CodeCorps.GitHub.Event.InstallationRepositoriesTest do
6892
github_repo_2 = Repo.get_by(GithubRepo, github_id: repo_2_payload["id"])
6993
assert github_repo_2
7094
assert github_repo_2.name == repo_2_payload["name"]
95+
assert github_repo_2.github_account_avatar_url == installation_account_avatar_url
96+
assert github_repo_2.github_account_id == installation_account_id
97+
assert github_repo_2.github_account_login == installation_account_login
98+
assert github_repo_2.github_account_type == installation_account_type
7199
assert github_repo_2.github_app_installation_id == installation.id
72100

73101
assert Repo.aggregate(GithubRepo, :count, :id) == 2

0 commit comments

Comments
 (0)