debian-mirror-gitlab/app/graphql/mutations/design_management/delete.rb

67 lines
1.9 KiB
Ruby
Raw Normal View History

2020-05-24 23:13:21 +05:30
# frozen_string_literal: true
module Mutations
module DesignManagement
class Delete < Base
graphql_name "DesignManagementDelete"
2022-04-04 11:22:00 +05:30
Errors = ::Gitlab::Graphql::Errors
2021-10-27 15:23:28 +05:30
argument :filenames, [GraphQL::Types::String],
2020-05-24 23:13:21 +05:30
required: true,
2021-10-27 15:23:28 +05:30
description: "Filenames of the designs to delete.",
2020-05-24 23:13:21 +05:30
prepare: ->(names, _ctx) do
names.presence || (raise Errors::ArgumentError, 'no filenames')
end
field :version, Types::DesignManagement::VersionType,
null: true, # null on error
2021-10-27 15:23:28 +05:30
description: 'New version in which the designs are deleted.'
2020-05-24 23:13:21 +05:30
authorize :destroy_design
def resolve(project_path:, iid:, filenames:)
issue = authorized_find!(project_path: project_path, iid: iid)
project = issue.project
designs = resolve_designs(issue, filenames)
result = ::DesignManagement::DeleteDesignsService
.new(project, current_user, issue: issue, designs: designs)
.execute
{
version: result[:version],
errors: Array.wrap(result[:message])
}
end
private
# Here we check that:
# * we find exactly as many designs as filenames
def resolve_designs(issue, filenames)
designs = issue.design_collection.designs_by_filename(filenames)
validate_all_were_found!(designs, filenames)
designs
end
def validate_all_were_found!(designs, filenames)
found_filenames = designs.map(&:filename)
missing = filenames.difference(found_filenames)
if missing.present?
raise Errors::ArgumentError, <<~MSG
Not all the designs you named currently exist.
The following filenames were not found:
#{missing.join(', ')}
They may have already been deleted.
MSG
end
end
end
end
end