Skip to content

Commit

Permalink
Roll task adapter into issue adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
joshsmith committed Oct 17, 2017
1 parent 6893c00 commit 0f2bd08
Show file tree
Hide file tree
Showing 10 changed files with 92 additions and 140 deletions.
49 changes: 38 additions & 11 deletions lib/code_corps/github/adapters/issue.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ defmodule CodeCorps.GitHub.Adapters.Issue do

alias CodeCorps.{
Adapter.MapTransformer,
GithubIssue
GitHub.Adapters.Utils.BodyDecorator,
GithubIssue,
Task
}

@mapping [
@issue_mapping [
{:body, ["body"]},
{:closed_at, ["closed_at"]},
{:comments_url, ["comments_url"]},
Expand All @@ -27,25 +29,50 @@ defmodule CodeCorps.GitHub.Adapters.Issue do
]

@doc ~S"""
Converts a GitHub Issue payload into a set of attributes used to update or
create a `GithubIssue` record.
Converts a GitHub Issue payload into a set of attributes used to create or
update a `GithubIssue` record.
"""
@spec from_api(map) :: map
def from_api(%{} = payload) do
payload |> MapTransformer.transform(@mapping)
@spec to_issue(map) :: map
def to_issue(%{} = payload) do
payload |> MapTransformer.transform(@issue_mapping)
end

@task_mapping [
{:created_at, ["created_at"]},
{:markdown, ["body"]},
{:modified_at, ["updated_at"]},
{:status, ["state"]},
{:title, ["title"]}
]

@doc ~S"""
Converts a GitHub Issue payload into a set of attributes used to create or
update a `Task` record.
"""
@spec to_task(map) :: map
def to_task(%{} = payload) do
payload |> MapTransformer.transform(@task_mapping)
end

@autogenerated_github_keys ~w(closed_at comments_url created_at events_url html_url id labels_url number updated_at url)

@doc ~S"""
Converts a `GithubIssue` into a set of attributes used to update or create an
associated GitHub Issue.
Converts a `GithubIssue` or `Task` into a set of attributes used to create or
update an associated GitHub Issue on the GitHub API.
"""
@spec to_api(GithubIssue.t) :: map
@spec to_api(GithubIssue.t | Task.t) :: map
def to_api(%GithubIssue{} = github_issue) do
github_issue
|> Map.from_struct
|> MapTransformer.transform_inverse(@mapping)
|> MapTransformer.transform_inverse(@issue_mapping)
|> Map.drop(@autogenerated_github_keys)
|> BodyDecorator.add_code_corps_header(github_issue)
end
def to_api(%Task{} = task) do
task
|> Map.from_struct
|> MapTransformer.transform_inverse(@task_mapping)
|> Map.drop(@autogenerated_github_keys)
|> BodyDecorator.add_code_corps_header(task)
end
end
47 changes: 0 additions & 47 deletions lib/code_corps/github/adapters/task.ex

This file was deleted.

6 changes: 3 additions & 3 deletions lib/code_corps/github/event/issues/changeset_builder.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ defmodule CodeCorps.GitHub.Event.Issues.ChangesetBuilder do
User,
Validators.TimeValidator
}
alias CodeCorps.GitHub.Adapters.Task, as: TaskAdapter
alias CodeCorps.GitHub.Adapters.Issue, as: IssueAdapter
alias Ecto.Changeset

