Permalink
Browse files

Move receive_hex_package_update to ProjectHookController

  • Loading branch information...
rrrene committed Jun 21, 2016
1 parent c7ecd30 commit 24f346caeca0557aa27b1880b978e7ca910f6bdc
@@ -31,7 +31,7 @@ defmodule HexFaktor.Deps do
|> Enum.flat_map(&(&1.available_versions |> List.wrap))
|> Enum.empty?
if all_deps |> Enum.any? && no_available_versions_loaded do
if Enum.any?(all_deps) && no_available_versions_loaded do
{:error, :could_not_reach_hex_server}
else
{:ok, all_deps}
@@ -3,10 +3,6 @@ defmodule HexFaktor.PageController do
require Logger
alias HexFaktor.Auth
alias HexFaktor.AppEvent
alias HexFaktor.Persistence.Project
alias HexFaktor.Persistence.Package
alias HexFaktor.ProjectBuilder
def index(conn, _params) do
if Auth.current_user(conn) do
@@ -28,31 +24,4 @@ defmodule HexFaktor.PageController do
1 / 0
render conn, "500.html"
end
# TODO: move to a proper controller
def hex_package_update(conn, %{"name" => name, "github_url" => github_url} = payload) do
Logger.info "Event: package.update_hex - #{name}"
HexFaktor.Endpoint.broadcast!("feeds:lobby", "update", %{name: name})
package = Package.ensure_and_update(name, payload)
package_project = Project.find_by_html_url(github_url, [:git_repo_branches])
if package_project && package_project.id != package.project_id do
package |> Package.update_project_id(package_project.id)
end
dependent_projects_with_branches =
([package_project] ++ Project.all_with_dep(name))
|> Enum.reject(&is_nil/1)
AppEvent.log(:hex_package_update, name, dependent_projects_with_branches)
dependent_projects_with_branches
|> Enum.each(&ProjectBuilder.run_notification_branches(&1, "package_update"))
HexFaktor.NotificationPublisher.handle_new_package_update(package)
render(conn, "ok.json")
end
end
@@ -1,13 +1,16 @@
defmodule HexFaktor.ProjectHookController do
use HexFaktor.Web, :controller
alias HexFaktor.AppEvent
alias HexFaktor.Auth
alias HexFaktor.Broadcast
alias HexFaktor.Persistence.Package
alias HexFaktor.Persistence.Project
alias HexFaktor.Persistence.ProjectHook
alias HexFaktor.ProjectAccess
alias HexFaktor.ProjectBuilder
alias HexFaktor.ProjectBuilder
alias HexFaktor.ProjectSyncer
alias HexFaktor.Persistence.Project
alias HexFaktor.Persistence.ProjectHook
require Logger
@@ -20,6 +23,8 @@ defmodule HexFaktor.ProjectHookController do
#
# /activate_webhook
#
# Creates webhooks on GitHub to be notified of pushes.
#
def activate_webhook(conn, %{"id" => id}) do
current_user = Auth.current_user(conn)
@@ -52,6 +57,8 @@ defmodule HexFaktor.ProjectHookController do
#
# /deactivate_webhook
#
# Deletes webhooks on GitHub.
#
def deactivate_webhook(conn, %{"id" => id}) do
current_user = Auth.current_user(conn)
@@ -99,4 +106,35 @@ defmodule HexFaktor.ProjectHookController do
defp unsynced_project?(project) do
project.last_github_sync |> is_nil()
end
#
# /receive_hex_package_update
#
# Receives the webhooks created with the functions above.
#
def receive_hex_package_update(conn, %{"name" => name, "github_url" => github_url} = payload) do
Logger.info "Event: package.update_hex - #{name}"
HexFaktor.Endpoint.broadcast!("feeds:lobby", "update", %{name: name})
package = Package.ensure_and_update(name, payload)
package_project = Project.find_by_html_url(github_url, [:git_repo_branches])
if package_project && package_project.id != package.project_id do
package |> Package.update_project_id(package_project.id)
end
dependent_projects_with_branches =
([package_project] ++ Project.all_with_dep(name))
|> Enum.reject(&is_nil/1)
AppEvent.log(:hex_package_update, name, dependent_projects_with_branches)
dependent_projects_with_branches
|> Enum.each(&ProjectBuilder.run_notification_branches(&1, "package_update"))
HexFaktor.NotificationPublisher.handle_new_package_update(package)
render(conn, "ok.json")
end
end
@@ -35,9 +35,9 @@ defmodule HexFaktor.Persistence.Project do
Repo.all(query)
end
def all_active_ids do
defp all_active_or_elixir_ids do
query = from r in Project,
where: r.active == true,
where: r.active == true or r.language == "Elixir",
select: r.id
Repo.all(query)
end
@@ -47,7 +47,7 @@ defmodule HexFaktor.Persistence.Project do
end
def all_with_dep(name) when is_binary(name) do
all_with_dep(name, all_active_ids)
all_with_dep(name, all_active_or_elixir_ids)
end
def all_with_dep(name, project_ids) when is_binary(name) do
query = from r in DepsObject,
@@ -86,7 +86,7 @@ defmodule HexFaktor.Router do
scope "/api", HexFaktor do
pipe_through :api
post "/hex_package_update", PageController, :hex_package_update
post "/hex_package_update", ProjectHookController, :receive_hex_package_update
post "/rebuild_via_hook", ProjectController, :rebuild_via_hook, as: :rebuild_via_hook
get "/packages", PackageController, :index_api

0 comments on commit 24f346c

Please sign in to comment.