2018-09-25 23:45:43 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-06-23 12:47:22 -04:00
|
|
|
class Projects::HooksController < Projects::ApplicationController
|
2021-11-02 14:12:13 -04:00
|
|
|
include ::Integrations::HooksExecution
|
2017-04-27 06:08:57 -04:00
|
|
|
|
2012-01-03 17:42:14 -05:00
|
|
|
# Authorize
|
2015-04-16 08:03:37 -04:00
|
|
|
before_action :authorize_admin_project!
|
2017-04-27 06:08:57 -04:00
|
|
|
before_action :hook_logs, only: :edit
|
2021-12-10 10:10:24 -05:00
|
|
|
before_action -> { check_rate_limit!(:project_testing_hook, scope: [@project, current_user]) }, only: :test
|
2012-01-03 17:42:14 -05:00
|
|
|
|
|
|
|
respond_to :html
|
|
|
|
|
2013-06-19 15:44:57 -04:00
|
|
|
layout "project_settings"
|
|
|
|
|
2020-10-08 14:08:32 -04:00
|
|
|
feature_category :integrations
|
2021-10-27 14:13:16 -04:00
|
|
|
urgency :low, [:test]
|
2020-10-08 14:08:32 -04:00
|
|
|
|
2017-07-20 11:12:06 -04:00
|
|
|
def index
|
2021-10-28 08:10:22 -04:00
|
|
|
@hooks = @project.hooks.load
|
2020-02-24 13:09:05 -05:00
|
|
|
@hook = ProjectHook.new
|
2017-07-20 11:12:06 -04:00
|
|
|
end
|
|
|
|
|
2012-01-03 17:42:14 -05:00
|
|
|
def create
|
2014-06-26 11:45:33 -04:00
|
|
|
@hook = @project.hooks.new(hook_params)
|
2012-01-03 17:42:14 -05:00
|
|
|
@hook.save
|
|
|
|
|
2017-04-10 06:05:29 -04:00
|
|
|
unless @hook.valid?
|
2014-04-06 11:42:24 -04:00
|
|
|
@hooks = @project.hooks.select(&:persisted?)
|
2017-01-18 13:09:32 -05:00
|
|
|
flash[:alert] = @hook.errors.full_messages.join.html_safe
|
2012-01-03 17:42:14 -05:00
|
|
|
end
|
2018-01-11 11:34:01 -05:00
|
|
|
|
2020-02-24 13:09:05 -05:00
|
|
|
redirect_to action: :index
|
2012-01-03 17:42:14 -05:00
|
|
|
end
|
|
|
|
|
2017-04-20 04:31:37 -04:00
|
|
|
def edit
|
2021-04-30 05:10:21 -04:00
|
|
|
redirect_to(action: :index) unless hook
|
2017-04-20 04:31:37 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
2018-07-02 06:43:06 -04:00
|
|
|
if hook.update(hook_params)
|
2019-03-27 12:52:52 -04:00
|
|
|
flash[:notice] = _('Hook was successfully updated.')
|
2020-02-24 13:09:05 -05:00
|
|
|
redirect_to action: :index
|
2017-04-20 04:31:37 -04:00
|
|
|
else
|
|
|
|
render 'edit'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-01-08 05:20:24 -05:00
|
|
|
def test
|
2017-07-20 11:12:06 -04:00
|
|
|
result = TestHooks::ProjectService.new(hook, current_user, params[:trigger]).execute
|
2014-12-04 08:07:01 -05:00
|
|
|
|
2017-07-20 11:12:06 -04:00
|
|
|
set_hook_execution_notice(result)
|
2012-01-08 05:20:24 -05:00
|
|
|
|
2020-02-24 13:09:05 -05:00
|
|
|
redirect_back_or_default(default: { action: :index })
|
2012-01-08 05:20:24 -05:00
|
|
|
end
|
|
|
|
|
2012-01-03 17:42:14 -05:00
|
|
|
def destroy
|
2020-10-07 05:09:13 -04:00
|
|
|
destroy_hook(hook)
|
2012-01-03 17:42:14 -05:00
|
|
|
|
2020-02-24 13:09:05 -05:00
|
|
|
redirect_to action: :index, status: :found
|
2012-01-03 17:42:14 -05:00
|
|
|
end
|
2014-01-15 07:38:50 -05:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def hook
|
|
|
|
@hook ||= @project.hooks.find(params[:id])
|
|
|
|
end
|
2014-06-26 11:45:33 -04:00
|
|
|
|
2017-04-27 06:08:57 -04:00
|
|
|
def hook_logs
|
2018-06-28 09:34:31 -04:00
|
|
|
@hook_logs ||= hook.web_hook_logs.recent.page(params[:page])
|
2017-04-27 06:08:57 -04:00
|
|
|
end
|
|
|
|
|
2014-06-26 11:45:33 -04:00
|
|
|
def hook_params
|
2016-04-30 04:34:31 -04:00
|
|
|
params.require(:hook).permit(
|
|
|
|
:enable_ssl_verification,
|
|
|
|
:token,
|
2016-05-12 23:25:33 -04:00
|
|
|
:url,
|
2018-06-07 03:35:17 -04:00
|
|
|
:push_events_branch_filter,
|
2017-12-18 05:14:51 -05:00
|
|
|
*ProjectHook.triggers.values
|
2016-04-30 04:34:31 -04:00
|
|
|
)
|
2014-06-26 11:45:33 -04:00
|
|
|
end
|
2012-01-03 17:42:14 -05:00
|
|
|
end
|