Skip to content

Commit

Permalink
Update github webhook controllers
Browse files Browse the repository at this point in the history
  • Loading branch information
npendery committed Apr 28, 2017
1 parent 9725b3b commit 9f62cc5
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
6 changes: 3 additions & 3 deletions web/controllers/github_issue_comment_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ defmodule CodeCorps.GithubIssueCommentController do
case payload["action"] do
"created" ->
# create comment
attributes = convert_comment_attributes(payload)
attributes = comment_attributes(payload)
changeset = %Comment{} |> Comment.create_changeset(attributes)
case Repo.insert(changeset) do
{:ok, _comment} ->
Expand All @@ -19,7 +19,7 @@ defmodule CodeCorps.GithubIssueCommentController do
"edited" ->
# update comment
comment = lookup_comment(payload)
attributes = convert_comment_attributes(payload)
attributes = comment_attributes(payload)
changeset = comment |> Comment.changeset(attributes)
case Repo.update(changeset) do
{:ok, _comment} ->
Expand All @@ -44,7 +44,7 @@ defmodule CodeCorps.GithubIssueCommentController do
Comment |> Repo.get_by(github_id: comment_id)
end

defp convert_comment_attributes(payload) do
defp comment_attributes(payload) do
comment = payload["comment"]
%{
"github_id" => comment["id"],
Expand Down
14 changes: 8 additions & 6 deletions web/controllers/github_issue_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@ defmodule CodeCorps.GithubIssueController do
alias CodeCorps.Task

def handle(conn, payload) do
attributes = convert_task_attributes(payload)
task = lookup_task(payload)

case payload["action"] do
"opened" ->
# create task
attributes = task_attributes(payload)
changeset = %Task{} |> Task.github_create_changeset(attributes)
case Repo.insert(changeset) do
{:ok, _task} ->
Expand All @@ -20,6 +18,8 @@ defmodule CodeCorps.GithubIssueController do
end
"edited" ->
# update task
task = lookup_task(payload)
attributes = task_attributes(payload)
changeset = task |> Task.github_update_changeset(attributes)
case Repo.update(changeset) do
{:ok, _task} ->
Expand All @@ -30,6 +30,7 @@ defmodule CodeCorps.GithubIssueController do
end
"deleted" ->
# delete task
task = lookup_task(payload)
Repo.delete(task)
conn
_ ->
Expand All @@ -40,16 +41,17 @@ defmodule CodeCorps.GithubIssueController do

defp lookup_task(payload) do
task_issue_id = payload["issue"]["id"]
Task |> Repo.get_by(issue_id: task_issue_id)
Task |> Repo.get_by(github_id: task_issue_id)
end

defp convert_task_attributes(payload) do
defp task_attributes(payload) do
issue = payload["issue"]
%{
"id" => issue["id"],
"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"]),
"project_id" => CodeCorps.Project |> Repo.get_by(github_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
Expand Down

0 comments on commit 9f62cc5

Please sign in to comment.