Commit bf40d3b9 authored by Jan Provaznik's avatar Jan Provaznik

Merge branch '208864-match-snippet-name-with-more-than-one-digit' into 'master'

Resolve "Match snippet name with more than one digit"

Closes #208864

See merge request gitlab-org/gitlab!26429
parents ae38f42f d3303176
......@@ -4,7 +4,7 @@ class SnippetRepository < ApplicationRecord
include Shardable
DEFAULT_EMPTY_FILE_NAME = 'snippetfile'
EMPTY_FILE_PATTERN = /^#{DEFAULT_EMPTY_FILE_NAME}(\d)\.txt$/.freeze
EMPTY_FILE_PATTERN = /^#{DEFAULT_EMPTY_FILE_NAME}(\d+)\.txt$/.freeze
CommitError = Class.new(StandardError)
......@@ -51,14 +51,14 @@ class SnippetRepository < ApplicationRecord
end
def transform_file_entries(files)
last_index = get_last_empty_file_index
next_index = get_last_empty_file_index + 1
files.each do |file_entry|
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(last_index)
last_index += 1
file_entry[:file_path] = build_empty_file_name(next_index)
next_index += 1
end
end
end
......@@ -70,12 +70,10 @@ class SnippetRepository < ApplicationRecord
end
def get_last_empty_file_index
last_file = repository.ls_files(nil)
.map! { |file| file.match(EMPTY_FILE_PATTERN) }
.compact
.max_by { |element| element[1] }
last_file ? (last_file[1].to_i + 1) : 1
repository.ls_files(nil).inject(0) do |max, file|
idx = file[EMPTY_FILE_PATTERN, 1].to_i
[idx, max].max
end
end
def build_empty_file_name(index)
......
......@@ -168,34 +168,42 @@ describe SnippetRepository do
end
end
context 'when files are not named' do
let(:data) do
[
{
file_path: '',
content: 'foo',
action: :create
},
{
file_path: '',
content: 'bar',
action: :create
},
{
file_path: 'foo.txt',
content: 'bar',
action: :create
}
]
shared_examples 'snippet repository with file names' do |*filenames|
it 'sets a name for unnamed files' do
ls_files = snippet.repository.ls_files(nil)
expect(ls_files).to include(*filenames)
end
end
let_it_be(:named_snippet) { { file_path: 'fee.txt', content: 'bar', action: :create } }
let_it_be(:unnamed_snippet) { { file_path: '', content: 'dummy', action: :create } }
it 'sets a name for non named files' do
context 'when some files are not named' do
let(:data) { [named_snippet] + Array.new(2) { unnamed_snippet.clone } }
before do
expect do
snippet_repository.multi_files_action(user, data, commit_opts)
end.not_to raise_error
end
it_behaves_like 'snippet repository with file names', 'snippetfile1.txt', 'snippetfile2.txt'
end
expect(snippet.repository.ls_files(nil)).to include('snippetfile1.txt', 'snippetfile2.txt', 'foo.txt')
context 'repository already has 10 unnamed snippets' do
let(:pre_populate_data) { Array.new(10) { unnamed_snippet.clone } }
let(:data) { [named_snippet] + Array.new(2) { unnamed_snippet.clone } }
before do
# Pre-populate repository with 9 unnamed snippets.
snippet_repository.multi_files_action(user, pre_populate_data, commit_opts)
expect do
snippet_repository.multi_files_action(user, data, commit_opts)
end.not_to raise_error
end
it_behaves_like 'snippet repository with file names', 'snippetfile10.txt', 'snippetfile11.txt'
end
end
......
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