@doc ~S"""
Expand Down Expand Up @@ -50,7 +50,7 @@ defmodule CodeCorps.GitHub.Event.Issues.ChangesetBuilder do
TaskList |> Repo.get_by(project_id: project_id, inbox: true)

task
|> Changeset.cast(TaskAdapter.from_api(issue_attrs), @create_attrs)
|> Changeset.cast(IssueAdapter.to_task(issue_attrs), @create_attrs)
|> MarkdownRendererService.render_markdown_to_html(:markdown, :body)
|> Changeset.put_change(:created_from, "github")
|> Changeset.put_change(:modified_from, "github")
Expand All @@ -71,7 +71,7 @@ defmodule CodeCorps.GitHub.Event.Issues.ChangesetBuilder do
@spec update_changeset(Task.t, map) :: Changeset.t
defp update_changeset(%Task{} = task, %{} = issue_attrs) do
task
|> Changeset.cast(TaskAdapter.from_api(issue_attrs), @update_attrs)
|> Changeset.cast(IssueAdapter.to_task(issue_attrs), @update_attrs)
|> MarkdownRendererService.render_markdown_to_html(:markdown, :body)
|> Changeset.put_change(:modified_from, "github")
|> TimeValidator.validate_time_after(:modified_at)
Expand Down
2 changes: 1 addition & 1 deletion lib/code_corps/github/event/issues/issue_linker.ex
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ defmodule CodeCorps.GitHub.Event.Issues.IssueLinker do
"""
@spec create_or_update_issue(GithubRepo.t, map) :: linking_result
def create_or_update_issue(%GithubRepo{} = github_repo, %{"id" => github_issue_id} = attrs) do
params = IssueAdapter.from_api(attrs)
params = IssueAdapter.to_issue(attrs)

case Repo.get_by(GithubIssue, github_id: github_issue_id) do
nil -> create_issue(github_repo, params)
Expand Down
6 changes: 3 additions & 3 deletions lib/code_corps/github/event/pull_request/changeset_builder.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ defmodule CodeCorps.GitHub.Event.PullRequest.ChangesetBuilder do
User,
Validators.TimeValidator
}
alias CodeCorps.GitHub.Adapters.Task, as: TaskAdapter
alias CodeCorps.GitHub.Adapters.Issue, as: IssueAdapter
alias Ecto.Changeset

