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
......
...@@ -302,22 +302,22 @@ RSpec.describe Snippets::UpdateService do ...@@ -302,22 +302,22 @@ RSpec.describe Snippets::UpdateService 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) { 'CHANGELOG' } let(:file_path) { 'CHANGELOG' }
let(:content) { 'snippet_content' } let(:content) { 'snippet_content' }
let(:new_title) { 'New title' } let(:new_title) { 'New title' }
let(:snippet_files) { [{ action: 'update', previous_path: file_path, file_path: file_path, content: content }] } let(:snippet_actions) { [{ action: 'update', previous_path: file_path, file_path: file_path, content: content }] }
let(:base_opts) do let(:base_opts) do
{ {
title: new_title, title: new_title,
snippet_files: snippet_files snippet_actions: snippet_actions
} }
end end
it 'updates a snippet with the provided attributes' do it 'updates a snippet with the provided attributes' do
file_path = 'foo' file_path = 'foo'
snippet_files[0][:action] = 'move' snippet_actions[0][:action] = 'move'
snippet_files[0][:file_path] = file_path snippet_actions[0][:file_path] = file_path
response = subject response = subject
snippet = response.payload[:snippet] snippet = response.payload[:snippet]
...@@ -350,7 +350,7 @@ RSpec.describe Snippets::UpdateService do ...@@ -350,7 +350,7 @@ RSpec.describe Snippets::UpdateService do
end end
context 'when snippet_file content is not present' do context 'when snippet_file content is not present' do
let(:snippet_files) { [{ action: :move, previous_path: file_path, file_path: 'new_file_path' }] } let(:snippet_actions) { [{ action: :move, previous_path: file_path, file_path: 'new_file_path' }] }
it 'does not update snippet content' do it 'does not update snippet content' do
content = snippet.content content = snippet.content
...@@ -361,15 +361,15 @@ RSpec.describe Snippets::UpdateService do ...@@ -361,15 +361,15 @@ RSpec.describe Snippets::UpdateService 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' }] } let(:snippet_actions) { [{ action: 'invalid_action' }] }
it 'raises a validation error' do it 'raises a validation error' do
response = subject response = subject
snippet = response.payload[:snippet] snippet = response.payload[:snippet]
expect(response).to be_error expect(response).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']
end end
end end
...@@ -391,7 +391,7 @@ RSpec.describe Snippets::UpdateService do ...@@ -391,7 +391,7 @@ RSpec.describe Snippets::UpdateService do
context 'commit actions' do context 'commit actions' do
let(:new_path) { 'created_new_file' } let(:new_path) { 'created_new_file' }
let(:base_opts) { { snippet_files: snippet_files } } let(:base_opts) { { snippet_actions: snippet_actions } }
shared_examples 'returns an error' do |error_msg| shared_examples 'returns an error' do |error_msg|
specify do specify do
...@@ -403,7 +403,7 @@ RSpec.describe Snippets::UpdateService do ...@@ -403,7 +403,7 @@ RSpec.describe Snippets::UpdateService do
end end
context 'update action' do context 'update action' do
let(:snippet_files) { [{ action: :update, file_path: file_path, content: content }] } let(:snippet_actions) { [{ action: :update, file_path: file_path, content: content }] }
it 'updates the file content' do it 'updates the file content' do
expect(subject).to be_success expect(subject).to be_success
...@@ -414,7 +414,7 @@ RSpec.describe Snippets::UpdateService do ...@@ -414,7 +414,7 @@ RSpec.describe Snippets::UpdateService do
end end
context 'when previous_path is present' do context 'when previous_path is present' do
let(:snippet_files) { [{ action: :update, previous_path: file_path, file_path: file_path, content: content }] } let(:snippet_actions) { [{ action: :update, previous_path: file_path, file_path: file_path, content: content }] }
it 'updates the file content' do it 'updates the file content' do
expect(subject).to be_success expect(subject).to be_success
...@@ -426,13 +426,13 @@ RSpec.describe Snippets::UpdateService do ...@@ -426,13 +426,13 @@ RSpec.describe Snippets::UpdateService do
end end
context 'when content is not present' do context 'when content is not present' do
let(:snippet_files) { [{ action: :update, file_path: file_path }] } let(:snippet_actions) { [{ action: :update, file_path: file_path }] }
it_behaves_like 'returns an error', 'Snippet files have invalid data' it_behaves_like 'returns an error', 'Snippet actions have invalid data'
end end
context 'when file_path does not exist' do context 'when file_path does not exist' do
let(:snippet_files) { [{ action: :update, file_path: 'makeup_name', content: content }] } let(:snippet_actions) { [{ action: :update, file_path: 'makeup_name', content: content }] }
it_behaves_like 'returns an error', 'Repository Error updating the snippet' it_behaves_like 'returns an error', 'Repository Error updating the snippet'
end end
...@@ -440,13 +440,13 @@ RSpec.describe Snippets::UpdateService do ...@@ -440,13 +440,13 @@ RSpec.describe Snippets::UpdateService do
context 'move action' do context 'move action' do
context 'when file_path and previous_path are the same' do context 'when file_path and previous_path are the same' do
let(:snippet_files) { [{ action: :move, previous_path: file_path, file_path: file_path }] } let(:snippet_actions) { [{ action: :move, previous_path: file_path, file_path: file_path }] }
it_behaves_like 'returns an error', 'Snippet files have invalid data' it_behaves_like 'returns an error', 'Snippet actions have invalid data'
end end
context 'when file_path and previous_path are different' do context 'when file_path and previous_path are different' do
let(:snippet_files) { [{ action: :move, previous_path: file_path, file_path: new_path }] } let(:snippet_actions) { [{ action: :move, previous_path: file_path, file_path: new_path }] }
it 'renames the file' do it 'renames the file' do
old_blob = blob(file_path) old_blob = blob(file_path)
...@@ -461,13 +461,13 @@ RSpec.describe Snippets::UpdateService do ...@@ -461,13 +461,13 @@ RSpec.describe Snippets::UpdateService do
end end
context 'when previous_path does not exist' do context 'when previous_path does not exist' do
let(:snippet_files) { [{ action: :move, previous_path: 'makeup_name', file_path: new_path }] } let(:snippet_actions) { [{ action: :move, previous_path: 'makeup_name', file_path: new_path }] }
it_behaves_like 'returns an error', 'Repository Error updating the snippet' it_behaves_like 'returns an error', 'Repository Error updating the snippet'
end end
context 'when user wants to rename the file and update content' do context 'when user wants to rename the file and update content' do
let(:snippet_files) { [{ action: :move, previous_path: file_path, file_path: new_path, content: content }] } let(:snippet_actions) { [{ action: :move, previous_path: file_path, file_path: new_path, content: content }] }
it 'performs both operations' do it 'performs both operations' do
expect(subject).to be_success expect(subject).to be_success
...@@ -481,7 +481,7 @@ RSpec.describe Snippets::UpdateService do ...@@ -481,7 +481,7 @@ RSpec.describe Snippets::UpdateService do
end end
context 'delete action' do context 'delete action' do
let(:snippet_files) { [{ action: :delete, file_path: file_path }] } let(:snippet_actions) { [{ action: :delete, file_path: file_path }] }
shared_examples 'deletes the file' do shared_examples 'deletes the file' do
specify do specify do
...@@ -496,32 +496,32 @@ RSpec.describe Snippets::UpdateService do ...@@ -496,32 +496,32 @@ RSpec.describe Snippets::UpdateService do
it_behaves_like 'deletes the file' it_behaves_like 'deletes the file'
context 'when previous_path is present and same as file_path' do context 'when previous_path is present and same as file_path' do
let(:snippet_files) { [{ action: :delete, previous_path: file_path, file_path: file_path }] } let(:snippet_actions) { [{ action: :delete, previous_path: file_path, file_path: file_path }] }
it_behaves_like 'deletes the file' it_behaves_like 'deletes the file'
end end
context 'when previous_path is present and is different from file_path' do context 'when previous_path is present and is different from file_path' do
let(:snippet_files) { [{ action: :delete, previous_path: 'foo', file_path: file_path }] } let(:snippet_actions) { [{ action: :delete, previous_path: 'foo', file_path: file_path }] }
it_behaves_like 'deletes the file' it_behaves_like 'deletes the file'
end end
context 'when content is present' do context 'when content is present' do
let(:snippet_files) { [{ action: :delete, file_path: file_path, content: 'foo' }] } let(:snippet_actions) { [{ action: :delete, file_path: file_path, content: 'foo' }] }
it_behaves_like 'deletes the file' it_behaves_like 'deletes the file'
end end
context 'when file_path does not exist' do context 'when file_path does not exist' do
let(:snippet_files) { [{ action: :delete, file_path: 'makeup_name' }] } let(:snippet_actions) { [{ action: :delete, file_path: 'makeup_name' }] }
it_behaves_like 'returns an error', 'Repository Error updating the snippet' it_behaves_like 'returns an error', 'Repository Error updating the snippet'
end end
end end
context 'create action' do context 'create action' do
let(:snippet_files) { [{ action: :create, file_path: new_path, content: content }] } let(:snippet_actions) { [{ action: :create, file_path: new_path, content: content }] }
it 'creates the file' do it 'creates the file' do
expect(subject).to be_success expect(subject).to be_success
...@@ -532,13 +532,13 @@ RSpec.describe Snippets::UpdateService do ...@@ -532,13 +532,13 @@ RSpec.describe Snippets::UpdateService do
end end
context 'when content is not present' do context 'when content is not present' do
let(:snippet_files) { [{ action: :create, file_path: new_path }] } let(:snippet_actions) { [{ action: :create, file_path: new_path }] }
it_behaves_like 'returns an error', 'Snippet files have invalid data' it_behaves_like 'returns an error', 'Snippet actions have invalid data'
end end
context 'when file_path is not present or empty' do context 'when file_path is not present or empty' do
let(:snippet_files) { [{ action: :create, content: content }, { action: :create, file_path: '', content: content }] } let(:snippet_actions) { [{ action: :create, content: content }, { action: :create, file_path: '', content: content }] }
it 'generates the file path for the files' do it 'generates the file path for the files' do
expect(blob('snippetfile1.txt')).to be_nil expect(blob('snippetfile1.txt')).to be_nil
...@@ -552,13 +552,13 @@ RSpec.describe Snippets::UpdateService do ...@@ -552,13 +552,13 @@ RSpec.describe Snippets::UpdateService do
end end
context 'when file_path already exists in the repository' do context 'when file_path already exists in the repository' do
let(:snippet_files) { [{ action: :create, file_path: file_path, content: content }] } let(:snippet_actions) { [{ action: :create, file_path: file_path, content: content }] }
it_behaves_like 'returns an error', 'Repository Error updating the snippet' it_behaves_like 'returns an error', 'Repository Error updating the snippet'
end end
context 'when previous_path is present' do context 'when previous_path is present' do
let(:snippet_files) { [{ action: :create, previous_path: 'foo', file_path: new_path, content: content }] } let(:snippet_actions) { [{ action: :create, previous_path: 'foo', file_path: new_path, content: content }] }
it 'creates the file' do it 'creates the file' do
expect(subject).to be_success expect(subject).to be_success
...@@ -577,7 +577,7 @@ RSpec.describe Snippets::UpdateService do ...@@ -577,7 +577,7 @@ RSpec.describe Snippets::UpdateService do
let(:move_previous_path) { 'VERSION' } let(:move_previous_path) { 'VERSION' }
let(:move_file_path) { 'VERSION_new' } let(:move_file_path) { 'VERSION_new' }
let(:snippet_files) do let(:snippet_actions) do
[ [
{ action: :create, file_path: create_file_path, content: content }, { action: :create, file_path: create_file_path, content: content },
{ action: :update, file_path: update_file_path, content: content }, { action: :update, file_path: update_file_path, content: content },
...@@ -678,7 +678,7 @@ RSpec.describe Snippets::UpdateService do ...@@ -678,7 +678,7 @@ RSpec.describe Snippets::UpdateService do
it_behaves_like 'updates repository content' it_behaves_like 'updates repository content'
it_behaves_like 'commit operation fails' it_behaves_like 'commit operation fails'
it_behaves_like 'committable attributes' it_behaves_like 'committable attributes'
it_behaves_like 'when snippet_files param is present' it_behaves_like 'when snippet_actions param is present'
it_behaves_like 'only file_name is present' it_behaves_like 'only file_name is present'
it_behaves_like 'only content is present' it_behaves_like 'only content is present'
it_behaves_like 'snippets spam check is performed' do it_behaves_like 'snippets spam check is performed' do
...@@ -705,7 +705,7 @@ RSpec.describe Snippets::UpdateService do ...@@ -705,7 +705,7 @@ RSpec.describe Snippets::UpdateService do
it_behaves_like 'updates repository content' it_behaves_like 'updates repository content'
it_behaves_like 'commit operation fails' it_behaves_like 'commit operation fails'
it_behaves_like 'committable attributes' it_behaves_like 'committable attributes'
it_behaves_like 'when snippet_files param is present' it_behaves_like 'when snippet_actions param is present'
it_behaves_like 'only file_name is present' it_behaves_like 'only file_name is present'
it_behaves_like 'only content is present' it_behaves_like 'only content is present'
it_behaves_like 'snippets spam check is performed' do it_behaves_like 'snippets spam check is performed' do
......
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