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

167 lines
5.2 KiB
Ruby
Raw Normal View History

2014-09-02 18:07:02 +05:30
module API
# Projects API
class Files < Grape::API
before { authenticate! }
2015-09-25 12:07:36 +05:30
helpers do
def commit_params(attrs)
{
file_path: attrs[:file_path],
source_branch: attrs[:branch_name],
2015-09-25 12:07:36 +05:30
target_branch: attrs[:branch_name],
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],
author_name: attrs[:author_name]
2015-09-25 12:07:36 +05:30
}
end
def commit_response(attrs)
{
file_path: attrs[:file_path],
2016-09-29 09:46:39 +05:30
branch_name: attrs[:branch_name]
2015-09-25 12:07:36 +05:30
}
end
end
2014-09-02 18:07:02 +05:30
resource :projects do
# Get file from repository
# File content is Base64 encoded
#
# Parameters:
# file_path (required) - The path to the file. Ex. lib/class.rb
# ref (required) - The name of branch, tag or commit
#
# Example Request:
# GET /projects/:id/repository/files
#
# Example response:
# {
# "file_name": "key.rb",
# "file_path": "app/models/key.rb",
# "size": 1476,
# "encoding": "base64",
# "content": "IyA9PSBTY2hlbWEgSW5mb3...",
# "ref": "master",
# "blob_id": "79f7bbd25901e8334750839545a9bd021f0e4c83",
2015-11-26 14:37:03 +05:30
# "commit_id": "d5a3ff139356ce33e37e73add446f16869741b50",
# "last_commit_id": "570e7b2abdd848b95f2f578043fc23bd6f6fd24d",
2014-09-02 18:07:02 +05:30
# }
#
get ":id/repository/files" do
authorize! :download_code, user_project
required_attributes! [:file_path, :ref]
attrs = attributes_for_keys [:file_path, :ref]
ref = attrs.delete(:ref)
file_path = attrs.delete(:file_path)
2015-09-11 14:41:01 +05:30
commit = user_project.commit(ref)
2015-04-26 12:48:37 +05:30
not_found! 'Commit' unless commit
2014-09-02 18:07:02 +05:30
2016-04-02 18:10:28 +05:30
repo = user_project.repository
blob = repo.blob_at(commit.sha, file_path)
2014-09-02 18:07:02 +05:30
if blob
2016-04-02 18:10:28 +05:30
blob.load_all_data!(repo)
2014-09-02 18:07:02 +05:30
status(200)
{
file_name: blob.name,
file_path: blob.path,
size: blob.size,
encoding: "base64",
2015-09-11 14:41:01 +05:30
content: Base64.strict_encode64(blob.data),
2014-09-02 18:07:02 +05:30
ref: ref,
blob_id: blob.id,
commit_id: commit.id,
2016-04-02 18:10:28 +05:30
last_commit_id: repo.last_commit_for_path(commit.sha, file_path).id
2014-09-02 18:07:02 +05:30
}
else
2015-04-26 12:48:37 +05:30
not_found! 'File'
2014-09-02 18:07:02 +05:30
end
end
# Create new file in repository
#
# Parameters:
# file_path (required) - The path to new file. Ex. lib/class.rb
# branch_name (required) - The name of branch
# content (required) - File content
# commit_message (required) - Commit message
#
# Example Request:
# POST /projects/:id/repository/files
#
post ":id/repository/files" do
authorize! :push_code, user_project
required_attributes! [:file_path, :branch_name, :content, :commit_message]
2016-09-29 09:46:39 +05:30
attrs = attributes_for_keys [:file_path, :branch_name, :content, :commit_message, :encoding, :author_email, :author_name]
2015-09-25 12:07:36 +05:30
result = ::Files::CreateService.new(user_project, current_user, commit_params(attrs)).execute
2014-09-02 18:07:02 +05:30
if result[:status] == :success
status(201)
2015-09-25 12:07:36 +05:30
commit_response(attrs)
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
# Update existing file in repository
#
# Parameters:
# file_path (optional) - The path to file. Ex. lib/class.rb
# branch_name (required) - The name of branch
# content (required) - File content
# commit_message (required) - Commit message
#
# Example Request:
# PUT /projects/:id/repository/files
#
put ":id/repository/files" do
authorize! :push_code, user_project
required_attributes! [:file_path, :branch_name, :content, :commit_message]
2016-09-29 09:46:39 +05:30
attrs = attributes_for_keys [:file_path, :branch_name, :content, :commit_message, :encoding, :author_email, :author_name]
2015-09-25 12:07:36 +05:30
result = ::Files::UpdateService.new(user_project, current_user, commit_params(attrs)).execute
2014-09-02 18:07:02 +05:30
if result[:status] == :success
status(200)
2015-09-25 12:07:36 +05:30
commit_response(attrs)
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
# Delete existing file in repository
#
# Parameters:
# file_path (optional) - The path to file. Ex. lib/class.rb
# branch_name (required) - The name of branch
# content (required) - File content
# commit_message (required) - Commit message
#
# Example Request:
# DELETE /projects/:id/repository/files
#
delete ":id/repository/files" do
authorize! :push_code, user_project
required_attributes! [:file_path, :branch_name, :commit_message]
2016-09-29 09:46:39 +05:30
attrs = attributes_for_keys [:file_path, :branch_name, :commit_message, :author_email, :author_name]
2015-09-25 12:07:36 +05:30
result = ::Files::DeleteService.new(user_project, current_user, commit_params(attrs)).execute
2014-09-02 18:07:02 +05:30
if result[:status] == :success
status(200)
2015-09-25 12:07:36 +05:30
commit_response(attrs)
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
end
end
end