Commit 0b67606a authored by Dmitriy Zaporozhets's avatar Dmitriy Zaporozhets

New API: create file in repo

Signed-off-by: default avatarDmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
parent c28786ec
...@@ -39,5 +39,6 @@ module API ...@@ -39,5 +39,6 @@ module API
mount DeployKeys mount DeployKeys
mount ProjectHooks mount ProjectHooks
mount Services mount Services
mount Files
end end
end end
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
...@@ -17,7 +17,7 @@ module Gitlab ...@@ -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
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment