2020-05-24 23:13:21 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module DesignManagement
|
|
|
|
class SaveDesignsService < DesignService
|
|
|
|
include RunsDesignActions
|
|
|
|
include OnSuccessCallbacks
|
|
|
|
|
|
|
|
MAX_FILES = 10
|
|
|
|
|
|
|
|
def initialize(project, user, params = {})
|
|
|
|
super
|
|
|
|
|
|
|
|
@files = params.fetch(:files)
|
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
|
|
|
return error("Not allowed!") unless can_create_designs?
|
|
|
|
return error("Only #{MAX_FILES} files are allowed simultaneously") if files.size > MAX_FILES
|
2021-01-03 14:25:43 +05:30
|
|
|
return error("Duplicate filenames are not allowed!") if files.map(&:original_filename).uniq.length != files.length
|
|
|
|
return error("Design copy is in progress") if design_collection.copy_in_progress?
|
2020-05-24 23:13:21 +05:30
|
|
|
|
|
|
|
uploaded_designs, version = upload_designs!
|
|
|
|
skipped_designs = designs - uploaded_designs
|
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
create_events
|
2021-01-03 14:25:43 +05:30
|
|
|
design_collection.reset_copy!
|
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
success({ designs: uploaded_designs, version: version, skipped_designs: skipped_designs })
|
|
|
|
rescue ::ActiveRecord::RecordInvalid => e
|
|
|
|
error(e.message)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
attr_reader :files
|
|
|
|
|
|
|
|
def upload_designs!
|
|
|
|
::DesignManagement::Version.with_lock(project.id, repository) do
|
|
|
|
actions = build_actions
|
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
[
|
|
|
|
actions.map(&:design),
|
|
|
|
actions.presence && run_actions(actions)
|
|
|
|
]
|
2020-05-24 23:13:21 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Returns `Design` instances that correspond with `files`.
|
|
|
|
# New `Design`s will be created where a file name does not match
|
|
|
|
# an existing `Design`
|
|
|
|
def designs
|
|
|
|
@designs ||= files.map do |file|
|
|
|
|
collection.find_or_create_design!(filename: file.original_filename)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def build_actions
|
2020-06-23 00:09:42 +05:30
|
|
|
@actions ||= files.zip(designs).flat_map do |(file, design)|
|
2020-05-24 23:13:21 +05:30
|
|
|
Array.wrap(build_design_action(file, design))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def build_design_action(file, design)
|
|
|
|
content = file_content(file, design.full_path)
|
|
|
|
return if design_unchanged?(design, content)
|
|
|
|
|
|
|
|
action = new_file?(design) ? :create : :update
|
2020-06-23 00:09:42 +05:30
|
|
|
on_success do
|
2021-01-03 14:25:43 +05:30
|
|
|
track_usage_metrics(action)
|
2020-06-23 00:09:42 +05:30
|
|
|
end
|
2020-05-24 23:13:21 +05:30
|
|
|
|
|
|
|
DesignManagement::DesignAction.new(design, action, content)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Returns true if the design file is the same as its latest version
|
|
|
|
def design_unchanged?(design, content)
|
|
|
|
content == existing_blobs[design]&.data
|
|
|
|
end
|
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
def create_events
|
|
|
|
by_action = @actions.group_by(&:action).transform_values { |grp| grp.map(&:design) }
|
|
|
|
|
|
|
|
event_create_service.save_designs(current_user, **by_action)
|
|
|
|
end
|
|
|
|
|
|
|
|
def event_create_service
|
|
|
|
@event_create_service ||= EventCreateService.new
|
|
|
|
end
|
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
def commit_message
|
|
|
|
<<~MSG
|
|
|
|
Updated #{files.size} #{'designs'.pluralize(files.size)}
|
|
|
|
|
|
|
|
#{formatted_file_list}
|
|
|
|
MSG
|
|
|
|
end
|
|
|
|
|
|
|
|
def formatted_file_list
|
|
|
|
filenames.map { |name| "- #{name}" }.join("\n")
|
|
|
|
end
|
|
|
|
|
|
|
|
def filenames
|
|
|
|
@filenames ||= files.map(&:original_filename)
|
|
|
|
end
|
|
|
|
|
|
|
|
def can_create_designs?
|
|
|
|
Ability.allowed?(current_user, :create_design, issue)
|
|
|
|
end
|
|
|
|
|
|
|
|
def new_file?(design)
|
|
|
|
!existing_blobs[design]
|
|
|
|
end
|
|
|
|
|
|
|
|
def file_content(file, full_path)
|
|
|
|
transformer = ::Lfs::FileTransformer.new(project, repository, target_branch)
|
|
|
|
transformer.new_file(full_path, file.to_io).content
|
|
|
|
end
|
|
|
|
|
|
|
|
# Returns the latest blobs for the designs as a Hash of `{ Design => Blob }`
|
|
|
|
def existing_blobs
|
|
|
|
@existing_blobs ||= begin
|
|
|
|
items = designs.map { |d| ['HEAD', d.full_path] }
|
|
|
|
|
|
|
|
repository.blobs_at(items).each_with_object({}) do |blob, h|
|
|
|
|
design = designs.find { |d| d.full_path == blob.path }
|
|
|
|
|
|
|
|
h[design] = blob
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2021-01-03 14:25:43 +05:30
|
|
|
|
|
|
|
def track_usage_metrics(action)
|
|
|
|
if action == :update
|
|
|
|
::Gitlab::UsageDataCounters::IssueActivityUniqueCounter.track_issue_designs_modified_action(author: current_user)
|
|
|
|
else
|
|
|
|
::Gitlab::UsageDataCounters::IssueActivityUniqueCounter.track_issue_designs_added_action(author: current_user)
|
|
|
|
end
|
|
|
|
|
|
|
|
::Gitlab::UsageDataCounters::DesignsCounter.count(action)
|
|
|
|
end
|
2020-05-24 23:13:21 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
DesignManagement::SaveDesignsService.prepend_if_ee('EE::DesignManagement::SaveDesignsService')
|