Commit 0aad74c8 authored by Stan Hu's avatar Stan Hu

Merge branch 'fj-228828-rename-snippet-files-to-snippet-actions' into 'master'

Rename snippet_files to snippet_actions in snippet services

See merge request gitlab-org/gitlab!36841
parents 7f2bd61f 1399dd2e
......@@ -85,9 +85,9 @@ module Mutations
def create_params(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
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
# it's the expected key param
......
......@@ -56,9 +56,9 @@ module Mutations
def update_params(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
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
......
......@@ -6,15 +6,15 @@ module Snippets
CreateRepositoryError = Class.new(StandardError)
attr_reader :uploaded_assets, :snippet_files
attr_reader :uploaded_assets, :snippet_actions
def initialize(project, user = nil, params = {})
super
@uploaded_assets = Array(@params.delete(:files).presence)
input_actions = Array(@params.delete(:snippet_files).presence)
@snippet_files = SnippetInputActionCollection.new(input_actions, allowed_actions: restricted_files_actions)
input_actions = Array(@params.delete(:snippet_actions).presence)
@snippet_actions = SnippetInputActionCollection.new(input_actions, allowed_actions: restricted_files_actions)
filter_spam_check_params
end
......@@ -32,18 +32,18 @@ module Snippets
end
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
def invalid_params_error(snippet)
if snippet_files.valid?
if snippet_actions.valid?
[:content, :file_name].each do |key|
snippet.errors.add(key, 'and snippet files cannot be used together') if params.key?(key)
end
else
snippet.errors.add(:snippet_files, 'have invalid data')
snippet.errors.add(:snippet_actions, 'have invalid data')
end
snippet_error_response(snippet, 403)
......@@ -75,7 +75,7 @@ module Snippets
end
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
def build_actions_from_params(snippet)
......
......@@ -37,13 +37,13 @@ module Snippets
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
# the model
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
def save_and_commit
......
......@@ -37,9 +37,9 @@ module Snippets
# is implemented.
# Once we can perform different operations through this service
# we won't need to keep track of the `content` and `file_name` fields
if snippet_files.any?
params[:content] = snippet_files[0].content if snippet_files[0].content
params[:file_name] = snippet_files[0].file_path
if snippet_actions.any?
params[:content] = snippet_actions[0].content if snippet_actions[0].content
params[:file_name] = snippet_actions[0].file_path
end
snippet.assign_attributes(params)
......@@ -109,7 +109,7 @@ module Snippets
end
def committable_attributes?
(params.stringify_keys.keys & COMMITTABLE_ATTRIBUTES).present? || snippet_files.any?
(params.stringify_keys.keys & COMMITTABLE_ATTRIBUTES).present? || snippet_actions.any?
end
def build_actions_from_params(snippet)
......
......@@ -174,7 +174,7 @@ RSpec.describe 'Creating a Snippet' do
context 'when action is invalid' do
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'
end
end
......
......@@ -228,15 +228,15 @@ RSpec.describe Snippets::CreateService do
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(: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
{
title: 'Test snippet',
visibility_level: Gitlab::VisibilityLevel::PRIVATE,
snippet_files: snippet_files
snippet_actions: snippet_actions
}
end
......@@ -266,28 +266,28 @@ RSpec.describe Snippets::CreateService do
end
end
context 'when snippet_files param is invalid' do
let(:snippet_files) { [{ action: 'invalid_action', file_path: 'snippet_file_path.rb', content: 'snippet_content' }] }
context 'when snippet_actions param is invalid' do
let(:snippet_actions) { [{ action: 'invalid_action', file_path: 'snippet_file_path.rb', content: 'snippet_content' }] }
it 'a validation error is raised' do
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
end
end
context 'when snippet_files contain an action different from "create"' do
let(:snippet_files) { [{ action: 'delete', file_path: 'snippet_file_path.rb' }] }
context 'when snippet_actions contain an action different from "create"' do
let(:snippet_actions) { [{ action: 'delete', file_path: 'snippet_file_path.rb' }] }
it 'a validation error is raised' do
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
end
end
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
expect(subject).to be_success
......@@ -311,7 +311,7 @@ RSpec.describe Snippets::CreateService do
it_behaves_like 'an error service response when save fails'
it_behaves_like 'creates repository and files'
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
let(:extra_opts) { { files: ['foo'] } }
......@@ -338,7 +338,7 @@ RSpec.describe Snippets::CreateService do
it_behaves_like 'an error service response when save fails'
it_behaves_like 'creates repository and files'
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
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