2020-11-24 15:15:51 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module JiraConnectSubscriptions
|
|
|
|
class CreateService < ::JiraConnectSubscriptions::BaseService
|
|
|
|
include Gitlab::Utils::StrongMemoize
|
2021-01-29 00:20:46 +05:30
|
|
|
MERGE_REQUEST_SYNC_BATCH_SIZE = 20
|
|
|
|
MERGE_REQUEST_SYNC_BATCH_DELAY = 1.minute.freeze
|
2020-11-24 15:15:51 +05:30
|
|
|
|
|
|
|
def execute
|
2022-07-23 23:45:48 +05:30
|
|
|
if !params[:jira_user]
|
|
|
|
return error(s_('JiraConnect|Could not fetch user information from Jira. ' \
|
|
|
|
'Check the permissions in Jira and try again.'), 403)
|
|
|
|
elsif !can_administer_jira?
|
|
|
|
return error(s_('JiraConnect|The Jira user is not a site administrator. ' \
|
|
|
|
'Check the permissions in Jira and try again.'), 403)
|
|
|
|
end
|
2021-09-04 01:27:46 +05:30
|
|
|
|
2020-11-24 15:15:51 +05:30
|
|
|
unless namespace && can?(current_user, :create_jira_connect_subscription, namespace)
|
2022-07-23 23:45:48 +05:30
|
|
|
return error(s_('JiraConnect|Cannot find namespace. Make sure you have sufficient permissions.'), 401)
|
2020-11-24 15:15:51 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
create_subscription
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2021-09-04 01:27:46 +05:30
|
|
|
def can_administer_jira?
|
2022-07-23 23:45:48 +05:30
|
|
|
params[:jira_user]&.site_admin?
|
2021-09-04 01:27:46 +05:30
|
|
|
end
|
|
|
|
|
2020-11-24 15:15:51 +05:30
|
|
|
def create_subscription
|
|
|
|
subscription = JiraConnectSubscription.new(installation: jira_connect_installation, namespace: namespace)
|
|
|
|
|
|
|
|
if subscription.save
|
2021-01-29 00:20:46 +05:30
|
|
|
schedule_sync_project_jobs
|
|
|
|
|
2020-11-24 15:15:51 +05:30
|
|
|
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
|
2021-01-29 00:20:46 +05:30
|
|
|
|
|
|
|
def schedule_sync_project_jobs
|
|
|
|
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-11-24 15:15:51 +05:30
|
|
|
end
|
|
|
|
end
|