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

142 lines
4.6 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-19 20:37:44 -04: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]
}
end
2017-03-01 15:22:29 -05:00
def assign_file_vars!
authorize! :download_code, user_project
2017-03-01 15:22:29 -05: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 15:22:29 -05:00
not_found!('File') unless @blob
@blob.load_all_data!(@repo)
end
def commit_response(attrs)
{
file_path: attrs[:file_path],
branch: attrs[:branch]
}
end
2016-11-28 16:15:12 -05: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 16:15:12 -05: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 16:15:12 -05: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 16:15:12 -05: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 16:15:12 -05:00
end
2017-03-01 15:22:29 -05: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 15:22:29 -05: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 15:22:29 -05:00
end
get ":id/repository/files/:file_path", requirements: { file_path: /.+/ } do
2017-03-01 15:22:29 -05:00
assign_file_vars!
2016-11-28 16:15:12 -05:00
{
2017-03-01 15:22:29 -05:00
file_name: @blob.name,
file_path: @blob.path,
size: @blob.size,
2016-11-28 16:15:12 -05:00
encoding: "base64",
2017-03-01 15:22:29 -05:00
content: Base64.strict_encode64(@blob.data),
2016-11-28 16:15:12 -05:00
ref: params[:ref],
2017-03-01 15:22:29 -05: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 16:15:12 -05:00
}
end
2016-11-28 16:15:12 -05:00
desc 'Create new file in repository'
params do
use :extended_file_params
end
2017-03-01 15:22:29 -05:00
post ":id/repository/files/:file_path", requirements: { file_path: /.+/ } do
2014-06-05 21:58:20 -04:00
authorize! :push_code, user_project
2016-11-28 16:15:12 -05: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 16:15:12 -05:00
commit_response(file_params)
else
render_api_error!(result[:message], 400)
end
end
2016-11-28 16:15:12 -05:00
desc 'Update existing file in repository'
params do
use :extended_file_params
end
2017-03-01 15:22:29 -05:00
put ":id/repository/files/:file_path", requirements: { file_path: /.+/ } do
2014-06-05 21:58:20 -04:00
authorize! :push_code, user_project
2016-11-28 16:15:12 -05: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 16:15:12 -05:00
commit_response(file_params)
else
http_status = result[:http_status] || 400
render_api_error!(result[:message], http_status)
end
end
2016-11-28 16:15:12 -05:00
desc 'Delete an existing file in repository'
params do
use :simple_file_params
end
2017-03-01 15:22:29 -05:00
delete ":id/repository/files/:file_path", requirements: { file_path: /.+/ } do
2014-06-05 21:58:20 -04:00
authorize! :push_code, user_project
2016-11-28 16:15:12 -05:00
file_params = declared_params(include_missing: false)
2017-04-19 20:37:44 -04:00
result = ::Files::DeleteService.new(user_project, current_user, commit_params(file_params)).execute
2017-02-20 13:18:12 -05:00
if result[:status] != :success
render_api_error!(result[:message], 400)
end
end
end
end
end