Rename snippet_files to snippet_actions in snippet services

parent 27b98a2a
...@@ -85,9 +85,9 @@ module Mutations ...@@ -85,9 +85,9 @@ module Mutations
def create_params(args) def create_params(args)
args.tap do |create_args| args.tap do |create_args|
# We need to rename `files` into `snippet_files` because # We need to rename `files` into `snippet_actions` because
# it's the expected key param # it's the expected key param
create_args[:snippet_files] = create_args.delete(:files)&.map(&:to_h) create_args[:snippet_actions] = create_args.delete(:files)&.map(&:to_h)
# We need to rename `uploaded_files` into `files` because # We need to rename `uploaded_files` into `files` because
# it's the expected key param # it's the expected key param
......
...@@ -56,9 +56,9 @@ module Mutations ...@@ -56,9 +56,9 @@ module Mutations
def update_params(args) def update_params(args)
args.tap do |update_args| args.tap do |update_args|
# We need to rename `files` into `snippet_files` because # We need to rename `files` into `snippet_actions` because
# it's the expected key param # it's the expected key param
update_args[:snippet_files] = update_args.delete(:files)&.map(&:to_h) update_args[:snippet_actions] = update_args.delete(:files)&.map(&:to_h)
end end
end end
end end
......
...@@ -6,15 +6,15 @@ module Snippets ...@@ -6,15 +6,15 @@ module Snippets
CreateRepositoryError = Class.new(StandardError) CreateRepositoryError = Class.new(StandardError)
attr_reader :uploaded_assets, :snippet_files attr_reader :uploaded_assets, :snippet_actions
def initialize(project, user = nil, params = {}) def initialize(project, user = nil, params = {})
super super
@uploaded_assets = Array(@params.delete(:files).presence) @uploaded_assets = Array(@params.delete(:files).presence)
input_actions = Array(@params.delete(:snippet_files).presence) input_actions = Array(@params.delete(:snippet_actions).presence)
@snippet_files = SnippetInputActionCollection.new(input_actions, allowed_actions: restricted_files_actions) @snippet_actions = SnippetInputActionCollection.new(input_actions, allowed_actions: restricted_files_actions)
filter_spam_check_params filter_spam_check_params
end end
...@@ -32,18 +32,18 @@ module Snippets ...@@ -32,18 +32,18 @@ module Snippets
end end
def valid_params? def valid_params?
return true if snippet_files.empty? return true if snippet_actions.empty?
(params.keys & [:content, :file_name]).none? && snippet_files.valid? (params.keys & [:content, :file_name]).none? && snippet_actions.valid?
end end
def invalid_params_error(snippet) def invalid_params_error(snippet)
if snippet_files.valid? if snippet_actions.valid?
[:content, :file_name].each do |key| [:content, :file_name].each do |key|
snippet.errors.add(key, 'and snippet files cannot be used together') if params.key?(key) snippet.errors.add(key, 'and snippet files cannot be used together') if params.key?(key)
end end
else else
snippet.errors.add(:snippet_files, 'have invalid data') snippet.errors.add(:snippet_actions, 'have invalid data')
end end
snippet_error_response(snippet, 403) snippet_error_response(snippet, 403)
...@@ -75,7 +75,7 @@ module Snippets ...@@ -75,7 +75,7 @@ module Snippets
end end
def files_to_commit(snippet) def files_to_commit(snippet)
snippet_files.to_commit_actions.presence || build_actions_from_params(snippet) snippet_actions.to_commit_actions.presence || build_actions_from_params(snippet)
end end
def build_actions_from_params(snippet) def build_actions_from_params(snippet)
......
...@@ -37,13 +37,13 @@ module Snippets ...@@ -37,13 +37,13 @@ module Snippets
end end
end end
# If the snippet_files param is present # If the snippet_actions param is present
# we need to fill content and file_name from # we need to fill content and file_name from
# the model # the model
def create_params def create_params
return params if snippet_files.empty? return params if snippet_actions.empty?
params.merge(content: snippet_files[0].content, file_name: snippet_files[0].file_path) params.merge(content: snippet_actions[0].content, file_name: snippet_actions[0].file_path)
end end
def save_and_commit def save_and_commit
......
...@@ -37,9 +37,9 @@ module Snippets ...@@ -37,9 +37,9 @@ module Snippets
# is implemented. # is implemented.
# Once we can perform different operations through this service # Once we can perform different operations through this service
# we won't need to keep track of the `content` and `file_name` fields # we won't need to keep track of the `content` and `file_name` fields
if snippet_files.any? if snippet_actions.any?
params[:content] = snippet_files[0].content if snippet_files[0].content params[:content] = snippet_actions[0].content if snippet_actions[0].content
params[:file_name] = snippet_files[0].file_path params[:file_name] = snippet_actions[0].file_path
end end
snippet.assign_attributes(params) snippet.assign_attributes(params)
...@@ -109,7 +109,7 @@ module Snippets ...@@ -109,7 +109,7 @@ module Snippets
end end
def committable_attributes? def committable_attributes?
(params.stringify_keys.keys & COMMITTABLE_ATTRIBUTES).present? || snippet_files.any? (params.stringify_keys.keys & COMMITTABLE_ATTRIBUTES).present? || snippet_actions.any?
end end
def build_actions_from_params(snippet) def build_actions_from_params(snippet)
......
...@@ -174,7 +174,7 @@ RSpec.describe 'Creating a Snippet' do ...@@ -174,7 +174,7 @@ RSpec.describe 'Creating a Snippet' do
context 'when action is invalid' do context 'when action is invalid' do
let(:file_1) { { filePath: 'example_file1' }} let(:file_1) { { filePath: 'example_file1' }}
it_behaves_like 'a mutation that returns errors in the response', errors: ['Snippet files have invalid data'] it_behaves_like 'a mutation that returns errors in the response', errors: ['Snippet actions have invalid data']
it_behaves_like 'does not create snippet' it_behaves_like 'does not create snippet'
end end
end end
......
...@@ -228,15 +228,15 @@ RSpec.describe Snippets::CreateService do ...@@ -228,15 +228,15 @@ RSpec.describe Snippets::CreateService do
end end
end end
shared_examples 'when snippet_files param is present' do shared_examples 'when snippet_actions param is present' do
let(:file_path) { 'snippet_file_path.rb' } let(:file_path) { 'snippet_file_path.rb' }
let(:content) { 'snippet_content' } let(:content) { 'snippet_content' }
let(:snippet_files) { [{ action: 'create', file_path: file_path, content: content }] } let(:snippet_actions) { [{ action: 'create', file_path: file_path, content: content }] }
let(:base_opts) do let(:base_opts) do
{ {
title: 'Test snippet', title: 'Test snippet',
visibility_level: Gitlab::VisibilityLevel::PRIVATE, visibility_level: Gitlab::VisibilityLevel::PRIVATE,
snippet_files: snippet_files snippet_actions: snippet_actions
} }
end end
...@@ -266,28 +266,28 @@ RSpec.describe Snippets::CreateService do ...@@ -266,28 +266,28 @@ RSpec.describe Snippets::CreateService do
end end
end end
context 'when snippet_files param is invalid' do context 'when snippet_actions param is invalid' do
let(:snippet_files) { [{ action: 'invalid_action', file_path: 'snippet_file_path.rb', content: 'snippet_content' }] } let(:snippet_actions) { [{ action: 'invalid_action', file_path: 'snippet_file_path.rb', content: 'snippet_content' }] }
it 'a validation error is raised' do it 'a validation error is raised' do
expect(subject).to be_error expect(subject).to be_error
expect(snippet.errors.full_messages_for(:snippet_files)).to eq ['Snippet files have invalid data'] expect(snippet.errors.full_messages_for(:snippet_actions)).to eq ['Snippet actions have invalid data']
expect(snippet.repository.exists?).to be_falsey expect(snippet.repository.exists?).to be_falsey
end end
end end
context 'when snippet_files contain an action different from "create"' do context 'when snippet_actions contain an action different from "create"' do
let(:snippet_files) { [{ action: 'delete', file_path: 'snippet_file_path.rb' }] } let(:snippet_actions) { [{ action: 'delete', file_path: 'snippet_file_path.rb' }] }
it 'a validation error is raised' do it 'a validation error is raised' do
expect(subject).to be_error expect(subject).to be_error
expect(snippet.errors.full_messages_for(:snippet_files)).to eq ['Snippet files have invalid data'] expect(snippet.errors.full_messages_for(:snippet_actions)).to eq ['Snippet actions have invalid data']
expect(snippet.repository.exists?).to be_falsey expect(snippet.repository.exists?).to be_falsey
end end
end end
context 'when "create" operation does not have file_path or is empty' do context 'when "create" operation does not have file_path or is empty' do
let(:snippet_files) { [{ action: 'create', content: content }, { action: 'create', content: content, file_path: '' }] } let(:snippet_actions) { [{ action: 'create', content: content }, { action: 'create', content: content, file_path: '' }] }
it 'generates the file path for the files' do it 'generates the file path for the files' do
expect(subject).to be_success expect(subject).to be_success
...@@ -311,7 +311,7 @@ RSpec.describe Snippets::CreateService do ...@@ -311,7 +311,7 @@ RSpec.describe Snippets::CreateService do
it_behaves_like 'an error service response when save fails' it_behaves_like 'an error service response when save fails'
it_behaves_like 'creates repository and files' it_behaves_like 'creates repository and files'
it_behaves_like 'after_save callback to store_mentions', ProjectSnippet it_behaves_like 'after_save callback to store_mentions', ProjectSnippet
it_behaves_like 'when snippet_files param is present' it_behaves_like 'when snippet_actions param is present'
context 'when uploaded files are passed to the service' do context 'when uploaded files are passed to the service' do
let(:extra_opts) { { files: ['foo'] } } let(:extra_opts) { { files: ['foo'] } }
...@@ -338,7 +338,7 @@ RSpec.describe Snippets::CreateService do ...@@ -338,7 +338,7 @@ RSpec.describe Snippets::CreateService do
it_behaves_like 'an error service response when save fails' it_behaves_like 'an error service response when save fails'
it_behaves_like 'creates repository and files' it_behaves_like 'creates repository and files'
it_behaves_like 'after_save callback to store_mentions', PersonalSnippet it_behaves_like 'after_save callback to store_mentions', PersonalSnippet
it_behaves_like 'when snippet_files param is present' it_behaves_like 'when snippet_actions param is present'
context 'when the snippet description contains files' do context 'when the snippet description contains files' do
include FileMoverHelpers include FileMoverHelpers
......
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