-
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.
- Loading branch information
Showing
4 changed files
with
83 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
defmodule CodeCorps.GithubIssueControllerTest do | ||
use CodeCorps.ConnCase | ||
|
||
alias CodeCorps.Task | ||
@valid_attrs %{} | ||
@invalid_attrs %{} | ||
|
||
setup %{conn: conn} do | ||
{:ok, conn: put_req_header(conn, "accept", "application/json")} | ||
end | ||
|
||
test "creates and renders resource when data is valid", %{conn: conn} do | ||
conn = post conn, github_issue_path(conn, :create), github_issue: @valid_attrs | ||
assert json_response(conn, 201)["data"]["id"] | ||
assert Repo.get_by(GithubIssue, @valid_attrs) | ||
end | ||
|
||
test "does not create resource and renders errors when data is invalid", %{conn: conn} do | ||
conn = post conn, github_issue_path(conn, :create), github_issue: @invalid_attrs | ||
assert json_response(conn, 422)["errors"] != %{} | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
defmodule CodeCorps.GithubIssueController do | ||
use CodeCorps.Web, :controller | ||
|
||
alias CodeCorps.Task | ||
|
||
def create(conn, payload) do | ||
attributes = convert_task_attributes(payload) | ||
task = lookup_task(payload) | ||
|
||
case payload["action"] do | ||
"opened" -> | ||
# create task | ||
changeset = %Task{} |> Task.github_create_changeset(attributes) | ||
case Repo.insert(changeset) do | ||
{:ok, _task} -> | ||
conn | ||
{:error, changeset} | ||
# log error | ||
conn | ||
end | ||
"edited" -> | ||
# update task | ||
changeset = task |> Task.github_update_changeset(attributes) | ||
case Repo.update(changeset) do | ||
{:ok, _task} -> | ||
conn | ||
{:error, changeset} | ||
# log error | ||
conn | ||
end | ||
"deleted" -> | ||
# delete task | ||
Repo.delete(task) | ||
conn | ||
_ -> | ||
# log error or do nothing | ||
conn | ||
end | ||
end | ||
|
||
defp lookup_task(payload) do | ||
task_issue_id = payload["issue"]["id"] | ||
Task |> Repo.get_by(issue_id: task_issue_id) | ||
end | ||
|
||
defp convert_task_attributes(payload) do | ||
issue = payload["issue"] | ||
%{ | ||
"title" => issue["title"], | ||
"markdown" => issue["body"], | ||
"task_list_id" => CodeCorps.TaskList |> Repo.get_by(name: "Inbox"), # Default to Inbox | ||
"project_id" => CodeCorps.Project |> Repo.get_by(github_repo_id: payload["respository"]["id"]), | ||
"user_id" => CodeCorps.User |> Repo.get_by(github_id: issue["user"]["id"]) # Need to add funtion to create random CC user if no user in system | ||
} | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
defmodule CodeCorps.GithubIssueView do | ||
use CodeCorps.Web, :view | ||
end |