Skip to content

Commit

Permalink
Add task to inbox task list when created from GitHub
Browse files Browse the repository at this point in the history
Fix tests by adding in inbox task list
  • Loading branch information
joshsmith committed Sep 4, 2017
1 parent 32ce8ef commit aa66677
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 5 deletions.
32 changes: 30 additions & 2 deletions lib/code_corps/github/event/issues/changeset_builder.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,57 @@ defmodule CodeCorps.GitHub.Event.Issues.ChangesetBuilder do
"""

alias CodeCorps.{
Services.MarkdownRendererService,
ProjectGithubRepo,
Repo,
Services.MarkdownRendererService,
Task,
TaskList,
User
}
alias CodeCorps.GitHub.Adapters.Task, as: TaskAdapter
alias Ecto.Changeset

import Ecto.Query, only: [where: 3]

@doc ~S"""
Constructs a changeset for syncing a task when processing an Issues webhook
"""
@spec build_changeset(Task.t, map, ProjectGithubRepo.t, User.t) :: Changeset.t
def build_changeset(
%Task{} = task,
%Task{id: task_id} = task,
%{"issue" => issue_attrs},
%ProjectGithubRepo{project_id: project_id},
%User{id: user_id}) do

case is_nil(task_id) do
true -> create_changeset(task, issue_attrs, project_id, user_id)
false -> update_changeset(task, issue_attrs)
end
end

defp create_changeset(%Task{} = task, issue_attrs, project_id, user_id) do
%TaskList{id: task_list_id} =
TaskList
|> where([l], l.project_id == ^project_id)
|> where([l], l.inbox == true)
|> Repo.one

task
|> Changeset.change(issue_attrs |> TaskAdapter.from_issue())
|> MarkdownRendererService.render_markdown_to_html(:markdown, :body)
|> Changeset.put_change(:project_id, project_id)
|> Changeset.put_change(:task_list_id, task_list_id)
|> Changeset.put_change(:user_id, user_id)
|> Changeset.validate_required([:project_id, :task_list_id, :user_id, :markdown, :body, :title])
|> Changeset.assoc_constraint(:project)
|> Changeset.assoc_constraint(:task_list)
|> Changeset.assoc_constraint(:user)
end

defp update_changeset(%Task{} = task, issue_attrs) do
task
|> Changeset.change(issue_attrs |> TaskAdapter.from_issue())
|> MarkdownRendererService.render_markdown_to_html(:markdown, :body)
|> Changeset.validate_required([:project_id, :user_id, :markdown, :body, :title])
|> Changeset.assoc_constraint(:project)
|> Changeset.assoc_constraint(:user)
Expand Down
4 changes: 2 additions & 2 deletions priv/repo/structure.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
-- PostgreSQL database dump
--

-- Dumped from database version 9.5.1
-- Dumped by pg_dump version 9.5.1
-- Dumped from database version 9.5.4
-- Dumped by pg_dump version 9.5.4

SET statement_timeout = 0;
SET lock_timeout = 0;
Expand Down
21 changes: 21 additions & 0 deletions test/lib/code_corps/github/event/issue_comment_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ defmodule CodeCorps.GitHub.Event.IssueCommentTest do
alias CodeCorps.{
Comment,
GitHub.Event.IssueComment,
Project,
Task,
Repo,
User
Expand Down Expand Up @@ -56,6 +57,11 @@ defmodule CodeCorps.GitHub.Event.IssueCommentTest do
insert_list(3, :project_github_repo, github_repo: github_repo)
|> Enum.map(&Map.get(&1, :project_id))

project_ids |> Enum.each(fn project_id ->
project = Project |> Repo.get_by(id: project_id)
insert(:task_list, project: project, inbox: true)
end)

{:ok, comments} = IssueComment.handle(@event, @payload)

assert Enum.count(comments) == 3
Expand Down Expand Up @@ -124,6 +130,11 @@ defmodule CodeCorps.GitHub.Event.IssueCommentTest do

project_ids = project_github_repos |> Enum.map(&Map.get(&1, :project_id))

project_ids |> Enum.each(fn project_id ->
project = Project |> Repo.get_by(id: project_id)
insert(:task_list, project: project, inbox: true)
end)

# there's a task for project 1
task_1 = insert(:task, project: project_1, user: issue_user, github_id: issue_github_id)

Expand Down Expand Up @@ -201,6 +212,11 @@ defmodule CodeCorps.GitHub.Event.IssueCommentTest do

project_ids = project_github_repos |> Enum.map(&Map.get(&1, :project_id))

project_ids |> Enum.each(fn project_id ->
project = Project |> Repo.get_by(id: project_id)
insert(:task_list, project: project, inbox: true)
end)

{:ok, comments} = IssueComment.handle(@event, @payload)

assert Enum.count(comments) == 3
Expand Down Expand Up @@ -271,6 +287,11 @@ defmodule CodeCorps.GitHub.Event.IssueCommentTest do

project_ids = project_github_repos |> Enum.map(&Map.get(&1, :project_id))

project_ids |> Enum.each(fn project_id ->
project = Project |> Repo.get_by(id: project_id)
insert(:task_list, project: project, inbox: true)
end)

# there's a task and comment for project 1
task_1 = insert(:task, project: project_1, user: issue_user, github_id: issue_github_id)
comment_1 = insert(:comment, task: task_1, user: comment_user, github_id: comment_github_id)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ defmodule CodeCorps.GitHub.Event.Issues.ChangesetBuilderTest do
test "assigns proper changes to the task" do
payload = load_event_fixture("issues_opened")
task = %Task{}
project_github_repo = insert(:project_github_repo)
project = insert(:project)
project_github_repo = insert(:project_github_repo, project: project)
user = insert(:user)
task_list = insert(:task_list, project: project, inbox: true)

changeset = ChangesetBuilder.build_changeset(
task, payload, project_github_repo, user
Expand All @@ -34,6 +36,7 @@ defmodule CodeCorps.GitHub.Event.Issues.ChangesetBuilderTest do

# relationships are proper
assert get_change(changeset, :project_id) == project_github_repo.project_id
assert get_change(changeset, :task_list_id) == task_list.id
assert get_change(changeset, :user_id) == user.id

assert changeset.valid?
Expand Down
7 changes: 7 additions & 0 deletions test/lib/code_corps/github/event/issues/task_syncer_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ defmodule CodeCorps.GitHub.Event.Issues.TaskSyncerTest do

alias CodeCorps.{
GitHub.Event.Issues.TaskSyncer,
Project,
Repo,
Task
}
Expand All @@ -24,6 +25,12 @@ defmodule CodeCorps.GitHub.Event.Issues.TaskSyncerTest do

task_1 = insert(:task, project: project_1, user: user, github_id: issue_github_id)

project_ids = project_github_repos |> Enum.map(&Map.get(&1, :project_id))

project_ids |> Enum.each(fn project_id ->
project = Project |> Repo.get_by(id: project_id)
insert(:task_list, project: project, inbox: true)
end)

{:ok, tasks} = %{github_repo | project_github_repos: project_github_repos}
|> TaskSyncer.sync_all(user, @payload)
Expand Down
41 changes: 41 additions & 0 deletions test/lib/code_corps/github/event/issues_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ defmodule CodeCorps.GitHub.Event.IssuesTest do

alias CodeCorps.{
GitHub.Event.Issues,
Project,
Repo,
Task,
User
Expand Down Expand Up @@ -52,6 +53,11 @@ defmodule CodeCorps.GitHub.Event.IssuesTest do
|> Enum.map(&Map.get(&1, :project))
|> Enum.map(&Map.get(&1, :id))

project_ids |> Enum.each(fn project_id ->
project = Project |> Repo.get_by(id: project_id)
insert(:task_list, project: project, inbox: true)
end)

{:ok, tasks} = Issues.handle(@event, @payload)

assert Enum.count(tasks) == 3
Expand Down Expand Up @@ -101,6 +107,11 @@ defmodule CodeCorps.GitHub.Event.IssuesTest do
|> Enum.map(&Map.get(&1, :project))
|> Enum.map(&Map.get(&1, :id))

project_ids |> Enum.each(fn project_id ->
project = Project |> Repo.get_by(id: project_id)
insert(:task_list, project: project, inbox: true)
end)

%{id: existing_task_id} = insert(:task, project: project, user: user, github_id: github_id)

{:ok, tasks} = Issues.handle(@event, @payload)
Expand Down Expand Up @@ -170,6 +181,11 @@ defmodule CodeCorps.GitHub.Event.IssuesTest do
|> Enum.map(&Map.get(&1, :project))
|> Enum.map(&Map.get(&1, :id))

project_ids |> Enum.each(fn project_id ->
project = Project |> Repo.get_by(id: project_id)
insert(:task_list, project: project, inbox: true)
end)

{:ok, tasks} = Issues.handle(@event, @payload)

assert Enum.count(tasks) == 3
Expand Down Expand Up @@ -219,6 +235,11 @@ defmodule CodeCorps.GitHub.Event.IssuesTest do
|> Enum.map(&Map.get(&1, :project))
|> Enum.map(&Map.get(&1, :id))

project_ids |> Enum.each(fn project_id ->
project = Project |> Repo.get_by(id: project_id)
insert(:task_list, project: project, inbox: true)
end)

%{id: existing_task_id} = insert(:task, project: project, user: user, github_id: github_id)

{:ok, tasks} = Issues.handle(@event, @payload)
Expand Down Expand Up @@ -288,6 +309,11 @@ defmodule CodeCorps.GitHub.Event.IssuesTest do
|> Enum.map(&Map.get(&1, :project))
|> Enum.map(&Map.get(&1, :id))

project_ids |> Enum.each(fn project_id ->
project = Project |> Repo.get_by(id: project_id)
insert(:task_list, project: project, inbox: true)
end)

{:ok, tasks} = Issues.handle(@event, @payload)

assert Enum.count(tasks) == 3
Expand Down Expand Up @@ -337,6 +363,11 @@ defmodule CodeCorps.GitHub.Event.IssuesTest do
|> Enum.map(&Map.get(&1, :project))
|> Enum.map(&Map.get(&1, :id))

project_ids |> Enum.each(fn project_id ->
project = Project |> Repo.get_by(id: project_id)
insert(:task_list, project: project, inbox: true)
end)

%{id: existing_task_id} = insert(:task, project: project, user: user, github_id: github_id)

{:ok, tasks} = Issues.handle(@event, @payload)
Expand Down Expand Up @@ -406,6 +437,11 @@ defmodule CodeCorps.GitHub.Event.IssuesTest do
|> Enum.map(&Map.get(&1, :project))
|> Enum.map(&Map.get(&1, :id))

project_ids |> Enum.each(fn project_id ->
project = Project |> Repo.get_by(id: project_id)
insert(:task_list, project: project, inbox: true)
end)

{:ok, tasks} = Issues.handle(@event, @payload)

assert Enum.count(tasks) == 3
Expand Down Expand Up @@ -455,6 +491,11 @@ defmodule CodeCorps.GitHub.Event.IssuesTest do
|> Enum.map(&Map.get(&1, :project))
|> Enum.map(&Map.get(&1, :id))

project_ids |> Enum.each(fn project_id ->
project = Project |> Repo.get_by(id: project_id)
insert(:task_list, project: project, inbox: true)
end)

%{id: existing_task_id} = insert(:task, project: project, user: user, github_id: github_id)

{:ok, tasks} = Issues.handle(@event, @payload)
Expand Down

0 comments on commit aa66677

Please sign in to comment.