diff options
Diffstat (limited to 'app/controllers/concerns/web_hooks')
3 files changed, 149 insertions, 0 deletions
diff --git a/app/controllers/concerns/web_hooks/hook_actions.rb b/app/controllers/concerns/web_hooks/hook_actions.rb new file mode 100644 index 00000000000..ea11f13c7ef --- /dev/null +++ b/app/controllers/concerns/web_hooks/hook_actions.rb @@ -0,0 +1,85 @@ +# 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 + + unless hook.valid? + self.hooks = relation.select(&:persisted?) + flash[:alert] = hook.errors.full_messages.join.html_safe + end + + redirect_to action: :index + end + + def update + if hook.update(hook_params) + flash[:notice] = _('Hook was successfully updated.') + redirect_to action: :index + 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 + + 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 + %i[enable_ssl_verification token url push_events_branch_filter] + end + + def destroy_hook(hook) + result = WebHooks::DestroyService.new(current_user).execute(hook) + + if result[:status] == :success + flash[:notice] = + if result[:async] + format(_("%{hook_type} was scheduled for deletion"), hook_type: hook.model_name.human) + else + format(_("%{hook_type} was deleted"), hook_type: hook.model_name.human) + end + else + flash[:alert] = result[:message] + end + end + end +end diff --git a/app/controllers/concerns/web_hooks/hook_execution_notice.rb b/app/controllers/concerns/web_hooks/hook_execution_notice.rb new file mode 100644 index 00000000000..d651313b30d --- /dev/null +++ b/app/controllers/concerns/web_hooks/hook_execution_notice.rb @@ -0,0 +1,20 @@ +# frozen_string_literal: true + +module WebHooks + module HookExecutionNotice + private + + def set_hook_execution_notice(result) + http_status = result[:http_status] + message = result[:message] + + if http_status && http_status >= 200 && http_status < 400 + flash[:notice] = "Hook executed successfully: HTTP #{http_status}" + elsif http_status + flash[:alert] = "Hook executed successfully but returned HTTP #{http_status} #{message}" + else + flash[:alert] = "Hook execution failed: #{message}" + end + end + end +end diff --git a/app/controllers/concerns/web_hooks/hook_log_actions.rb b/app/controllers/concerns/web_hooks/hook_log_actions.rb new file mode 100644 index 00000000000..f3378d7c857 --- /dev/null +++ b/app/controllers/concerns/web_hooks/hook_log_actions.rb @@ -0,0 +1,44 @@ +# frozen_string_literal: true + +module WebHooks + module HookLogActions + extend ActiveSupport::Concern + include HookExecutionNotice + + included do + before_action :hook, only: [:show, :retry] + before_action :hook_log, only: [:show, :retry] + + respond_to :html + + feature_category :integrations + urgency :low, [:retry] + end + + def show + hide_search_settings + end + + def retry + execute_hook + redirect_to after_retry_redirect_path + end + + private + + # rubocop:disable Gitlab/ModuleWithInstanceVariables + def hook_log + @hook_log ||= hook.web_hook_logs.find(params[:id]) + end + # rubocop:enable Gitlab/ModuleWithInstanceVariables + + def execute_hook + result = hook.execute(hook_log.request_data, hook_log.trigger) + set_hook_execution_notice(result) + end + + def hide_search_settings + @hide_search_settings ||= true + end + end +end |