debian-mirror-gitlab/lib/api/files.rb

195 lines
6.7 KiB
Ruby
Raw Normal View History

2018-12-05 23:21:45 +05:30
# frozen_string_literal: true
2014-09-02 18:07:02 +05:30
module API
class Files < Grape::API
2019-02-15 15:39:39 +05:30
include APIGuard
FILE_ENDPOINT_REQUIREMENTS = API::NAMESPACE_OR_PROJECT_REQUIREMENTS.merge(file_path: API::NO_SLASH_URL_PART_REGEX)
2018-03-17 18:26:18 +05:30
2017-09-10 17:25:29 +05:30
# Prevents returning plain/text responses for files with .txt extension
after_validation { content_type "application/json" }
2018-11-08 19:23:39 +05:30
helpers ::API::Helpers::HeadersHelpers
2015-09-25 12:07:36 +05:30
helpers do
def commit_params(attrs)
{
file_path: attrs[:file_path],
2017-09-10 17:25:29 +05:30
start_branch: attrs[:start_branch] || attrs[:branch],
2017-08-17 22:00:37 +05:30
branch_name: attrs[:branch],
2015-09-25 12:07:36 +05:30
commit_message: attrs[:commit_message],
file_content: attrs[:content],
2016-09-29 09:46:39 +05:30
file_content_encoding: attrs[:encoding],
author_email: attrs[:author_email],
2017-09-10 17:25:29 +05:30
author_name: attrs[:author_name],
last_commit_sha: attrs[:last_commit_id]
2015-09-25 12:07:36 +05:30
}
end
2017-08-17 22:00:37 +05:30
def assign_file_vars!
authorize! :download_code, user_project
@commit = user_project.commit(params[:ref])
not_found!('Commit') unless @commit
@repo = user_project.repository
@blob = @repo.blob_at(@commit.sha, params[:file_path])
not_found!('File') unless @blob
2017-09-10 17:25:29 +05:30
@blob.load_all_data!
2017-08-17 22:00:37 +05:30
end
2015-09-25 12:07:36 +05:30
def commit_response(attrs)
{
file_path: attrs[:file_path],
2017-08-17 22:00:37 +05:30
branch: attrs[:branch]
2015-09-25 12:07:36 +05:30
}
end
2018-11-08 19:23:39 +05:30
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
2017-08-17 22:00:37 +05:30
params :simple_file_params do
requires :file_path, type: String, desc: 'The url encoded path to the file. Ex. lib%2Fclass%2Erb'
2018-12-05 23:21:45 +05:30
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-11-20 20:47:30 +05:30
requires :commit_message, type: String, allow_blank: false, desc: 'Commit message'
2017-09-10 17:25:29 +05:30
optional :start_branch, type: String, desc: 'Name of the branch to start the new commit from'
2017-08-17 22:00:37 +05:30
optional :author_email, type: String, desc: 'The email of the author'
optional :author_name, type: String, desc: 'The name of the author'
end
2014-09-02 18:07:02 +05:30
2017-08-17 22:00:37 +05:30
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-09-10 17:25:29 +05:30
optional :last_commit_id, type: String, desc: 'Last known commit id for this file'
2017-08-17 22:00:37 +05:30
end
end
2014-09-02 18:07:02 +05:30
2017-08-17 22:00:37 +05:30
params do
requires :id, type: String, desc: 'The project ID'
end
2018-03-17 18:26:18 +05:30
resource :projects, requirements: FILE_ENDPOINT_REQUIREMENTS do
2019-02-15 15:39:39 +05:30
allow_access_with_scope :read_repository, if: -> (request) { request.get? || request.head? }
2018-11-08 19:23:39 +05:30
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-12-05 23:21:45 +05:30
requires :ref, type: String, desc: 'The name of branch, tag or commit', allow_blank: false
2018-11-08 19:23:39 +05:30
end
head ":id/repository/files/:file_path/raw", requirements: FILE_ENDPOINT_REQUIREMENTS do
assign_file_vars!
set_http_headers(blob_data)
end
2017-08-17 22:00:37 +05:30
desc 'Get raw file contents from the repository'
params do
requires :file_path, type: String, desc: 'The url encoded path to the file. Ex. lib%2Fclass%2Erb'
2018-12-05 23:21:45 +05:30
requires :ref, type: String, desc: 'The name of branch, tag commit', allow_blank: false
2017-08-17 22:00:37 +05:30
end
2018-03-17 18:26:18 +05:30
get ":id/repository/files/:file_path/raw", requirements: FILE_ENDPOINT_REQUIREMENTS do
2017-08-17 22:00:37 +05:30
assign_file_vars!
2014-09-02 18:07:02 +05:30
2018-11-08 19:23:39 +05:30
set_http_headers(blob_data)
2017-08-17 22:00:37 +05:30
send_git_blob @repo, @blob
end
2014-09-02 18:07:02 +05:30
2018-11-08 19:23:39 +05:30
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-12-05 23:21:45 +05:30
requires :ref, type: String, desc: 'The name of branch, tag or commit', allow_blank: false
2018-11-08 19:23:39 +05:30
end
head ":id/repository/files/:file_path", requirements: FILE_ENDPOINT_REQUIREMENTS do
assign_file_vars!
set_http_headers(blob_data)
end
2017-08-17 22:00:37 +05:30
desc 'Get a file from the repository'
params do
requires :file_path, type: String, desc: 'The url encoded path to the file. Ex. lib%2Fclass%2Erb'
2018-12-05 23:21:45 +05:30
requires :ref, type: String, desc: 'The name of branch, tag or commit', allow_blank: false
2017-08-17 22:00:37 +05:30
end
2018-03-17 18:26:18 +05:30
get ":id/repository/files/:file_path", requirements: FILE_ENDPOINT_REQUIREMENTS do
2017-08-17 22:00:37 +05:30
assign_file_vars!
2014-09-02 18:07:02 +05:30
2018-11-08 19:23:39 +05:30
data = blob_data
set_http_headers(data)
data.merge(content: Base64.strict_encode64(@blob.data))
2014-09-02 18:07:02 +05:30
end
2017-08-17 22:00:37 +05:30
desc 'Create new file in repository'
params do
use :extended_file_params
end
2018-03-17 18:26:18 +05:30
post ":id/repository/files/:file_path", requirements: FILE_ENDPOINT_REQUIREMENTS do
2014-09-02 18:07:02 +05:30
authorize! :push_code, user_project
2017-08-17 22:00:37 +05:30
file_params = declared_params(include_missing: false)
result = ::Files::CreateService.new(user_project, current_user, commit_params(file_params)).execute
2014-09-02 18:07:02 +05:30
if result[:status] == :success
status(201)
2017-08-17 22:00:37 +05:30
commit_response(file_params)
2014-09-02 18:07:02 +05:30
else
2015-04-26 12:48:37 +05:30
render_api_error!(result[:message], 400)
2014-09-02 18:07:02 +05:30
end
end
2017-08-17 22:00:37 +05:30
desc 'Update existing file in repository'
params do
use :extended_file_params
end
2018-03-17 18:26:18 +05:30
put ":id/repository/files/:file_path", requirements: FILE_ENDPOINT_REQUIREMENTS do
2014-09-02 18:07:02 +05:30
authorize! :push_code, user_project
2017-08-17 22:00:37 +05:30
file_params = declared_params(include_missing: false)
2017-09-10 17:25:29 +05:30
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
2014-09-02 18:07:02 +05:30
if result[:status] == :success
status(200)
2017-08-17 22:00:37 +05:30
commit_response(file_params)
2014-09-02 18:07:02 +05:30
else
2015-04-26 12:48:37 +05:30
http_status = result[:http_status] || 400
render_api_error!(result[:message], http_status)
2014-09-02 18:07:02 +05:30
end
end
2017-08-17 22:00:37 +05:30
desc 'Delete an existing file in repository'
params do
use :simple_file_params
end
2018-03-17 18:26:18 +05:30
delete ":id/repository/files/:file_path", requirements: FILE_ENDPOINT_REQUIREMENTS do
2014-09-02 18:07:02 +05:30
authorize! :push_code, user_project
2017-08-17 22:00:37 +05:30
file_params = declared_params(include_missing: false)
result = ::Files::DeleteService.new(user_project, current_user, commit_params(file_params)).execute
2014-09-02 18:07:02 +05:30
2017-08-17 22:00:37 +05:30
if result[:status] != :success
2015-04-26 12:48:37 +05:30
render_api_error!(result[:message], 400)
2014-09-02 18:07:02 +05:30
end
end
end
end
end