hooks_controller.rb 1.2 KB
Newer Older
1
class Projects::HooksController < Projects::ApplicationController
D
Dmitriy Zaporozhets 已提交
2
  # Authorize
3
  before_filter :authorize_admin_project!
D
Dmitriy Zaporozhets 已提交
4 5 6

  respond_to :html

7 8
  layout "project_settings"

D
Dmitriy Zaporozhets 已提交
9
  def index
S
skv 已提交
10
    @hooks = @project.hooks
11
    @hook = ProjectHook.new
D
Dmitriy Zaporozhets 已提交
12 13 14
  end

  def create
15
    @hook = @project.hooks.new(hook_params)
D
Dmitriy Zaporozhets 已提交
16 17 18
    @hook.save

    if @hook.valid?
19
      redirect_to project_hooks_path(@project)
D
Dmitriy Zaporozhets 已提交
20
    else
21
      @hooks = @project.hooks.select(&:persisted?)
22
      render :index
D
Dmitriy Zaporozhets 已提交
23 24 25
    end
  end

26
  def test
27
    if !@project.empty_repo?
28
      status = TestHookService.new.execute(hook, current_user)
D
Dmitriy Zaporozhets 已提交
29

30 31 32 33 34 35
      if status
        flash[:notice] = 'Hook successfully executed.'
      else
        flash[:alert] = 'Hook execution failed. '\
                        'Ensure hook URL is correct and service is up.'
      end
36 37 38
    else
      flash[:alert] = 'Hook execution failed. Ensure the project has commits.'
    end
39 40 41 42

    redirect_to :back
  end

D
Dmitriy Zaporozhets 已提交
43
  def destroy
44
    hook.destroy
D
Dmitriy Zaporozhets 已提交
45 46 47

    redirect_to project_hooks_path(@project)
  end
48 49 50 51 52 53

  private

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

  def hook_params
56
    params.require(:hook).permit(:url, :push_events, :issues_events, :merge_requests_events, :tag_push_events)
57
  end
D
Dmitriy Zaporozhets 已提交
58
end