debian-mirror-gitlab/app/controllers/projects/lfs_locks_api_controller.rb

77 lines
1.8 KiB
Ruby
Raw Normal View History

2018-12-05 23:21:45 +05:30
# frozen_string_literal: true
2018-03-17 18:26:18 +05:30
class Projects::LfsLocksApiController < Projects::GitHttpClientController
include LfsRequest
def create
2019-03-02 22:35:43 +05:30
@result = Lfs::LockFileService.new(project, user, lfs_params).execute
2018-03-17 18:26:18 +05:30
render_json(@result[:lock])
end
def unlock
2019-03-02 22:35:43 +05:30
@result = Lfs::UnlockFileService.new(project, user, lfs_params).execute
2018-03-17 18:26:18 +05:30
render_json(@result[:lock])
end
def index
2019-03-02 22:35:43 +05:30
@result = Lfs::LocksFinderService.new(project, user, lfs_params).execute
2018-03-17 18:26:18 +05:30
render_json(@result[:locks])
end
def verify
@result = Lfs::LocksFinderService.new(project, user, {}).execute
ours, theirs = split_by_owner(@result[:locks])
render_json({ ours: ours, theirs: theirs }, false)
end
private
def render_json(data, process = true)
render json: build_payload(data, process),
content_type: LfsRequest::CONTENT_TYPE,
status: @result[:http_status]
end
def build_payload(data, process)
data = LfsFileLockSerializer.new.represent(data) if process
return data if @result[:status] == :success
# When the locking failed due to an existent Lock, the existent record
# is returned in `@result[:lock]`
error_payload(@result[:message], @result[:lock] ? data : {})
end
def error_payload(message, custom_attrs = {})
custom_attrs.merge({
message: message,
documentation_url: help_url
})
end
def split_by_owner(locks)
groups = locks.partition { |lock| lock.user_id == user.id }
groups.map! do |records|
LfsFileLockSerializer.new.represent(records, root: false)
end
end
def download_request?
params[:action] == 'index'
end
def upload_request?
%w(create unlock verify).include?(params[:action])
end
2019-03-02 22:35:43 +05:30
def lfs_params
params.permit(:id, :path, :force)
end
2018-03-17 18:26:18 +05:30
end