hooks_controller.rb 1.19 KB
Newer Older
1
class Admin::HooksController < Admin::ApplicationController
2 3 4
  include HooksExecution

  before_action :hook_logs, only: :edit
5

6 7 8 9 10 11
  def index
    @hooks = SystemHook.all
    @hook = SystemHook.new
  end

  def create
12
    @hook = SystemHook.new(hook_params.to_h)
13

Valeriy Sizov's avatar
Valeriy Sizov committed
14 15 16 17
    if @hook.save
      redirect_to admin_hooks_path, notice: 'Hook was successfully created.'
    else
      @hooks = SystemHook.all
18
      render :index
19 20 21
    end
  end

22 23 24 25
  def edit
  end

  def update
Lin Jen-Shin's avatar
Lin Jen-Shin committed
26
    if hook.update(hook_params)
27 28 29 30 31 32 33
      flash[:notice] = 'System hook was successfully updated.'
      redirect_to admin_hooks_path
    else
      render 'edit'
    end
  end

34
  def destroy
35
    hook.destroy
36

Lin Jen-Shin's avatar
Lin Jen-Shin committed
37
    redirect_to admin_hooks_path, status: :found
38 39 40
  end

  def test
41
    result = TestHooks::SystemService.new(hook, current_user, params[:trigger]).execute
42

43
    set_hook_execution_notice(result)
44

45
    redirect_back_or_default
46
  end
47

48 49 50 51 52 53
  private

  def hook
    @hook ||= SystemHook.find(params[:id])
  end

54
  def hook_logs
55
    @hook_logs ||= hook.web_hook_logs.recent.page(params[:page])
56 57
  end

58
  def hook_params
59 60 61
    params.require(:hook).permit(
      :enable_ssl_verification,
      :token,
62 63
      :url,
      *SystemHook.triggers.values
64
    )
65
  end
66
end