debian-mirror-gitlab/app/controllers/projects/settings/operations_controller.rb

141 lines
4.1 KiB
Ruby
Raw Normal View History

2019-02-15 15:39:39 +05:30
# frozen_string_literal: true
module Projects
module Settings
class OperationsController < Projects::ApplicationController
2019-12-04 20:38:33 +05:30
before_action :authorize_admin_operations!
2020-04-22 19:07:51 +05:30
before_action :authorize_read_prometheus_alerts!, only: [:reset_alerting_token]
2020-07-28 23:09:34 +05:30
respond_to :json, only: [:reset_alerting_token, :reset_pagerduty_token]
2019-02-15 15:39:39 +05:30
2020-07-28 23:09:34 +05:30
helper_method :error_tracking_setting
2019-02-15 15:39:39 +05:30
def update
result = ::Projects::Operations::UpdateService.new(project, current_user, update_params).execute
2019-12-21 20:55:43 +05:30
track_events(result)
2019-07-07 11:18:12 +05:30
render_update_response(result)
end
2019-12-21 20:55:43 +05:30
# overridden in EE
def track_events(result)
2020-03-13 15:44:24 +05:30
if result[:status] == :success
::Gitlab::Tracking::IncidentManagement.track_from_params(
update_params[:incident_management_setting_attributes]
)
end
2019-12-21 20:55:43 +05:30
end
2020-04-22 19:07:51 +05:30
def reset_alerting_token
result = ::Projects::Operations::UpdateService
.new(project, current_user, alerting_params)
.execute
if result[:status] == :success
render json: { token: project.alerting_setting.token }
else
render json: {}, status: :unprocessable_entity
end
end
2020-07-28 23:09:34 +05:30
def reset_pagerduty_token
result = ::Projects::Operations::UpdateService
.new(project, current_user, pagerduty_token_params)
.execute
if result[:status] == :success
pagerduty_token = project.incident_management_setting&.pagerduty_token
2020-10-24 23:57:45 +05:30
webhook_url = project_incidents_integrations_pagerduty_url(project, token: pagerduty_token)
2020-07-28 23:09:34 +05:30
render json: { pagerduty_webhook_url: webhook_url, pagerduty_token: pagerduty_token }
else
render json: {}, status: :unprocessable_entity
end
end
2019-07-07 11:18:12 +05:30
private
2020-04-22 19:07:51 +05:30
def alerting_params
{ alerting_setting_attributes: { regenerate_token: true } }
end
2020-07-28 23:09:34 +05:30
def pagerduty_token_params
{ incident_management_setting_attributes: { regenerate_token: true } }
2020-04-08 14:13:33 +05:30
end
2019-07-07 11:18:12 +05:30
def render_update_response(result)
respond_to do |format|
2020-03-13 15:44:24 +05:30
format.html do
render_update_html_response(result)
end
2019-07-07 11:18:12 +05:30
format.json do
render_update_json_response(result)
end
end
end
2020-03-13 15:44:24 +05:30
def render_update_html_response(result)
if result[:status] == :success
flash[:notice] = _('Your changes have been saved')
redirect_to project_settings_operations_path(@project)
else
render 'show'
end
end
2019-07-07 11:18:12 +05:30
def render_update_json_response(result)
2019-02-15 15:39:39 +05:30
if result[:status] == :success
flash[:notice] = _('Your changes have been saved')
2019-07-07 11:18:12 +05:30
render json: {
status: result[:status]
}
2019-02-15 15:39:39 +05:30
else
2019-07-07 11:18:12 +05:30
render(
status: result[:http_status] || :bad_request,
json: {
status: result[:status],
message: result[:message]
}
)
2019-02-15 15:39:39 +05:30
end
end
def error_tracking_setting
@error_tracking_setting ||= project.error_tracking_setting ||
project.build_error_tracking_setting
end
def update_params
params.require(:project).permit(permitted_project_params)
end
# overridden in EE
def permitted_project_params
2020-03-13 15:44:24 +05:30
project_params = {
incident_management_setting_attributes: ::Gitlab::Tracking::IncidentManagement.tracking_keys.keys,
2020-06-23 00:09:42 +05:30
metrics_setting_attributes: [:external_dashboard_url, :dashboard_timezone],
2019-07-31 22:56:46 +05:30
2019-07-07 11:18:12 +05:30
error_tracking_setting_attributes: [
:enabled,
:api_host,
:token,
project: [:slug, :name, :organization_slug, :organization_name]
2019-12-21 20:55:43 +05:30
],
2019-12-26 22:10:19 +05:30
grafana_integration_attributes: [:token, :grafana_url, :enabled]
2019-07-07 11:18:12 +05:30
}
2020-03-13 15:44:24 +05:30
if Feature.enabled?(:settings_operations_prometheus_service, project)
project_params[:prometheus_integration_attributes] = [:manual_configuration, :api_url]
end
project_params
2019-02-15 15:39:39 +05:30
end
end
end
end
2019-12-04 20:38:33 +05:30
Projects::Settings::OperationsController.prepend_if_ee('::EE::Projects::Settings::OperationsController')