hooks_controller.rb 1.68 KB
Newer Older
1
class Projects::HooksController < Projects::ApplicationController
2 3
  include HooksExecution

4
  # Authorize
5
  before_action :authorize_admin_project!
6
  before_action :hook_logs, only: :edit
7 8 9

  respond_to :html

10 11
  layout "project_settings"

12 13 14 15
  def index
    redirect_to project_settings_integrations_path(@project)
  end

16
  def create
17
    @hook = @project.hooks.new(hook_params)
18 19
    @hook.save

20
    unless @hook.valid?
21
      @hooks = @project.hooks.select(&:persisted?)
22
      flash[:alert] = @hook.errors.full_messages.join.html_safe
23
    end
24
    redirect_to project_settings_integrations_path(@project)
25 26
  end

27 28 29 30 31 32
  def edit
  end

  def update
    if hook.update_attributes(hook_params)
      flash[:notice] = 'Hook was successfully updated.'
33
      redirect_to project_settings_integrations_path(@project)
34 35 36 37 38
    else
      render 'edit'
    end
  end

39
  def test
40
    result = TestHooks::ProjectService.new(hook, current_user, params[:trigger]).execute
41

42
    set_hook_execution_notice(result)
43

44
    redirect_back_or_default(default: { action: 'index' })
45 46
  end

47
  def destroy
48
    hook.destroy
49

50
    redirect_to project_settings_integrations_path(@project), status: 302
51
  end
52 53 54 55 56 57

  private

  def hook
    @hook ||= @project.hooks.find(params[:id])
  end
58

59 60 61 62 63
  def hook_logs
    @hook_logs ||=
      Kaminari.paginate_array(hook.web_hook_logs.order(created_at: :desc)).page(params[:page])
  end

64
  def hook_params
65
    params.require(:hook).permit(
66
      :job_events,
67
      :pipeline_events,
68 69
      :enable_ssl_verification,
      :issues_events,
70
      :confidential_issues_events,
71 72 73 74 75
      :merge_requests_events,
      :note_events,
      :push_events,
      :tag_push_events,
      :token,
76 77
      :url,
      :wiki_page_events
78
    )
79
  end
80
end