2013-11-07 11:53:09 -05:00
|
|
|
module API
|
|
|
|
# Projects API
|
|
|
|
class Files < Grape::API
|
|
|
|
before { authenticate! }
|
|
|
|
|
|
|
|
resource :projects do
|
2014-02-18 05:27:02 -05:00
|
|
|
# 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",
|
|
|
|
# "commit_id": "d5a3ff139356ce33e37e73add446f16869741b50"
|
|
|
|
# }
|
|
|
|
#
|
|
|
|
get ":id/repository/files" do
|
2014-06-05 21:58:20 -04:00
|
|
|
authorize! :download_code, user_project
|
|
|
|
|
2014-02-18 05:27:02 -05:00
|
|
|
required_attributes! [:file_path, :ref]
|
|
|
|
attrs = attributes_for_keys [:file_path, :ref]
|
|
|
|
ref = attrs.delete(:ref)
|
|
|
|
file_path = attrs.delete(:file_path)
|
|
|
|
|
|
|
|
commit = user_project.repository.commit(ref)
|
2014-12-30 08:29:55 -05:00
|
|
|
not_found! 'Commit' unless commit
|
2014-02-18 05:27:02 -05:00
|
|
|
|
|
|
|
blob = user_project.repository.blob_at(commit.sha, file_path)
|
|
|
|
|
|
|
|
if blob
|
|
|
|
status(200)
|
|
|
|
|
|
|
|
{
|
|
|
|
file_name: blob.name,
|
|
|
|
file_path: blob.path,
|
|
|
|
size: blob.size,
|
|
|
|
encoding: "base64",
|
|
|
|
content: Base64.encode64(blob.data),
|
|
|
|
ref: ref,
|
|
|
|
blob_id: blob.id,
|
|
|
|
commit_id: commit.id,
|
|
|
|
}
|
|
|
|
else
|
2014-12-30 08:29:55 -05:00
|
|
|
not_found! 'File'
|
2014-02-18 05:27:02 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-11-07 11:53:09 -05:00
|
|
|
# Create new file in repository
|
|
|
|
#
|
|
|
|
# Parameters:
|
2014-02-18 05:27:02 -05:00
|
|
|
# file_path (required) - The path to new file. Ex. lib/class.rb
|
2013-11-07 11:53:09 -05:00
|
|
|
# branch_name (required) - The name of branch
|
|
|
|
# content (required) - File content
|
|
|
|
# commit_message (required) - Commit message
|
|
|
|
#
|
|
|
|
# Example Request:
|
|
|
|
# POST /projects/:id/repository/files
|
2013-11-11 07:58:06 -05:00
|
|
|
#
|
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
|
|
|
|
|
2013-11-20 03:03:50 -05:00
|
|
|
required_attributes! [:file_path, :branch_name, :content, :commit_message]
|
2014-01-16 11:11:00 -05:00
|
|
|
attrs = attributes_for_keys [:file_path, :branch_name, :content, :commit_message, :encoding]
|
2013-11-07 11:53:09 -05:00
|
|
|
branch_name = attrs.delete(:branch_name)
|
|
|
|
file_path = attrs.delete(:file_path)
|
2014-01-16 13:35:21 -05:00
|
|
|
result = ::Files::CreateService.new(user_project, current_user, attrs, branch_name, file_path).execute
|
2013-11-07 11:53:09 -05:00
|
|
|
|
|
|
|
if result[:status] == :success
|
|
|
|
status(201)
|
|
|
|
|
|
|
|
{
|
|
|
|
file_path: file_path,
|
|
|
|
branch_name: branch_name
|
|
|
|
}
|
|
|
|
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
|
|
|
|
|
|
|
# Update existing file in repository
|
|
|
|
#
|
|
|
|
# Parameters:
|
2013-11-19 09:33:38 -05:00
|
|
|
# file_path (optional) - The path to file. Ex. lib/class.rb
|
2013-11-11 07:58:06 -05:00
|
|
|
# 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
|
2014-06-05 21:58:20 -04:00
|
|
|
authorize! :push_code, user_project
|
|
|
|
|
2013-11-11 07:58:06 -05:00
|
|
|
required_attributes! [:file_path, :branch_name, :content, :commit_message]
|
2014-01-16 11:11:00 -05:00
|
|
|
attrs = attributes_for_keys [:file_path, :branch_name, :content, :commit_message, :encoding]
|
2013-11-11 07:58:06 -05:00
|
|
|
branch_name = attrs.delete(:branch_name)
|
|
|
|
file_path = attrs.delete(:file_path)
|
2014-01-16 13:35:21 -05:00
|
|
|
result = ::Files::UpdateService.new(user_project, current_user, attrs, branch_name, file_path).execute
|
2013-11-11 07:58:06 -05:00
|
|
|
|
|
|
|
if result[:status] == :success
|
|
|
|
status(200)
|
|
|
|
|
|
|
|
{
|
|
|
|
file_path: file_path,
|
|
|
|
branch_name: branch_name
|
|
|
|
}
|
|
|
|
else
|
2014-09-25 10:43:23 -04:00
|
|
|
render_api_error!(result[:message], 400)
|
2013-11-11 07:58:06 -05:00
|
|
|
end
|
|
|
|
end
|
2013-11-19 09:33:38 -05:00
|
|
|
|
|
|
|
# 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
|
2014-06-05 21:58:20 -04:00
|
|
|
authorize! :push_code, user_project
|
|
|
|
|
2013-11-19 09:33:38 -05:00
|
|
|
required_attributes! [:file_path, :branch_name, :commit_message]
|
|
|
|
attrs = attributes_for_keys [:file_path, :branch_name, :commit_message]
|
|
|
|
branch_name = attrs.delete(:branch_name)
|
|
|
|
file_path = attrs.delete(:file_path)
|
2014-01-16 13:35:21 -05:00
|
|
|
result = ::Files::DeleteService.new(user_project, current_user, attrs, branch_name, file_path).execute
|
2013-11-19 09:33:38 -05:00
|
|
|
|
|
|
|
if result[:status] == :success
|
|
|
|
status(200)
|
|
|
|
|
|
|
|
{
|
|
|
|
file_path: file_path,
|
|
|
|
branch_name: branch_name
|
|
|
|
}
|
|
|
|
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
|