Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
gitlab-ce
Commits
602f5d08
Commit
602f5d08
authored
Mar 20, 2020
by
Francisco Javier López
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Reuse default generated snippet name
parent
5ac9eb84
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
57 additions
and
5 deletions
+57
-5
app/models/snippet_repository.rb
app/models/snippet_repository.rb
+17
-5
changelogs/unreleased/fj-reuse-default-snippet-name.yml
changelogs/unreleased/fj-reuse-default-snippet-name.yml
+5
-0
spec/models/snippet_repository_spec.rb
spec/models/snippet_repository_spec.rb
+35
-0
No files found.
app/models/snippet_repository.rb
View file @
602f5d08
...
...
@@ -48,15 +48,27 @@ class SnippetRepository < ApplicationRecord
next_index
=
get_last_empty_file_index
+
1
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
]
if
file_entry
[
:file_path
].
blank?
file_entry
[
:file_path
]
=
build_empty_file_name
(
next_index
)
next_index
+=
1
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
)
return
:create
if
file_entry
[
:previous_path
].
blank?
...
...
changelogs/unreleased/fj-reuse-default-snippet-name.yml
0 → 100644
View file @
602f5d08
---
title
:
Reuse default generated snippet file name in repository
merge_request
:
27673
author
:
type
:
fixed
spec/models/snippet_repository_spec.rb
View file @
602f5d08
...
...
@@ -140,6 +140,41 @@ describe SnippetRepository do
let_it_be
(
:named_snippet
)
{
{
file_path:
'fee.txt'
,
content:
'bar'
,
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
let
(
:data
)
{
[
named_snippet
]
+
Array
.
new
(
2
)
{
unnamed_snippet
.
clone
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment