Commit 42e63196 authored by Peter Leitzen's avatar Peter Leitzen

Merge branch 'fj-reuse-default-snippet-name' into 'master'

Reuse default generated snippet name

Closes #211931

See merge request gitlab-org/gitlab!27673
parents afee38dd 602f5d08
...@@ -48,15 +48,27 @@ class SnippetRepository < ApplicationRecord ...@@ -48,15 +48,27 @@ class SnippetRepository < ApplicationRecord
next_index = get_last_empty_file_index + 1 next_index = get_last_empty_file_index + 1
files.each do |file_entry| files.each do |file_entry|
file_entry[:file_path] = file_path_for(file_entry, next_index) { next_index += 1 }
file_entry[:action] = infer_action(file_entry) unless file_entry[:action] file_entry[:action] = infer_action(file_entry) unless file_entry[:action]
if file_entry[:file_path].blank?
file_entry[:file_path] = build_empty_file_name(next_index)
next_index += 1
end
end end
end end
def file_path_for(file_entry, next_index)
return file_entry[:file_path] if file_entry[:file_path].present?
return file_entry[:previous_path] if reuse_previous_path?(file_entry)
build_empty_file_name(next_index).tap { yield }
end
# If the user removed the file_path and the previous_path
# matches the EMPTY_FILE_PATTERN, we don't need to
# rename the file and build a new empty file name,
# we can just reuse the existing file name
def reuse_previous_path?(file_entry)
file_entry[:file_path].blank? &&
EMPTY_FILE_PATTERN.match?(file_entry[:previous_path])
end
def infer_action(file_entry) def infer_action(file_entry)
return :create if file_entry[:previous_path].blank? return :create if file_entry[:previous_path].blank?
......
---
title: Reuse default generated snippet file name in repository
merge_request: 27673
author:
type: fixed
...@@ -140,6 +140,41 @@ describe SnippetRepository do ...@@ -140,6 +140,41 @@ describe SnippetRepository do
let_it_be(:named_snippet) { { file_path: 'fee.txt', content: 'bar', action: :create } } let_it_be(:named_snippet) { { file_path: 'fee.txt', content: 'bar', action: :create } }
let_it_be(:unnamed_snippet) { { file_path: '', content: 'dummy', action: :create } } let_it_be(:unnamed_snippet) { { file_path: '', content: 'dummy', action: :create } }
context 'when existing file has a default name' do
let(:default_name) { 'snippetfile1.txt' }
let(:new_file) { { file_path: '', content: 'bar' } }
let(:existing_file) { { previous_path: default_name, file_path: '', content: 'new_content' } }
before do
expect(blob_at(snippet, default_name)).to be_nil
snippet_repository.multi_files_action(user, [new_file], commit_opts)
expect(blob_at(snippet, default_name)).to be
end
it 'reuses the existing file name' do
snippet_repository.multi_files_action(user, [existing_file], commit_opts)
blob = blob_at(snippet, default_name)
expect(blob.data).to eq existing_file[:content]
end
end
context 'when file name consists of one or several whitespaces' do
let(:default_name) { 'snippetfile1.txt' }
let(:new_file) { { file_path: ' ', content: 'bar' } }
it 'assigns a new name to the file' do
expect(blob_at(snippet, default_name)).to be_nil
snippet_repository.multi_files_action(user, [new_file], commit_opts)
blob = blob_at(snippet, default_name)
expect(blob.data).to eq new_file[:content]
end
end
context 'when some files are not named' do context 'when some files are not named' do
let(:data) { [named_snippet] + Array.new(2) { unnamed_snippet.clone } } let(:data) { [named_snippet] + Array.new(2) { unnamed_snippet.clone } }
......
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