New API: create file in repo

Signed-off-by: Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
This commit is contained in:
Dmitriy Zaporozhets 2013-11-07 18:53:09 +02:00
parent c28786ecbe
commit 0b67606af6
No known key found for this signature in database
GPG key ID: 2CEAFD2671262EC2
3 changed files with 43 additions and 1 deletions

View file

@ -39,5 +39,6 @@ module API
mount DeployKeys mount DeployKeys
mount ProjectHooks mount ProjectHooks
mount Services mount Services
mount Files
end end
end end

41
lib/api/files.rb Normal file
View file

@ -0,0 +1,41 @@
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
# file_path (optiona) - The path to new file. Ex. lib/
# 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
required_attributes! [:file_name, :branch_name, :content]
attrs = attributes_for_keys [:file_name, :file_path, :branch_name, :content]
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
end
end
end

View file

@ -17,7 +17,7 @@ module Gitlab
repo.git.checkout({raise: true, timeout: true, b: true}, ref, "origin/#{ref}") repo.git.checkout({raise: true, timeout: true, b: true}, ref, "origin/#{ref}")
# update the file in the satellite's working dir # update the file in the satellite's working dir
file_path_in_satellite = File.join(repo.working_dir, file_path, file_name) file_path_in_satellite = File.join(repo.working_dir, file_path || '', file_name)
File.open(file_path_in_satellite, 'w') { |f| f.write(content) } File.open(file_path_in_satellite, 'w') { |f| f.write(content) }
# add new file # add new file