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

131 lines
4.0 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],
target_branch: 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]
}
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 path to new file. Ex. lib/class.rb'
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'
end
end
2016-11-28 21:15:12 +00:00
params do
requires :id, type: String, desc: 'The project ID'
end
resource :projects do
2016-11-28 21:15:12 +00:00
desc 'Get a file from repository'
params do
requires :file_path, type: String, desc: 'The path to the file. Ex. lib/class.rb'
requires :ref, type: String, desc: 'The name of branch, tag, or commit'
end
get ":id/repository/files" do
2014-06-06 01:58:20 +00:00
authorize! :download_code, user_project
2016-11-28 21:15:12 +00:00
commit = user_project.commit(params[:ref])
not_found!('Commit') unless commit
2016-02-01 09:41:52 +00:00
repo = user_project.repository
2016-11-28 21:15:12 +00:00
blob = repo.blob_at(commit.sha, params[:file_path])
not_found!('File') unless blob
2016-11-28 21:15:12 +00:00
blob.load_all_data!(repo)
status(200)
2016-11-28 21:15:12 +00:00
{
file_name: blob.name,
file_path: blob.path,
size: blob.size,
encoding: "base64",
content: Base64.strict_encode64(blob.data),
ref: params[:ref],
blob_id: blob.id,
commit_id: commit.id,
2016-12-20 13:43:59 +00:00
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
post ":id/repository/files" 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
put ":id/repository/files" 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::UpdateService.new(user_project, current_user, commit_params(file_params)).execute
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
delete ":id/repository/files" 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::DestroyService.new(user_project, current_user, commit_params(file_params)).execute
if result[:status] == :success
status(200)
2016-11-28 21:15:12 +00:00
commit_response(file_params)
else
render_api_error!(result[:message], 400)
end
end
end
end
end