debian-mirror-gitlab/app/controllers/concerns/web_hooks/hook_actions.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

85 lines
2 KiB
Ruby
Raw Normal View History

2022-10-11 01:57:18 +05:30
# frozen_string_literal: true
module WebHooks
module HookActions
extend ActiveSupport::Concern
include HookExecutionNotice
included do
attr_writer :hooks, :hook
end
def index
self.hooks = relation.select(&:persisted?)
self.hook = relation.new
end
def create
self.hook = relation.new(hook_params)
hook.save
2023-03-04 22:38:38 +05:30
if hook.valid?
flash[:notice] = _('Webhook was created')
else
2022-10-11 01:57:18 +05:30
self.hooks = relation.select(&:persisted?)
2023-01-13 00:05:48 +05:30
flash[:alert] = hook.errors.full_messages.to_sentence.html_safe
2022-10-11 01:57:18 +05:30
end
redirect_to action: :index
end
def update
if hook.update(hook_params)
2023-03-04 22:38:38 +05:30
flash[:notice] = _('Webhook was updated')
redirect_to action: :edit
2022-10-11 01:57:18 +05:30
else
render 'edit'
end
end
def destroy
destroy_hook(hook)
redirect_to action: :index, status: :found
end
def edit
redirect_to(action: :index) unless hook
end
private
def hook_params
permitted = hook_param_names + trigger_values
permitted << { url_variables: [:key, :value] }
ps = params.require(:hook).permit(*permitted).to_h
2023-01-13 00:05:48 +05:30
ps.delete(:token) if action_name == 'update' && ps[:token] == WebHook::SECRET_MASK
2022-10-11 01:57:18 +05:30
ps[:url_variables] = ps[:url_variables].to_h { [_1[:key], _1[:value].presence] } if ps.key?(:url_variables)
if action_name == 'update' && ps.key?(:url_variables)
supplied = ps[:url_variables]
ps[:url_variables] = hook.url_variables.merge(supplied).compact
end
ps
end
def hook_param_names
2023-03-04 22:38:38 +05:30
%i[enable_ssl_verification token url push_events_branch_filter branch_filter_strategy]
2022-10-11 01:57:18 +05:30
end
def destroy_hook(hook)
result = WebHooks::DestroyService.new(current_user).execute(hook)
if result[:status] == :success
2023-03-04 22:38:38 +05:30
flash[:notice] = result[:async] ? _('Webhook was scheduled for deletion') : _('Webhook was deleted')
2022-10-11 01:57:18 +05:30
else
flash[:alert] = result[:message]
end
end
end
end