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
0
Merge Requests
0
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
Léo-Paul Géneau
gitlab-ce
Commits
1110def8
Commit
1110def8
authored
Jun 06, 2017
by
Roman Safronov
Committed by
Grzegorz Bizon
Jun 06, 2017
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Introduce optimistic locking support via optional parameter last_commit_id on File Update API
parent
9f3a1203
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
33 additions
and
2 deletions
+33
-2
changelogs/unreleased/32642_last_commit_id_in_file_api.yml
changelogs/unreleased/32642_last_commit_id_in_file_api.yml
+4
-0
doc/api/repository_files.md
doc/api/repository_files.md
+1
-0
lib/api/files.rb
lib/api/files.rb
+9
-2
spec/requests/api/files_spec.rb
spec/requests/api/files_spec.rb
+19
-0
No files found.
changelogs/unreleased/32642_last_commit_id_in_file_api.yml
0 → 100644
View file @
1110def8
---
title
:
'
Introduce
optimistic
locking
support
via
optional
parameter
last_commit_sha
on
File
Update
API'
merge_request
:
11694
author
:
electroma
doc/api/repository_files.md
View file @
1110def8
...
@@ -111,6 +111,7 @@ Parameters:
...
@@ -111,6 +111,7 @@ Parameters:
-
`author_name`
(optional) - Specify the commit author's name
-
`author_name`
(optional) - Specify the commit author's name
-
`content`
(required) - New file content
-
`content`
(required) - New file content
-
`commit_message`
(required) - Commit message
-
`commit_message`
(required) - Commit message
-
`last_commit_id`
(optional) - Last known file commit id
If the commit fails for any reason we return a 400 error with a non-specific
If the commit fails for any reason we return a 400 error with a non-specific
error message. Possible causes for a failed commit include:
error message. Possible causes for a failed commit include:
...
...
lib/api/files.rb
View file @
1110def8
...
@@ -10,7 +10,8 @@ module API
...
@@ -10,7 +10,8 @@ module API
file_content:
attrs
[
:content
],
file_content:
attrs
[
:content
],
file_content_encoding:
attrs
[
:encoding
],
file_content_encoding:
attrs
[
:encoding
],
author_email:
attrs
[
:author_email
],
author_email:
attrs
[
:author_email
],
author_name:
attrs
[
:author_name
]
author_name:
attrs
[
:author_name
],
last_commit_sha:
attrs
[
:last_commit_id
]
}
}
end
end
...
@@ -46,6 +47,7 @@ module API
...
@@ -46,6 +47,7 @@ module API
use
:simple_file_params
use
:simple_file_params
requires
:content
,
type:
String
,
desc:
'File content'
requires
:content
,
type:
String
,
desc:
'File content'
optional
:encoding
,
type:
String
,
values:
%w[base64]
,
desc:
'File encoding'
optional
:encoding
,
type:
String
,
values:
%w[base64]
,
desc:
'File encoding'
optional
:last_commit_id
,
type:
String
,
desc:
'Last known commit id for this file'
end
end
end
end
...
@@ -111,7 +113,12 @@ module API
...
@@ -111,7 +113,12 @@ module API
authorize!
:push_code
,
user_project
authorize!
:push_code
,
user_project
file_params
=
declared_params
(
include_missing:
false
)
file_params
=
declared_params
(
include_missing:
false
)
result
=
::
Files
::
UpdateService
.
new
(
user_project
,
current_user
,
commit_params
(
file_params
)).
execute
begin
result
=
::
Files
::
UpdateService
.
new
(
user_project
,
current_user
,
commit_params
(
file_params
)).
execute
rescue
::
Files
::
UpdateService
::
FileChangedError
=>
e
render_api_error!
(
e
.
message
,
400
)
end
if
result
[
:status
]
==
:success
if
result
[
:status
]
==
:success
status
(
200
)
status
(
200
)
...
...
spec/requests/api/files_spec.rb
View file @
1110def8
...
@@ -258,6 +258,25 @@ describe API::Files do
...
@@ -258,6 +258,25 @@ describe API::Files do
expect
(
last_commit
.
author_name
).
to
eq
(
user
.
name
)
expect
(
last_commit
.
author_name
).
to
eq
(
user
.
name
)
end
end
it
"returns a 400 bad request if update existing file with stale last commit id"
do
params_with_stale_id
=
valid_params
.
merge
(
last_commit_id:
'stale'
)
put
api
(
route
(
file_path
),
user
),
params_with_stale_id
expect
(
response
).
to
have_http_status
(
400
)
expect
(
json_response
[
'message'
]).
to
eq
(
'You are attempting to update a file that has changed since you started editing it.'
)
end
it
"updates existing file in project repo with accepts correct last commit id"
do
last_commit
=
Gitlab
::
Git
::
Commit
.
last_for_path
(
project
.
repository
,
'master'
,
URI
.
unescape
(
file_path
))
params_with_correct_id
=
valid_params
.
merge
(
last_commit_id:
last_commit
.
id
)
put
api
(
route
(
file_path
),
user
),
params_with_correct_id
expect
(
response
).
to
have_http_status
(
200
)
end
it
"returns a 400 bad request if no params given"
do
it
"returns a 400 bad request if no params given"
do
put
api
(
route
(
file_path
),
user
)
put
api
(
route
(
file_path
),
user
)
...
...
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