2020-03-13 15:44:24 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Projects
|
|
|
|
module Alerting
|
|
|
|
class NotificationsController < Projects::ApplicationController
|
2021-11-18 22:05:49 +05:30
|
|
|
include ActionController::HttpAuthentication::Basic
|
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
respond_to :json
|
|
|
|
|
|
|
|
skip_before_action :verify_authenticity_token
|
|
|
|
skip_before_action :project
|
|
|
|
|
|
|
|
prepend_before_action :repository, :project_without_auth
|
|
|
|
|
2021-02-22 17:27:13 +05:30
|
|
|
feature_category :incident_management
|
2021-01-03 14:25:43 +05:30
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
def create
|
|
|
|
token = extract_alert_manager_token(request)
|
2021-01-29 00:20:46 +05:30
|
|
|
result = notify_service.execute(token, integration)
|
2020-03-13 15:44:24 +05:30
|
|
|
|
2021-12-11 22:18:48 +05:30
|
|
|
if result.success?
|
|
|
|
render json: AlertManagement::AlertSerializer.new.represent(result.payload[:alerts]), code: result.http_status
|
|
|
|
else
|
|
|
|
head result.http_status
|
|
|
|
end
|
2020-03-13 15:44:24 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def project_without_auth
|
|
|
|
@project ||= Project
|
|
|
|
.find_by_full_path("#{params[:namespace_id]}/#{params[:project_id]}")
|
|
|
|
end
|
|
|
|
|
|
|
|
def extract_alert_manager_token(request)
|
2021-11-18 22:05:49 +05:30
|
|
|
extract_bearer_token(request) || extract_basic_auth_token(request)
|
|
|
|
end
|
|
|
|
|
|
|
|
def extract_bearer_token(request)
|
2020-03-13 15:44:24 +05:30
|
|
|
Doorkeeper::OAuth::Token.from_bearer_authorization(request)
|
|
|
|
end
|
|
|
|
|
2021-11-18 22:05:49 +05:30
|
|
|
def extract_basic_auth_token(request)
|
|
|
|
_username, token = user_name_and_password(request)
|
|
|
|
|
|
|
|
token
|
|
|
|
end
|
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
def notify_service
|
2021-02-22 17:27:13 +05:30
|
|
|
notify_service_class.new(project, notification_payload)
|
2020-06-23 00:09:42 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def notify_service_class
|
|
|
|
# We are tracking the consolidation of these services in
|
|
|
|
# https://gitlab.com/groups/gitlab-org/-/epics/3360
|
|
|
|
# to get rid of this workaround.
|
|
|
|
if Projects::Prometheus::Alerts::NotifyService.processable?(notification_payload)
|
|
|
|
Projects::Prometheus::Alerts::NotifyService
|
|
|
|
else
|
|
|
|
Projects::Alerting::NotifyService
|
|
|
|
end
|
2020-03-13 15:44:24 +05:30
|
|
|
end
|
|
|
|
|
2021-01-29 00:20:46 +05:30
|
|
|
def integration
|
|
|
|
AlertManagement::HttpIntegrationsFinder.new(
|
|
|
|
project,
|
|
|
|
endpoint_identifier: endpoint_identifier,
|
|
|
|
active: true
|
|
|
|
).execute.first
|
|
|
|
end
|
|
|
|
|
|
|
|
def endpoint_identifier
|
|
|
|
params[:endpoint_identifier] || AlertManagement::HttpIntegration::LEGACY_IDENTIFIER
|
|
|
|
end
|
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
def notification_payload
|
2020-06-23 00:09:42 +05:30
|
|
|
@notification_payload ||= params.permit![:notification]
|
2020-03-13 15:44:24 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|