-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #967 from code-corps/939-correct-user-assignment-w…
…hen-syncing-task Fix user linking of github issues and comments to account for bot user
- Loading branch information
Showing
15 changed files
with
744 additions
and
149 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,66 @@ | ||
defmodule CodeCorps.GitHub.Event.IssueComment.UserLinker do | ||
@moduledoc ~S""" | ||
In charge of finding a user to link with a Task when processing an | ||
In charge of finding a user to link with a Comment when processing an | ||
IssueComment webhook. | ||
""" | ||
|
||
import Ecto.Query | ||
|
||
alias CodeCorps.{ | ||
Accounts, | ||
Comment, | ||
Repo, | ||
User | ||
} | ||
|
||
@typep linking_result :: {:ok, User.t} | | ||
{:error, Ecto.Changeset.t} | ||
{:error, :multiple_users} | ||
|
||
@doc ~S""" | ||
Finds or creates a user using information contained in an Issues webhook | ||
payload | ||
Finds or creates a user using information contained in a GitHub IssueComment | ||
webhook payload. | ||
The process is as follows: | ||
- Find all affected comments and extract their user data. | ||
- Search for the user in our database. | ||
- If we match a single user, then the comment should be for that user. | ||
- If there are no matching users, then the comment was created on Github by | ||
someone who does not have a matching GitHub-connected Code Corps account. | ||
We create a placeholder user account until that GitHub user is claimed by | ||
a Code Corps user. | ||
created. | ||
- If there are multiple matching users, this is an unexpected scenario and | ||
should error out. | ||
""" | ||
@spec find_or_create_user(map) :: {:ok, User.t} | ||
def find_or_create_user(%{"comment" => %{"user" => user_attrs}}) do | ||
case user_attrs |> find_user() do | ||
nil -> user_attrs |> Accounts.create_from_github | ||
%User{} = user -> {:ok, user} | ||
end | ||
def find_or_create_user(%{"comment" => %{"user" => user_attrs}} = attrs) do | ||
attrs | ||
|> match_users | ||
|> marshall_response(user_attrs) | ||
end | ||
|
||
@spec match_users(map) :: list(User.t) | ||
defp match_users(%{"comment" => %{"id" => github_id}}) do | ||
query = from u in User, | ||
distinct: u.id, | ||
join: c in Comment, on: u.id == c.user_id, where: c.github_id == ^github_id | ||
|
||
query |> Repo.all | ||
end | ||
|
||
@spec marshall_response(list, map) :: linking_result | ||
defp marshall_response([%User{} = single_user], %{}), do: {:ok, single_user} | ||
defp marshall_response([], %{} = user_attrs) do | ||
user_attrs |> find_or_create_disassociated_user() | ||
end | ||
defp marshall_response([_head | _tail], %{}), do: {:error, :multiple_users} | ||
|
||
@spec find_user(map) :: User.t | nil | ||
defp find_user(%{"id" => github_id}) do | ||
User |> Repo.get_by(github_id: github_id) | ||
@spec find_or_create_disassociated_user(map) :: {:ok, User.t} | ||
def find_or_create_disassociated_user(%{"id" => github_id} = attrs) do | ||
case User |> Repo.get_by(github_id: github_id) do | ||
nil -> attrs |> Accounts.create_from_github | ||
%User{} = user -> {:ok, user} | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
defmodule CodeCorps.Repo.Migrations.AddTypeToUsers do | ||
use Ecto.Migration | ||
|
||
def change do | ||
alter table(:users) do | ||
add :type, :string, default: "user" | ||
end | ||
end | ||
end |
Oops, something went wrong.