@doc ~S"""
Expand Down Expand Up @@ -50,7 +50,7 @@ defmodule CodeCorps.GitHub.Event.PullRequest.ChangesetBuilder do
TaskList |> Repo.get_by(project_id: project_id, inbox: true)

task
|> Changeset.cast(TaskAdapter.from_api(pull_request_attrs), @create_attrs)
|> Changeset.cast(IssueAdapter.to_task(pull_request_attrs), @create_attrs)
|> MarkdownRendererService.render_markdown_to_html(:markdown, :body)
|> Changeset.put_change(:created_from, "github")
|> Changeset.put_change(:modified_from, "github")
Expand All @@ -71,7 +71,7 @@ defmodule CodeCorps.GitHub.Event.PullRequest.ChangesetBuilder do
@spec update_changeset(Task.t, map) :: Changeset.t
defp update_changeset(%Task{} = task, %{} = pull_request_attrs) do
task
|> Changeset.cast(TaskAdapter.from_api(pull_request_attrs), @update_attrs)
|> Changeset.cast(IssueAdapter.to_task(pull_request_attrs), @update_attrs)
|> MarkdownRendererService.render_markdown_to_html(:markdown, :body)
|> Changeset.put_change(:modified_from, "github")
|> TimeValidator.validate_time_after(:modified_at)
Expand Down
4 changes: 2 additions & 2 deletions lib/code_corps/github/issue/issue.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ defmodule CodeCorps.GitHub.Issue do
} = task) do

endpoint = github_repo |> get_endpoint()
attrs = task |> GitHub.Adapters.Task.to_api
attrs = task |> GitHub.Adapters.Issue.to_api

with opts when is_list(opts) <- opts_for(user, installation) do
GitHub.request(:post, endpoint, %{}, attrs, opts)
Expand All @@ -33,7 +33,7 @@ defmodule CodeCorps.GitHub.Issue do
} = task) do

endpoint = "#{github_repo |> get_endpoint()}/#{number}"
attrs = task |> GitHub.Adapters.Task.to_api
attrs = task |> GitHub.Adapters.Issue.to_api

with opts when is_list(opts) <- opts_for(user, installation) do
GitHub.request(:patch, endpoint, %{}, attrs, opts)
Expand Down
42 changes: 38 additions & 4 deletions test/lib/code_corps/github/adapters/issue_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ defmodule CodeCorps.GitHub.Adapters.IssueTest do

import CodeCorps.GitHub.TestHelpers

alias CodeCorps.{GitHub.Adapters, GithubIssue}
alias CodeCorps.{GitHub.Adapters, GithubIssue, Task}

describe "from_api/1" do
describe "to_issue/1" do
test "maps api payload correctly" do
%{"issue" => payload} = load_event_fixture("issues_opened")

assert Adapters.Issue.from_api(payload) == %{
assert Adapters.Issue.to_issue(payload) == %{
body: payload["body"],
closed_at: payload["closed_at"],
comments_url: payload["comments_url"],
Expand All @@ -30,8 +30,22 @@ defmodule CodeCorps.GitHub.Adapters.IssueTest do
end
end

describe "to_task/1" do
test "maps api payload correctly" do
%{"issue" => payload} = load_event_fixture("issues_opened")

assert Adapters.Issue.to_task(payload) == %{
created_at: payload["created_at"],
markdown: payload["body"],
modified_at: payload["updated_at"],
status: payload["state"],
title: payload["title"]
}
end
end

describe "to_api/1" do
test "maps Issue correctly" do
test "maps GithubIssue correctly" do
payload =
%GithubIssue{body: "bar", locked: false, number: 5, state: "open", title: "Foo"}
|> Adapters.Issue.to_api
Expand All @@ -51,5 +65,25 @@ defmodule CodeCorps.GitHub.Adapters.IssueTest do
refute payload["updated_at"]
refute payload["url"]
end

test "maps Task correctly" do
payload =
%Task{created_at: DateTime.utc_now, markdown: "bar", modified_at: DateTime.utc_now, status: "open", title: "Foo"}
|> Adapters.Issue.to_api

assert payload["body"] == "bar"
assert payload["state"] == "open"
assert payload["title"] == "Foo"
refute payload["closed_at"]
refute payload["comments_url"]
refute payload["created_at"]
refute payload["events_url"]
refute payload["html_url"]
refute payload["id"]
refute payload["labels_url"]
refute payload["number"]
refute payload["updated_at"]
refute payload["url"]
end
end
end
62 changes: 0 additions & 62 deletions test/lib/code_corps/github/adapters/task_test.exs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ defmodule CodeCorps.GitHub.Event.Issues.IssueLinkerTest do

created_attributes =
attrs
|> IssueAdapter.from_api
|> IssueAdapter.to_issue
|> Map.delete(:closed_at)
|> Map.delete(:repository_url)
returned_issue = Repo.get_by(GithubIssue, created_attributes)
Expand Down
12 changes: 6 additions & 6 deletions test/lib/code_corps/github/issue/issue_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ defmodule CodeCorps.GitHub.IssueTest do
_options
})

assert body == Adapters.Task.to_api(task) |> Poison.encode!
assert body == Adapters.Issue.to_api(task) |> Poison.encode!
end

test "calls github API to create an issue for assigned task, makes integration request if user is not connected, returns response" do
Expand All @@ -50,7 +50,7 @@ defmodule CodeCorps.GitHub.IssueTest do
_options
})

assert body == Adapters.Task.to_api(task) |> Poison.encode!
assert body == Adapters.Issue.to_api(task) |> Poison.encode!
end

test "returns error response if there was trouble" do
Expand All @@ -73,7 +73,7 @@ defmodule CodeCorps.GitHub.IssueTest do
_options
})

assert body == Adapters.Task.to_api(task) |> Poison.encode!
assert body == Adapters.Issue.to_api(task) |> Poison.encode!
end
end

Expand All @@ -97,7 +97,7 @@ defmodule CodeCorps.GitHub.IssueTest do
_options
})

assert body == Adapters.Task.to_api(task) |> Poison.encode!
assert body == Adapters.Issue.to_api(task) |> Poison.encode!
end

test "calls github API to create an issue for assigned task, makes integration request if user is not connected, returns response" do
Expand All @@ -119,7 +119,7 @@ defmodule CodeCorps.GitHub.IssueTest do
_options
})

assert body == Adapters.Task.to_api(task) |> Poison.encode!
assert body == Adapters.Issue.to_api(task) |> Poison.encode!
end

test "returns error response if there was trouble" do
Expand All @@ -143,7 +143,7 @@ defmodule CodeCorps.GitHub.IssueTest do
_options
})

assert body == Adapters.Task.to_api(task) |> Poison.encode!
assert body == Adapters.Issue.to_api(task) |> Poison.encode!
end
end
end

0 comments on commit 0f2bd08

Please sign in to comment.