2013-11-07 11:53:09 -05:00
|
|
|
module API
|
|
|
|
# Projects API
|
|
|
|
class Files < Grape::API
|
2015-08-11 08:33:31 -04:00
|
|
|
helpers do
|
|
|
|
def commit_params(attrs)
|
|
|
|
{
|
|
|
|
file_path: attrs[:file_path],
|
2017-01-06 10:50:08 -05:00
|
|
|
start_branch: attrs[:branch_name],
|
2015-08-11 08:33:31 -04:00
|
|
|
target_branch: attrs[:branch_name],
|
|
|
|
commit_message: attrs[:commit_message],
|
|
|
|
file_content: attrs[:content],
|
2016-09-08 11:40:07 -04:00
|
|
|
file_content_encoding: attrs[:encoding],
|
|
|
|
author_email: attrs[:author_email],
|
|
|
|
author_name: attrs[:author_name]
|
2015-08-11 08:33:31 -04:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def commit_response(attrs)
|
|
|
|
{
|
|
|
|
file_path: attrs[:file_path],
|
2016-09-08 11:40:07 -04:00
|
|
|
branch_name: attrs[:branch_name]
|
2015-08-11 08:33:31 -04:00
|
|
|
}
|
|
|
|
end
|
2016-11-28 16:15:12 -05:00
|
|
|
|
|
|
|
params :simple_file_params do
|
|
|
|
requires :file_path, type: String, desc: 'The path to new file. Ex. lib/class.rb'
|
|
|
|
requires :branch_name, type: String, desc: 'The name of branch'
|
|
|
|
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
|
2015-08-11 08:33:31 -04:00
|
|
|
end
|
|
|
|
|
2016-11-28 16:15:12 -05:00
|
|
|
params do
|
|
|
|
requires :id, type: String, desc: 'The project ID'
|
|
|
|
end
|
2013-11-07 11:53:09 -05:00
|
|
|
resource :projects do
|
2016-11-28 16:15:12 -05: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
|
2014-02-18 05:27:02 -05:00
|
|
|
get ":id/repository/files" do
|
2014-06-05 21:58:20 -04:00
|
|
|
authorize! :download_code, user_project
|
|
|
|
|
2016-11-28 16:15:12 -05:00
|
|
|
commit = user_project.commit(params[:ref])
|
|
|
|
not_found!('Commit') unless commit
|
2014-02-18 05:27:02 -05:00
|
|
|
|
2016-02-01 04:41:52 -05:00
|
|
|
repo = user_project.repository
|
2016-11-28 16:15:12 -05:00
|
|
|
blob = repo.blob_at(commit.sha, params[:file_path])
|
|
|
|
not_found!('File') unless blob
|
2014-02-18 05:27:02 -05:00
|
|
|
|
2016-11-28 16:15:12 -05:00
|
|
|
blob.load_all_data!(repo)
|
|
|
|
status(200)
|
2014-02-18 05:27:02 -05:00
|
|
|
|
2016-11-28 16:15:12 -05: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 08:43:59 -05:00
|
|
|
last_commit_id: repo.last_commit_id_for_path(commit.sha, params[:file_path])
|
2016-11-28 16:15:12 -05:00
|
|
|
}
|
2014-02-18 05:27:02 -05:00
|
|
|
end
|
|
|
|
|
2016-11-28 16:15:12 -05:00
|
|
|
desc 'Create new file in repository'
|
|
|
|
params do
|
|
|
|
use :extended_file_params
|
|
|
|
end
|
2013-11-07 11:53:09 -05:00
|
|
|
post ":id/repository/files" 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
|
2013-11-07 11:53:09 -05:00
|
|
|
|
|
|
|
if result[:status] == :success
|
|
|
|
status(201)
|
2016-11-28 16:15:12 -05:00
|
|
|
commit_response(file_params)
|
2013-11-07 11:53:09 -05:00
|
|
|
else
|
2014-09-25 10:43:23 -04:00
|
|
|
render_api_error!(result[:message], 400)
|
2013-11-07 11:53:09 -05:00
|
|
|
end
|
|
|
|
end
|
2013-11-11 07:58:06 -05:00
|
|
|
|
2016-11-28 16:15:12 -05:00
|
|
|
desc 'Update existing file in repository'
|
|
|
|
params do
|
|
|
|
use :extended_file_params
|
|
|
|
end
|
2013-11-11 07:58:06 -05:00
|
|
|
put ":id/repository/files" 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
|
2013-11-11 07:58:06 -05:00
|
|
|
|
|
|
|
if result[:status] == :success
|
|
|
|
status(200)
|
2016-11-28 16:15:12 -05:00
|
|
|
commit_response(file_params)
|
2013-11-11 07:58:06 -05:00
|
|
|
else
|
2015-02-22 18:01:49 -05:00
|
|
|
http_status = result[:http_status] || 400
|
|
|
|
render_api_error!(result[:message], http_status)
|
2013-11-11 07:58:06 -05:00
|
|
|
end
|
|
|
|
end
|
2013-11-19 09:33:38 -05:00
|
|
|
|
2016-11-28 16:15:12 -05:00
|
|
|
desc 'Delete an existing file in repository'
|
|
|
|
params do
|
|
|
|
use :simple_file_params
|
|
|
|
end
|
2013-11-19 09:33:38 -05:00
|
|
|
delete ":id/repository/files" 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::DeleteService.new(user_project, current_user, commit_params(file_params)).execute
|
2013-11-19 09:33:38 -05:00
|
|
|
|
|
|
|
if result[:status] == :success
|
|
|
|
status(200)
|
2016-11-28 16:15:12 -05:00
|
|
|
commit_response(file_params)
|
2013-11-19 09:33:38 -05:00
|
|
|
else
|
2014-09-25 10:43:23 -04:00
|
|
|
render_api_error!(result[:message], 400)
|
2013-11-19 09:33:38 -05:00
|
|
|
end
|
|
|
|
end
|
2013-11-07 11:53:09 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|