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

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

251 lines
9.2 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
module API
class Files < ::API::Base
include APIGuard
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
# Prevents returning plain/text responses for files with .txt extension
after_validation { content_type "application/json" }
feature_category :source_code_management
2018-06-28 02:10:51 -04:00
helpers ::API::Helpers::HeadersHelpers
helpers do
def commit_params(attrs)
{
file_path: attrs[:file_path],
start_branch: attrs[: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],
last_commit_sha: attrs[:last_commit_id],
execute_filemode: attrs[:execute_filemode]
}
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], limit: Gitlab::Git::Blob::LFS_POINTER_MAX_SIZE)
2017-03-01 15:22:29 -05:00
not_found!('File') unless @blob
end
def commit_response(attrs)
{
file_path: attrs[:file_path],
branch: attrs[:branch]
}
end
2016-11-28 16:15:12 -05:00
def content_sha
Rails.cache.fetch("blob_content_sha256:#{user_project.full_path}:#{@blob.id}") do
@blob.load_all_data!
Digest::SHA256.hexdigest(@blob.data)
end
end
def fetch_blame_range(blame_params)
return if blame_params[:range].blank?
range = Range.new(blame_params[:range][:start], blame_params[:range][:end])
render_api_error!('range[start] must be less than or equal to range[end]', 400) if range.begin > range.end
range
end
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: content_sha,
2018-06-28 02:10:51 -04:00
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], literal_pathspec: true),
execute_filemode: @blob.executable?
2018-06-28 02:10:51 -04:00
}
end
2016-11-28 16:15:12 -05:00
params :simple_file_params do
requires :file_path, type: String, file_path: true, desc: 'The url encoded path to the file. Ex. lib%2Fclass%2Erb'
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'
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'
optional :last_commit_id, type: String, desc: 'Last known commit id for this file'
optional :execute_filemode, type: Boolean, desc: 'Enable / Disable the executable flag on the file path'
2016-11-28 16:15:12 -05:00
end
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
allow_access_with_scope :read_repository, if: -> (request) { request.get? || request.head? }
2019-07-04 07:59:10 -04:00
desc 'Get blame file metadata from repository'
params do
requires :file_path, type: String, file_path: true, desc: 'The url encoded path to the file. Ex. lib%2Fclass%2Erb'
2019-07-04 07:59:10 -04:00
requires :ref, type: String, desc: 'The name of branch, tag or commit', allow_blank: false
end
head ":id/repository/files/:file_path/blame", requirements: FILE_ENDPOINT_REQUIREMENTS do
assign_file_vars!
set_http_headers(blob_data)
end
desc 'Get blame file from the repository'
params do
requires :file_path, type: String, file_path: true, desc: 'The url encoded path to the file. Ex. lib%2Fclass%2Erb'
2019-07-04 07:59:10 -04:00
requires :ref, type: String, desc: 'The name of branch, tag or commit', allow_blank: false
optional :range, type: Hash do
requires :start, type: Integer, desc: 'The first line of the range to blame', allow_blank: false, values: ->(v) { v > 0 }
requires :end, type: Integer, desc: 'The last line of the range to blame', allow_blank: false, values: ->(v) { v > 0 }
end
2019-07-04 07:59:10 -04:00
end
get ":id/repository/files/:file_path/blame", requirements: FILE_ENDPOINT_REQUIREMENTS do
blame_params = declared_params(include_missing: false)
2019-07-04 07:59:10 -04:00
assign_file_vars!
set_http_headers(blob_data)
blame_ranges = Gitlab::Blame.new(@blob, @commit, range: fetch_blame_range(blame_params)).groups(highlight: false)
2019-07-04 07:59:10 -04:00
present blame_ranges, with: Entities::BlameRange
end
2018-06-28 02:10:51 -04:00
desc 'Get raw file metadata from repository'
params do
requires :file_path, type: String, file_path: true, desc: 'The url encoded path to the file. Ex. lib%2Fclass%2Erb'
optional :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, urgency: :low do
2018-06-28 02:10:51 -04:00
assign_file_vars!
set_http_headers(blob_data)
end
desc 'Get raw file contents from the repository'
2016-11-28 16:15:12 -05:00
params do
requires :file_path, type: String, file_path: true, desc: 'The url encoded path to the file. Ex. lib%2Fclass%2Erb'
optional :ref, type: String, desc: 'The name of branch, tag or commit', allow_blank: false
2016-11-28 16:15:12 -05:00
end
get ":id/repository/files/:file_path/raw", requirements: FILE_ENDPOINT_REQUIREMENTS, urgency: :low do
2017-03-01 15:22:29 -05:00
assign_file_vars!
no_cache_headers
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, file_path: true, desc: 'The url encoded path to the file. Ex. lib%2Fclass%2Erb'
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, urgency: :low do
2018-06-28 02:10:51 -04:00
assign_file_vars!
set_http_headers(blob_data)
end
desc 'Get a file from the repository'
2017-03-01 15:22:29 -05:00
params do
requires :file_path, type: String, file_path: true, desc: 'The url encoded path to the file. Ex. lib%2Fclass%2Erb'
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!
@blob.load_all_data!
2018-06-28 02:10:51 -04:00
data = blob_data
set_http_headers(data)
data.merge(content: Base64.strict_encode64(@blob.data))
end
2016-11-28 16:15:12 -05:00
desc 'Create new file in repository'
params do
use :extended_file_params
end
post ":id/repository/files/:file_path", requirements: FILE_ENDPOINT_REQUIREMENTS, urgency: :low 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
put ":id/repository/files/:file_path", requirements: FILE_ENDPOINT_REQUIREMENTS, urgency: :low 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)
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
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-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
2017-02-20 13:18:12 -05:00
if result[:status] != :success
render_api_error!(result[:message], 400)
end
end
end
end
end