gitlab-org--gitlab-foss/lib/api/files.rb

149 lines
4.9 KiB
Ruby
Raw Normal View History

module API
class Files < Grape::API
helpers do
def commit_params(attrs)
{
file_path: attrs[:file_path],
start_branch: attrs[:branch],
2017-04-20 00:37:44 +00:00
branch_name: attrs[:branch],
commit_message: attrs[:commit_message],
file_content: attrs[:content],
file_content_encoding: attrs[:encoding],
author_email: attrs[:author_email],
author_name: attrs[:author_name],
last_commit_sha: attrs[:last_commit_id]
}
end
2017-03-01 20:22:29 +00:00
def assign_file_vars!
authorize! :download_code, user_project
2017-03-01 20:22:29 +00:00
@commit = user_project.commit(params[:ref])
not_found!('Commit') unless @commit
@repo = user_project.repository
@blob = @repo.blob_at(@commit.sha, params[:file_path])
2017-03-01 20:22:29 +00:00
not_found!('File') unless @blob
@blob.load_all_data!
2017-03-01 20:22:29 +00:00
end
def commit_response(attrs)
{
file_path: attrs[:file_path],
branch: attrs[:branch]
}
end
2016-11-28 21:15:12 +00:00
params :simple_file_params do
requires :file_path, type: String, desc: 'The url encoded path to the file. Ex. lib%2Fclass%2Erb'
requires :branch, type: String, desc: 'The name of branch'
2016-11-28 21:15:12 +00:00
requires :commit_message, type: String, desc: 'Commit Message'
optional :author_email, type: String, desc: 'The email of the author'
optional :author_name, type: String, desc: 'The name of the author'
end
params :extended_file_params do
use :simple_file_params
requires :content, type: String, desc: 'File content'
optional :encoding, type: String, values: %w[base64], desc: 'File encoding'
optional :last_commit_id, type: String, desc: 'Last known commit id for this file'
2016-11-28 21:15:12 +00:00
end
end
2016-11-28 21:15:12 +00:00
params do
requires :id, type: String, desc: 'The project ID'
end
resource :projects, requirements: { id: %r{[^/]+} } do
desc 'Get raw file contents from the repository'
2016-11-28 21:15:12 +00:00
params do
requires :file_path, type: String, desc: 'The url encoded path to the file. Ex. lib%2Fclass%2Erb'
requires :ref, type: String, desc: 'The name of branch, tag commit'
2016-11-28 21:15:12 +00:00
end
2017-03-01 20:22:29 +00:00
get ":id/repository/files/:file_path/raw" do
assign_file_vars!
send_git_blob @repo, @blob
end
desc 'Get a file from the repository'
2017-03-01 20:22:29 +00:00
params do
requires :file_path, type: String, desc: 'The url encoded path to the file. Ex. lib%2Fclass%2Erb'
requires :ref, type: String, desc: 'The name of branch, tag or commit'
2017-03-01 20:22:29 +00:00
end
get ":id/repository/files/:file_path", requirements: { file_path: /.+/ } do
2017-03-01 20:22:29 +00:00
assign_file_vars!
2016-11-28 21:15:12 +00:00
{
2017-03-01 20:22:29 +00:00
file_name: @blob.name,
file_path: @blob.path,
size: @blob.size,
2016-11-28 21:15:12 +00:00
encoding: "base64",
2017-03-01 20:22:29 +00:00
content: Base64.strict_encode64(@blob.data),
2016-11-28 21:15:12 +00:00
ref: params[:ref],
2017-03-01 20:22:29 +00:00
blob_id: @blob.id,
commit_id: @commit.id,
last_commit_id: @repo.last_commit_id_for_path(@commit.sha, params[:file_path])
2016-11-28 21:15:12 +00:00
}
end
2016-11-28 21:15:12 +00:00
desc 'Create new file in repository'
params do
use :extended_file_params
end
2017-03-01 20:22:29 +00:00
post ":id/repository/files/:file_path", requirements: { file_path: /.+/ } do
2014-06-06 01:58:20 +00:00
authorize! :push_code, user_project
2016-11-28 21:15:12 +00:00
file_params = declared_params(include_missing: false)
result = ::Files::CreateService.new(user_project, current_user, commit_params(file_params)).execute
if result[:status] == :success
status(201)
2016-11-28 21:15:12 +00:00
commit_response(file_params)
else
render_api_error!(result[:message], 400)
end
end
2016-11-28 21:15:12 +00:00
desc 'Update existing file in repository'
params do
use :extended_file_params
end
2017-03-01 20:22:29 +00:00
put ":id/repository/files/:file_path", requirements: { file_path: /.+/ } do
2014-06-06 01:58:20 +00:00
authorize! :push_code, user_project
2016-11-28 21:15:12 +00:00
file_params = declared_params(include_missing: false)
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
status(200)
2016-11-28 21:15:12 +00:00
commit_response(file_params)
else
http_status = result[:http_status] || 400
render_api_error!(result[:message], http_status)
end
end
2016-11-28 21:15:12 +00:00
desc 'Delete an existing file in repository'
params do
use :simple_file_params
end
2017-03-01 20:22:29 +00:00
delete ":id/repository/files/:file_path", requirements: { file_path: /.+/ } do
2014-06-06 01:58:20 +00:00
authorize! :push_code, user_project
2016-11-28 21:15:12 +00:00
file_params = declared_params(include_missing: false)
2017-04-20 00:37:44 +00:00
result = ::Files::DeleteService.new(user_project, current_user, commit_params(file_params)).execute
2017-02-20 18:18:12 +00:00
if result[:status] != :success
render_api_error!(result[:message], 400)
end
end
end
end
end