2020-09-01 12:11:01 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module JiraConnectSubscriptions
|
|
|
|
class CreateService < ::JiraConnectSubscriptions::BaseService
|
|
|
|
include Gitlab::Utils::StrongMemoize
|
2020-11-11 12:09:06 +00:00
|
|
|
MERGE_REQUEST_SYNC_BATCH_SIZE = 20
|
|
|
|
MERGE_REQUEST_SYNC_BATCH_delay = 1.minute.freeze
|
2020-09-01 12:11:01 +00:00
|
|
|
|
|
|
|
def execute
|
|
|
|
unless namespace && can?(current_user, :create_jira_connect_subscription, namespace)
|
|
|
|
return error('Invalid namespace. Please make sure you have sufficient permissions', 401)
|
|
|
|
end
|
|
|
|
|
|
|
|
create_subscription
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def create_subscription
|
|
|
|
subscription = JiraConnectSubscription.new(installation: jira_connect_installation, namespace: namespace)
|
|
|
|
|
|
|
|
if subscription.save
|
2020-11-11 12:09:06 +00:00
|
|
|
schedule_sync_project_jobs
|
|
|
|
|
2020-09-01 12:11:01 +00:00
|
|
|
success
|
|
|
|
else
|
|
|
|
error(subscription.errors.full_messages.join(', '), 422)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def namespace
|
|
|
|
strong_memoize(:namespace) do
|
|
|
|
Namespace.find_by_full_path(params[:namespace_path])
|
|
|
|
end
|
|
|
|
end
|
2020-11-11 12:09:06 +00:00
|
|
|
|
|
|
|
def schedule_sync_project_jobs
|
|
|
|
return unless Feature.enabled?(:jira_connect_full_namespace_sync)
|
|
|
|
|
|
|
|
namespace.all_projects.each_batch(of: MERGE_REQUEST_SYNC_BATCH_SIZE) do |projects, index|
|
|
|
|
JiraConnect::SyncProjectWorker.bulk_perform_in_with_contexts(
|
|
|
|
index * MERGE_REQUEST_SYNC_BATCH_delay,
|
|
|
|
projects,
|
|
|
|
arguments_proc: -> (project) { [project.id, Atlassian::JiraConnect::Client.generate_update_sequence_id] },
|
|
|
|
context_proc: -> (project) { { project: project } }
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
2020-09-01 12:11:01 +00:00
|
|
|
end
|
|
|
|
end
|