Skip to content

Commit

Permalink
Add github issue webhook
Browse files Browse the repository at this point in the history
  • Loading branch information
npendery committed Apr 26, 2017
1 parent 46d7410 commit c59d2e1
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 0 deletions.
22 changes: 22 additions & 0 deletions test/controllers/github_issue_controller_test.exs
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
56 changes: 56 additions & 0 deletions web/controllers/github_issue_controller.ex
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
2 changes: 2 additions & 0 deletions web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ defmodule CodeCorps.Router do

post "/webhooks/stripe/connect", StripeConnectEventsController, :create
post "/webhooks/stripe/platform", StripePlatformEventsController, :create
post "/webhooks/github_issues", GithubIssueController, :create
post "/webhooks/github_issue_comments", GithubIssueCommentController, :create
end

scope "/", CodeCorps, host: "api." do
Expand Down
3 changes: 3 additions & 0 deletions web/views/github_issue_view.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
defmodule CodeCorps.GithubIssueView do
use CodeCorps.Web, :view
end

0 comments on commit c59d2e1

Please sign in to comment.