1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
|
# frozen_string_literal: true
module Projects
module Alerting
class NotificationsController < Projects::ApplicationController
include ActionController::HttpAuthentication::Basic
respond_to :json
skip_before_action :verify_authenticity_token
skip_before_action :project
prepend_before_action :repository, :project_without_auth
feature_category :incident_management
urgency :low, [:create]
def create
token = extract_alert_manager_token(request)
result = notify_service.execute(token, integration)
has_something_to_return = result.success? && result.http_status != :created
if has_something_to_return
render json: AlertManagement::AlertSerializer.new.represent(result.payload[:alerts]), code: result.http_status
else
head result.http_status
end
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)
extract_bearer_token(request) || extract_basic_auth_token(request)
end
def extract_bearer_token(request)
Doorkeeper::OAuth::Token.from_bearer_authorization(request)
end
def extract_basic_auth_token(request)
_username, token = user_name_and_password(request)
token
end
def notify_service
notify_service_class.new(project, notification_payload)
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
end
def integration
AlertManagement::HttpIntegrationsFinder.new(
project,
endpoint_identifier: params[:endpoint_identifier],
active: true
).execute.first
end
def notification_payload
@notification_payload ||= params.permit![:notification]
end
end
end
end
|