Commit a346e9ea authored by Igor Drozdov's avatar Igor Drozdov

Move body of /post_receive handler into service

This commit just moves body of API endpoint handler
into a service. Breaking down the service into methods and
refactoring is going to be performed in another commits

Tests aren't touched in order to prove that it works as before
parent cabac822
# frozen_string_literal: true
# PostReceiveService class
#
# Used for scheduling related jobs after a push action has been performed
class PostReceiveService
attr_reader :user, :project, :params
def initialize(user, project, params)
@user = user
@project = project
@params = params
end
def execute
response = Gitlab::InternalPostReceive::Response.new
push_options = Gitlab::PushOptions.new(params[:push_options])
response.reference_counter_decreased = Gitlab::ReferenceCounter.new(params[:gl_repository]).decrease
PostReceive.perform_async(params[:gl_repository], params[:identifier],
params[:changes], push_options.as_json)
mr_options = push_options.get(:merge_request)
if mr_options.present?
message = process_mr_push_options(mr_options, project, user, params[:changes])
response.add_alert_message(message)
end
broadcast_message = BroadcastMessage.current&.last&.message
response.add_alert_message(broadcast_message)
response.add_merge_request_urls(merge_request_urls)
# Neither User nor Project are guaranteed to be returned; an orphaned write deploy
# key could be used
if user && project
redirect_message = Gitlab::Checks::ProjectMoved.fetch_message(user.id, project.id)
project_created_message = Gitlab::Checks::ProjectCreated.fetch_message(user.id, project.id)
response.add_basic_message(redirect_message)
response.add_basic_message(project_created_message)
end
response
end
def process_mr_push_options(push_options, project, user, changes)
Gitlab::QueryLimiting.whitelist('https://gitlab.com/gitlab-org/gitlab-foss/issues/61359')
service = ::MergeRequests::PushOptionsHandlerService.new(
project, user, changes, push_options
).execute
if service.errors.present?
push_options_warning(service.errors.join("\n\n"))
end
end
def push_options_warning(warning)
options = Array.wrap(params[:push_options]).map { |p| "'#{p}'" }.join(' ')
"WARNINGS:\nError encountered with push options #{options}: #{warning}"
end
def merge_request_urls
::MergeRequests::GetUrlsService.new(project).execute(params[:changes])
end
end
......@@ -55,30 +55,6 @@ module API
::Users::ActivityService.new(actor).execute if commands.include?(params[:action])
end
def merge_request_urls
::MergeRequests::GetUrlsService.new(project).execute(params[:changes])
end
def process_mr_push_options(push_options, project, user, changes)
Gitlab::QueryLimiting.whitelist('https://gitlab.com/gitlab-org/gitlab-foss/issues/61359')
service = ::MergeRequests::PushOptionsHandlerService.new(
project,
user,
changes,
push_options
).execute
if service.errors.present?
push_options_warning(service.errors.join("\n\n"))
end
end
def push_options_warning(warning)
options = Array.wrap(params[:push_options]).map { |p| "'#{p}'" }.join(' ')
"WARNINGS:\nError encountered with push options #{options}: #{warning}"
end
def redis_ping
result = Gitlab::Redis::SharedState.with { |redis| redis.ping }
......
......@@ -212,40 +212,7 @@ module API
post '/post_receive' do
status 200
response = Gitlab::InternalPostReceive::Response.new
# Try to load the project and users so we have the application context
# available for logging before we schedule any jobs.
user = actor.user
project
push_options = Gitlab::PushOptions.new(params[:push_options])
response.reference_counter_decreased = Gitlab::ReferenceCounter.new(params[:gl_repository]).decrease
PostReceive.perform_async(params[:gl_repository], params[:identifier],
params[:changes], push_options.as_json)
mr_options = push_options.get(:merge_request)
if mr_options.present?
message = process_mr_push_options(mr_options, project, user, params[:changes])
response.add_alert_message(message)
end
broadcast_message = BroadcastMessage.current&.last&.message
response.add_alert_message(broadcast_message)
response.add_merge_request_urls(merge_request_urls)
# Neither User nor Project are guaranteed to be returned; an orphaned write deploy
# key could be used
if user && project
redirect_message = Gitlab::Checks::ProjectMoved.fetch_message(user.id, project.id)
project_created_message = Gitlab::Checks::ProjectCreated.fetch_message(user.id, project.id)
response.add_basic_message(redirect_message)
response.add_basic_message(project_created_message)
end
response = PostReceiveService.new(actor.user, project, params).execute
ee_post_receive_response_hook(response)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment