diff options
author | Jarka Kadlecova <jarka@gitlab.com> | 2017-05-22 13:07:12 +0300 |
---|---|---|
committer | Jarka Kadlecova <jarka@gitlab.com> | 2017-06-01 08:47:15 +0300 |
commit | 33d82ccb453ef020d28f1307be1a3a97130c9e0b (patch) | |
tree | 11884c9d626b30777695193443a7e4873f7a7e49 /app/controllers/projects/services_controller.rb | |
parent | dd0f8b8ccc3b5f61e31703f7391a919b702934a5 (diff) |
simplify test&save actions when setting a service integration
Diffstat (limited to 'app/controllers/projects/services_controller.rb')
-rw-r--r-- | app/controllers/projects/services_controller.rb | 32 |
1 files changed, 19 insertions, 13 deletions
diff --git a/app/controllers/projects/services_controller.rb b/app/controllers/projects/services_controller.rb index f9d798d0455..89c4e81a36f 100644 --- a/app/controllers/projects/services_controller.rb +++ b/app/controllers/projects/services_controller.rb @@ -4,6 +4,7 @@ class Projects::ServicesController < Projects::ApplicationController # Authorize before_action :authorize_admin_project! before_action :service, only: [:edit, :update, :test] + before_action :build_service, only: [:update, :test] respond_to :html @@ -13,36 +14,41 @@ class Projects::ServicesController < Projects::ApplicationController end def update - @service.assign_attributes(service_params[:service]) if @service.save(context: :manual_change) - redirect_to( - edit_namespace_project_service_path(@project.namespace, @project, @service.to_param), - notice: 'Successfully updated.' - ) + redirect_to(namespace_project_settings_integrations_path(@project.namespace, @project), notice: success_message) else render 'edit' end end def test - return render_404 unless @service.can_test? + return render json: {}, status: :not_found unless @service.can_test? data = @service.test_data(project, current_user) outcome = @service.test(data) - if outcome[:success] - message = { notice: 'We sent a request to the provided URL' } - else - error_message = "We tried to send a request to the provided URL but an error occurred" - error_message << ": #{outcome[:result]}" if outcome[:result].present? - message = { alert: error_message } + message = {} + unless outcome[:success] + message = { error: true, message: 'Test failed', service_response: outcome[:result].to_s } end - redirect_back_or_default(options: message) + render json: message, status: :ok end private + def success_message + if @service.active? + "#{@service.title} activated." + else + "#{@service.title} settings saved, but not activated." + end + end + + def build_service + @service.assign_attributes(service_params[:service]) + end + def service @service ||= @project.find_or_initialize_service(params[:id]) end |