2013-11-07 11:53:09 -05:00
|
|
|
module API
|
|
|
|
# Projects API
|
|
|
|
class Files < Grape::API
|
|
|
|
before { authenticate! }
|
|
|
|
before { authorize! :push_code, user_project }
|
|
|
|
|
|
|
|
resource :projects do
|
|
|
|
# Create new file in repository
|
|
|
|
#
|
|
|
|
# Parameters:
|
|
|
|
# file_name (required) - The name of new file. Ex. class.rb
|
2013-11-10 03:43:04 -05:00
|
|
|
# file_path (optional) - The path to new file. Ex. lib/
|
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
|
2013-11-11 07:58:06 -05:00
|
|
|
required_attributes! [:file_name, :branch_name, :content, :commit_message]
|
|
|
|
attrs = attributes_for_keys [:file_name, :file_path, :branch_name, :content, :commit_message]
|
2013-11-07 11:53:09 -05:00
|
|
|
branch_name = attrs.delete(:branch_name)
|
|
|
|
file_path = attrs.delete(:file_path)
|
|
|
|
result = ::Files::CreateContext.new(user_project, current_user, attrs, branch_name, file_path).execute
|
|
|
|
|
|
|
|
if result[:status] == :success
|
|
|
|
status(201)
|
|
|
|
|
|
|
|
{
|
|
|
|
file_name: attrs[:file_name],
|
|
|
|
file_path: file_path,
|
|
|
|
branch_name: branch_name
|
|
|
|
}
|
|
|
|
else
|
|
|
|
render_api_error!(result[:error], 400)
|
|
|
|
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
|
|
|
|
required_attributes! [:file_path, :branch_name, :content, :commit_message]
|
|
|
|
attrs = attributes_for_keys [:file_path, :branch_name, :content, :commit_message]
|
|
|
|
branch_name = attrs.delete(:branch_name)
|
|
|
|
file_path = attrs.delete(:file_path)
|
|
|
|
result = ::Files::UpdateContext.new(user_project, current_user, attrs, branch_name, file_path).execute
|
|
|
|
|
|
|
|
if result[:status] == :success
|
|
|
|
status(200)
|
|
|
|
|
|
|
|
{
|
|
|
|
file_path: file_path,
|
|
|
|
branch_name: branch_name
|
|
|
|
}
|
|
|
|
else
|
|
|
|
render_api_error!(result[:error], 400)
|
|
|
|
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
|
|
|
|
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)
|
|
|
|
result = ::Files::DeleteContext.new(user_project, current_user, attrs, branch_name, file_path).execute
|
|
|
|
|
|
|
|
if result[:status] == :success
|
|
|
|
status(200)
|
|
|
|
|
|
|
|
{
|
|
|
|
file_path: file_path,
|
|
|
|
branch_name: branch_name
|
|
|
|
}
|
|
|
|
else
|
|
|
|
render_api_error!(result[:error], 400)
|
|
|
|
end
|
|
|
|
end
|
2013-11-07 11:53:09 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|