2018-09-29 18:34:47 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-11-07 11:53:09 -05:00
|
|
|
module API
|
|
|
|
class Files < Grape::API
|
2018-12-04 05:55:34 -05:00
|
|
|
include APIGuard
|
|
|
|
|
2018-11-08 07:18:17 -05:00
|
|
|
FILE_ENDPOINT_REQUIREMENTS = API::NAMESPACE_OR_PROJECT_REQUIREMENTS.merge(file_path: API::NO_SLASH_URL_PART_REGEX)
|
2017-08-21 17:09:45 -04:00
|
|
|
|
2017-08-10 15:16:54 -04:00
|
|
|
# Prevents returning plain/text responses for files with .txt extension
|
|
|
|
after_validation { content_type "application/json" }
|
|
|
|
|
2018-06-28 02:10:51 -04:00
|
|
|
helpers ::API::Helpers::HeadersHelpers
|
|
|
|
|
2015-08-11 08:33:31 -04:00
|
|
|
helpers do
|
|
|
|
def commit_params(attrs)
|
|
|
|
{
|
|
|
|
file_path: attrs[:file_path],
|
2017-08-04 13:18:07 -04:00
|
|
|
start_branch: attrs[:start_branch] || attrs[:branch],
|
2017-04-19 20:37:44 -04:00
|
|
|
branch_name: attrs[:branch],
|
2015-08-11 08:33:31 -04:00
|
|
|
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],
|
2017-06-06 09:55:29 -04:00
|
|
|
author_name: attrs[:author_name],
|
|
|
|
last_commit_sha: attrs[:last_commit_id]
|
2015-08-11 08:33:31 -04:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2017-03-01 15:22:29 -05:00
|
|
|
def assign_file_vars!
|
2017-03-07 19:14:33 -05:00
|
|
|
authorize! :download_code, user_project
|
|
|
|
|
2017-03-01 15:22:29 -05:00
|
|
|
@commit = user_project.commit(params[:ref])
|
|
|
|
not_found!('Commit') unless @commit
|
|
|
|
|
2017-03-07 19:14:33 -05:00
|
|
|
@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
|
2017-06-06 17:20:24 -04:00
|
|
|
@blob.load_all_data!
|
2017-03-01 15:22:29 -05:00
|
|
|
end
|
|
|
|
|
2015-08-11 08:33:31 -04:00
|
|
|
def commit_response(attrs)
|
|
|
|
{
|
|
|
|
file_path: attrs[:file_path],
|
2017-02-02 09:24:30 -05:00
|
|
|
branch: attrs[:branch]
|
2015-08-11 08:33:31 -04:00
|
|
|
}
|
|
|
|
end
|
2016-11-28 16:15:12 -05:00
|
|
|
|
2018-06-28 02:10:51 -04:00
|
|
|
def blob_data
|
|
|
|
{
|
|
|
|
file_name: @blob.name,
|
|
|
|
file_path: @blob.path,
|
|
|
|
size: @blob.size,
|
|
|
|
encoding: "base64",
|
|
|
|
content_sha256: Digest::SHA256.hexdigest(@blob.data),
|
|
|
|
ref: params[:ref],
|
|
|
|
blob_id: @blob.id,
|
|
|
|
commit_id: @commit.id,
|
|
|
|
last_commit_id: @repo.last_commit_id_for_path(@commit.sha, params[:file_path])
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2016-11-28 16:15:12 -05:00
|
|
|
params :simple_file_params do
|
2017-03-07 19:14:33 -05:00
|
|
|
requires :file_path, type: String, desc: 'The url encoded path to the file. Ex. lib%2Fclass%2Erb'
|
2018-09-11 18:02:09 -04:00
|
|
|
requires :branch, type: String, desc: 'Name of the branch to commit into. To create a new branch, also provide `start_branch`.', allow_blank: false
|
2018-08-21 05:14:31 -04:00
|
|
|
requires :commit_message, type: String, allow_blank: false, desc: 'Commit message'
|
2017-08-04 13:18:07 -04:00
|
|
|
optional :start_branch, type: String, desc: 'Name of the branch to start the new commit from'
|
2016-11-28 16:15:12 -05:00
|
|
|
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'
|
2017-06-06 09:55:29 -04:00
|
|
|
optional :last_commit_id, type: String, desc: 'Last known commit id for this file'
|
2016-11-28 16:15:12 -05:00
|
|
|
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
|
2017-08-21 17:09:45 -04:00
|
|
|
resource :projects, requirements: FILE_ENDPOINT_REQUIREMENTS do
|
2018-12-04 05:55:34 -05:00
|
|
|
allow_access_with_scope :read_repository, if: -> (request) { request.get? || request.head? }
|
|
|
|
|
2018-06-28 02:10:51 -04:00
|
|
|
desc 'Get raw file metadata from repository'
|
|
|
|
params do
|
|
|
|
requires :file_path, type: String, desc: 'The url encoded path to the file. Ex. lib%2Fclass%2Erb'
|
2018-09-11 18:02:09 -04:00
|
|
|
requires :ref, type: String, desc: 'The name of branch, tag or commit', allow_blank: false
|
2018-06-28 02:10:51 -04:00
|
|
|
end
|
|
|
|
head ":id/repository/files/:file_path/raw", requirements: FILE_ENDPOINT_REQUIREMENTS do
|
|
|
|
assign_file_vars!
|
|
|
|
|
|
|
|
set_http_headers(blob_data)
|
|
|
|
end
|
|
|
|
|
2017-03-07 19:14:33 -05:00
|
|
|
desc 'Get raw file contents from the repository'
|
2016-11-28 16:15:12 -05:00
|
|
|
params do
|
2017-03-07 19:14:33 -05:00
|
|
|
requires :file_path, type: String, desc: 'The url encoded path to the file. Ex. lib%2Fclass%2Erb'
|
2018-09-11 18:02:09 -04:00
|
|
|
requires :ref, type: String, desc: 'The name of branch, tag commit', allow_blank: false
|
2016-11-28 16:15:12 -05:00
|
|
|
end
|
2017-08-21 17:09:45 -04:00
|
|
|
get ":id/repository/files/:file_path/raw", requirements: FILE_ENDPOINT_REQUIREMENTS do
|
2017-03-01 15:22:29 -05:00
|
|
|
assign_file_vars!
|
|
|
|
|
2018-06-28 02:10:51 -04:00
|
|
|
set_http_headers(blob_data)
|
|
|
|
|
2017-03-01 15:22:29 -05:00
|
|
|
send_git_blob @repo, @blob
|
|
|
|
end
|
|
|
|
|
2018-06-28 02:10:51 -04:00
|
|
|
desc 'Get file metadata from repository'
|
|
|
|
params do
|
|
|
|
requires :file_path, type: String, desc: 'The url encoded path to the file. Ex. lib%2Fclass%2Erb'
|
2018-09-11 18:02:09 -04:00
|
|
|
requires :ref, type: String, desc: 'The name of branch, tag or commit', allow_blank: false
|
2018-06-28 02:10:51 -04:00
|
|
|
end
|
|
|
|
head ":id/repository/files/:file_path", requirements: FILE_ENDPOINT_REQUIREMENTS do
|
|
|
|
assign_file_vars!
|
|
|
|
|
|
|
|
set_http_headers(blob_data)
|
|
|
|
end
|
|
|
|
|
2017-03-07 19:14:33 -05:00
|
|
|
desc 'Get a file from the repository'
|
2017-03-01 15:22:29 -05:00
|
|
|
params do
|
2017-03-07 19:14:33 -05:00
|
|
|
requires :file_path, type: String, desc: 'The url encoded path to the file. Ex. lib%2Fclass%2Erb'
|
2018-09-11 18:02:09 -04:00
|
|
|
requires :ref, type: String, desc: 'The name of branch, tag or commit', allow_blank: false
|
2017-03-01 15:22:29 -05:00
|
|
|
end
|
2017-08-21 17:09:45 -04:00
|
|
|
get ":id/repository/files/:file_path", requirements: FILE_ENDPOINT_REQUIREMENTS do
|
2017-03-01 15:22:29 -05:00
|
|
|
assign_file_vars!
|
2014-02-18 05:27:02 -05:00
|
|
|
|
2018-06-28 02:10:51 -04:00
|
|
|
data = blob_data
|
|
|
|
|
|
|
|
set_http_headers(data)
|
|
|
|
|
|
|
|
data.merge(content: Base64.strict_encode64(@blob.data))
|
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
|
2017-08-21 17:09:45 -04:00
|
|
|
post ":id/repository/files/:file_path", requirements: FILE_ENDPOINT_REQUIREMENTS 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
|
2017-08-21 17:09:45 -04:00
|
|
|
put ":id/repository/files/:file_path", requirements: FILE_ENDPOINT_REQUIREMENTS 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-06-06 09:55:29 -04:00
|
|
|
|
|
|
|
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
|
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
|
2017-08-21 17:09:45 -04:00
|
|
|
delete ":id/repository/files/:file_path", requirements: FILE_ENDPOINT_REQUIREMENTS 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
|
2013-11-19 09:33:38 -05:00
|
|
|
|
2017-02-20 13:18:12 -05:00
|
|
|
if result[:status] != :success
|
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
|