REST API - How to avoid concurrent updates
Created by: robertjchristian
The REST API looks like this
Update existing file in repository
PUT /projects/:id/repository/files
Parameters:
file_path (required) - Full path to file. Ex. lib/class.rb
branch_name (required) - The name of branch
encoding (optional) - 'text' or 'base64'. Text is default.
content (required) - New file content
commit_message (required) - Commit message
It would be nice if PUT took a sha or tag for an individual file update, and used it to prevent overwrites due to concurrent edits.
Use case:
- Application thread A hits GitLab REST to GET resource Foo
- Foo is returned, along with the SHA for Foo (12345)
- Application thread B hits GitLab REST to GET resource Foo
- Foo is returned, along with the SHA for Foo (12345)
- At this point, two separate "threads" have the same resource
- Application thread A makes changes to Foo and POSTS to GitLab REST API
- Application thread B makes changes to Foo and POSTS to GitLab REST API
Expected:
- When B makes POST, GitLab REST API should say "cannot push, not in sync with repository", just like it would if this were to occur in a local sandbox.
Suggested:
- In order to accomplish this, GET requests could return sha. The sha can be used in the POST request. If the sha sent in the post request is not the most recent version of the file resource, then the POST should return (ie) 409 Conflict.
Is there another way to accomplish this?