From 0f2bd08fbd8cde0b35571f365c03ff550520d00f Mon Sep 17 00:00:00 2001 From: Josh Smith Date: Tue, 17 Oct 2017 15:11:10 -0700 Subject: [PATCH] Roll task adapter into issue adapter --- lib/code_corps/github/adapters/issue.ex | 49 +++++++++++---- lib/code_corps/github/adapters/task.ex | 47 -------------- .../github/event/issues/changeset_builder.ex | 6 +- .../github/event/issues/issue_linker.ex | 2 +- .../event/pull_request/changeset_builder.ex | 6 +- lib/code_corps/github/issue/issue.ex | 4 +- .../code_corps/github/adapters/issue_test.exs | 42 +++++++++++-- .../code_corps/github/adapters/task_test.exs | 62 ------------------- .../github/event/issues/issue_linker_test.exs | 2 +- .../code_corps/github/issue/issue_test.exs | 12 ++-- 10 files changed, 92 insertions(+), 140 deletions(-) delete mode 100644 lib/code_corps/github/adapters/task.ex delete mode 100644 test/lib/code_corps/github/adapters/task_test.exs diff --git a/lib/code_corps/github/adapters/issue.ex b/lib/code_corps/github/adapters/issue.ex index 799f37e6e..752b39ec6 100644 --- a/lib/code_corps/github/adapters/issue.ex +++ b/lib/code_corps/github/adapters/issue.ex @@ -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"]}, @@ -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 diff --git a/lib/code_corps/github/adapters/task.ex b/lib/code_corps/github/adapters/task.ex deleted file mode 100644 index 8af86c8b6..000000000 --- a/lib/code_corps/github/adapters/task.ex +++ /dev/null @@ -1,47 +0,0 @@ -defmodule CodeCorps.GitHub.Adapters.Task do - @moduledoc """ - Used to adapt a GitHub issue payload into attributes for creating or updating - a `CodeCorps.Task` and vice-versa. - """ - - alias CodeCorps.{ - Adapter.MapTransformer, - GitHub.Adapters.Utils.BodyDecorator, - Task - } - - @mapping [ - {:created_at, ["created_at"]}, - {:github_issue_number, ["number"]}, - {:markdown, ["body"]}, - {:modified_at, ["updated_at"]}, - {:status, ["state"]}, - {:title, ["title"]} - ] - - @doc ~S""" - Converts a GitHub issue payled into a set of attributes used to update or - create a `Task` record. - """ - @spec from_api(map) :: map - def from_api(%{} = payload) do - payload - |> BodyDecorator.remove_code_corps_header() - |> MapTransformer.transform(@mapping) - end - - @autogenerated_github_keys ~w(created_at number updated_at) - - @doc ~S""" - Converts a `Task` into a set of attributes used to update or create an - associated GitHub issue - """ - @spec to_api(Task.t) :: map - def to_api(%Task{} = task) do - task - |> Map.from_struct - |> MapTransformer.transform_inverse(@mapping) - |> Map.drop(@autogenerated_github_keys) - |> BodyDecorator.add_code_corps_header(task) - end -end diff --git a/lib/code_corps/github/event/issues/changeset_builder.ex b/lib/code_corps/github/event/issues/changeset_builder.ex index f721fa4aa..113e3da5f 100644 --- a/lib/code_corps/github/event/issues/changeset_builder.ex +++ b/lib/code_corps/github/event/issues/changeset_builder.ex @@ -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""" @@ -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") @@ -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) diff --git a/lib/code_corps/github/event/issues/issue_linker.ex b/lib/code_corps/github/event/issues/issue_linker.ex index bf61829a1..079a61e35 100644 --- a/lib/code_corps/github/event/issues/issue_linker.ex +++ b/lib/code_corps/github/event/issues/issue_linker.ex @@ -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) diff --git a/lib/code_corps/github/event/pull_request/changeset_builder.ex b/lib/code_corps/github/event/pull_request/changeset_builder.ex index 4aad1c0d6..b928db4d1 100644 --- a/lib/code_corps/github/event/pull_request/changeset_builder.ex +++ b/lib/code_corps/github/event/pull_request/changeset_builder.ex @@ -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""" @@ -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") @@ -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) diff --git a/lib/code_corps/github/issue/issue.ex b/lib/code_corps/github/issue/issue.ex index 198b480ef..a71f721ae 100644 --- a/lib/code_corps/github/issue/issue.ex +++ b/lib/code_corps/github/issue/issue.ex @@ -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) @@ -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) diff --git a/test/lib/code_corps/github/adapters/issue_test.exs b/test/lib/code_corps/github/adapters/issue_test.exs index d5046c3d1..e1dda2614 100644 --- a/test/lib/code_corps/github/adapters/issue_test.exs +++ b/test/lib/code_corps/github/adapters/issue_test.exs @@ -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"], @@ -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 @@ -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 diff --git a/test/lib/code_corps/github/adapters/task_test.exs b/test/lib/code_corps/github/adapters/task_test.exs deleted file mode 100644 index 77c9755f3..000000000 --- a/test/lib/code_corps/github/adapters/task_test.exs +++ /dev/null @@ -1,62 +0,0 @@ -defmodule CodeCorps.GitHub.Adapters.TaskTest do - @moduledoc false - - use CodeCorps.DbAccessCase - - import CodeCorps.GitHub.TestHelpers - - alias CodeCorps.{GitHub.Adapters, Task} - - describe "from_api/1" do - test "maps api payload correctly" do - %{"issue" => payload} = load_event_fixture("issues_opened") - - assert Adapters.Task.from_api(payload) == %{ - created_at: payload["created_at"], - github_issue_number: payload["number"], - markdown: payload["body"], - modified_at: payload["updated_at"], - title: payload["title"], - status: payload["state"] - } - end - - test "removes 'Posted by' header from body if one is present" do - %{"issue" => %{"body" => body} = payload} = load_event_fixture("issues_opened") - modified_payload = payload |> Map.put("body", "Posted by \r\n\r\n[//]: # (Please type your edits below this line)\r\n\r\n---\r\n\r\n" <> body) - - assert Adapters.Task.from_api(modified_payload) == %{ - created_at: payload["created_at"], - github_issue_number: payload["number"], - markdown: payload["body"], - modified_at: payload["updated_at"], - title: payload["title"], - status: payload["state"] - } - end - end - - describe "to_api/1" do - test "maps Task correctly" do - payload = - %Task{number: 5, title: "Foo", markdown: "bar", status: "open"} - |> Adapters.Task.to_api - - assert payload["body"] == "bar" - assert payload["state"] == "open" - assert payload["title"] == "Foo" - refute payload["created_at"] - refute payload["number"] - refute payload["updated_at"] - end - - test "adds 'Posted by' header to to body if user is not github connected" do - user = insert(:user, github_id: nil) - task = insert(:task, user: user) - - payload = task |> Adapters.Task.to_api - - assert payload["body"] =~ "Posted by" - end - end -end diff --git a/test/lib/code_corps/github/event/issues/issue_linker_test.exs b/test/lib/code_corps/github/event/issues/issue_linker_test.exs index 6821aa579..90aa9cdab 100644 --- a/test/lib/code_corps/github/event/issues/issue_linker_test.exs +++ b/test/lib/code_corps/github/event/issues/issue_linker_test.exs @@ -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) diff --git a/test/lib/code_corps/github/issue/issue_test.exs b/test/lib/code_corps/github/issue/issue_test.exs index 97bd28135..d9877dc19 100644 --- a/test/lib/code_corps/github/issue/issue_test.exs +++ b/test/lib/code_corps/github/issue/issue_test.exs @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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